Interfacing of LCD with 8051 Microcontroller in Proteus ISIS
Hello friends, hope you all are fine and having fun with your lives. Today's post is about Interfacing of LCD with 8051 Microcontroller. In my previous post, we have seen How to do Serial Communication with 8051 Microcontroller, which was quite a basic tutorial and doesn't need much hardware attached to it. Now today we are gonna have a look at Interfacing of LCD with 8051 Microcontroller. LCD is always the basic step towards learning embedded as it serves as a great debugging tool for engineering projects.
LCD is also used almost in every Engineering Project for displaying different values. For example, if you have used the ATM machine, which you must have, then you have seen an LCD there displaying the options to select. Obviously that's quite a big LCD but still LCD. Similarly, all mobile phones are also equipped with LCDs. The LCD we are gonna use in this project is quite small and basic. It is normally known as the 16x2 LCD as it has rows and 2 columns for writing purposes. So, we are gonna interface that LCD with 8051 Microcontroller. The proteus Simulation along with hex file and the programming code in keil uvision 3 is given at the end of this post for download. If you are working with Arduino, then you should have a look at Interfacing of LCD with Arduino. The next level from LCD is Graphical LCD also known as GLCD, so if you wanna know more about that then you should read Interfacing of Arduino with GLCD. So, let's get started with it.
Interfacing of LCD with 8051 Microcontroller in Proteus ISIS
- First of all, we are gonna need to design the Proteus Simulation as we always did.
- After designing the simulation, we are gonna write our embedded code for 8051 Microcontroller.
- I will be designing the code in Keil uvision3 compiler and the 8051 Microcontroller I am gonna use is AT89C51.
- So, let's first get started with Proteus Simulation for interfacing of LCD with 8051 Microcontroller.
Proteus Simulation
- First of all, get the below components from the Proteus components Library and place them in your workspace.
- Now design a circuit in Proteus using these above components as shown in below figure:
- If you have read the previous tutorial, you have noticed a small change, which is the RESET button.
- Its a good thing to have a RESET button in your project. When you press this button, your 8051 Microcontroller will get reset and will start again.
- Moreover, we have added a 20x4 LCD. The data pins of this LCD are attached with Port 2, while the RS and enable pins are connected to 0 and 1 pins of Port 1.
- So, now let's design the programming code for interfacing of LCD with 8051 Microcontroller.
Programming Code
- For programming code I have used Keil uvision 3 software. I am gonna first explain the code in bits so let's get started with it.
- Before starting the LCD programming, let me clear few basic concepts.
- In LCD, there are two types of data, we need to sent.
- The first type is the command like we need to tell the LCD either to start from first column or second column so we need to place the LCD cursor at some point from where we need to start writing. So, this type of data is called commands to LCD.
- The second type of data is the actual data we need to print on the LCD.
- So first of all we send commands to the LCD like the cursor should go to second line and then we send the actual data which will start printing at that point.
- The first function, I have used is named as lcdinit() , this function will initialize the LCD and will give the initializing commands to it.
void lcdinit(void)
{
delay(15000);
writecmd(0x30);
delay(4500);
writecmd(0x30);
delay(300);
writecmd(0x30);
delay(650);
writecmd(0x38); //function set
writecmd(0x0c); //display on,cursor off,blink off
writecmd(0x01); //clear display
writecmd(0x06); //entry mode, set increment
}
- Now in this function I have used another function which is writcmd, which is as follows:
void writecmd(int z)
{
RS = 0; // => RS = 0
P2 = z; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}
- In order to send the commands to LCD with 8051 Microcontroller, we have to make the RS pin LOW and then we send the data and make the Enable pin HIGH to LOW which I have done in the above writecmd() function.
- Next function, we have used is writedata() function, which is as follows:
void writedata(char t)
{
RS = 1; // => RS = 1
P2 = t; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}
- So, if you check above two functions then its quite clear that when we send command to the LCD then we have to make RS pin 0 but when we need to send data to be printed on LCD then we need to make RS pin 1. That's the only thing worth understanding in interfacing of LCD with 8051 Microcontroller.
- Now below is the complete code for interfacing of LCD with 8051 Microcontroller and I think now you can get it quite easily.
#include<reg51.h>
//Function declarations
void cct_init(void);
void delay(int);
void lcdinit(void);
void writecmd(int);
void writedata(char);
void ReturnHome(void);
//*******************
//Pin description
/*
P2 is data bus
P1.0 is RS
P1.1 is E
*/
//********************
// Defines Pins
sbit RS = P1^0;
sbit E = P1^1;
// ***********************************************************
// Main program
//
void main(void)
{
cct_init(); //Make all ports zero
lcdinit(); //Initilize LCD
writecmd(0x81);
writedata('w'); //write
writedata('w'); //write
writedata('w'); //write
writedata('.'); //write
writedata('T'); //write
writedata('h'); //write
writedata('e'); //write
writedata('E'); //write
writedata('n'); //write
writedata('g'); //write
writedata('i'); //write
writedata('n'); //write
writedata('e'); //write
writedata('e'); //write
writedata('r'); //write
writedata('i'); //write
writedata('n'); //write
writedata('g'); //write
writecmd(0xc4);
writedata('P'); //write
writedata('r'); //write
writedata('o'); //write
writedata('j'); //write
writedata('e'); //write
writedata('c'); //write
writedata('t'); //write
writedata('s'); //write
writedata('.'); //write
writedata('c'); //write
writedata('o'); //write
writedata('m'); //write
ReturnHome(); //Return to 0 position
while(1)
{
}
}
void cct_init(void)
{
P0 = 0x00; //not used
P1 = 0x00; //not used
P2 = 0x00; //used as data port
P3 = 0x00; //used for generating E and RS
}
void delay(int a)
{
int i;
for(i=0;i<a;i++); //null statement
}
void writedata(char t)
{
RS = 1; // => RS = 1
P2 = t; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}
void writecmd(int z)
{
RS = 0; // => RS = 0
P2 = z; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}
void lcdinit(void)
{
delay(15000);
writecmd(0x30);
delay(4500);
writecmd(0x30);
delay(300);
writecmd(0x30);
delay(650);
writecmd(0x38); //function set
writecmd(0x0c); //display on,cursor off,blink off
writecmd(0x01); //clear display
writecmd(0x06); //entry mode, set increment
}
void ReturnHome(void) //Return to 0 location
{
writecmd(0x02);
delay(1500);
}
- So, place this code in your keil software and get the hex file.
- Upload this hex file in your Proteus software and Run it.
- If everything goes fine then you will get something as shown in below figure:
- Now, you can see we have printed our website address on the LCD with 8051 Microcontroller.
- You can print anything you wanna print on this LCD instead of our address.
- You can download the Proteus Simulation along with hex file and the programming code in keil uvision 3 by clicking on below button.
Download Proteus Simulation & Code
That's all for today, in the next post I am gonna share how to display custom characters on LCD with 8051 Microcontroller, because till now you can just display the simple characters like alphabets and numbers on it but can't display the custom characters like arrowhead etc. You should have a look at
LCD Interfacing with Microcontrollers, where I have combined all tutorials related to LCD. So stay tuned and have fun.
Serial Communication with 8051 Microcontroller in Proteus
Hello friends, hope you are having fun. In today's post, we will have a look at Serial Communication with 8051 Microcontroller in Proteus ISIS. In the previous post, we have seen a detailed post on LED Blinking Project using 8051 Microcontroller in Proteus ISIS, which was quite a simple tutorial. And I hope if you are new to 8051 Microcontroller then from that post you must have got some idea about C Programming of 8051 Microcontroller.
Now, today we are gonna go a little further and will have a look at Serial Communication with 8051 Microcontroller and we will also design the simulation of this project in Proteus ISIS software. 8051 Microcontroller also supports Serial port similar to Arduino and PIC Microcontroller. And the communication protocol is exactly the same as its a Serial Port. But obviously the syntax is bit different as we are not working in Arduino software or MPLAB. So let's get started with it.
Serial Communication with 8051 Microcontroller in Proteus
- Let's first have a little recall of Serial communication. In serial communication we have two pins which are named as TX and RX.
- TX pin is used for transmitting data while the RX pin is used for receiving data.
- So, our microcontroller has these two pins as it supports Serial Communication and these pins are located at Pin no 10 and 11 in AT89C52 Microcontroller, which I am gonna use today.
- First of all, we will design a Simulation of this project in which there will be 8 LEDs attached to Port 1 and by sending characters through Serial port, we will either turn these LEDs ON or OFF.
- After designing the Simulation, we will then design the programming code for 8051 Microcontroller and will test our result.
- So, let's get started with Proteus Simulation of Serial Communication with 8051 Microcontroller.
Proteus Simulation
- Open your Proteus software and get these components from your Proteus Component Library:
- Now, design a circuit for Serial Communication with 8051 Microcontroller in Proteus software as shown in below figure:
- Now in the above figure, I have used crystal Oscillator of 16MHz as I did in the previous post LED Blinking Project using 8051 Microcontroller and again the reset is pull Down.
- Next I have attached Virtual Terminal with TX and RX of 8051 Microcontroller, if you don't know about Virtual Terminal much then I suggest to read How to use Virtual Terminal in Proteus ISIS.
- Finally, I have attached the 8 LEDs on Port 1 so that we could check whether we are getting correct data or not.
- Now let's design the programming code.
Programming Code
- Now open your Keil micro vision 4 software and paste the below code into it.
#include <reg52.h>
#define Baud_rate 0xFD // BAUD RATE 9600
void SerialInitialize(void);
void SendByteSerially(unsigned char);
void cct_init(void);
sbit Appliance1 = P1^0;
sbit Appliance2 = P1^1;
sbit Appliance3 = P1^2;
sbit Appliance4 = P1^3;
sbit Appliance5 = P1^4;
sbit Appliance6 = P1^5;
sbit Appliance7 = P1^6;
sbit Appliance8 = P1^7;
void main()
{
cct_init();
SerialInitialize();
EA = 1;
ES = 1;
while(1) {;}
}
void cct_init(void) //initialize cct
{
P0 = 0x00; //not used
P1 = 0x00; //Used for Appliances
P2 = 0x00; //not used
P3 = 0x03; //used for serial
}
void SerialInitialize(void) // INITIALIZE SERIAL PORT
{
TMOD = 0x20; // Timer 1 IN MODE 2 -AUTO RELOAD TO GENERATE BAUD RATE
SCON = 0x50; // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED
TH1 = Baud_rate; // LOAD BAUDRATE TO TIMER REGISTER
TR1 = 1; // START TIMER
}
void SendByteSerially(unsigned char serialdata)
{
SBUF = serialdata; // LOAD DATA TO SERIAL BUFFER REGISTER
while(TI == 0); // WAIT UNTIL TRANSMISSION TO COMPLETE
TI = 0; // CLEAR TRANSMISSION INTERRUPT FLAG
}
void serial_ISR (void) interrupt 4
{
//receive character
char chr;
if(RI==1)
{
chr = SBUF;
RI = 0;
}
P0 = ~P0; //Show the data has been updated
switch(chr)
{
case '1': Appliance1 = 1; SendByteSerially('k'); break;
case '2': Appliance2 = 1; SendByteSerially('k'); break;
case '3': Appliance3 = 1; SendByteSerially('k'); break;
case '4': Appliance4 = 1; SendByteSerially('k'); break;
case '5': Appliance5 = 1; SendByteSerially('k'); break;
case '6': Appliance6 = 1; SendByteSerially('k'); break;
case '7': Appliance7 = 1; SendByteSerially('k'); break;
case '8': Appliance8 = 1; SendByteSerially('k'); break;
case 'a': Appliance1 = 0; SendByteSerially('k'); break;
case 'b': Appliance2 = 0; SendByteSerially('k'); break;
case 'c': Appliance3 = 0; SendByteSerially('k'); break;
case 'd': Appliance4 = 0; SendByteSerially('k'); break;
case 'e': Appliance5 = 0; SendByteSerially('k'); break;
case 'f': Appliance6 = 0; SendByteSerially('k'); break;
case 'g': Appliance7 = 0; SendByteSerially('k'); break;
case 'h': Appliance8 = 0; SendByteSerially('k'); break;
default: ; break; //do nothing
}
RI = 0;
}
- You can see in the above code that baud rate we have used is 9600and we have used a switch case method for turning ON or OFF Leds.
- So, now what it will do is when you send 1 on Serial Monitor, it will turn ON the first LED and when you send "a" on Serial Terminal then it will turn OFF the first LED. The same will go on for 8 LEDs.
- Character 1,2,3,4,5,6,7,8 will turn ON the LEDs from 1 to 8 respectively.
- While the character a,b,c,d,e,f,g,h will turn OFF the LEDs from 1 to 8 respectively.
- For each command it will reply us back a single character which is "k". So in this way we are doing the two way communication i.e. sending as well as receiving the serial data.
- So, now after adding the code, get your hex file and then upload it to your Proteus Simulation and click the RUN button on your Proteus software.
- When you start your Proteus Simulation, all the LEDs will be OFF and the virtual terminal will be open as shown in below figure:
- So, now click in the Virtual Terminal and press 1 and the first LED will get ON and you will get k in response as shown in below figure:
- You can see in the above figure, I have pressed 1 and the first LED goes ON as well as we get a response "k" in the virtual Terminal.
- So, that's how we can turn ON or OFF LEDs so in the below figure, I have turned ON all the 8 LEDs.
- Now you can see in the above figure,all leds are on and the virtual terminal is showing k for 8 times as I have given 8 instructions.
- You can download the Proteus Simulation along with hex file and the programming code by clicking the below button.
Download Proteus Simulation and Code
So, that's how we can do Serial communication with 8051 Microcontroller. I don't think its much difficult but still if you have problems then ask in comments and I will resolve them. That's all for today and will meet in the next tutorial soon.
LED Blinking Project Using 8051 Microcontroller
Hello friends, hope you all are fine and having fun with your lives. In today's tutorial, we will see LED Blinking Project Using 8051 Microcontroller. I haven't yet posted any project or tutorial on 8051 Microcontroller. I have posted quite a lot of tutorials on Arduino and PIC Microcontroller, so today I thought of posting tutorials on 8051 Microcontroller. Its my first tutorial on it and I am gonna post quite a lot of tutorials on 8051 Microcontroller in coming week.
So, as its our first tutorial on 8051 Microcontroller that's why its quite a simple one and as we did in Arduino we will first of all have a look at LED Blinking Project Using 8051 Microcontroller. In this project, we will design a basic circuit for 8051 Microcontroller which involves crystal oscillator etc. The basic circuit of 8051 Microcontroller is quite the same as we did for PIC Microcontroller. After that, we will attach a small LED on any of its I/O pins and then will make it blink. I have also given the Proteus Simulation along with Programming code designed in keil uvision 4 for download at the end of this post. So, let's get started with it. :)
LED Blinking Project Using 8051 Microcontroller in Proteus ISIS
- I am gonna first design the simulation of LED Blinking Project using 8051 Microcontroller in Proteus ISIS, as you all know Proteus is my favorite simulation software.
- After designing the simulation, we will design the programming code for 8051 Microcontroller.
- In order to design the code we are gonna use Keil micro vision compiler and the version I have rite now is 4. So its keil micro vision 4 compiler for 8051 Microcontrollers.
- So let's first design the Proteus Simulation for LED Blinking PRoject Using 8051 Microcontroller.
Proteus Simulation for LED Blinking Project
- So, get these components from Proteus components library and place it in your workspace, these components are shown in below figure:
- So, now I hope you have got all these components, now design a circuit in your Proteus software as shown in below figure:
- Now you can see in the above image, I have used crystal oscillator of 16MHz which is used to provide frequency to 8051 Microcontroller.
- After that we have placed a 10k resistance in path of our Reset pin.
- LED is connected to first pin of Port 1 which is P1.0.
- So, now let's design the programming code for 8051 Microcontroller as we are done with the Proteus Simulation.
Keil Programming Code for LED Blinking Project
- Now as I have mentioned earlier, the compiler I have used for designing the programming code for LED Blinking Project is Keil micro vision 4.
- So I hope you have this installed on your computer and if not then must install it as otherwise you wont be able to compile this code, but I have also placed the hex file so that you can run the simulation easily.
- You can download them easily by clicking the Download buttons present at the end of this post.
- So now create a new project in your keil compiler and paste the below code in your c file.
#include<reg51.h>
sbit LED = P1^0;
void cct_init(void);
void delay(int a);
int main(void)
{
cct_init();
while(1)
{
LED = 0;
delay(30000);
LED = 1;
delay(30000);
}
}
void cct_init(void)
{
P1 = 0x00;
}
void delay(int a)
{
int i;
for(i=0;i<a;i++);
}
- Now let me explain this code a bit, first of all, I declare the pin1.0 as LED so that its easy to use it in our code in future.
- After that I declared two functions. One of them is the delay function which is just adding the delay, while the second function is for initialization of Port 1 as output port.
- While in the Main function, we have used the LED blinking code in which LED is ON and then OFF continuously and so that make it blink.
- Now after adding the code in your Keil software, compile it and get the hex file.
- Upload this hex file into your 8051 Microcontroller which I have used is AT89C52 and hit the RUN button.
- If everything's goes fine then you will get results as shown in below figure:
- Now click the below button to get the simulation and the programming code and let me know did it work for you. :)
Download Proteus Simulation & Keil Code
That's all for today, will come soon with new tutorial on 8051 Microcontroller so stay tuned and have fun. Cheers !!! :)