-->
Showing posts with label 8051. Show all posts
Showing posts with label 8051. Show all posts

LCD 4-bit Mode Interfacing

By
In  LCD 4-bit Mode Interfacing  we need to connect only 4+3 wire to LCD from Microcontroler . So we save 4 pins of micro controller.But we need to send 8 bit data or command to lcd by using   4 wire How ?
 By spiting the data or command  into two parts (4 bit nibble) ie ,lower 4 bits and higher 4 bits (the technique is called masking ). 
We need first initialize the LCD  as 4 bit according to the data sheet .
that is as follows
Powering LCD .Wait for abour 20mS
Send the first init value (0x30) 
Wait for about 10mS
Send second init value (0x30)  
Wait for about 1mS
Send third init value (0x30)
Wait for 1mS
Select bus width  (0x20) so we give 0x20 and selected as 4 bit mode
Wait for 1mS

Then, we send higher4bit  and then lower 4bit . This means in both command and data sending function we need to separate the higher 4-bits and lower 4-bits.




Mask lower 4-bits
Send to the LCD port (that to 4 pins of the LCD )
Send enable signal  
Mask higher 4-bits
Send to LCD port
Send enable signal

 Programming section as follows 

I am using the controller 8051  with the compailer Keil  and with C language .

The port 2 of the 8051  is used . the circuit as follows




                         LCD.h header File


 #include"delay.h"
#define LCDPORT P2 // 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++);
}
}



Finaly The code is lcd.c

#include <REGX51.H>   
#include "lcd.h"


void main(){
lcd_init();
string("Lcd Testing ");
LINE2
string("its displaying ");
while(1);
}

Download the full project including Proteus File Clik here

LCD 4-bit Mode Interfacing

By
In  LCD 4-bit Mode Interfacing  we need to connect only 4+3 wire to LCD from Microcontroler . So we save 4 pins of micro controller.But we need to send 8 bit data or command to lcd by using   4 wire How ?
 By spiting the data or command  into two parts (4 bit nibble) ie ,lower 4 bits and higher 4 bits (the technique is called masking ). 
We need first initialize the LCD  as 4 bit according to the data sheet .
that is as follows
Powering LCD . Wait for abour 20mS
Send the first init value (0x30) 
Wait for about 10mS
Send second init value (0x30)  
Wait for about 1mS
Send third init value (0x30)
Wait for 1mS
Select bus width  (0x20) so we give 0x20 and selected as 4 bit mode
Wait for 1mS

Then, we send higher4bit  and then lower 4bit . This means in both command and data sending function we need to separate the higher 4-bits and lower 4-bits.




Mask lower 4-bits
Send to the LCD port (that to 4 pins of the LCD )
Send enable signal  
Mask higher 4-bits
Send to LCD port
Send enable signal

 Programming section as follows 

I am using the controller 8051  with the compailer Keil  and with C language .

The port 2 of the 8051  is used . the circuit as follows




                         LCD.h header File


 #include"delay.h"
 #define LCDPORT P2  // 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++);
    }
}



Finaly The code is lcd.c

#include <REGX51.H>   
#include "lcd.h"


void main(){
lcd_init();
string("Lcd Testing ");
LINE2
string("its displaying ");
while(1);
}

Download the full project including Proteus File Clik here

LED Blinking Program by using PIC 16f877a MPLAB X .

By
 compiler - sdcc ,XC8.
 


#define _XTAL_FREQ 8000000 // setting the crystal frequency 

#include <xc.h> // Header file inclusion ,to add function like _delay_ms()

#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 = ON // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)



int main()
{
TRISB0 = 0; //RB0 as Output PIN
while(1) // endless loop
{
RB0 = 1; // LED ON
__delay_ms(1000); // 1 Second Delay
RB0 = 0; // LED OFF
__delay_ms(1000); // 1 Second Delay
}
return 0;
}




LED Blinking By using 8051 in Keil IDE
/led-blinking-program-by-using-atmega32

LED Blinking Program by using PIC 16f877a MPLAB X .

By
 compiler - sdcc ,XC8.
 


#define _XTAL_FREQ 8000000 // setting the crystal frequency 

#include <xc.h>           // Header file inclusion ,to add function like _delay_ms() 

#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 = ON         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)



int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  while(1)    // endless loop
  {
    RB0 = 1;  // LED ON
    __delay_ms(1000); // 1 Second Delay
    RB0 = 0;  // LED OFF
    __delay_ms(1000); // 1 Second Delay
  }
  return 0;
}




LED Blinking By using 8051 in Keil IDE
/led-blinking-program-by-using-atmega32

LED Blinking By using 8051 in Keil IDE

By
sbit LED1 = P2^0;  // P2.0 of the pin name cahnged to LED1
void delay(); // a delayis used to give some delay in between turn ON and OFF

void main()
{
while(1)
{

LED1 = 0;

delay();

LED1 = 1 ;
delay();

}
}

void delay()
{
int j;

for(j=0; j<500; j++)
;
}

LED Blinking By using 8051 in Keil IDE

By
sbit LED1 = P2^0;  // P2.0 of the pin name cahnged to LED1
void delay();    // a delayis used  to give some delay in between turn ON  and OFF  

void main()
{
  while(1)
  {
  
 LED1 = 0;
 
 delay();

 LED1 = 1 ;
 delay();
 
   }
}

 void delay()
 {
   int j;
     
    for(j=0; j<500; j++)
    ;
 }

LCD 4-BIT MODE with 8051

By
LCD 4-BIT MODE with 8051

TO know the Basic of LCD go to the previous post click here .
There are many reasons why sometime we prefer to use LCD in 4-bit mode instead of 8-bit. One basic reason is lesser number of pins are needed to interface LCD.

