Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef F_CPU
- #define F_CPU 1000000UL
- #endif // F_CPU
- #include <avr/io.h>
- #include <util/delay.h>
- #define DELAY_IN_MS 500 /* 0.5 sec */
- int numbers[] = {
- 0b01000000,
- 0b01110011,
- 0b00100100,
- 0b00100001,
- 0b00010011,
- 0b00001001,
- 0b00001000,
- 0b01100011,
- 0b00000000,
- 0b00000001,
- 0b11111111 // off
- };
- uint8_t digits[3];
- void initADC()
- {
- ADMUX=(1<<REFS1)|(1<<REFS0);
- ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
- }
- uint16_t ReadADC(uint8_t ch)
- {
- //Select ADC Channel ch must be 0-7
- ch=ch&0b00000111;
- ADMUX|=ch;
- //Start Single conversion
- ADCSRA|=(1<<ADSC);
- //Wait for conversion to complete
- while(!(ADCSRA & (1<<ADIF)));
- //Clear ADIF by writing one to it
- ADCSRA|=(1<<ADIF);
- return(ADC);
- }
- int main()
- {
- DDRD = 0xFF;
- PORTD = 0xFF;
- DDRB = 0b00000001;
- PORTB = 1;
- initADC();
- uint16_t adc_value;
- uint16_t res;
- while(1)
- {
- adc_value = 0;
- for (int i = 0; i < 250; i++)
- {
- adc_value += ReadADC(0);
- }
- adc_value=(adc_value/25)/4;
- res = adc_value;
- for(int j = 2; j >= 0; j--) {
- digits[j] = res%10;
- res /= 10;
- }
- uint8_t dig = digits[0];
- PORTD = numbers[dig];
- _delay_ms(DELAY_IN_MS);
- // if following is uncommented there blinks digit two correctly
- // if commented there is unblinking digit 1
- PORTD = numbers[10]; // display off
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment