Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lab7.c
- *
- * Created: 2019-05-14 10:06:10
- * Author : JARACZ&GRUZEG
- */
- #define F_CPU 16000000
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- void InicjalizacjaADC(void);
- void InicjalizacjaTIMER1(void);
- void PomiarADC(void);
- uint8_t napjed;
- uint8_t napdzies;
- uint8_t napset;
- uint8_t zrodlo1 = 0;
- uint8_t zrodlo2 = 0;
- uint8_t zrodlo3 = 0;
- uint8_t zrodlo4 = 0;
- uint8_t avg = 0;
- volatile uint8_t is_avg = 0;
- volatile uint8_t wyswietlacz = 0;
- unsigned char WyswietlanieLED[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0xFF };
- uint16_t cwiercsekundy=60; //podzial z prescalera 16000000/256=62500 /4=15625
- void HandleBtn(uint8_t* btn, uint8_t val)
- {
- switch(*btn)
- {
- // Jesli klikniety
- case 0: if ((PIND & val) == 0) *btn = 1;
- break;
- // Jesli dalej klikniety
- case 1: if ((PIND & val) == 0) *btn = 2;
- else *btn = 0;
- break;
- // Obsluga kolegi w main
- case 2: break;
- // Zostal klikniety RAZ
- case 3: if ((PIND & val) != 0) *btn = 4;
- break;
- // Zostal odklikniety LUB nie to czeka
- case 4: if ((PIND & val) != 0) *btn = 0;
- else *btn = 3;
- break;
- default: *btn = 0;
- }
- }
- ISR (TIMER1_COMPA_vect)
- {
- if(is_avg == 0)
- PomiarADC();
- else {
- uint16_t n_sum = 0;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x00;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- ADCSRA |= (1<<ADSC);
- while((ADCSRA & (1<<ADSC)));
- n_sum += ADC;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x01;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- ADCSRA |= (1<<ADSC);
- while((ADCSRA & (1<<ADSC)));
- n_sum += ADC;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x02;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- ADCSRA |= (1<<ADSC);
- while((ADCSRA & (1<<ADSC)));
- n_sum += ADC;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x03;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- ADCSRA |= (1<<ADSC);
- while((ADCSRA & (1<<ADSC)));
- n_sum += ADC;
- n_sum *= 2.5;
- n_sum /= 4;
- uint16_t napiecie = n_sum; // 2560
- /*napset = napiecie % 10;
- napiecie = napiecie/10;
- napdzies = napiecie % 10;
- napiecie = napiecie/10;
- napjed = napiecie;*/
- napset=(napiecie/10)%10 + (napiecie%10)/2;
- uint8_t carry = 0;
- if(napset > 9) {
- carry = 1;
- napset = 0;
- }
- napdzies= (napiecie/100)%10 + carry;
- if(napdzies > 9) {
- carry = 1;
- napdzies = 0;
- } else carry = 0;
- napjed=napiecie/1000 + carry;
- }
- PORTB = ~8;
- _delay_ms(2);
- PORTC = WyswietlanieLED[napjed] & 0x7F;
- PORTB = ~4;
- _delay_ms(2);
- PORTC = WyswietlanieLED[napdzies];
- PORTB = ~2;
- _delay_ms(2);
- PORTC = WyswietlanieLED[napset];
- HandleBtn(&zrodlo1, 0x04);
- HandleBtn(&zrodlo2, 0x08);
- HandleBtn(&zrodlo3, 0x10);
- HandleBtn(&zrodlo4, 0x20);
- HandleBtn(&avg, 0x02);
- PORTB = ~1;
- _delay_ms(2);
- PORTC = WyswietlanieLED[wyswietlacz + 1];
- }
- int main(void)
- {
- DDRC = 0xFF;
- DDRB = 0x0F;
- //Przyciski
- PORTD = 0xFF;
- DDRD = 0x00;
- InicjalizacjaTIMER1();
- InicjalizacjaADC();
- ADCSRA |= (1<<ADSC);
- while (1)
- {
- if(avg == 2)
- {
- is_avg = 1;
- wyswietlacz = 9;
- avg = 3;
- }
- if(zrodlo1 == 2)
- {
- is_avg = 0;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x00;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- zrodlo1 = 3;
- wyswietlacz = 0;
- }
- if(zrodlo2 == 2)
- {
- is_avg = 0;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x01;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- zrodlo2 = 3;
- wyswietlacz = 1;
- }
- if(zrodlo3 == 2)
- {
- is_avg = 0;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x02;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- zrodlo3 = 3;
- wyswietlacz = 2;
- }
- if(zrodlo4 == 2)
- {
- is_avg = 0;
- ADCSRA &= (0<<ADEN);
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x03;
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);
- zrodlo4 = 3;
- wyswietlacz = 3;
- }
- }
- }
- void InicjalizacjaADC()
- {
- DDRA &=~(1<<PA1); //wejscie dla ADC0
- ADMUX = (1<<REFS1) | (1<<REFS0) | 0x00; //wewnetrzne zrodlo 2,56V
- //wybor kanalu ADC0
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0); //wlaczenie ADC
- //prescaler /32 //ADIE - odblokowanie przerwania
- sei();
- }
- void PomiarADC()
- {
- int16_t napiecie;
- ADCSRA |= (1<<ADSC);
- while((ADCSRA & (1<<ADSC)));
- napiecie = ADC * 2.5; // 2560
- /*napset = napiecie % 10;
- napiecie = napiecie/10;
- napdzies = napiecie % 10;
- napiecie = napiecie/10;
- napjed = napiecie;*/
- napset=(napiecie/10)%10 + (napiecie%10);
- uint8_t carry = 0;
- if(napset > 9) {
- carry = 1;
- napset = 0;
- }
- napdzies= (napiecie/100)%10 + carry;
- if(napdzies > 9) {
- carry = 1;
- napdzies = 0;
- } else carry = 0;
- napjed=napiecie/1000 + carry;
- }
- void InicjalizacjaTIMER1()
- {
- TCCR1B |= (1<<CS12); //preskaler 256
- TCCR1B |= (1<<WGM12); //praca CTC
- OCR1A = cwiercsekundy;
- TIMSK |= (1<<OCIE1A); // odblokowanie przerwania TIMER1_COMPA
- sei();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement