8051 Lab Manual
8051 Lab Manual
)
Beside Mount opera, Near Ramoji Film City,Hayath Nagar ,R.R.Dist.Hyderabad- 501512
A.P.
DEPARTMENT OF
ELECTRONICS AND COMMUNICATIONS
ENGINEERING
ARJUN COLLEGE OF TECHNOLOGY AND SCIENCES
Beside Mount opera, Near Ramoji Film City,Hayath Nagar ,R.R.Dist.Hyderabad-
501512 A.P.
1 Lab Objective 4
3 LAB CODE 6
6 Programs List 9
7 References 52
-3-
Lab Objective:
Since the embedded system is dedicated to specific tasks, design engineers can
optimize it, reducing the size and cost of the product, or increasing the reliability and
performance. Some embedded systems are mass-produced, benefiting from
economies of scale.
-4-
INTRODUCTION ABOUT LAB
Software
1 All systems are configured in DUAL BOOT mode i.e, Students can boot from
Windows XP or Linux as per their lab requirement.
This is very useful for students because they are familiar with different
Operating Systems so that they can execute their programs in different
programming environments.
2 Each student has a separate login for database access
Oracle 9i client version is installed in all systems. On the server, account for
each student has been created.
This is very useful because students can save their work ( scenarios’,
pl/sql programs, data related projects ,etc) in their own accounts. Each student
work is safe and secure from other students.
3 Latest Technologies like DOT NET and J2EE are installed in some
systems. Before submitting their final project, they can start doing mini
project from 2nd year onwards.
4 MASM ( Macro Assembler ) is installed in all the systems
Students can execute their assembly language programs using MASM.
MASM is very useful students because when they execute their
programs they can see contents of Processor Registers and how each
instruction is being executed in the CPU.
1 Rational Rose Software is installed in some systems
Using this software, students can depict UML diagrams of their
projects.
2 Software installed : C, C++, JDK1.5, MASM, OFFICE-XP, J2EE and
DOT NET, Rational Rose.
3 Systems are provided for students in the 1:1 ratio.
4 Systems are assigned numbers and same system is allotted for students when
they do the lab.
-5-
LAB CODE
1. Students should report to the concerned labs as per time table schedule.
2. Students who turn up late to the labs will in no case be permited to do the
program scheduled for the day.
3. After completion of the program , certification of the concerned staff in-
charge in the observation book is necessary.
4. Students should bring a notebook of about 100 pages and should enter the
reading/observations into the notebook while performing the experiment.
5. The record of observations along with the detailed experimental procedure of
the experiment performed in the immediate last session should be submitted
and certified by the staff ember in-charge.
6. Not more than three students in a group are permitted to perform the
experiment on a setup.
7. The group-wise division made in the beginning should be adhered to and no
mix up student among different groups will be permitted later.
8. The components required pertaining to the experiment should be collected
from stores in-charge after duly filling in the requisition form.
9. When the experiment is completed, students should disconnect the setup
made by them, and should return all the components/instruments taken for the
purpose.
10. Any damage of the equipment or burn-out of components will be viewed
seriously either by putting penalty or by dismissing the total group of students
from the lab for the semester/year.
11. Students should be present in the labs for the total scheduled duration.
12. Students are required to prepare thoroughly to perform the experiment before
coming to Laboratory.
13. Procedure sheets/data sheets provided to the student’s groups should be
maintained neatly and to be returned after the experiment.
-6-
List of Lab Excercises
Submission – 1
1. Write a Program to
a). Read inputs from switches.
Week – 1
b). To make LEDs blink.
Submission – 2
Week – 5 5. Sort RTOs (mCOS) on to 89CS1 board and Verify.
Week – 6 6. Simulate on elevator movement using RTO’s on 89CSI board.
-7-
Description about ES Concepts:
Embedded systems are not always separate devices. Most often they are
physically built-in to the devices they control.
The software written for embedded systems is often called firmware, and is
stored in read-only memory or Flash memory chips rather than a disk drive. It
often runs with limited computer hardware resources: small or no keyboard,
screen, and little memory.
Embedded systems range from no user interface at all — dedicated only to one
task — to full user interfaces similar to desktop operating systems in devices such
as
PDAs.
Simple embedded devices use buttons, LEDs, and small character- or
digit-only displays, often with a simple menu system.
-8-
1. Write a Program to
a) Read inputs from switches.
b) To make LEDs blink.
Aim:
Program
This is another simple interface, 8to the port lines, and for some control application
i) The Development board has one no. of 8used to provide digital inputs to the
microcontroller’s ports.
ii) User can change the level of digital inputs whatever they want, either high
or low by simply selecting the jumper+5V, in order to should be used.
-9-
/*---------------------------------------------------------------------
Example 1 : Read Switch status & displayed in LED’s
Description : Depends upon the dip switch position
the corresponding leds are on (or) off
Company : PANTECH SOLUTIONS PVT LTD
----------------------------------------------------------------------*/
FLOW Chart
- 10 -
CODE:
/************************** Main
Function*****************************/
void main(void)
{
SWITCH = 0xff; //Initialize the switch as input port
LED = 0x00; //Initialize the led as output port
while(1)
{
LED = ~SWITCH;
}
}
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 11 -
/*---------------------------------------------------------------------
Example 2 : Read Switch status & scrolling the LED’s
Description : Depends upon the dip switch position
the led will be scrolling
Company : PANTECH SOLUTIONS PVT LTD
----------------------------------------------------------------------*/
- 12 -
Code:
/************************** Main
Function*****************************/
void main(void)
{
Switch =0xff; //Intialize the port1
Led =0x00; //Intialize the port0
while(1)
{
if(Switch ==0xfe)
{ scrollLeft(); //scrollLeft function
}
else if(Switch ==0xfd)
{ scrollRight(); // scrollRight function
}
else
{ Led =0x00;
}
}
}
- 13 -
/*************************** LED Scroll Left *************************/
void scrollRight()
{
port=128;
for(loop=0;loop<8;loop++)
{
Led =port;
port=port/2;
DelayMs(500); //Delay for 500ms
}
}
Execution:
Note: After loading corresponding Examples Hex file located in “OUT” Folder to
The microcontroller kit, press “RST” Button, user program now executes.
- 14 -
b) To make LEDs blink
----------------------------------------------------------------------*/
Example 1 : Program to Blink LED at P0.0
Description : Connect a LED at a port pin and make it
flash at predefined intervals.
Company : PANTECH SOLUTIONS PVT LTD
----------------------------------------------------------------------*/
Flow Chart
- 15 -
Code:
/
*------------------------------------------------------------------------------------------------------
-*/
// Main Program
void main()
{
P0=0x00; //Port0 as output port
while(1) //Loop forever
{
led = 1; //Set the led to high
DelayMs(500); //Delay for 500ms
led = 0; //Set the led to low
DelayMs(500); //Delay for 500ms
}
}
/***************************
// Delay for 1 msec
****************************/
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 16 -
/*---------------------------------------------------------------------
Example 2 : Program to Blink LED at Port P0
Description : Connect a LED at a port pins and make it
flash at predefined intervals.
Company : PANTECH SOLUTIONS PVT LTD
----------------------------------------------------------------------*/
Flow Chart
- 17 -
Code:
/*---------------------------------------------------------------------
// Main Program
----------------------------------------------------------------------*/
void main()
{
P0=0x00; //Port0 as output port
while(1) //Loop forever
{
led = 0xff; //Set the all led to high
DelayMs(500); //Delay for 500ms
led = 0x00; //Set the all led to low
DelayMs(500); //Delay for 500ms
}
}
/***************************
// Delay for 1 msec
****************************/
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 18 -
2. Write a Program for serial communication.
Aim
Program
2) The voltage levels of a microcontroller and PC are not directly compatible with
those of RS-232, a level transition buffer such as MAX232 be used.
- 19 -
/*-----------------------------------------------------------------------*/
/* Example 1 : Program to send data serially through serial port */
/* */
/* Company : PANTECH SOLUTIONS */
/* */
/* Description: Output can be viewed through system's hyper terminal */
/* window by setting the baud rate to 9600 */
/*-----------------------------------------------------------------------*/
Flow Chart
- 20 -
Code:
//-------------------------------------------------
//Setup the serial port for 9600 baud at 11.0592MHz.
//-------------------------------------------------
void serial_init(void)
{
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; /* TH1: reload value for 9600 baud,11.0592MHz*/
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
}
//--------------------------
//Main Program Starts Here
//--------------------------
void main(void)
{
serial_init();
while (1){
printf ("Hello! World\n"); /* Print "Hello World" */
}
}
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
Output:
- 21 -
/*----------------------------------------------------------------------
Example 2 : Program for serial receive data echo using interrupt
Company : PANTECH SOLUTIONS
Description: The program is designed so as to echo the characters typed
through the HyperTerminal window
-----------------------------------------------------------------------*/
Flow Chart
- 22 -
Code:
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 23 -
3. Write a Program for encryption / decryption.
Aim
Writing a program for encryption/decryption
Program
Cryptography(or cryptology; derived from Greek κρύπτω krýpto "hidden" and the
verb γράφω gráfo "to write" or λέγειν legein "to speak") is the practice and study of
hiding information. In modern times, cryptography is considered to be a branch of
both mathematics and computer science, and is affiliated closely with information
theory, computer security, and engineering. Cryptography is used in applications
present in technologically advanced societies; examples include the security of ATM
cards, computer passwords, and electronic commerce, which all depend on
cryptography.
- 24 -
/*----------------------------------------------------------------------
Example : Encryption & Decryption
Description : Get data from serial port, decrypted data displayed in
serial window.
Company : PANTECH SOLUTIONS PVT LTD
------------------------------------------------------------------------*/
Flow Chart
- 25 -
Code:
void main()
{
EA=1; //Enable the Global interrpt
ES=1; //Enable serial Interrupt
serial(); //call serial routine
delay(100);
while(1); //loop forever
}
- 26 -
delay(50);
}
}
}
/******************** Delay Subroutine ****************************/
void delay(unsigned int k)
{
unsigned int a;
for(a=0;a<k;a++);
}
/************************** Encryption ****************************/
void encrypt()
{
i = 0;
SBUF = 0x0d; //CR
delay(100);
while(enter[i] != '\0')
{
SBUF = enter[i];
delay(100);
i++;
}
while(RI == 0);
n = SBUF;
RI = 0;
SBUF = n;
delay(100);
SBUF = '\n';
delay(100);
SBUF = '\n';
delay(100);
n = n-0x30;
for(i=0;i<n;i++)
{
while(RI==0);
msg1[i]=SBUF;
delay(100);
printf("\n");
SBUF=msg1[i];
delay(100);
p=(n-i)-1;
printf(":still %d character remaining:\n",p);
printf("\n");
RI=0;
}
printf("Encrypted Text: ");
for(i=0;msg1[i]!='\0';i++) //Encryption progress
{
x[i]=(msg1[i]+10);
delay(100);
printf("%c",x[i]);
- 27 -
}
delay(100);
printf("\n");
EA=1; //enable the serial interrupt
ES=1;
}
/************************** Decryption ****************************/
void decrypt()
{
printf("\nDecrypted Text: ");
for(i=0;i<n;i++) //Decryption progress
{
y[i]=(x[i]-10);
delay(100);
printf("%c",y[i]);
}
printf("\n");
}
/************************** Serial Interrupt Routine
*********************/
void serin() interrupt 4
{
unsigned char z;
if(RI==1)
{
z=SBUF;
RI=0;
switch(z)
{
case 0x31: //Adcii '1'
encrypt(); //encrypt function
break;
case 0x32: //Ascii '2'
decrypt(); //decrypt function
break;
}
}
}
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 28 -
Output Simulation:
- 29 -
4. Develop necessary interfacing circuit to read data from a sensor and
process using the 801 and 8051 boards. The data to be displayed on a PC
monitor.
Aim
Program
- 30 -
/*----------------------------------------------------------------------*/
/* Example : Program to read Temperature value from ADC */
/*----------------------------------------------------------------------*/
/* Company :PANTECH SOLUTIONS */
/*----------------------------------------------------------------------*/
/* Note :The ADC data lines are interfaced in the Port1 and the
Obtained value in Port1 is converted to decimal value
/*----------------------------------------------------------------------*/
Flow Chart
- 31 -
#include <stdio.h> //Define I/O Functions
#include <reg51.h> //Define 8051 Registers
#include <ctype.h>
//-----------------------------
// ADC Control Lines
//-----------------------------
//-----------------------------
//Delay Function
//-----------------------------
void delay1(int n)
{
int i;
for(i=0;i<n;i++);
}
void delay2(int n)
{
int i;
for(i=0;i<n;i++)
delay1(1000);
}
//-------------------------------------
//Serial Port Initialization
//-------------------------------------
void serial()
{
SCON=0x50; //Serial Mode-1, REN enable
TMOD=0x20; //Timer1, Mode2, 8-bit (Auto Relod mde)
TH1=0xfd; //9600 Baud rate at 11.0592MHz
TR1=1; //Timer 1 ON
TI=1; //Transmit Interrupt Enable
}
- 32 -
//--------------------------
//ADC Function
//--------------------------
//--------------------------
// Main Program
//--------------------------
void main(void)
{
P1=0xFF;
serial(); //Serial port Initialization
A0 = 0; // channel '0' LM35 Sensor
A1 = 0;
A2 = 0;
printf("ADC Demo - Channel '0' LM35(Temp Sensor) \n");
printf("--------------------------------------------\n");
while(1)
{
ch = READ_ADC(); //Get the value from Channel-0
printf("\rCH0(Temperature) = %2bd’C",toascii(ch*2));
delay2(2);
}
}
Execution:
- 33 -
5. Sort RTOs (mCOS) on to 89CS1 board and Verify.
Aim
Program
- 34 -
/
*********************************************************************
**/
/* Round Robin Scheduling Multitasking */
/* Note: LED’s Blinked in different Delay intervals */
/
*********************************************************************
**/
/
*********************************************************************
***/
/* Task 0 'job0': RTX-51 tiny starts execution with task 0 */
/
*********************************************************************
***/
job0 () _task_ 0
{
P1 = 0x00; //P1 make it output port
os_create_task (1); /* start task 1 */
os_create_task (2); /* start task 2 */
os_create_task (3); /* start task 3 */
while (1) { /* endless loop */
os_wait (K_TMO, 5, 0); /* wait for timeout: 5 ticks */
}
}
/
*********************************************************************
*/
Task 1 'job1': RTX-51 tiny starts this task with os_create_task (1)
/
*********************************************************************
*/
job1 () _task_ 1
{
while (1) { /* endless loop */
LED0 = 1;
os_wait (K_TMO, 30, 0);
LED0 = 0;
os_wait (K_TMO, 30, 0); /* wait for timeout: 10 ticks */
}
- 35 -
}
- 36 -
/
*********************************************************************
*/
/* Task 2 'job2': RTX-51 tiny starts this task with os_create_task (2) */
/
*********************************************************************
*/
job2 () _task_ 2
{
while (1) { /* endless loop */
LED1 = 1;
os_wait (K_TMO, 50, 0);
LED1 = 0;
os_wait (K_TMO, 50, 0); /* wait for timeout: 50 ticks */
}
}
/
*********************************************************************
***/
/* Task 3 'job3': RTX-51 tiny starts this task with os_create_task (3) */
/
*********************************************************************
****/
job3 () _task_ 3
{
while (1) { /* endless loop */
LED2 = 1;
os_wait (K_TMO, 80, 0);
LED2 = 0;
os_wait (K_TMO, 80, 0); /* wait for timeout: 80 ticks */
}
}
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 37 -
Example 2: Cooperative Scheduling Multitasking
- 38 -
/
*********************************************************************
*/
/* Cooperative Scheduling Multitasking */
/* Note: LED Blink, UART, LCD (runs simultaneously) */
/
*********************************************************************
*/
/*******************************************************************/
/* Task 0 : RTX-51 tiny Initializ task */
/*******************************************************************/
void init(void)_task_ 0
{
os_create_task (1); // start task 1 INIT_UART
os_create_task (2); // start task 2 SEND_UART
os_create_task (3); // start task 2 LED Blink
os_create_task (4); // start task 2 LCD Display
while (1)
{ // endless loop
os_wait (K_TMO, 5, 0); // wait for timeout: 5 ticks
}
}
- 39 -
/*******************************************************************/
/* Task 1 : RTX-51 tiny starts initialize serial port with task 0 */
/*******************************************************************/
/
*********************************************************************
/
/* Task 2 : RTX-51 tiny starts send UART data with task 0 */
/********************************************************************/
void uart_send(void) _task_ 2
{
while (1)
{
os_wait (K_TMO, 10, 0);
SBUF = 'A';
}
}
/********************************************************************/
/* Task 3 : RTX-51 tiny starts LED Blink with task 0 */
/********************************************************************/
- 40 -
/
*********************************************************************
*/
/* Task 4 : RTX-51 tiny starts LCD Initializaion with task 0 */
/
*********************************************************************
*/
//-------------------------------------------
// First Line Message Display
//-------------------------------------------
//-------------------------------------------
// Second Line Message Display
//-------------------------------------------
- 41 -
//----------------------------------
// LCD command Function
//----------------------------------
//----------------------------------
// LCD Data Function
//----------------------------------
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 42 -
6. Simulate on elevator movement using RTO’s on 89CSI board.
- 43 -
/
*********************************************************************
***/
/* Task_Init.h:
/* Project specific header for the Elevator example
/
*********************************************************************
***/
- 44 -
/
*********************************************************************
***/
/* Elevator Simulation by using RTOS(Multitasking) */
/
*********************************************************************
***/
/
*********************************************************************
*/
/* Task 0 : RTX-51 tiny Initializ task */
/
*********************************************************************
*/
- 45 -
{ // endless loop
os_wait (K_TMO, 5, 0); // wait for timeout: 5 ticks
}
}
- 46 -
/
*********************************************************************
*/
/* Task 1 : RTX-51 tiny starts initialize serial port with task 0 */
/
*********************************************************************
*/
/
*********************************************************************
/
/* Task 2 : RTX-51 tiny starts send UART data with task 0 */
/
*********************************************************************
/
/
*********************************************************************
**/
/* Task 3 : RTX-51 tiny starts LCD Initializaion with task 0 */
/
*********************************************************************
**/
- 47 -
lcd_cmd(0x06); //Shift Cursor to right
os_wait (K_TMO, 2, 0);
lcd_cmd(0x01); //Clear display screen
os_wait (K_TMO, 2, 0);
- 48 -
//-------------------------------------------
// First Line Message Display
//-------------------------------------------
//-------------------------------------------
// Second Line Message Display
//-------------------------------------------
/***************************Elevator Open
******************************/
void open(void)
{
lcd_cmd(0xca); //Second Line Initialization
os_wait (K_TMO, 2, 0);
i=0;
while(msg2[i]!='\0')
{
lcd_display(msg2[i]);
os_wait (K_TMO, 1, 0);
SBUF = msg2[i];
i++;
}
os_wait (K_TMO, 4, 0);
}
- 49 -
/***************************Elevator Close
******************************/
void close(void)
{
lcd_cmd(0xca); //Second Line Initialization
os_wait (K_TMO, 2, 0);
i=0;
while(msg3[i]!='\0')
{
lcd_display(msg3[i]);
os_wait (K_TMO, 1, 0);
SBUF = msg3[i];
i++;
}
os_wait (K_TMO, 4, 0);
}
/***************************LED UP scroll
******************************/
void upscroll(int n)
{
int i,j=0;
for(i=0;i<n;i++)
{
for(j=0x10;j<=0x80;j<<=1) //shift led one position
{
P0=j; //Initialize Port1
os_wait (K_TMO, 20, 0);
}
}
}
/***************************LED Downscroll
*****************************/
void downscroll(int n)
{
int i,j=0;
for(i=0;i<n;i++)
{
for(j=0x80;j>=0x10;j>>=1) //shift led one position
{
P0=j; //Initialize Port1
os_wait (K_TMO, 20, 0);
}
}
}
- 50 -
/***************************LCD Command
*****************************/
/***************************LCD Display
******************************/
/
*********************************************************************
*/
/* Task 4 : RTX-51 tiny starts Elevator outside key with task 0 */
/
*********************************************************************
*/
- 51 -
if(P2 == 0xE0) //Checking from Column 0 to 3
C = 0;
else if(P2 == 0xD0)
C = 1;
else if(P2 == 0xB0)
C = 2;
else if(P2 == 0x70)
C = 3;
os_wait (K_TMO, 10, 0);
//Floor Status
ch = Key[R][C];
if(ch=='0')
{
downscroll(1);
open(); os_wait (K_TMO, 200, 0);
close(); os_wait (K_TMO, 2, 0);
os_create_task (KEY_IN); //create keyscan inside
P0 =0x08;
}
if(ch=='1')
{
downscroll(2);
open(); os_wait (K_TMO, 200, 0);
close(); os_wait (K_TMO, 1, 0);
os_create_task (KEY_IN); //create keyscan inside
P0 = 0x04;
}
if(ch=='2')
{
downscroll(3);
open(); os_wait (K_TMO, 200, 0);
close(); os_wait (K_TMO, 1, 0);
os_create_task (KEY_IN); //create keyscan inside
P0 = 0x02;
}
if(ch=='3')
{
downscroll(4);
open(); os_wait (K_TMO, 200, 0);
close(); os_wait (K_TMO, 1, 0);
os_create_task (KEY_IN); //create keyscan inside
P0 = 0x01;
}
}
}
- 52 -
/
*********************************************************************
*/
/* Task 5 : RTX-51 tiny starts Elevator inside key with task 0 */
/
*********************************************************************
*/
- 53 -
os_create_task (KEY_OUT); //create keyscan inside
}
if(ch=='7')
{
upscroll(3);
open(); os_wait (K_TMO, 200, 0);
close(); os_wait (K_TMO, 2, 0);
P0 = 0x02;
os_create_task (KEY_OUT); //create keyscan inside
}
if(ch=='8')
{
upscroll(4);
open(); os_wait (K_TMO, 200, 0);
close(); os_wait (K_TMO, 2, 0);
P0 = 0x01;
os_create_task (KEY_OUT); //create keyscan inside
}
}
}
Execution:
Note: After Loading corresponding Examples Hex file located in “OUT” Folder to
the microcontroller kit, press “RST” Button, user program now executes.
- 54 -
TEXT BOOKS:
REFERENCES:
- 55 -