Tuesday 20 March 2012

HOW TO INTERFACE LEDs WITH MICROCONTROLLER (AT89c51)


        In this post I will explain how to take output from microcontroller using LEDs. Before that lets check how actually a LED works ?

How LED works ?
            LED is a most suitable component to display an output status because it emits the light when it is on. It is very cheap and comes in different size and colors. The principal of working of a LED is equivalent to the p-n junction diode. The difference is, it emits the light in visible spectrum instead of heat. The forward voltage drop of LED is around 1.7v. it works around 10mA current supply.
            The circuit diagram for connection of LED is show below.
            
The circuit diagram and microcontroller code for interfacing a LED with microcontroller (AT89c51) is given below.

Component Required :
1.      8 LEDs.
2.      Microcontroller AT89c51.
3.      8 registers.
4.      12MHz crystal oscillator.
5.      10 KΩ fixed register.
6.      10 μf (25v) capacitor.

Circuit Diagram :

           
               8 LEDs are connected with port P1 of AT89c51. Here LEDs are running of negative logic means cathode of every LED is connected with Microcontroller port pin and anode of all the LEDs are connected to +5v supply so we don’t required to give +5v at the output of microcontroller in order to on the LEDs.
            We are sending the sequence of 0x055 and 0x0AA alternatively with some delay. Delay is in the range of few milliseconds.

Code :

        #include<reg51.h>

void delay(int time)        //This function produces a delay in msec.
{
    int i,j;
    for(i=0;i<time;i++)
      for(j=0;j<1275;j++);
}

void main()
{
      while(1)
      {
          P1=0x00;
          delay(100);
          P1=0xff;
          delay(100);
      }
}


Monday 19 March 2012

How to interface 16x2 alphanumeric LCD with AT89c51 Microcontroller


In my previous blog I have explained that how a 16x2 alphanumeric LCD works. So in this blog I am going to explain how we can interface 16x2 alphanumeric LCD with microcontroller AT89c51.

Component required for interfacing LCD with microcontroller :
1.      Microcontroller AT9c51
2.      16x2 alphanumeric LCD
3.      12 MHz crystal oscillator
4.      10 KΩ fixed register
5.      10 KΩ variable register
6.      10 μf (25 V) capcitor

Circuit diagram for interfacing LCD with microcontroller :


Procedure to program LCD :
1.      Connect p2.o to p2.7 of AT89c51 to DB0-DB7 of LCD.
2.      Connect p3.0, p3.1 & p3.6 pin of  AT89c51 to RS, R/W & En pin of LCD respectively.
3.      Connect the rest of the circuit as shown in figure.
4.      To send command to LCD we have to clear RS pin of LCD by sending 0 to it. To send data to LCD
we have to set RS pin of  LCD by sending 1 to it.
5.      To write either command or data to LCD by clearing the R/W pin. To read the data from LCD we have   to set the R/W pin.
6.      First we have to clear the LCD by sending 01h command to LCD.
7.      Then we can send any command to perform any function on LCD.
8.      After latching the data into the DB0-DB7 pin of LCD we have to send high to low pulse on En pin of  LCD. 

Code (in c language) for interfacing LCD with microcontroller :


#include<reg51.h>


#define cmd_port P3


#define data_port P2


#define q 100


sbit rs = cmd_port^0;  //register select pin
sbit rw = cmd_port^1;  // read write pin
sbit e = cmd_port^6;  //enable pin

void delay(unsigned int msec)  // Function to provide time delay in msec.
{
int i,j ;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}


void lcd_cmd(unsigned char item)  //Function to send command to LCD
{
data_port = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
}


void lcd_data(unsigned char item)  //Function to send data to LCD
{
data_port = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
}


void main()
{


lcd_cmd(0x38);  // for using 8-bit 2 row mode of LCD
delay(100);


lcd_cmd(0x0E);  // turn display ON for cursor blinking
delay(100);

lcd_cmd(0x01);  //clear screen
delay(100);


lcd_cmd(0x06);  //display ON
delay(100);


lcd_cmd(0x86);  // bring cursor to position 6 of line 1
delay(100);


lcd_data('A');
}

Saturday 17 March 2012

HOW 16x2 ALPHANUMERIC LCD WORKS ?


          LCD (Liquid Crystal Display) is used in all the electronics projects to display the status of the process. A 16x2 alphanumeric LCD is most widely used module of LCD nowadays. There are several others type of  LCD available in market also.
The reason for choosing LCD over other display component or devices is that it is
  • Low cost
  • Easily programmable
  • Large number of display character etc.


Introduction  :
          16x2 LCD has 2 horizontal line which comprising a space of 16 displaying character. It has two type of register inbuilt that is

  • Command Register
  • Data Register.


    Command register is used to insert a special command into the LCD. While Data register is used to insert a data into the LCD. Command is a special set of  data which is used to give the internal command to LCD like Clear screen, move to line 1 character 1, setting up the cursor etc.


Pin Diagram of 16x2 LCD :


Sr. No
Pin No.
Pin Description
1
Pin 1 (GND)
This is a ground pin to apply a ground to LCD.
2
Pin 2 (VCC)
This is the supply voltage pin to apply voltage to LCD.
3
Pin 3 (VEE)
This is the pin for adjusting a contrast of  the LCD display by attaching a veriable resistor in between VCC and GND.
4
Pin 4 (RS)
RS stands for Register Select. This pin is used to select command/data register.
If RS=0 then command register is selected.
If RS=1 then data register is selected.
5
Pin 5 (R/W)
R/W stands for Read/Write. This pin is used to select the operation Read/Write.
If R/W=0 then Write operation is performed.
If R/W=1 then Read operation is performed.
6
Pin 6 (EN)
En stand for Enable signal. A positive going pulse on this pin will perform a read/write function to the LCD.
7
Pin 7-14 (DB0-DB7)
This 8 pin is used as a Data pin of  LCD.
8
Pin 15 (LED+)
This pin is used with pin 16(LED-) to setting up the illumination of back light of LCD. This pin is connected with VCC.
9
Pin 16 (LEC-)
This pin is used with pin 15(LED+) to setting up the illumination of back light of LCD. This pin is connected with GND.





Important commands codes for LCD :

Sr.No.
Hex Code
Command to LCD instruction Register
1
01
Clear display screen
2
02
Return home
3
04
Decrement cursor (shift cursor to left)
4
06
Increment cursor (shift cursor to right)
5
05
Shift display right
6
07
Shift display left
7
08
Display off, cursor off
8
0A
Display off, cursor on
9
0C
Display on, cursor off
10
0E
Display on, cursor blinking
11
0F
Display on, cursor blinking
12
10
Shift cursor position to left
13
14
Shift cursor position to right
14
18
Shift the entire display to the left
15
1C
Shift the entire display to the right
16
80
Force cursor to beginning to 1st line
17
C0
Force cursor to beginning to 2nd line
18
38
2 lines and 5x7 matrix