/*
#include "Includes.h"
// Configuration word for PIC16F877
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON
& LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);
#define BAUDRATE 9600 //bps
#define AN0 0
#define AN1 1
#define AN2 2
#define AN3 3
#define AN4 4
#define AN5 5
#define AN6 6
#define AN7 7
void InitADC(void)
{
ADCON1 = 0x80; // Make PORTA and PORTE analog pins
// Also, Vref+ = 5v and Vref- = GND
TRISA = 0x2f; // Make RA5, RA3, RA2, RA1, RA0 input
TRISE = 0x07; // Make RE0, RE1 and RE2 input
ADCON0 = 0x81; // Turn on the A/D Converter
}
unsigned int GetADCValue(unsigned char Channel)
{
ADCON0 &= 0xc7; // Clear Channel selection bits
ADCON0 |= (Channel<<3); // Select channel pin as ADC input
__delay_ms(10); // Time for Acqusition capacitor
// to charge up and show correct value
GO_nDONE = 1; // Enable Go/Done
while(GO_nDONE); // Wait for conversion completion
return ((ADRESH<<8)+ADRESL); // Return 10 bit ADC value
}
void InitUART(void)
{
TRISC6 = 1; // TX Pin
TRISC7 = 1; // RX Pin
SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
BRGH = 1; // Fast baudrate
SYNC = 0; // Asynchronous
SPEN = 1; // Enable serial port pins
CREN = 1; // Enable reception
SREN = 0; // No effect
TXIE = 0; // Disable tx interrupts
RCIE = 1; // Enable rx interrupts
TX9 = 0; // 8-bit transmission
RX9 = 0; // 8-bit reception
TXEN = 0; // Reset transmitter
TXEN = 1; // Enable the transmitter
}
void SendByteSerially(unsigned char Byte) // Writes a character to the serial port
{
while(!TXIF); // wait for previous transmission to finish
TXREG = Byte;
}
unsigned char ReceiveByteSerially(void) // Reads a character from the serial port
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}
while(!RCIF); // Wait for transmission to receive
return RCREG;
}
void SendStringSerially(const unsigned char* st)
{
while(*st)
SendByteSerially(*st++);
}
// Main Function
void main(void)
{
void InitUART(void);
void SendByteSerially(unsigned char);
unsigned char ReceiveByteSerially(void);
void SendStringSerially(const unsigned char*);
void InitADC(void);
unsigned int GetADCValue(unsigned char);
unsigned int ADC_value = 0;
unsigned int digit1, digit2, digit3, digit4;
InitADC(); // Initialize ADC
InitLCD(); // Initialize LCD
InitUART;
while(1)
{
ClearLCDScreen(); // Clear LCD screen
ADC_value = GetADCValue(AN7); // Read ADC value from RE2(AN7) pin
// ADC_value can have a value from 0 (0v) to 1023(5v) only.
// SO display 4 digits of ADC_value
digit1 = (unsigned int)(ADC_value/1000); // Calculate digit1 of ADC_value
digit2 = (unsigned int)((ADC_value - digit1*1000)/100); // Calculate digit2 of ADC_value
digit3 = (unsigned int)((ADC_value - (digit1*1000+digit2*100))/10); // Calculate digit3 of ADC_value
digit4 = (unsigned int)(ADC_value - (digit1*1000+digit2*100+digit3*10)); // Calculate digit4 of ADC_value
WriteDataToLCD(digit1+0x30); // Display digit1 of ADC_value on LCD
WriteDataToLCD(digit2+0x30); // Display digit2 of ADC_value on LCD
WriteDataToLCD(digit3+0x30); // Display digit3 of ADC_value on LCD
WriteDataToLCD(digit4+0x30); // Display digit4 of ADC_value on LCD
__delay_ms(500); // Half second delay before next reading
}
}