Guest User

Untitled

a guest
Jan 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #ifndef F_CPU
  2. #define F_CPU 1000000UL
  3. #endif // F_CPU
  4.  
  5. #include <avr/io.h>
  6. #include <util/delay.h>
  7.  
  8. #define DELAY_IN_MS 500 /* 0.5 sec */
  9.  
  10. int numbers[] = {
  11. 0b01000000,
  12. 0b01110011,
  13. 0b00100100,
  14. 0b00100001,
  15. 0b00010011,
  16. 0b00001001,
  17. 0b00001000,
  18. 0b01100011,
  19. 0b00000000,
  20. 0b00000001,
  21. 0b11111111 // off
  22. };
  23.  
  24. uint8_t digits[3];
  25.  
  26. void initADC()
  27. {
  28. ADMUX=(1<<REFS1)|(1<<REFS0);
  29. ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
  30. }
  31.  
  32. uint16_t ReadADC(uint8_t ch)
  33. {
  34. //Select ADC Channel ch must be 0-7
  35. ch=ch&0b00000111;
  36. ADMUX|=ch;
  37.  
  38. //Start Single conversion
  39.  
  40. ADCSRA|=(1<<ADSC);
  41.  
  42. //Wait for conversion to complete
  43. while(!(ADCSRA & (1<<ADIF)));
  44.  
  45. //Clear ADIF by writing one to it
  46. ADCSRA|=(1<<ADIF);
  47.  
  48. return(ADC);
  49. }
  50.  
  51. int main()
  52. {
  53. DDRD = 0xFF;
  54. PORTD = 0xFF;
  55.  
  56. DDRB = 0b00000001;
  57. PORTB = 1;
  58.  
  59. initADC();
  60.  
  61. uint16_t adc_value;
  62. uint16_t res;
  63. while(1)
  64. {
  65. adc_value = 0;
  66. for (int i = 0; i < 250; i++)
  67. {
  68. adc_value += ReadADC(0);
  69. }
  70.  
  71. adc_value=(adc_value/25)/4;
  72. res = adc_value;
  73.  
  74. for(int j = 2; j >= 0; j--) {
  75. digits[j] = res%10;
  76. res /= 10;
  77. }
  78.  
  79. uint8_t dig = digits[0];
  80. PORTD = numbers[dig];
  81. _delay_ms(DELAY_IN_MS);
  82.  
  83. // if following is uncommented there blinks digit two correctly
  84. // if commented there is unblinking digit 1
  85. PORTD = numbers[10]; // display off
  86. }
  87.  
  88. return 0;
  89. }
Add Comment
Please, Sign In to add comment