-->

ADC reading with Internal adc of PIC16F77 and displaying on LCD

By
In PIC16F877 has 8 channels are available for adc input ie AN0 to AN7.Any one can be use ,we can select the the channel by selecting channel selection bit.

The Whole project Can be download at the Bottom of the post .


            In ADC the Vref voltage is important in accuracy of the ADC conversion.Low Vref gets more accuracy . But This PIC has a 10 bit result ion so it is enough As follows some example

If    Vref             =    4.096 volt
     Bit resolution  = 8 ie 2^8 =256
then  4.096/256 = 16Millvolt Per Bit
        3.88/256 = 15mv/bit
        2.56/256 = 10mv/bit
giving vcc as vref (5Volt) in 10 bit Resolution
Vref =5 v
Bit resolution  = 10 bit ,ie 2^10 = 1024
so
            5/1024 = 4.8828 millivolt /Bit
Here  taking 5 volt as vref

For getting ADC value the following steps are needed

 * Initialization of ADC
 * Reading ADC value


 Initialization of ADC

 Initialization of ADC mean that
Configure the ADC PORT  for that PIC has 4 registers they are
* ADCON 0
* ADCON 1
* ADRESH
*ADRESL

ADCON0 :- it is an 8 bit register it decide how the ADC worrks in accordance with the following bit have to set .
Lasr(msb bit) two bit decide the clock of the ADC ie the conversion speed.if we select the the main PIC crystal frequency as 20MHz
then I will select the First tw register value is 10
ie 10 = Fosc/32
after calculating the clock speed there must be a time 1.6 micro second for one bit conversion these time is called TAD . So to convert  10 bit  need 10x1.6 microsecond .,Look Data sheet page No .133 Section 11-2.

 I am selecting the ADC clock selection bit is 10

ADCS1  = 1
ADCS0  = 0
ie fosc/32    Look Datasheet Page No. 130 section 11.2

Calculation
            20Mhz/32 = 625000Hz ie 1.6Microsecond
           For one TAD is 1.6 is correct .(Look Data sheet page No .133 Section 11-2)

Then ,
The the 3rd,4the and 5th bit is the analog channel selection Bit from the Port AN0 to AN7.




The second bit(GO/DONE) is used to Start the conversion  of analog value to digital when its value is one .And automatically reset (become zero) whenever the completion take place  

 GO/DONE: A/D Conversion Status bit
When ADON = 1:
1 = A/D conversion in progress (setting this bit starts the A/D conversion which is automatically
cleared by hardware when the A/D conversion is complete)
0 = A/D conversion not in progress  

The Second Bit is not used Leave it as Zero

The First Bit is the Bit to Turn On the ADC .(powering on the ADC).

So the ADCON0 value is 10000001 in Hex a 0x81;

ADCON1 :-
         
         
       
The Bit From Bit 0 to Bit 3  are used decide  to How act the Analog PIN . How the Vref and GND voltage are given. I am selecting the first one
0000 becuase the 8 bit of AN is selected as Analog (denoted above as "A")
And the Vref is taken as VDD ie 5volt (VCC)
* channel selected
So the ADCON1 value is 10000000 in hex format = 0x80;

The Last Bit of ADCON1 is ADFM is used to alignment the result of adc (ie the value )
If its value is One The 10 bit adc value is right alimented
If its value is zero The 10 bit is left aligned

ADRESH :- is the Register (8bit) the result of ADC saved here . Our result is 10 bit if we use 10 bit resolution. But the ADRESH register is 8 bit So the 8 bit of the 10 bit adc result is saved in ADRESH the balance 2 bit is saved in ADRESL .



Circuit diagram 






Main Programm
/* 
* File: adc_main.c
* Author: gElectron/Gireesh
*
* Created on September 27, 2015, 6:24 AM
*/


// PIC16F877 Configuration Bit Settings

// 'C' source line config statements

