Friday, April 17, 2009

LCD plus Keypad interfacing with 8051 Atmel

What is inside our code and how does this amazing code work??

Our code is quite simple we used simple commands to initialize the LCD all the data transported is in decimal. RS, RW and E pins of LCD are handled separately. We not used much function in our program as they usually make the program tricky. We tried to make the program as much simple as possible.
LCD:
We used blue LCD 16 * 2, JHD 1602 with 16 pins (actually 14 last two are for back light)
LCD interfacing is quite simple and easy to understand as each pin is handled and feeded with the data individually. This made the code quite easy.

PINs of LCD:

Data pins of LCD are connected to the port 1 of micro controller’s to the pin 1of port 2, RW pin 2 of port 2 and E to the pin 3 of port 2.
Pin 15 is directly given the 5 volts and pin 16 is grounded so that the back light can be utilized the basic function of the back light is to increase the visual ability of observer as we are using blue LCD and the text is written in grayish white which is not visible without the back light.

PIN 1, 2 & 3:
Pin 1 is grounded, pin 2 is connected to 5 volts & pin 3 of contrast is again grounded manually.

RS, RW & E PIN:
These are connected to port 2 and are feeded individually when ever and as needed. RS is 0 for command on LCD and 1 for data writing. RW is always 0 as we dont need it and E is changed from high to low pulse whenever the either command or data is sent to LCD.

EXPLAINATION:

FUNCTION SET:
1st of all function set command is feeded to the LCD data ports are feeded with 56 dec or 38 hex. That conveys that we are using 16 * 2 LCD. RS is zero. RW is zero. Enable pin is feeded with 1st high and then low signal.
P1 = 56, P2_1 = 0, P2_2 = 0,P2_3 = 1;
P2_3 = 0;

DISPLAY & Cursor ON OFF:
Secondly display on off and cursor on off (also blinking)command is sent to the LCD where data pins are feeded with 15 dec. RS is zero. RW is zero. Enable pin is feeded with 1st high and then low signal.
P1 = 15, P2_1 = 0, P2_2 = 0,P2_3 = 1;
P2_3 = 0;


CLEAR DISPLAY:
Data pins are feeded with 1. RS is zero. RW is zero. Enable pin is feeded with 1st high and then low signal to clear display.
P1 = 1, P2_1 = 0,P2_2 = 0,P2_3 = 1;
P2_3 = 0;


DISPLAY SHIFT:
To shift cursor right data pins are feeded with 28.RS is zero. RW is
zero. Enable pin is feeded with 1st high and then low signal.
P1 = 28, P2_1 = 0, P2_2 = 0, P2_3 = 1;
P2_3 = 0;


STARTING POSITION:
data pin is feeded with 81 hex or 129 decimal according to the Mazzidi`s book in assembly language. RS is zero. RW is zero. Enable pin is feeded with 1st high and then low signal.
P1 = 129, P2_1 = 0, P2_2 = 0, P2_3 = 1;
P2_3 = 0;

Now LCD`s initializing is complete....

KEYPAD INTERFACING:

Keypad interfacing involves a function call which also enables us to handle the debouncing problem delay is there but after the display of the key pressed. Proper use of delay by using for loop made it quite practical.

Whenever there is need to input data a function is called which calculate the key pressed from the keypad and display it or use the command as desired.

EXPLAINATION:

The data is saved in another variable 'g' at the same time transferred to variable 'f' and 'f' is multiplied with 10 and again if key is scanned the value of f changes by addition on new value of 'g' in 'f' i.e.- "f=f+g" again it is multiplied with 10 and procedure continues until enter or reset key is not pressed.

if(a!='*' && a!='#')
{
P1 = a, P2_1 = 1, P2_2 = 0, P2_3 = 1;
P2_3 = 0;
for(c=0;c<32000;c++);
f=f+g;
f=f*10;
}

ENTER:
Whenever enter key is pressed the value of 'f' is divided by 10 and the calculated no (which is actually the ASCII code) is sent to the LCD to display (this portion is in last of the code which can be operated by GOTO statement not else) its corresponding character. Again the value of 'f' and 'g' are made zero.
"f=0;
g=0;
"

if(a=='#')
{
f=f/10;
a=f;
P1 ='=', P2_1 = 1,P2_2 = 0,P2_3 = 1;
P2_3 = 0;
for(c=0;c<32000;c++);
f=0;
g=0;
goto label;
}
while(0)
{
label:
P1 = a, P2_1 = 1, P2_2 = 0, P2_3 = 1;
P2_3 = 0;
for(c=0;c<32000;c++);
P1 =' ', P2_1 = 1, P2_2 = 0, P2_3 = 1;
P2_3 = 0;
for(c=0;c<32000;c++);
}


RESET:
Whenever the reset key is pressed clear display command is executed and values of 'f' and 'g' are made zero.
"f=0;
g=0;
"
if(a=='*')
{
P1 = 1, P2_1 = 0,P2_2 = 0,P2_3 = 1;
P2_3 = 0;
f=0;
g=0;
for(c=0;c<32000;c++);
}


This all procedure is in wild loop. So in this way we have a numeric keyboard every character can be written on LCD with its corresponding ASCII code.

No comments:

Post a Comment