Escolar Documentos
Profissional Documentos
Cultura Documentos
∗
Microcontroller(8051) Lab
Rajeshwari Hegde
This work is produced by The Connexions Project and licensed under the
Creative Commons Attribution License †
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 2
Table 1
• - Bit Addressable
Table 3: Byte & Bit Addresses for 8051 Hardware Register
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 3
Table 2
Table 4
Table 3
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 4
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 5
To convert packed BCD to ASCII, it must rst be converted to to unpacked BCD. Then the unpacked
BCD is tagged with 30h.
mov dptr,#9000h ;Load 9000h into dptr register
movx a,@dptr ;Move the content of location 9000h to a
mov r2,a ;Move the data from a to r2
anl a,#0f0h ;And a with 0f0h
swap a ;Swap a
orl a,#30h ;Or a with 30h
inc dptr ;increment dptr
movx @dptr,a ;move the data from a to memory location
mov a,r2 ; Move the data from r2 to a
anl a,#0fh ; And a with 0fh
orl a,#30h ;Or a with 30h
inc dptr ;increment dptr
movx @dptr,a ;move the data from a to memory location
here:sjmp here
end
Input: 45
Output:34 35
3;ASCII to BCD conversion
To convert ASCII to packed BCD, it is rst converted to unpacked BCD(to mask 3) and
then combined to make packed BCD. For example, for 4 and 5 the keyboard gives 34 and
35, respectively. The goal is to produce 45h, which is packed BCD.
;ASCII to packed BCD conversion
mov r0,#10h ;R0=10h,Internal memory adress
mov a,@r0 ;a=hex value of Ist ASCII number
anl a,#0fh ;mask upper nibble
swap a ;swap upper and lower nibble of a
mov b,a ;save the number in B
inc r0 ;Increment R0
mov a,@r0 ;Get the next number from memory
anl a,#0fh ;Mask Higher nibble
orl a,b ;Or a & B
inc r0 ;Increment memory address to store the result
mov @r0,a ;Move the content of A to internal RAM location
here:sjmp here
end
Input:35
Output:
4. Program to nd the average of 10 numbers
mov dptr,#9000h ;Load 9000h into dptr register
clr a ;Clear a register
mov 0f0h,a ;move data from a to b
mov r1,#05h ;move 05 to r1 register
up: movx a,@dptr ; ;move data from external memory location to a
add a,0f0h ;add a and b
mov 0f0h,a ;move the result to b
dec r1 ;decrement r1
inc dptr ;increment dptr
cjne r1,#00h,up ;compare r1 with 0, if not equal, jump to label up
mov a,0f0h ;move data from b to a
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 6
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 7
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 8
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 9
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 10
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 11
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 12
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 13
end
21. Program to add two BCD numbers
mov dptr,#9000h ;move dptr with 9000h
movx a,@dptr ; move data from external memory location to a
mov b,a ;move data from a to b
inc dptr ;increment dptr
movx a,@dptr ; move data from external memory location to a
add a,b ;add a and b
da a ;decimal adjust accumulator after addition
jc down ;if carry, jump to label down
inc dptr ;increment dptr
movx @dptr,a ;move data from a to external memory location
sjmp last ;jump to label last
down:mov r2,a ;move data from a to r2
mov a,#01h ;move data 01h to a
movx @dptr,a ;move data from a to external memory location
inc dptr ;increment dptr
mov a,r2 ;move data from r2 to a
movx @dptr,a ;move data from external memory location to a
last:sjmp last
end
22.Program to nd the square of an 8 bit number
mov dptr,#9000h ;load dptr with 9000h
movx a,@dptr ;move data from external memory location to a
mov b,a ;move data from a to b
mul ab ;multiply and b
inc dptr ;increment dptr
mov r0,a ;move data from a to r0
mov a,b ;move data from b to a
movx @dptr,a ;move data from a to external memory location
inc dptr ;increment dptr
mov a,r0 ;move data from r0 to a
movx @dptr,a ;move data from a to external memory location
here:sjmp here
end
23. Program to count from 0-9
here:mov a,#0h ;move 0 to a
up:mov p0,a ;move data from a to port0
acall delay ;call delay routine
add a,#01 ;increment a
cjne a,#010,up ;compare a with 10, if not equal, jump to location up
sjmp here ;jump to location here
;delay routine
delay:mov r1,#0h ;move 0h to r1
l3:mov r2,#0h ;move 0h to r2
l2:mov r3,#0h ;move 0h to r3
l1:djnz r1,l1 ;decrement r1, repeat till r1=0
djnz r3,l2 ;decrement r3,repeat l2 till r3=0
djnz r2,l3 ;decrement r2, repeat l3 till r2=0
ret ;return
end
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 14
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 15
end
Part B: Interfacing programs
1.Write a C program to display the code of the key pressed
#include <REG51xD2.H>
#include "lcd.h"
unsigned char getkey();
void delay(unsigned int);
main()
{
unsigned char key;
InitLcd(); /* Initialise LCD */
WriteString("Key Pressed="); /* Display msg on LCD */
while(1)
{
GotoXY(12,0); /* Set Cursor Position */
key = getkey(); /* Call Getkey method */
}
}
unsigned char getkey()
{
unsigned char i,j,k,indx,t;
P0=0x0;
P1=0x0;
P2 = 0x00; /* P2 as Output port */
indx = 0x00; /* Index for storing the rst value of
scanline */
for(i=0x00E;i>=0x00B;i=1) /* for 4 scanlines */
{
P2 = i; /* write data to scanline */
t = P0; /* Read readlines connected to P0*/
t = ∼t;
if(t>0) /* If key press is true */
{
delay(6000); /* Delay for bouncing */
for(j=0;j<=7;j++) /* Check for 8 lines */
{
t =1;
if(t==0) /* if get pressed key*/
{
k = indx+j; /* Display that by converting to Ascii */
t = k4;
t +=0x30;
WriteChar(t); /* Write upper nibble */
t = k & 0x0f;
if(t > 9)
t+=0x37;
else
t+=0x30;
WriteChar(t); /* write lower nibble */
return(indx+j); /* Return index of the key pressed */
}
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 16
}
}
indx += 8; /* If no key pressed increment index */
}
}
void delay(unsigned int x) /* Delay routine */
{
for(;x>0;x);
}
2.Elevator Interface
#include <REG51F.H>
void delay(unsigned int);
main()
{
unsigned char Flr[9] = {0x,0x00,0x03,0x,0x06,0x,0x,0x,0x09};
unsigned char FClr[9] = {0x,0x0E0,0x0D3,0x,0x0B6,0x,0x,0x,0x79};
unsigned char ReqFlr,CurFlr = 0x01,i,j;
P0 = 0x00;
P0 = 0x0f0;
while(1)
{
P1 = 0x0f;
ReqFlr = P1 | 0x0f0;
while(ReqFlr == 0x0)
ReqFlr = P1 | 0x0f0; /* Read Request Floor from P1 */
ReqFlr = ∼ReqFlr;
if(CurFlr == ReqFlr) /* If Request oor is equal to Current Floor */
{
P0 = FClr[CurFlr]; /* Clear Floor Indicator */
continue; /* Go up to read again */
}
else if(CurFlr > ReqFlr) /* If Current oor is > request oor */
{
i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of oors to travel */
j = Flr[CurFlr];
for(;i>0;i) /* Move the indicator down */
{
P0 = 0x0f0|j;
j;
delay(25000);
}
}
else /* If Current oor is < request oor */
{
i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of oors to travel */
j = Flr[CurFlr];
for(;i>0;i) /* Move the indicator Up */
{
P0 = 0x0f0 | j;
j++;
delay(25000);
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 17
}
}
CurFlr = ReqFlr; /* Update Current oor */
P0 = FClr[CurFlr]; /* Clear the indicator */
}
}
void delay(unsigned int x)
{
for(;x>0;x);
}
3.Stepper motor interface
#include <REG51xD2.H>
static bit Dir=0;
void delay(unsigned int x) /* Delay Routine */
{
for(;x>0;x);
}
void ChangeDir(void) interrupt 0 /* Int Vector at 000BH, Reg Bank 1 */
{
Dir = ∼Dir; /* Complement the Direction ag */
ClrLcd();
if(Dir)
delay(32000);
else
delay(32000);
}
main()
{
unsigned char Val,i;
EA=0x1; /* Enable Interrupt ag and Interrupt 0 & Serial Interrupt */
EX0=0x1;
ES=0x1;
P0=0x00; /*since the monitor is using the serial interrupt it has to be enabled*/
while(1)
{
if(Dir) /* If Dir Clockwise */
{
Val = 0x88;
for(i=0;i<4;i++)
{
P0 = Val; /* Write data for clock wise direction*/
Val = Val1;
delay(575);
}
}
else /* AntiClockwise Direction */
{
Val = 0x11;
for(i=0;i<4;i++)
{
P0 = Val; /* Write data for anticlock wise direction*/
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 18
Val = Val1;
delay(575);
}
}
}
}
4. Seven segment display
#include<stdio.h>
#include <REG51.H>
void delay(int g);
int port[20] = {0x,0x,0x,0x,
0xc6,0x86,0xc7,0x86,
0xbf,0xc0,0xde,0x87,
0xbf,0x92,0x91,0x92,
0x92,0xc8,0x86,0x87},i;
void main(){
int d,b,j,k,s;
while(1){
i=0;
for(d=0;d<5;d++)
{
for(b=0;b<4;b++)
{
k=port[i++];
for(j=0;j<8;j++){
s=k;
s=s&0x80;
if(s==0x00)
P1= 0x00;
else
P1=0x1;
P2 = 0x1;
P2=0x00;
s=k;
s=s1;
k=s;
}
}
delay(10000);
delay(10000);
}
}
}
void delay(int g)
{
int h;
for(h=0;h<=g;g++)
{
;
}
}
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 19
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 20
http://cnx.org/content/m32822/1.1/
Connexions module: m32822 21
http://cnx.org/content/m32822/1.1/