#include
#include"lcd.h"
#include"adc.h"
#include
#define _XTAL_FREQ 20000000 // 20Mh
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF // FLASH Program Memory Write Enable (Unprotected program memory may not be written to by EECON control)


void main()
{
TRISB = 0X00; // output for the 8 bit
TRISA =0XFF;
int adc_value=0x00,ch_sec = 0;
unsigned char digits[5];
lcd_init(); // lcd initialization
adc_init(); // adc initialization
adc_value = read_adc(ch_sec); // reading adc value
string("gElectron ");
LINE2;
digits[0] = adc_value / 1000 + 48;
digits[1] = (adc_value / 100) % 10 + 48;
digits[2] = (adc_value / 10) % 10 + 48;
digits[3] = adc_value % 10 + 48;
digits[4] = '\0';
string(digits);

while(1); // endless loop
}

ADC file
/* 
* File: adc.c
* Author: Krishna
*
* Created on September 27, 2015, 7:14 AM
*/
#include
#include"adc.h"
//#define _XTAL_FREQ 20000000
void adc_init()
{

ADCON0 = 0x81;// 10000001 TAD is fosci/32 and last bit is adc poweron
ADCON1 = 0x80; //all port selected as output ,vref= vcc,
TRISA = 0x2f; //ENANLING AS ANALOG INPUT
TRISE = 0X07; // ENANLING AS ANALOG INPUT
}

