Sunday, January 12, 2014

Interfacing a 2x16 Alpha Numeric LCD Using PIC Microcontroller to Display ADC Value.

circuit diagram for interfacing LCD with PIC 16F870 Microcontroller.



Interfacing LCD with a micro controller is often necessary when you are doing a project or some expriement. So lets see how to do this in this tutorial.

The micro controller we are going to use for this purpose is PIC 16F870 manufactured by Microchip Inc.
It is a 28 Pin IC available in DIP package also.

The compiler used is PIC CCS C and here is the source code below:

#include <16f870.h>
#use delay(clock=4000000)
int a,b,c,value;

void lcd_cmd()                       ////////function to execute command to LCD
{
      output_low(pin_b1);
      output_high(pin_b0);
      delay_us(50);
      output_low(pin_b0);
}
void lcd_data()                         ///////function to display data
{
      output_high(pin_b1);
      output_high(pin_b0);
      delay_us(50);
      output_low(pin_b0);
}
void lcd_init()                          ///////function to initialize LCD
{
      output_c(0x38);
      lcd_cmd();
      output_c(0x0c);
      lcd_cmd();
      output_c(0x80);
      lcd_cmd();
      output_c(0x06);
      lcd_cmd();
      output_c(0x01);
      lcd_cmd();
      delay_ms(50);
}
void lcd_clr()                                   //////////clear LCD
{
      output_c(0x01);
      lcd_cmd();
}
void split_disp()                             /////// split and display in LCD
{
   a=(value/100) | 0x30;
   output_c(0xc0);
   lcd_cmd();
   output_c(a);
   lcd_data();

   b=((value%100)/10) | 0x30;
   output_c(b);
   lcd_data();

   c=((value%100)%10) | 0x30;
   output_c(c);
   lcd_data();
}
void main()
{
   setup_adc_ports(RA0_ANALOG);                   ///all analog pins
   setup_adc(ADC_CLOCK_INTERNAL);         ///adc clock internal
   set_adc_channel(0);                                           ////select adc channel
   lcd_init();                                                           ////initialize LCD
   delay_ms(100);
   while(true)
   {
      delay_ms(200);                                              ///////delay for adc conversion
      value=read_adc();                                          //////read adc value
      split_disp();                                                    ///////call to display
   }
}

Just assemble the above circuit and compile the program to use. I had checked the program and its working.