In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
  • Wait for abour 20mS
  • Send the first init value (0x30)
  • Wait for about 10mS
  • Send second init value (0x30)
  • Wait for about 1mS
  • Send third init value (0x30)
  • Wait for 1mS
  • Select bus width(0x30 - for 8-bit and 0x20 for 4-bit)
  • Wait for 1mS

The busy flag will only be valid after the above reset sequence. Usually we do not use busy flag in 4-bit mode as we have to write code for reading two nibbles from the LCD. Instead we simply put a certain ammount of delay usually 300 to 600uS. This delay might vary depending on the LCD you are using, as you might have a different crystal frequency on which LCD controller is running. So it actually depends on the LCD MODULE you are using. So if you feel any problem running the LCD, simply try to increase the delay. This usually works. For me about 400uS works perfect.
LCD connections in 4-bit Mode




Above is the connection diagram of LCD in 4-bit mode, where we only need 6 pins to interface an LCD. D4-D7 are the data pins connection and Enable and Register select are for LCD control pins. We are not using Read/Write (RW) Pin of the LCD, as we are only writing on the LCD so we have made it grounded permanently. If you want to use it.. then you may connect it on your controller but that will only increase another pin and does not make any big difference. Potentiometer RV1 is used to control the LCD contrast. The unwanted data pins of LCD i.e. D0-D3 are connected to ground.
LCD connections in 4-bit Mode
We will now look into the common steps to send data/command to LCD when working in 4-bit mode. As i already explained in 4-bit mode data is sent nibble by nibble, first we send higher nibble and then lower nibble. This means in both command and data sending function we need to saperate the higher 4-bits and lower 4-bits.


The common steps are:
  • Mask lower 4-bits
  • Send to the LCD port
  • Send enable signal
  • Mask higher 4-bits
  • Send to LCD port
  • Send enable signal
Program
 lcd.c file 

#include<reg51.h>

#include<stdio.h>   

#define LCDPORT P2

sbit RS = LCDPORT ^ 0;
sbit RW = LCDPORT ^ 1;
sbit E = LCDPORT ^ 2;
#include "lcd.h"     
void main()
{
lcd_init();
lcd_com(15);
lcd_puts("test");
lcd_com(0XC0);
lcd_puts("World");
while (1);
}

  ---------------------------------------------------------------------------------------------------------------------------


lcd.h file .




bit status = 0;
#define lcd_delay 400 
void delay(unsigned int j)
{
    unsigned int i = 0;
    for (; i < j; i++);
} 
void_lcd_init_write(unsigned char a)
{
    RS = 0;
    RW = 0;
    LCDPORT = a;
    E = 1;
    delay(lcd_delay);
    E = 0;
} 
void lcd_com(unsigned char a)
{
    unsigned char temp;
    if (status) {
     status = 0;
     gotonext;
    }
    RS = 0;
  next:
  RW = 0;
    temp = a;
    temp &= 0xf0;
    LCDPORT &= 0x0f;
    LCDPORT |= temp;
    E = 1;
    delay(lcd_delay);
    E = 0;
    temp = a << 4;
    temp &= 0xf0;
    LCDPORT &= 0x0f;
    LCDPORT |= temp;
    E = 1;
    delay(lcd_delay);
    E = 0;
}

void lcd_data(unsigned char a)
{
    status = 1;
    RS = 1;
    lcd_com(a);
}



void lcd_init(void)
{
    delay(lcd_delay);
    lcd_init_write(0x30);
    delay(lcd_delay);
    lcd_init_write(0x30);
    delay(lcd_delay);
    lcd_init_write(0x30);
    delay(lcd_delay);
    lcd_init_write(0x20);
    delay(lcd_delay);
    lcd_com(0x28);
    delay(lcd_delay);
    lcd_com(4);
    delay(lcd_delay);
    lcd_com(0x85);
    delay(lcd_delay);
    lcd_com(6);
    delay(lcd_delay);
    lcd_com(1);
    delay(lcd_delay);
}


void lcd_puts(char *aaa)
{
    unsigned int i = 0;
    for (; aaa[i] != 0; i++)
     lcd_data(aaa[i]);
}

Download here the Proteus and Keil File Click Here






 
 
Till now whatever we discussed in the previous part of ths LCD tutorial, we were dealing with 8-bit mode. Now we are going to learn how to use LCD in 4-bit mode. There are many reasons why sometime we prefer to use LCD in 4-bit mode instead of 8-bit. One basic reason is lesser number of pins are needed to interface LCD.

In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
  1. Wait for abour 20mS
  2. Send the first init value (0x30)
  3. Wait for about 10mS
  4. Send second init value (0x30)
  5. Wait for about 1mS
  6. Send third init value (0x30)
  7. Wait for 1mS
  8. Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
  9. Wait for 1mS
- See more at: http://www.8051projects.net/lcd-interfacing/lcd-4-bit.php#sthash.MwfMgDYA.dpuf
Till now whatever we discussed in the previous part of ths LCD tutorial, we were dealing with 8-bit mode. Now we are going to learn how to use LCD in 4-bit mode. There are many reasons why sometime we prefer to use LCD in 4-bit mode instead of 8-bit. One basic reason is lesser number of pins are needed to interface LCD.

In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
  1. Wait for abour 20mS
  2. Send the first init value (0x30)
  3. Wait for about 10mS
  4. Send second init value (0x30)
  5. Wait for about 1mS
  6. Send third init value (0x30)
  7. Wait for 1mS
  8. Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
  9. Wait for 1mS
- See more at: http://www.8051projects.net/lcd-interfacing/lcd-4-bit.php#sthash.MwfMgDYA.dpuf

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