unsigned int read_adc(unsigned char ch_selection)
{
if(ch_selection>7) // checking is channel greater than 7
return 0; // yes return zero
__delay_us(50); //adc acquisition time as 19.72 ?s as datasheet
GO_nDONE = 1;
while(GO_nDONE); // after conversion GO_nDONE value become 0 autmatically.
return((ADRESH<<8)+ADRESL);

Downlaod the Whole Project Including Proteus Simulation click heredownload

ADC reading with Internal adc of PIC16F77 and displaying on LCD

By
In PIC16F877 has 8 channels are available for adc input ie AN0 to AN7.Any one can be use ,we can select the the channel by selecting channel selection bit.

The Whole project Can be download at the Bottom of the post .


            In ADC the Vref voltage is important in accuracy of the ADC conversion.Low Vref gets more accuracy . But This PIC has a 10 bit result ion so it is enough As follows some example

If    Vref             =    4.096 volt
     Bit resolution  = 8 ie 2^8 =256
then  4.096/256 = 16Millvolt Per Bit
        3.88/256 = 15mv/bit
        2.56/256 = 10mv/bit
giving vcc as vref (5Volt) in 10 bit Resolution
Vref =5 v
Bit resolution  = 10 bit ,ie 2^10 = 1024
so
            5/1024 = 4.8828 millivolt /Bit
Here  taking 5 volt as vref

For getting ADC value the following steps are needed

 * Initialization of ADC
 * Reading ADC value


 Initialization of ADC

 Initialization of ADC mean that
Configure the ADC PORT  for that PIC has 4 registers they are
* ADCON 0
* ADCON 1
* ADRESH
*ADRESL

ADCON0 :- it is an 8 bit register it decide how the ADC worrks in accordance with the following bit have to set .
Lasr(msb bit) two bit decide the clock of the ADC ie the conversion speed.if we select the the main PIC crystal frequency as 20MHz
then I will select the First tw register value is 10
ie 10 = Fosc/32
after calculating the clock speed there must be a time 1.6 micro second for one bit conversion these time is called TAD . So to convert  10 bit  need 10x1.6 microsecond .,Look Data sheet page No .133 Section 11-2.

 I am selecting the ADC clock selection bit is 10

ADCS1  = 1
ADCS0  = 0
ie fosc/32    Look Datasheet Page No. 130 section 11.2

Calculation
            20Mhz/32 = 625000Hz ie 1.6Microsecond
           For one TAD is 1.6 is correct .(Look Data sheet page No .133 Section 11-2)

Then ,
The the 3rd,4the and 5th bit is the analog channel selection Bit from the Port AN0 to AN7.




The second bit(GO/DONE) is used to Start the conversion  of analog value to digital when its value is one .And automatically reset (become zero) whenever the completion take place  

 GO/DONE: A/D Conversion Status bit
When ADON = 1:
1 = A/D conversion in progress (setting this bit starts the A/D conversion which is automatically
cleared by hardware when the A/D conversion is complete)
0 = A/D conversion not in progress  

The Second Bit is not used Leave it as Zero

The First Bit is the Bit to Turn On the ADC .(powering on the ADC).

So the ADCON0 value is 10000001 in Hex a 0x81;

ADCON1 :-
         
         
       
The Bit From Bit 0 to Bit 3  are used decide  to How act the Analog PIN . How the Vref and GND voltage are given. I am selecting the first one
0000 becuase the 8 bit of AN is selected as Analog (denoted above as "A")
And the Vref is taken as VDD ie 5volt (VCC)
* channel selected
So the ADCON1 value is 10000000 in hex format = 0x80;

The Last Bit of ADCON1 is ADFM is used to alignment the result of adc (ie the value )
If its value is One The 10 bit adc value is right alimented
If its value is zero The 10 bit is left aligned

ADRESH :- is the Register (8bit) the result of ADC saved here . Our result is 10 bit if we use 10 bit resolution. But the ADRESH register is 8 bit So the 8 bit of the 10 bit adc result is saved in ADRESH the balance 2 bit is saved in ADRESL .



Circuit diagram 






Main Programm
/* 
 * File:   adc_main.c
 * Author: gElectron/Gireesh
 *
 * Created on September 27, 2015, 6:24 AM
 */


// PIC16F877 Configuration Bit Settings

// 'C' source line config statements

#include 
#include"lcd.h"
#include"adc.h"
#include 
#define _XTAL_FREQ 20000000 // 20Mh
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF        // FLASH Program Memory Write Enable (Unprotected program memory may not be written to by EECON control)


void main()
{
    TRISB = 0X00;  // output for the 8 bit
    TRISA =0XFF;
    int adc_value=0x00,ch_sec = 0;
    unsigned char digits[5];
    lcd_init();   // lcd initialization
    adc_init();   // adc initialization
    adc_value = read_adc(ch_sec);  // reading adc value
    string("gElectron ");
    LINE2;
        digits[0] = adc_value / 1000 + 48;
        digits[1] = (adc_value / 100) % 10 + 48;
        digits[2] = (adc_value / 10) % 10 + 48;
        digits[3] = adc_value % 10 + 48;
        digits[4] = '\0';
        string(digits);
   
    while(1);             // endless loop
}

ADC file
/* 
 * File:   adc.c
 * Author: Krishna
 *
 * Created on September 27, 2015, 7:14 AM
 */
#include
#include"adc.h"
//#define _XTAL_FREQ 20000000
void adc_init()
{

   ADCON0 = 0x81;// 10000001 TAD is fosci/32 and last bit is adc poweron
   ADCON1 = 0x80; //all port selected as output ,vref= vcc,
   TRISA = 0x2f; //ENANLING AS  ANALOG INPUT
   TRISE = 0X07; // ENANLING AS  ANALOG INPUT
}

unsigned int read_adc(unsigned char ch_selection)
{
 if(ch_selection>7) // checking is  channel greater than 7
      return 0;      // yes return zero
  __delay_us(50); //adc  acquisition time as 19.72 ?s as datasheet
  GO_nDONE = 1;
 while(GO_nDONE); // after conversion GO_nDONE value become 0 autmatically.
 return((ADRESH<<8)+ADRESL);
 
} 
Downlaod the Whole Project Including Proteus Simulation click heredownload

LCD Interfacing with PIC16F877 by using MIKRO C Compiler

By
              LCD Interfacing with PIC16F877 by using MIKRO C Compiler 

            To interface a LCD 16x2 with 16F877 microcontroller PIC 16F887 microcontroller  ,First we need to learn LCD 16x2  second PIC 16F887 microcontroller . 

     At last we need to learn a compiler to program and debug. I am here taking the compiler called MIKRO C . which is very simple 

              Mikro C i s very Simple compiler .It can be use without  deep knowledge in programming . the great advantage of Mikro C it has a lot of helpful libraries which can be easily understandable .Anyway the hex file generated by mikroc  is large comparability  with other compiler.

 Then  draw the circuit diagram as we want .In that circuit diagram decide how and where the Pin of LCD connect with PIC16f877 . 
Here I used 4-bit lcd interfacing (to know more click here. )
 circuit drawn  .
So we want to write program in accordance with the above circuit.
In Mikro c there is library for interfacing the  LCD it is very simple .
First the Pin number of the PIC change to a understandable one for that we do as follows .
 sbit LCD_RS at RB4_bit;
 The above line of code means that the compiler changes RB4 pin of the PIC to LCD_RS . after the initialization of the above code the RB4 is called as LCD_RS through the program. So the PINs are configuring as follows .

  •  sbit LCD_RS at RB4_bit; // PIN name Changed 
  •  sbit LCD_EN at RB5_bit;
  •  sbit LCD_D7 at RB3_bit;
  •  sbit LCD_D6 at RB2_bit; 
  •  sbit LCD_D5 at RB1_bit;
  •  sbit LCD_D4 at RB0_bit;                                                                                                           Then the Port PB is makes as output the following code is needed . The compiler recognize it as output 


  •  sbit LCD_RS_Direction at TRISB4_bit; // making output 
  •  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;

Next step is to initialize the LCD . .

void Lcd_Init();

It means that the compiler tells how the PIC connect to the LCD 
that is the PIN renaming , what type of LCD Modes (8bit or 4bit ) etc .

Next Step is giving the LCD Command by using LCD_Command function (ie Lcd_Cmd() .

which decide how the lcd shows look the command below .



for example to clear the LCD we put the following function 

Lcd_Cmd(_LCD_ClEAR);


To write Messages on LCD dispaly we use the following simple function 
void Lcd_Out(char row, char column, char *text);

For example Write text "Hello!" on Lcd starting from row 1, column 3:

Lcd_Out(1, 3, "Hello!"); 

It means the first Number "1"  the starting row .ie first line 
The secind Number specify the starting position of column .
the Last one is the message .

Another helpful  function is 
 void Lcd_Out_Cp(char *text);

 Example : Lcd_Out_Cp("Here!");

It uses to display the text message at the current position .


 void Lcd_Chr(char row, char column, char out_char);

The above function is used to display the value which is placed in variable 

Example : Lcd_Chr(2, 3, 'i');

It displays the 'I" value in the second line  at the third position .

Full Programm
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;


void main()

{
Lcd_Init();
Lcd_Cmd(_LCD_Clear);
Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_out(1,1,"Hello");

while(1)
{}

}
Full Project Can be download here Including Proteus File click

LCD Interfacing with PIC16F877 by using MIKRO C Compiler

By
              LCD Interfacing with PIC16F877 by using MIKRO C Compiler 

            To interface a LCD 16x2 with 16F877 microcontroller PIC 16F887 microcontroller  ,First we need to learn LCD 16x2  second PIC 16F887 microcontroller . 

     At last we need to learn a compiler to program and debug. I am here taking the compiler called MIKRO C . which is very simple 

              Mikro C i s very Simple compiler .It can be use without  deep knowledge in programming . the great advantage of Mikro C it has a lot of helpful libraries which can be easily understandable .Anyway the hex file generated by mikroc  is large comparability  with other compiler.

 Then  draw the circuit diagram as we want .In that circuit diagram decide how and where the Pin of LCD connect with PIC16f877 . 
Here I used 4-bit lcd interfacing (to know more click here. )
 circuit drawn  .
So we want to write program in accordance with the above circuit.
In Mikro c there is library for interfacing the  LCD it is very simple .
First the Pin number of the PIC change to a understandable one for that we do as follows .
 sbit LCD_RS at RB4_bit;
 The above line of code means that the compiler changes RB4 pin of the PIC to LCD_RS . after the initialization of the above code the RB4 is called as LCD_RS through the program. So the PINs are configuring as follows .

  •  sbit LCD_RS at RB4_bit; // PIN name Changed 
  •  sbit LCD_EN at RB5_bit;
  •  sbit LCD_D7 at RB3_bit;
  •  sbit LCD_D6 at RB2_bit; 
  •  sbit LCD_D5 at RB1_bit;
  •  sbit LCD_D4 at RB0_bit;                                                                                                           Then the Port PB is makes as output the following code is needed . The compiler recognize it as output 


  •  sbit LCD_RS_Direction at TRISB4_bit; // making output 
  •  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;

Next step is to initialize the LCD . .

void Lcd_Init();

It means that the compiler tells how the PIC connect to the LCD 
that is the PIN renaming , what type of LCD Modes (8bit or 4bit ) etc .

Next Step is giving the LCD Command by using LCD_Command function (ie Lcd_Cmd() .

which decide how the lcd shows look the command below .



for example to clear the LCD we put the following function 

Lcd_Cmd(_LCD_ClEAR);


To write Messages on LCD dispaly we use the following simple function 
void Lcd_Out(char row, char column, char *text);

For example Write text "Hello!" on Lcd starting from row 1, column 3:

Lcd_Out(1, 3, "Hello!"); 

It means the first Number "1"  the starting row .ie first line 
The secind Number specify the starting position of column .
the Last one is the message .

Another helpful  function is 
 void Lcd_Out_Cp(char *text);

 Example : Lcd_Out_Cp("Here!");

It uses to display the text message at the current position .


 void Lcd_Chr(char row, char column, char out_char);

The above function is used to display the value which is placed in variable 

Example : Lcd_Chr(2, 3, 'i');

It displays the 'I" value in the second line  at the third position .

Full Programm
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;


 void main()

   {
      Lcd_Init();
      Lcd_Cmd(_LCD_Clear);
      Lcd_Cmd(_LCD_CURSOR_OFF);
      
      Lcd_out(1,1,"Hello");
      
      while(1)
      {}

   }

Full Project Can be download here Including Proteus File click

ADC Interfacing with 8051

By
ADC Interfacing with 8051


Read the Introduction here for the ADC  tutorials .   For LCD tutorials Click here

Both the above two tutorials combined  and  tried to read the adc value and Displaying the value in 16x2 LCD only for the learning purpose . If you have any doubt ask me By emailing thnnara123@gmail.com ,or comment below .


 Circuit 



Program In written Keil  with C compiler 
**********************************************************************************
Main file // ADC File
***********************************************************************************
#include <REGX51.H>  
#include "lcd.h" 
#include"adc.h"
#define test_port P2
#include<stdio.h>

void main(){
unsigned char buffer[8];

int adc_value;
LCDPORT = 0x00;
adc_port = 0xFF;
test_port =0x00;
lcd_init();
string("adc ");
get_adc();
P2 = adc_port;
adc_value = 0x60;//adc_port;
LINE2
sprintf(buffer,"%d",adc_value);   // used to convert Binary value from ADC to ASCI value for Lcd

string(buffer);

while(1);

}

***********************************************************************************
    adc.h // ADC Header File
************************************************************************************
#define adc_port P3
sbit RD_adc = P1^0;
sbit WR_adc = P1^1;
sbit INTR = P1^2; 

 get_adc()
 { 
  
 WR_adc = 0;
 delay(1);
 WR_adc =1;
 while(INTR==1);
 RD_adc = 1;
 delay(1);
 RD_adc = 0;

  }
**********************************************************************************
LCD.h // LCD Header File
***********************************************************************************
 #include"delay.h"
#define LCDPORT P0 // named the Port2 as LCDPORT
sbit RS=LCDPORT^2; // P2.2 named RS
sbit E=LCDPORT^3; // P2.3 named as E
#define LINE2 lcd_cmd(0xc0); // used to display the second line oxc0 is


void latch(void) // used to a high to low pulse the pin E
{
E = 1;
delay(1);
E = 0;
}

void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
RS = 0; // send a '0' value to select to send command
delay(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
delay(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}

void lcd_data(unsigned char c)
{
RS =1; // send 1 to send data
delay(1);
LCDPORT = c & 0xf0 | 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
delay(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}

void lcd_init()
{
delay(20);
lcd_cmd(0x30); //as per data sheet
delay(20);
lcd_cmd(0x30); //as per data sheet
delay(4);
lcd_cmd(0x32); //as per data sheet
delay(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}

void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}

***********************************************************************************
To download the Whole Project Click here

ADC Interfacing with 8051

By
ADC Interfacing with 8051


Read the Introduction here for the ADC  tutorials .   For LCD tutorials Click here

Both the above two tutorials combined  and  tried to read the adc value and Displaying the value in 16x2 LCD only for the learning purpose . If you have any doubt ask me By emailing thnnara123@gmail.com ,or comment below .


 Circuit 



Program In written Keil  with C compiler 
**********************************************************************************
Main file // ADC File
***********************************************************************************
#include <REGX51.H>  
#include "lcd.h" 
#include"adc.h"
#define test_port P2
#include<stdio.h>

void main(){
unsigned char buffer[8];

int adc_value;
LCDPORT = 0x00;
adc_port = 0xFF;
test_port =0x00;
lcd_init();
string("adc ");
get_adc();
P2 = adc_port;
adc_value = 0x60;//adc_port;
LINE2
sprintf(buffer,"%d",adc_value);   // used to convert Binary value from ADC to ASCI value for Lcd

string(buffer);

while(1);

}

***********************************************************************************
    adc.h // ADC Header File
************************************************************************************
#define adc_port P3
sbit RD_adc = P1^0;
sbit WR_adc = P1^1;
sbit INTR = P1^2; 

 get_adc()
 { 
  
 WR_adc = 0;
 delay(1);
 WR_adc =1;
 while(INTR==1);
 RD_adc = 1;
 delay(1);
 RD_adc = 0;

  }
**********************************************************************************
LCD.h // LCD Header File
***********************************************************************************
 #include"delay.h"
 #define LCDPORT P0  // named the Port2 as LCDPORT
 sbit RS=LCDPORT^2;  // P2.2 named RS
 sbit E=LCDPORT^3;  // P2.3 named as E
 #define LINE2 lcd_cmd(0xc0);   // used to display the second line oxc0 is 
 
 
 void latch(void)  // used to a high to low pulse the pin E 
{
    E = 1;
    delay(1);
    E = 0;
}

void lcd_cmd(unsigned char c)   // used to send the command / Instruction to the lcd port 
{          
RS = 0;    // send a '0' value to select to send command
delay(1);     
LCDPORT = c & 0xf0;  //  send  the command c only 4 bit by masking the lower bit 
latch();
delay(1); 
LCDPORT =  (c << 4); // giving the lowerbit  by shifting the 4 bit to left 
latch(); 
}

 void lcd_data(unsigned char c)
 {
 RS =1;    // send 1 to send data 
 delay(1);
LCDPORT = c & 0xf0 | 0x4; //send  the data  only  4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
delay(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit  by shifting the 4 bit to left 
latch(); 
}
 
void lcd_init()
{
  delay(20);
    lcd_cmd(0x30);  //as per data sheet
  delay(20);
    lcd_cmd(0x30);    //as per data sheet
  delay(4);
    lcd_cmd(0x32);    //as per data sheet
 delay(4);
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x0c);            // Make cursorinvisible
    lcd_cmd(0x6);            // Set entry Mode(auto increment of cursor)
}

 void string(const char *q)    // used to send single charcter to display the lcd 
{
    while (*q) {
        lcd_data(*q++);
    }
}
***********************************************************************************
To download the Whole Project Click here

LCD 4 Bit Interfacing with AVR

By
LCD 4 Bit Interfacing with AVR  -Atmega16

After interfacing with 8051 and PIC16f877  am providing AVR interfacing to LCD ,with a light changing in the previous code (ClikHere) .

I am taken Atmeg16 Controller . 




************* Please Comment If you have Doubt **********


/*
 * LCD4bit.c
 *
 * Created: 16-Sep-15 10:22:26 PM
 * Author: Krishna
 */

#define F_CPU 12000000UL
#include <avr/io.h>
#include<util/delay.h>
#define LCDPORT PORTD // Renaming the PORTD to LCDPORT
#define RS PD2 // Renaming the RS pin to 2
#define E PD3 // Renaming the E to number 3
#define LINE2 lcd_cmd(0xc0);
void latch(void) // used to a high to low pulse the pin E
{
PORTD |= ~(1<<E); // here we give a high to PORTD.3
_delay_ms(1);
PORTD |= (1<<E); // Here we give a LOW to PORTD.#
}




void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
PORTD |= ~(1<<RS); // send a '0' value to select to send command
_delay_ms(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
_delay_ms(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}

 void gotoxy(unsigned char  x,unsigned char y)  
{
if(x<40)
{
if(y) x|=0b01000000;
x|=0b10000000;
lcd_cmd(x);
}

void lcd_data(unsigned char c)
{
PORTD |= (1<<RS); // send 1 to send data
_delay_ms(1);
LCDPORT = c & 0xf0| 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
_delay_ms(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}

void lcd_init()
{
_delay_ms(20);
lcd_cmd(0x30); //as per data sheet
_delay_ms(20);
lcd_cmd(0x30); //as per data sheet
_delay_ms(4);
lcd_cmd(0x32); //as per data sheet
_delay_ms(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}

void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}


void main(){

DDRD = 0xFF;
lcd_init();
string("Lcd Testing ");
LINE2
string("its displaying ");
while(1);
}



Circuit Diagram 
Downloa here the Full project including Proteus Click here

LCD 4 Bit Interfacing with AVR

By
LCD 4 Bit Interfacing with AVR  -Atmega16

After interfacing with 8051 and PIC16f877  am providing AVR interfacing to LCD ,with a light changing in the previous code (ClikHere) .

I am taken Atmeg16 Controller . 




************* Please Comment If you have Doubt **********


/*
 * LCD4bit.c
 *
 * Created: 16-Sep-15 10:22:26 PM
 *  Author: Krishna
 */ 

#define F_CPU 12000000UL
#include <avr/io.h>
#include<util/delay.h>
#define LCDPORT PORTD   // Renaming the PORTD to LCDPORT
#define RS PD2          // Renaming the RS pin to 2 
#define E PD3            // Renaming the E to number 3
#define LINE2 lcd_cmd(0xc0);  
 void latch(void)  // used to a high to low pulse the pin E
 {
  PORTD |= ~(1<<E);  // here we give a high to PORTD.3
 _delay_ms(1);
  PORTD |= (1<<E);   // Here we give a LOW to PORTD.#
 }
 
 
 
 
 void lcd_cmd(unsigned char c)   // used to send the command / Instruction to the lcd port
 {
  PORTD |= ~(1<<RS);    // send a '0' value to select to send command
  _delay_ms(1);
  LCDPORT = c & 0xf0;  //  send  the command c only 4 bit by masking the lower bit
  latch();
  _delay_ms(1);
  LCDPORT =  (c << 4); // giving the lowerbit  by shifting the 4 bit to left
  latch();
 }

 void gotoxy(unsigned char  x,unsigned char y)  
{
 if(x<40)
 {
  if(y) x|=0b01000000;
  x|=0b10000000;
  lcd_cmd(x);
  }
 
 void lcd_data(unsigned char c)
 {
  PORTD |= (1<<RS);    // send 1 to send data
  _delay_ms(1);
  LCDPORT = c & 0xf0| 0x4; //send  the data  only  4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
  _delay_ms(1);
  latch();
  
  LCDPORT = (c << 4)| 0x4; ; // giving the lower bit  by shifting the 4 bit to left
  latch();
 }
 
  void lcd_init()
  {
   _delay_ms(20);
   lcd_cmd(0x30);  //as per data sheet
   _delay_ms(20);
   lcd_cmd(0x30);    //as per data sheet
   _delay_ms(4);
   lcd_cmd(0x32);    //as per data sheet
   _delay_ms(4);
   lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
   lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
   lcd_cmd(0x0c);            // Make cursorinvisible
   lcd_cmd(0x6);            // Set entry Mode(auto increment of cursor)
  }
 
  void string(const char *q)    // used to send single charcter to display the lcd
  {
   while (*q) {
    lcd_data(*q++);
   }
  }
 
 
void main(){
 
  DDRD = 0xFF;
 lcd_init();
  string("Lcd Testing ");
 LINE2
 string("its displaying ");
 while(1);
}


Circuit Diagram 
Downloa here the Full project including Proteus Click here

4-Bit LCD Interfacing with PIC 16f877 -MPLABX

By
4-Bit LCD Interfacing with PIC 16f877 -MPLABX :-

The Previous Post click Here talked about How to interface LCD in 4 bit mode with 8051 . with a little changes of the previous  code, we can use it in PIC16F877a with MPLABx ..




#include 
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#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)


#define _XTAL_FREQ 8000000
#define LCDPORT PORTB
#define RS RB2
#define E RB3
//#define LIINE2 lcd_cmd(0xc0)

void latch(void)
{
E = 1;
__delay_ms(1);
E = 0;

}
void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
RS = 0; // send a '0' value to select to send command
__delay_ms(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
__delay_ms(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}
void lcd_data(unsigned char c)
{
RS =1; // send 1 to send data
__delay_ms(1);
LCDPORT = c & 0xf0 | 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
__delay_ms(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}

void lcd_init()
{
__delay_ms(20);
lcd_cmd(0x30); //as per data sheet
__delay_ms(20);
lcd_cmd(0x30); //as per data sheet
__delay_ms(4);
lcd_cmd(0x32); //as per data sheet
__delay_ms(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}

void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}


void main()
{
TRISB = 0x00;

lcd_init();
string("Lcd working ");
lcd_cmd(0xc0);
string("its displaying ");
while(1);
}


Download the full project including Proteus File Clik here

More Posts

gElectron. Powered by Blogger.

Contributors

16x2 LCD Interfacing with STM32,STM32F103C6

 16x2 LCD  Interfacing with STM32,STM32F103C6 lcd_init(); LCD_LINE1; lcd_String(" GeElectron"); LCD_LINE2; lc...

Contact Form

Name

Email *

Message *

Contact us

Name

Email *

Message *

Follow Us

https://www.facebook.com/gElectron-393939667321867/ FBbox/https://www.facebook.com/IVYthemes

Comments

[blogger]

MKRdezign

Test

Latest

[recent][newsticker]

Technology

Top Ads

RECENT COMMENTS

Subscribe Via Email

Subscribe to our newsletter to get the latest updates to your inbox. ;-)


Your email address is safe with us!

RECENT COMMENTS