-->

Microcontroller Based ammeter and voltmeter using Atmega 16

By
Here  Atmega16  measuring the  current and voltage by inbuilt comparator  .Previous post described how to read ADC value click here . Two  ADC port is used  PA0/ADC0  is  measuring voltage .And PA1/ADC1 is  measuring current .

For Advancedtutorials click here Voltmeter Design

                We have to measure 0 to 12 volt, but controller can only read as maximum voltage 5 volt .
Hence a voltage divider circuit used to reduce the corresponding voltage level  at a mximum 5Volt.










take
Vin  = 12volt
Vout = 5 volt.
Assume one of the resistor value and find out the other .
***********************************************************************************
  we have to measure current ,But we cant directly measure the current . there are more technique to measure the current. we uses a shunt resistor method .
The millivoltmeter voltage is taken and calculated by some equation and displayed as current .for more click here

For example .
According Ohm's law,  0.1 Ohm resistor under 1A current will give 0.1V voltage drop
*********************************************************************************




                                             The program shown below
* Created: 14-Oct-15 6:44:15 PM
 *  Author: Gireesh 
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include<util/delay.h>
#include"lcd.h"
#include "adc.h"

int main(void)
  
{   
 
 unsigned char volt[5] ,current[5];
   DDRD = 0xFF;
   int v,i;
   char ch_sec =0;
    adc_init();
    lcd_init(); 
 string ("G Electron ");
 LINE2
 string("Power Supply");
 _delay_ms(50);
 CLEAR
 while(1)
 
 {
   v =   read_adc(0);  // reading voltage    
   i =   read_adc(1);  // reading voltage for current  
   v = (v * 4.89 * 2.4)/10;
   i = (i * 2.92);
   i = ((i*4.89)/0.47)/10;
   
   
      
   if(v< 1200)
   {
   volt[0] = ((v /1000)%10)+0x30;
   volt[1] = ((v /100) % 10) + 0x30;
   volt[2] = '.';
   volt[3] = ((v/ 10) %10) + 0x30;
   volt[4] = (v % 10) + 0x30 ;
   
   
   string("Volt    : ");
   for(int i =0; i<5;i++)
  lcd_data(volt[i]); // just displaying ADC value 
  string("V");
  }
  else
  {
 
   string("Over Voltage");
    }
 LINE2
 if(i<3100)
 {
 current[0] =((i /1000)%10)+0x30;
 current[1] = '.';
 current[2] = ((i /100) % 10) + 0x30;
 current[3] = ((i/ 10) %10) + 0x30;
 string("Current : ");
 for(int i =0; i<4;i++)
 lcd_data(current[i]);
 string("A");
 } 
 else
 {
  
  string("Over Current");
 }
 _delay_ms(50);
 CLEAR 
 }  
}
design circuits to feed controller for current and Volt. the whole project can be download Below including proteus file For downloading click below
      
                                                 Download

ADC Reading With Internal Adc Of AVR - Atemga 32 And Displaying On LCD

By

Download HereADC Reading With Internal Adc Of  AVR - Atemga 32 And Displaying On LCD

According to data sheet there are 3 registers are used to works the ADC in AVR atmega 32 
ie , 

1) ADMUX
2)ADSCR
3)ADC



1) ADMUX :-

 It is an 8 bit Register ,each bit has some functions as follows 

REFS1 & REFS0 :- is used for the reference Voltage  with respect to our analog input (for resolution)  to know more search this blog.
I am giving 5volt as reference so selected REFS1 = 0 & REFS0 = 1

ADMUX=(1<<REFS0); // AVcc with external capacitor at AREF  ,giving a high to REFS0.

MUX0-MUX4  :- it is used to select the input channel ,ie 8  analog input channel can be given so desired one selection by giving appropriate value .
  ADLAR : - used to shift the resulted data (ADC Value) left or right .I am using right shift so makes as low.
.....................................................................................................................................

2) ADSCR :-  ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);

It is also an 8 bit register .

Bit 7 used to ADC ON (ADC Turn ONing by applying a high bit )
Bit6 ADSC is used to start the conversion .and still maintain it value as high  up to the conversion . after conversion it becomes low (Zero)
ADATE and ADIF is not used by me now .(not by using Intruppet
Bit 0 to bit2  is used for select ADC frequency . 

\
2) ADC: It is a 16 bit to 8 bit register ADCL & ADCH. By applying right shift value it is autmatically set as normal value so we can take simple value from it

 eg :  return (ADC);
 adc_value = read_adc(ch_sec); 

The program as follows . The full program can be download below including Proteus simulation file.






ADC Reading With Internal Adc Of AVR - Atemga 32 And Displaying On LCD

By

Download HereADC Reading With Internal Adc Of  AVR - Atemga 32 And Displaying On LCD

According to data sheet there are 3 registers are used to works the ADC in AVR atmega 32 
ie , 

1) ADMUX
2)ADSCR
3)ADC



1) ADMUX :-

 It is an 8 bit Register ,each bit has some functions as follows 

REFS1 & REFS0 :- is used for the reference Voltage  with respect to our analog input (for resolution)  to know more search this blog.
I am giving 5volt as reference so selected REFS1 = 0 & REFS0 = 1

ADMUX=(1<<REFS0); // AVcc with external capacitor at AREF  ,giving a high to REFS0.

MUX0-MUX4  :- it is used to select the input channel ,ie 8  analog input channel can be given so desired one selection by giving appropriate value .
  ADLAR : - used to shift the resulted data (ADC Value) left or right .I am using right shift so makes as low.
.....................................................................................................................................

2) ADSCR :-  ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);

It is also an 8 bit register .

Bit 7 used to ADC ON (ADC Turn ONing by applying a high bit )
Bit6 ADSC is used to start the conversion .and still maintain it value as high  up to the conversion . after conversion it becomes low (Zero)
ADATE and ADIF is not used by me now .(not by using Intruppet
Bit 0 to bit2  is used for select ADC frequency . 

\
2) ADC: It is a 16 bit to 8 bit register ADCL & ADCH. By applying right shift value it is autmatically set as normal value so we can take simple value from it

 eg :  return (ADC);
 adc_value = read_adc(ch_sec); 

The program as follows . The full program can be download below including Proteus simulation file.






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

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