muhammad_nasif

adc

Jun 5th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #ifndef F_CPU
  2. #define F_CPU 1000000UL // 16 MHz clock speed
  3. #endif
  4. #define D4 eS_PORTD4
  5. #define D5 eS_PORTD5
  6. #define D6 eS_PORTD6
  7. #define D7 eS_PORTD7
  8. #define RS eS_PORTC6
  9. #define EN eS_PORTC7
  10.  
  11. #include <avr/io.h>
  12. #include <util/delay.h>
  13. #include "lcd.h" //Can be download from the bottom of this article
  14.  
  15. int main(void)
  16. {
  17.     DDRD = 0xFF;
  18.     DDRC = 0xFF;
  19.     int i;
  20.     Lcd4_Init();
  21.     while(1)
  22.     {
  23.         Lcd4_Set_Cursor(1,1);
  24.         Lcd4_Write_String("electroSome LCD Hello World");
  25.         for(i=0;i<15;i++)
  26.         {
  27.             _delay_ms(250);
  28.             Lcd4_Shift_Left();
  29.         }
  30.         for(i=0;i<15;i++)
  31.         {
  32.             _delay_ms(250);
  33.             Lcd4_Shift_Right();
  34.         }
  35.         Lcd4_Clear();
  36.         Lcd4_Set_Cursor(2,1);
  37.         Lcd4_Write_Char('e');
  38.         Lcd4_Write_Char('S');
  39.         _delay_ms(1000);
  40.     }
  41.    
  42.    
  43.     long long int result_h;
  44.     long long int result_l;
  45.     unsigned char output_l;
  46.     unsigned char output_h;
  47.    
  48.    
  49.     DDRB = 0xFF;
  50.     //Configure the ADC module of the ATmega16
  51.     ADMUX = 0b00000011;         //REFS1:0 = 01    -> AVCC as reference
  52.                                 //ADLAR   =  0    -> Right adjust
  53.                                 //MUX4:0  = 00000 -> ADC0 as input
  54.                                
  55.     ADCSRA = 0b10000100;        //ADEN = 1  : enable
  56.                                 //ADSC = 0  :
  57.     while(1)
  58.     {
  59.         ADCSRA |= (1 << ADSC);
  60.         while(ADCSRA & (1 << ADSC)){;}
  61.        
  62.        
  63.        
  64.         output_l = ADCL;   
  65.         result_l = ADCL;
  66.         result_h = ADCH;
  67.         output_h = ADCH;
  68.        
  69.         result_h = result_h << 8;
  70.         result_h = result_h | result_l;
  71.        
  72.         output_h = output_h << 6;
  73.         output_l = output_l >> 2;
  74.        
  75.         output_h = output_h | output_l;
  76.        
  77.         float voltage = (result_h)*4/1024;
  78.         PORTB = ~output_h;
  79.        
  80.     }
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment