muhammad_nasif

Untitled

Jun 5th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 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 <string.h>
  12. #include <stdio.h>
  13. #include <avr/io.h>
  14. #include <util/delay.h>
  15. #include "lcd.h" //Can be download from the bottom of this article
  16.  
  17. int main(void)
  18. {
  19.     DDRD = 0xFF;
  20.     DDRC = 0xFF;
  21.     int i;
  22.     Lcd4_Init();
  23.     int result_h;
  24.     int result_l;
  25.     unsigned char output_l;
  26.     unsigned char output_h=0b00000000;
  27.    
  28.     DDRB = 0xFF;
  29.     //Configure the ADC module of the ATmega16
  30.     ADMUX = 0b00000011;         //REFS1:0 = 01    -> AVCC as reference
  31.                                 //ADLAR   =  0    -> Right adjust
  32.                                 //MUX4:0  = 00011 -> ADC3 as input
  33.    
  34.     ADCSRA = 0b10000100;                                       
  35.    
  36.     while(1)
  37.     {
  38.        
  39.         ADCSRA |= (1 << ADSC);
  40.         while(ADCSRA & (1 << ADSC)){;}
  41.        
  42.         output_l = ADCL;
  43.         result_l = ADCL;
  44.         output_h = ADCH | output_h;
  45.         result_h = output_h;
  46.        
  47.         result_h = (output_h << 8) | output_l;
  48.         float voltage = (result_h )*4/1024;
  49.        
  50.        
  51.        
  52.         int c = voltage * 100;
  53.         int b = c / 10;
  54.         int a = b/10;
  55.        
  56.        
  57.        
  58.         a = a % 10;
  59.         b = b % 10;
  60.         c = c % 10;
  61.        
  62.  
  63.        
  64.         Lcd4_Set_Cursor(1,1);
  65.  
  66.         Lcd4_Write_Char(a + '0');
  67.         Lcd4_Write_Char('.');
  68.         Lcd4_Write_Char(b + '0');
  69.         Lcd4_Write_Char(c + '0');
  70.        
  71.         PORTB = result_h;
  72.    
  73.        
  74.     }
  75.    
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment