Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _XTAL_FREQ 8000000
- #define RS RD2
- #define EN RD3
- #define D4 RD4
- #define D5 RD5
- #define D6 RD6
- #define D7 RD7
- #include <xc.h>
- #include "lcd.h";
- #include <stdio.h>
- // BEGIN CONFIG
- #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
- #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
- #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
- #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)
- #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
- #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
- #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
- //END CONFIG
- unsigned int ADC_Read(unsigned int channel)
- {
- if(channel > 7) //Channel range is 0 ~ 7
- return 0;
- // clear channel bits
- ADCON0bits.CHS0 = 0;
- ADCON0bits.CHS1 = 0;
- ADCON0bits.CHS2 = 0;
- ADCON0 |= channel<<3; //Setting channel selection bits
- __delay_ms(2); //Acquisition time to charge hold capacitor
- GO_nDONE = 1; //Initializes A/D conversion
- while(GO_nDONE); //Waiting for conversion to complete
- return ((ADRESH<<8)+ADRESL); //Return result
- }
- int main()
- {
- TRISA = 0xFF; //Analog pins as Input
- ADCON0 = 0x81;
- ADCON1 = 0b10000000; //All pins are analog + internal reference.
- unsigned int a;
- unsigned int reading;
- float final_value;
- char display_value[16];
- TRISD = 0x00;
- Lcd_Init();
- Lcd_Clear();
- while(1)
- {
- Lcd_Clear();
- reading = ADC_Read(0);
- final_value = (reading/1023.0)*5.0;
- sprintf(display_value, "reading = %f", final_value);
- Lcd_Set_Cursor(1,1);
- Lcd_Write_String(display_value);
- __delay_ms(1000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement