Home PICPIC Code PIC16F877 and ADC example

PIC16F877 and ADC example

In the following example project we will convert an analog input to channel 0 to 10 bit digital number and display it on an LCD

 

Schematic

PIC16F877 LCD ADC

PIC16F877 LCD ADC

Code

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
unsigned int adcvalue,value,temp_res;
unsigned char car,x,y;

// Routine to show the value of the ADC_read
void ShowADC(int x, int y, unsigned int adcvalue)
{
car = adcvalue / 1000;
LCD_Chr(x,y,48+car);
adcvalue=adcvalue-1000*car;
car = (adcvalue / 100);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-100*car;
car = (adcvalue / 10);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-10*car;
car = adcvalue;
LCD_Chr_CP(48+car);
delay_ms(30);
}

void main()
{
TRISA = 0xFF; // PORTA is input
TRISB = 0;
PORTB = 0;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
do {
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
adcvalue = temp_res;
ShowADC (1,1,adcvalue);
Delay_ms(300);

} while(1);

} // end main

 

Links
Microcontroller Programmer Kit Development Board for PIC microcontroller evaluation QL200 PIC

You may also like