You are on page 1of 5

8051 Programming for UART

In 8051, we make use of Timer 1 to generate the required baud rate. Following are the registers that are need to be
configured to commnunicate over UART.

 TMOD
 SCON
 TH1
 TL1
 TCON

TMOD: This register is used to set the mode of Timer0 and Timer1. It is also used to select whether the timers are used as
Timer or Counter.

SCON: Serial Control register has various functions like.. it has flags for Framing error, Transmit interrup and receive
interrupt. Its used to select the serial port mode, to enable or disable the reception etc.

TCON: This register has varios flag and control bits e.g. Timer overflow flags, interrupt edge flags, timer control bits to
start/stop the timer.

TH1 & TL1: Timer registers for Timer 1 determines the baudrate of UART.
More information on the above registers can be found in the 8051 Hardware manual.
►Initializing USART in 8051

CODE:
Serial_Init:
        ;Set timer 1 mode to 8-bit Auto-Reload
        mov TMOD,#20H
        ;Enable reception
        ;Set Serial port mode to 8-bit UART
        mov SCON,#50H
        ;Set baudrate to 9600 at 11.0592MHz
        mov TH1,#0FDH
        mov TL1,#0FDH
        ;Start Timer
        setb TR1
        ret
 

in C we can do this as..

CODE:
#include <reg51.h>.
void serial_init(){
        TMOD = 0x20;
        SCON = 0x50;
        TH1  = 0xFD;
        TL1  = 0xFD;
        TR1  = 1;
}
 

To Send data to the serial port we just have to move the data in SBUF (serial buffer register) and wait for the Transmit
Interrupt flag to be set. While receiving we wait for the Receive interrupt flag to be set and read the data from SBUF
register. This can be done as shown below...

CODE:
Serial_Send:
        ;wait for last data to be
        ;sent completely
        jnb TI,Serial_Send
        ;clear the transmit interrupt flag
        clr TI
        ;Then move the data to send in SBUF
        mov SBUF,A
        ret
       
Serial_Read:
        ;Wait for Receive interrupt flag
        jnb RI,Serial_Read
        ;If falg is set then clear it
        clr RI
        ;Then read data from SBUF
        mov A,SBUF
        ret
 

in C we can do this as..

CODE:
void serial_send(unsigned char dat){
        while(!TI);
        TI = 0;
        SBUF = dat;
}
unsigned char serial_read(){
        while(!RI);
        RI = 0;
        return SBUF;
}
 
Martix Keypad Interfacing with Microcontrollers: Introduction

►Introduction
Keypads are a part of HMI or Human Machine Interface and play really important role in a small embedded system where
human interaction or human input is needed. Martix keypads are well known for their simple architecture and ease of
interfacing with any microcontroller. In this part of tutorial we will learn how to interface a 4x4 matrix keypad with AVR and
8051 microcontroller. Also we will see how to program then in Assembly and C.

►Constructing a Matrix Keypad


Constuction of a keypad is really simple. As per the outline shown in the figure below we have four rows and four columns.
In between each overlapping row and column line there is a key.
So keeping this outline we can constuct a keypad using simple SPST Switches as shown below:

Now our keypad is ready, all we have to do is connect the rows and columns to a port of microcontroller and program the
controller to read the input.

►Scanning a Matrix Keypad


There are many methods depending on how you connect your keypad with your controller, but the basic logic is same. We
make the coloums as i/p and we drive the rows making them o/p, this whole procedure of reading the keyboard is called
scanning.

In order to detect which key is pressed from the matrix, we make row lines low one by one and read the coloums. Lets say
we first make Row1 low, then read the columns. If any of the key in row1 is pressed will make the corrosponding column as
low i.e if second key is pressed in Row1, then column2 will give low. So we come to know that key 2 of Row1 is pressed.
This is how scanning is done.

So to scan the keypad completely, we need to make rows low one by one and read the columns. If any of the button is
pressed in a row, it will take the corrosponding column to a low state which tells us that a key is pressed in that row. If
button 1 of a row is pressed then Column 1 will become low, if button 2 then column2 and so on...
Now lets move on to the programming part of keypad in our next section.
Keypad Connections with 8051 Microcontroller

Please click on the image to enlarge it

Circuit shown above is for demonstration and does not include any reset and crystal circuit. For practical use you need to
have a reset circuit and crystal.

C Program for Keypad

CODE:
#include <AT89X51.H>    //Include file for 8051
#define keyport P2      //keypad connected to P2
#define col1 P2_0       //column 1
#define col2 P2_1       //column 2
#define col3 P2_2       //column 3
#define col4 P2_3       //column 4
#define TRUE 1          //some defines
#define FALSE 0

/*
+---------------------------------------+
| Prototype: void key_init(void);       |
| Return Type: void                     |
| Arguments: None                       |
| Description: Initialize ports and     |
|              Keypad.                  |
+---------------------------------------+
*/
void key_init(){
        keyport &=0x0F; //make Rows as o/p and cols are i/p
}

/*
+-----------------------------------------------+
| Prototype: unsigned char get_key(void);       |
| Return Type: unsigned char                    |
| Arguments: None                               |
| Description: To read key from the keypad      |
+-----------------------------------------------+
*/
unsigned char get_key(){
        unsigned char i,k,key=0;
        k=1;
        for(i=0;i<4;i++){               //loop for 4 rows
                keyport &=~(0x80>>i);   //to make rows low 1 by 1
                        if(!col1){      //check if key1 is pressed
                                key = k+0;      //set key number
                                while(!col1);   //wait for release
                                return key;     //return key number
                        }
                        if(!col2){      //check if key2 is pressed
                                key = k+1;      //set key number
                                while(!col2);   //wait for release
                                return key;     //return key number
                        }
                        if(!col3){      //check if key3 is pressed
                                key = k+2;      //set key number
                                while(!col3);   //wait for release
                                return key;     //return key number
                        }
                        if(!col4){      //check if key4 is pressed
                                key = k+3;      //set key number
                                while(!col4);   //wait for release
                                return key;     //return key number
                        }
                k+=4;                   //next row key number
                keyport |= 0x80>>i;     //make the row high again
        }
        return FALSE;                   //return false if no key pressed
}
 

You might also like