Advertisement
Guest User

Untitled

a guest
Jan 21st, 2021
1,777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #define _XTAL_FREQ 8000000
  2.  
  3. #define RS RD2
  4. #define EN RD3
  5. #define D4 RD4
  6. #define D5 RD5
  7. #define D6 RD6
  8. #define D7 RD7
  9.  
  10. #include <xc.h>
  11. #include "lcd.h";
  12. #include <stdio.h>
  13.  
  14. // BEGIN CONFIG
  15. #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
  16. #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
  17. #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
  18. #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
  19. #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  20. #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  21. #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  22. #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
  23. //END CONFIG
  24.  
  25. unsigned int ADC_Read(unsigned int channel)
  26. {
  27.   if(channel > 7)              //Channel range is 0 ~ 7
  28.     return 0;
  29.  
  30.   // clear channel bits
  31.  ADCON0bits.CHS0 = 0;
  32.  ADCON0bits.CHS1 = 0;
  33.  ADCON0bits.CHS2 = 0;
  34.  
  35.   ADCON0 |= channel<<3;        //Setting channel selection bits
  36.   __delay_ms(2);               //Acquisition time to charge hold capacitor
  37.   GO_nDONE = 1;                //Initializes A/D conversion
  38.   while(GO_nDONE);             //Waiting for conversion to complete
  39.   return ((ADRESH<<8)+ADRESL); //Return result
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45.     TRISA = 0xFF;                 //Analog pins as Input
  46.    
  47.     ADCON0 = 0x81;  
  48.     ADCON1 = 0b10000000;                       //All pins are analog + internal reference.
  49.     unsigned int a;
  50.     unsigned int reading;
  51.     float final_value;
  52.     char display_value[16];
  53.     TRISD = 0x00;
  54.     Lcd_Init();
  55.     Lcd_Clear();
  56.     while(1)
  57.     {
  58.        
  59.        
  60.        
  61.        
  62.        Lcd_Clear();
  63.        
  64.         reading = ADC_Read(0);
  65.         final_value = (reading/1023.0)*5.0;
  66.      
  67.         sprintf(display_value, "reading = %f", final_value);
  68.         Lcd_Set_Cursor(1,1);
  69.         Lcd_Write_String(display_value);
  70.        
  71.      
  72.         __delay_ms(1000);
  73.    
  74.     }
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement