Module 2 : Intel 8051 Microcontroller

Lecture 14 : 8051 Sample Assembly Lanaguage Programs

Example programs

Character transmission using a time delay

A program shown below takes the character in 'A' register, transmits it, delays for transmission time, and returns to the calling program. Timer-1 is used to set the baud rate, which is 1200 baud in this program

The delay for one character transmission (in Mode 1 i.e.10 bits) is

10/2400 = 0.00833 seconds
Or, 8.33 milliseconds

Hence software delay of 10ms is used.
Timer-1 generates a baud rate close to 1200. Using a 12MHz crystal, the reload value is


Or, 230  i.e. E6H

This gives rise to an actual baud rate of 1202.
SMOD is programmed to be 0.

Assembly language Program is as follows

            ; Code to wait for the transmission to complete

The subroutine TRMITTIME generates a delay of about 10ms. With a clock of 12MHz, one instruction cycle time is
The loop "MILSEC" generates a delay of about 1 x 10-3 sec. This gets executed 10 times for a total delay of 10 x 10-3 sec or 10ms
   

Interrupt driven character transmission

In 8051, when a character is transmitted, SBUF register becomes empty and this generates a serial port interrupt (TI). TI and RI both point to the vector location 0023H in the program memory. An interrupt service routine can be written at 0023H to send the next character.

A program is written here to transmit a character say 'A' continuously based on interrupt. The microcontroller uses a clock of 12MHz with a baud rate of 1202. The program is executed following a hardware reset.

Assembly language program is as follows.

Interrupt driven data reception

When a character is received, if receive mode is enabled, RI flag is set. This leads to the interruption of the main program and the processor goes to the interrupt vector location, i.e. 0023H for serial port. The interrupt service routine at 0023H gets executed to read the character so that the next character can be received. The following program receives a character on interrupt basis and outputs the character to port-1, possibly for a display.

The crystal frequency is12MHz and baud rate is set at 1202 baud.

Assembly language program is as follows