Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PART 1- PROGRAMMING
I. Data Transfer
1. Write an ALP to transfer n=5 bytes of data from location 8035h to
location 8041h (without overlap)
org 9000h
mov dph,#80h
mov r0,#35h
mov r1,#41h
mov r3,#05h
back:mov dpl,r0
movx a,@dptr
mov dpl,r1
movx @dptr,a
inc r0
inc r1
djnz r3,back
lcall 0003h
org 8035h
db 01h
db 02h
db 03h
db 04h
db 05h
end
Result: 8041: 01, 02,03,04,05
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
2. Write an ALP to exchange n=5 bytes of data at location 35h & at location
41h.
org 9000h
mov r0,#35h
mov r1,#41h
mov r3,#05h
back:mov a,@r0
mov r2,a
mov a,@r1
mov @r0,a
mov a,r2
mov @r1,a
inc r0
inc r1
djnz r3,back
l
call 0003h
org 35h
db 01h,02h,03h,04h,05h
org 41h
db 06h,07h,08h,09h,0ah
end
Result:
0035:06,07,08,09,0a
0041:01,02,03,04,05
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
6. Write an ALP to find the smallest element in a given string of n=6 bytes
& store the smallest element at location 8062h.
org 9000h
mov r3,#05h
mov dptr,#8000h
movx a,@dptr
mov r1,a
nextbyte:inc dptr
movx a,@dptr
clr c
mov r2,a
subb a,r1
jnc skip
mov a,r2
mov r1,a
skip:djnz r3,nextbyte
mov dpl,#62h
mov a,r1
movx @dptr,a
lcall 0003h
org 8000h
db 04,05,06,07,08,02
end
Result: 806202
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
7.Write an ALP to find the largest element in a given string of n=6 bytes &
store the largest element at location 8062h.
org 9000h
mov r3,#05h
mov dptr,#8000h
movx a,@dptr
mov r1,a
nextbyte:inc dptr
movx a,@dptr
clr c
mov r2,a
subb a,r1
jc skip
mov a,r2
mov r1,a
skip:djnz r3,nextbyte
mov dpl,#62h
mov a,r1
movx @dptr,a
lcall 0003h
org 8000h
db 04,05,06,07,08,02
end
Result: 806208
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
II.Arithmetic Instructions
1. Write an ALP to add 2 8-bit numbers.
org 9000h
mov a,#30h
add a,#50h
lcall 0003h
Result: A=80h
2. Write an ALP to add 2 16-bit numbers.
org 9000h
mov dptr,#1234h
mov a,#12h
mov b,#32h
add a,dpl
mov dpl,a
mov a,b
addc a,dph
mov dph,a
lcall 0003h
Result:1234
3. Write an ALP to add n 8-bit numbers.
org 9000h
mov dptr,#1234h
mov r0,#08h
movx a,@dptr
up:mov b,a
inc dptr
movx a,@dptr
add a,b
djnz r0,up
lcall 0003h
org 1234h
db 1,2,3,4,5,6,7,8,9
end
Result: A=2D
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
10
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
0C 58
DEPT OF ECE
11
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
inc dptr
movx @dptr,a
mov a,b
inc dptr
movx @dptr,a
lcall 0003h
org 9000h
db 56h
end
Result: 900056 98
DEPT OF ECE
B4 09
12
MICROCONTROLLER LAB
end
R.Y.M.E.C,BELLARY
DEPT OF ECE
13
MICROCONTROLLER LAB
III.Counters
1. Write a program to display decimal up-counter.
org 8000h
start:mov r6,#00h
back:push r6
lcall 66d3h
lcall 6796h
lcall 6796h
pop r6
mov a,r6
add a,#01h
da a
mov r6,a
cjne r6,#00h,back
sjmp start
end
2. Write a program to display decimal down-counter
org 8000h
start:mov r6,#99h
back:push r6
lcall 66d3h
lcall 6796h
lcall 6796h
pop r6
mov a,r6
add a,#99h
da a
mov r6,a
cjne r6,#00h,back
sjmp start
end
R.Y.M.E.C,BELLARY
DEPT OF ECE
14
MICROCONTROLLER LAB
DEPT OF ECE
15
MICROCONTROLLER LAB
pop r6
mov a,r6
add a,#01h
mov r6,a
cjne r6,#00h,back
mov a,r7
add a,#01h
mov r7,a
cjne r7,#00h,l1
lcall 0003h
6. Write an ALP to display 8-bit up/down count.
org 8000h
start:mov r6,#00h
back:push r6
lcall 66d3h
lcall 6796h
lcall 6796h
pop r6
mov a,r6
add a,#01h
da a
mov r6,a
cjne r6,#00h,back
mov r6,#99h
back1:push r6
lcall 66d3h
lcall 6796h
lcall 6796h
pop r6
mov a,r6
add a,#99h
da a
mov r6,a
cjne r6,#00h,back1
sjmp start
end
R.Y.M.E.C,BELLARY
DEPT OF ECE
16
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
17
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
18
MICROCONTROLLER LAB
4. Write an ALP to check whether the given number is +ve or -ve.If the
number is +ve store 00h else store ffh.
org 8000h
mov a,#70h
anl a,#80h
jz posi
mov a,#0ffh
lcall 0003h
posi:mov a,#00h
lcall 0003h
Result: 00 [Positive]
5. Write an ALP to check whether the given number is odd or even.If
number is odd store ffh else store 00h
org 8000h
mov a,#11h
rrc a
jc add
mov a,#00h
sjmp last
add:mov a,#0ffh
last:lcall 0003h
lcall 0003h
Result: Affh
6. Write an ALP to count number of 1's and 0's in a byte.
org 8000h
mov r0,#00h
mov r1,#00h
mov r3,#08h
mov dptr,#9000h
movx a,@dptr
back:clr c
rlc a
jc down
inc r0
R.Y.M.E.C,BELLARY
DEPT OF ECE
19
MICROCONTROLLER LAB
sjmp next
down:inc r1
next:djnz r3,back
lcall 0003h
org 9000h
db 12h
end
Result:
R0-->06(0's)
R1-->02(1's)
R.Y.M.E.C,BELLARY
DEPT OF ECE
20
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
21
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
22
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
Ascii:mov r4,a
clr c
subb a,#0ah
mov a,r4
jc skip
add a,#07h
skip:add a,#30h
ret
end
Result:50:2c 43 32
DEPT OF ECE
23
MICROCONTROLLER LAB
VI.Code Conversion
1. Write a program to convert Decimal to Hex.
org 9000h
mov dptr,#1234h
movx a,@dptr
anl a,#0f0h
swap a
mov b,#0ah
mul ab
mov r1,a
movx a,@dptr
anl a,#0fh
add a,r1
inc dptr
movx @dptr,a
lcall 0003h
org 1234h
db 45h
end
Result: 123445 2D
2. Write an ALP to convert Hex to Decimal
org 8000h
mov dptr,#9000h
movx a,@dptr
mov b,#10
div ab
inc dptr
xch a,b
movx @dptr,a
xch a,b
mov b,#10
div ab
inc dptr
xch a,b
movx @dptr,a
xch a,b
inc dptr
movx @dptr,a
lcall 0003h
org 9000h
db 0ffh
end
Result: 900005 05 02
R.Y.M.E.C,BELLARY
DEPT OF ECE
24
MICROCONTROLLER LAB
DEPT OF ECE
25
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
26
MICROCONTROLLER LAB
2. Write an ALP to transfer the message "HELLO" serially at 9600 baud,8bit,1 stop bit.
org 9000h
mov tmod, #20h
mov th1,#fdh
mov scon,#50h
setb tr1
start:mov a,#"h"
acall trans
mov a,#"e"
acall trans
mov a,#"l"
acall trans
mov a,#"l"
acall trans
mov a,#"o"
acall trans
lcall 0003h
trans:mov sbuf,a
here:jnb ti,here
clr ti
ret
end
TI flag=1,after the transmission.Hence in the subroutine wait untill TI is set.
Later clear the TI flag &continue with transmission of the next byte by
writing into the SBUF register.
R.Y.M.E.C,BELLARY
DEPT OF ECE
27
MICROCONTROLLER LAB
II.INTERFACING
1. Write C 'program to display the key pressed (no. of times) by interfacing
Alphanumeric LCD panel & Hex keypad interface module to 8051 micro
controller.
#include <Intel\8051.h>
#include <standard.h>
#define PORTA
0x2040
#define
PORTB
0x2041
#define
PORTC
0x2042
#define
CNTL
0x2043
#define
CNTL_BYTE
0x81
void lcd_init(void);
void clr_disp(void);
void lcd_com(void);
void lcd_data(void);
void scan(void);
void get_key(void);
void display(void);
void display1(void);
void dec_ascii(void);
xdata unsigned char *p8255_cntl ;
xdata unsigned char *p8255_porta ;
xdata unsigned char *p8255_portb ;
xdata unsigned char *p8255_portc ;
idata unsigned char row,col,key;
code unsigned char scan code[16]={
0xED, 0xDD, 0xBD, 0x7D,
0xEB, 0xDB, 0xBB, 0x7B,
0xE7, 0xD7, 0xB7, 0x77};
0xEE, 0xDE,
0xBE, 0x7E,
R.Y.M.E.C,BELLARY
DEPT OF ECE
28
MICROCONTROLLER LAB
p8255_cntl =
CNTL;
*p8255_cntl =
CNTL_BYTE ;
p8255_portc = PORTC;
p8255_porta = PORTA;
p8255_portb =PORTB;
count=1;
lcd_init();
delay_ms(5);
pre_key = 0xFF;
while(1)
{
get_key();
display();
DEPT OF ECE
29
MICROCONTROLLER LAB
} // end of for
if(flag == 0xff)
break;
} // end of while
for(i=0;i<16;i++)
{
if(scan_code[i] == res1) //scan code is same as res1
{
//position of that value is taken
result = ASCII_CODE[i]; //corresponding ascii value is stored in result
break;
}
}
}// end of get_key();
void scan(void)
{
unsigned char t;
temp2 = *p8255_portc;
temp2
temp2 = temp2 & 0x0f;
if(temp2 != 0x0f)
{
delay_ms(30);
delay_ms(30);
temp2 = *p8255_portc;
temp2 = temp2 & 0x0f;
if(temp2 != 0x0f) // check again portc changes
{
flag = 0xff; //if yes make flag bit high
res1 = temp2;
//
res1 = res1 << 4;
t = (temp3 << 4) & 0xf0; //temp3 value is left shifted by 4 times
res1 = res1 | t;
//mask with 0x0f,or with res1
}
//final value stored in res1
only
else
{
flag = 0x00;
}
}
} // end of scan()
R.Y.M.E.C,BELLARY
DEPT OF ECE
30
MICROCONTROLLER LAB
void display(void)
{
temp = 0x80;
lcd_com();
delay_ms(5);
temp = result;
lcd_data();
delay_ms(5);
}
void display1(void)
{
temp = 0xC0;
lcd_com();
delay_ms(5);
temp = acount1;
lcd_data();
delay_ms(5);
temp = acount2;
lcd_data();
delay_ms(5);
}
// lcd initialisation routine.
void lcd_init(void)
{
temp = 0x38;
lcd_com();
delay_ms(5);
//address of display
temp = 0x38;
lcd_com();
delay_ms(5);
temp = 0x0f;
lcd_com();
delay_ms(5);
temp = 0x06;
lcd_com();
delay_ms(5);
clr_disp();
}// clears display
void clr_disp(void)
{
temp = 0x01;
lcd_com();
delay_ms(5);
R.Y.M.E.C,BELLARY
//disply on,cursor on
//shift cursor right
DEPT OF ECE
31
MICROCONTROLLER LAB
}
// get the command to be presented to lcd in byte fashion
// and present it in nibble fashion
void lcd_com(void)
{
*p8255_porta = temp; //copy temp value to porta
temp1=0x0f;// RS=0
*p8255_portc = temp1;
temp1=temp1 | 0x4f; // enable 1
*p8255_portc = temp1;
delay(5);
temp1=temp1 & 0xbf;
*p8255_portc = temp1;
}
// get the data to be presented to lcd in byte fashion in temp1
// and present it in nibble fashion
void lcd_data(void)
{
*p8255_porta=temp;
//put the temp value in porta
temp1=0x1f;
//RS=1
*p8255_portc = temp1;
temp1=temp1 | 0xdf;
//r/w=0 for write
*p8255_portc = temp1;
temp1=temp1 | 0x4f;
//E=1 for high pulse
*p8255_portc = temp1;
delay(5);
temp1=temp1 & 0xbf;
//E=0 for high to low pulse
*p8255_portc = temp1;
}
void dec_ascii(void)
{
acount1 = (count/10) +0x30;
acount2 = (count % 10) + 0x30;
}
Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
32
MICROCONTROLLER LAB
2. Write C' program to measure temperature by interfacing External ADC &
Temperature control interface module to 8051 microcontroller.
#include <Intel\8051.h>
#include <standard.h>
#define PORTA
0x2040
#define
PORTB
0x2041
#define
PORTC
0x2042
#define
CNTL
0x2043
#define
buff
0x196
xdata unsigned char *p8255_cntl ;
xdata unsigned char *p8255_porta ;
xdata unsigned char *p8255_portb ;
xdata unsigned char *p8255_portc ;
xdata unsigned char *buff_ptr;
idata unsigned char temp1,adc_val,temperature,dtemp,i;
xdata unsigned char adc_table[100]={
0X00,0X03,0X05,0X08,0X0A,0X0D,0X0F,0X11,0X14,0X16,
0X19,0X1C,0X1E,0X21,0X24,0X27,0X2A,0X2C,0X2E,0X32,
0X34,0X36,0X39,0X3C,0X3F,0X42,0X45,0X48,0X4A,0X4C,
0X4E,0X50,0X52,0X54,0X56,0X58,0X5B,0X5E,0X61,0X64,
0X67,0X6A,0X6D,0X70,0X72,0X74,0X77,0X7A,0X7D,0X7F,
0X82,0X85,0X87,0X8A,0X8D,0X90,0X92,0X95,0X97,0X9A,
0X9D,0XA0,0XA2,0XA5,0XA8,0XAA,0XAD,0XAF,0XB0,0XB3,
0XB6,0XB9,0XBC,0XBF,0XC1,0XC4,0XC6,0XC9,0XCC,0XCF,
0XD0,0XD2,0XD5,0XD7,0XDA,0XDC,0XDF,0XE0,0XE2,0XE5,
0XE7,0XE9,0XEB,0XEE,0XF1,0XF4,0XF6,0XF9,0XFC,0XFF};
extern void display(void);
void main ()
{
buff_ptr=buff; // mem. locn to hold adc data to display
p8255_porta = PORTA;
p8255_portc = PORTC;
p8255_portb = PORTB;
p8255_cntl =
*p8255_cntl =
CNTL;
0x98;// Ppa=i/p,Pb=o/p,PCu=i/p,PCl=o/p,
delay(200);
while(1)
{
*p8255_portc =
*p8255_portc =
*p8255_portc =
delay(200);
R.Y.M.E.C,BELLARY
DEPT OF ECE
33
MICROCONTROLLER LAB
do
{
temp1=*p8255_portc;
temp1=temp1 & 0x80;
} while(temp1 != 0x80);
delay(200);
adc_val = *p8255_porta; // display adc result on the data field
//
*buff_ptr = adc_val;
for(i=0;i<100;i++)
{
if(adc_val==adc_table[i])
{
temperature = i;
break;
}
}
dtemp = ((temperature/10) <<4 ) | ((temperature%10) & 0x0f);
*buff_ptr=dtemp; // WRITE TO 0X196 LOCAN
display();
delay(200);
}
// end of while(1)
}
DISPLAY PROGRAM:
PUBLIC _display
_display:
lcall 676fh
ret
Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
34
MICROCONTROLLER LAB
3. DAC PROGRAMS:
(i) Write C'program to generate Ramp waveform using DAC Interface to
8051.
#include <Intel\8051.h>
#include <standard.h>
#define p8255_ctl 0x2043
#define porta 0x2040
#define portb 0x2041
#define ctl_word
0x80
#define SET_FREQ
0xF000
xdata unsigned char *ptr_8255_ctl;
xdata unsigned char *ptr_8255_porta;
xdata unsigned char *ptr_8255_portb;
xdata unsigned char *buff_ptr;
idata unsigned char count,con1,con2,temp2;
interrupt(INT_TMR0) tmr_isr(void) using 1
{
TL0 = con2;
TH0 = con1;
if(count!=0xff)
{
*ptr_8255_porta = count;
count++;
}
else
{
count=0x00;
*ptr_8255_porta = count;
}
}
void main ()
{
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctl_word;
ptr_8255_porta= porta;
buff_ptr = SET_FREQ;
temp2 = *buff_ptr;
switch(temp2)
{
case 0x10: con1 = 0xfe;
con2 = 0x90;
break;
R.Y.M.E.C,BELLARY
DEPT OF ECE
35
MICROCONTROLLER LAB
case 0x20:
con1 = 0xff;
con2 = 0x4d;
break;
case 0x30: con1 = 0xff;
con2 = 0x8a;
break;
case 0x40: con1 = 0xff;
con2 = 0xa6;
break;
default: con1 = 0xfe;
con2 =0x90;
break;
}
TMOD = 0x01;
TL0 = con2;
TH0 = con1;
ET0 = 1;
EA = 1;
TR0 = 1;
here: goto here;
} // end of main()
Result:
DEPT OF ECE
36
MICROCONTROLLER LAB
else
{
count = 0xFF;
*ptr_8255_porta = count;
}
}
void main ()
{
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctl_word;
ptr_8255_porta= porta;
buff_ptr = SET_FREQ;
temp2 = *buff_ptr;
count = 0x00;
switch(temp2)
{
//depending on different frq
case 0x10: con1 = 0xdb; //0xff; //we are setting diff values to timer0
con2 = 0xff; //0x4d;
break;
case 0x20:
con1 = 0xed; //0xfe;
con2 = 0xff; //0x90;
break;
case 0x30:
con1 = 0xf3; //0xfd;
con2 = 0xff; //0xa6;
break;
case 0x40: con1 = 0xf6 ;//0xfc;
con2 = 0xff; //0x8a;
break;
default: con1 = 0xdb; //0xfb;
con2 = 0xff; //0x4d;
break;
}
TMOD = 0x01;
//mode 1 selection
TL0 = con2;
TH0 = con1;
ET0 = 1;
//enable timer0,all interrupt
bit
EA = 1;
TR0 = 1;
//start timer0
here: goto here;
} // end of main()
Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
37
MICROCONTROLLER LAB
(iii)Write C'program to generate Triangle waveform using DAC Interface to
8051.
#include <Intel\8051.h>
#include <standard.h>
#define p8255_ctl 0x2043
#define porta 0x2040
#define portb 0x2041
#define ctl_word
0x80
#define SET_FREQ
0xF000
xdata unsigned char *ptr_8255_ctl;
xdata unsigned char *ptr_8255_porta;
xdata unsigned char *ptr_8255_portb;
xdata unsigned char *buff_ptr;
idata unsigned char count,con1,con2,temp2,df;
interrupt(INT_TMR0) tmr_isr(void) using 1
{
TL0 = con2;
TH0 = con1;
if(df)
{
*ptr_8255_porta = count;
count = count + 1;
if(count==0x7f)
df=0x00;
}
else
{
*ptr_8255_porta = count;
count = count - 1;
if(count==0x00)
df=0xff;
}
}
void main ()
{
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctl_word;
ptr_8255_porta= porta;
buff_ptr = SET_FREQ;
temp2 = *buff_ptr;
df = 0xff;
switch(temp2)
//different counts for timer depending on frq
{
case 0x10: con1 = 0x0fe;//0xdb; //0xff;
con2 = 0x90; //0xff; //0x4d;
break;
R.Y.M.E.C,BELLARY
DEPT OF ECE
38
MICROCONTROLLER LAB
case 0x20:
con1 =0xff;//0xed; //0xfe;
con2 = 0x4d; //0xff; //0x90;
break;
case 0x30:
con1 = 0xff;//0xf3; //0xfd;
con2 =0x79; //0xff; //0xa6;
break;
case 0x40: con1 = 0xff;//0xf6 ;//0xfc;
con2 =0xc6;//0xff; //0x8a;
break;
default: con1 = 0xfe;//0xdb; //0xfb;
con2 =0x90; //0xff; //0x4d;
break;
}
TMOD = 0x01;
//mode1 selection
TL0 = con2;
TH0 = con1;
ET0 = 1;
EA = 1;
//enable all interrupt bit,timer0 bit
TR0 = 1;
//start timer0
here: goto here;
} // end of main()
Result:
(iv) Write C' program to generate Sinusoidal waveform using DAC Interface
to 8051.
#include <Intel\8051.h>
#include <standard.h>
#define p8255_ctl 0x2043
#define porta 0x2040
#defineportb 0x2041
#define ctl_word
0x80
#define SET_FREQ
0xF000
xdata unsigned char sine_tab[48]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,0xFF,
0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,0x80,
0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,0x00,
0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80 };
xdata unsigned char *ptr_8255_ctl;
xdata unsigned char *ptr_8255_porta;
xdata unsigned char *ptr_8255_portb;
xdata unsigned char *buff_ptr;
idata unsigned char sine_val,con1,con2,temp2;
idata int count;
R.Y.M.E.C,BELLARY
DEPT OF ECE
39
MICROCONTROLLER LAB
interrupt(INT_TMR0) tmr_isr(void) using 1
{
TL0 = con2;
TH0 = con1;
if(count<48)
{
sine_val = sine_tab[count];
*ptr_8255_porta = sine_val;
count++;
}
else
count = 0;
}
void main ()
{
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctl_word;
ptr_8255_porta= porta;
buff_ptr = SET_FREQ;
//value is from location F000h
temp2 = *buff_ptr;
count = 0;
switch(temp2)
//different count values are loaded
{
//according to the different frequency
case 0x10: con1 = 0xf8;
con2 = 0x7f;
break;
case 0x20:
con1 = 0xfc;
con2 = 0x3f;
break;
case 0x30:
con1 = 0xfd;
con2 = 0x7f;
break;
case 0x40: con1 = 0xfe;
con2 = 0x1f;
break;
default: con1 = 0xf8;
con2 = 0x7f;
break;
}
TMOD = 0x01;
TL0 = con2;
TH0 = con1;
R.Y.M.E.C,BELLARY
DEPT OF ECE
40
MICROCONTROLLER LAB
ET0 = 1;
timer0
EA = 1;
TR0 = 1;
R.Y.M.E.C,BELLARY
DEPT OF ECE
41
MICROCONTROLLER LAB
4. STEPPER MOTOR:
(i) Write C' program to rotate Stepper motor in Clockwise direction by
interfacing Stepper motor interface module with 8051 microcontroller.
#include <Intel\8051.h>
#define p8255_ctl
0x2043
#define portc
0x2042
#define ctlr_word
0x80
#define phasea
0x0d
#define phaseb
0x0e
#define phasec
0x07
#define phased
0x0b
xdata unsigned char *ptr_8255_ctl;
xdata unsigned char *ptr_8255_portc;
void delay(void);
void main ()
{
int i;
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctlr_word;
while(1)
{
ptr_8255_portc=portc;
is taken in pointer variable
//different speeds are loaded into pointer
*ptr_8255_portc = phasea;
delay();
delay();
*ptr_8255_portc = phaseb;
delay();
delay();
*ptr_8255_portc = phasec;
delay();
delay();
*ptr_8255_portc = phased;
delay();
delay();
}
}
void delay(void)
{
int i=0;
//port a address
{
for(i=0;i<=10000;i++)
}
}
Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
42
MICROCONTROLLER LAB
(ii) Write C'program to rotate Stepper motor in AntiClockwise direction by
interfacing Stepper motor interface module with 8051 microcontroller.
#include <Intel\8051.h>
#define p8255_ctl
0x2043
#define portc
0x2042
#define ctlr_word
0x80
#define phasea
0x0b
#define phaseb
0x07
#define phasec
0x0e
#define phased
0x0d
xdata unsigned char *ptr_8255_ctl;
xdata unsigned char *ptr_8255_portc;
void delay(void);
void main ()
{
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctlr_word;
while(1)
{
ptr_8255_portc=portc;
//address of portc in pointer
//different speeds are loaded in pointer
*ptr_8255_portc = phasea;
delay();
delay();
*ptr_8255_portc = phaseb;
delay();
delay();
*ptr_8255_portc = phasec;
delay();
delay();
*ptr_8255_portc = phased;
delay();
delay();
}
}
void delay(void)
{
unsigned int i;
{
for(i=0;i<=10000;i++)
}
}
Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
43
MICROCONTROLLER LAB
5. DC MOTOR:
Write C' program to rotate DC motor by interfacing Dc motor interface
module to 8051 microcontroller.
// program to test nifc55 dc motor
// by varying off-time and on-time with different values
// user can observe different speed on dc motor
// typically the off values are 30,24,18 & 12
// on values are 10,16,12 & 28
// in this source off value is taken as 30 and
// on value is taken as 10
#include <Intel\8051.h>
#define p8255_ctl
0x2043
#define ctlr_word
0x80
idata unsigned char off_time,on_time;
idata unsigned char i;
xdata unsigned char *ptr_8255_ctl;
xdata unsigned char *ptr_8255_portc;
void main ()
{
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctlr_word; // configure 8255 all o/p
TCON = 0;
TMOD = 0x21; //select mode1, timer 0
off_time = 30;
on_time = 10;
while(1)
{
TL0 = 0x66; //timer count set for 5msec
TH0 = 0xFC;
*ptr_8255_ctl = 0x09;
// make PC4 high
for(i=0;i<off_time;i++)
{
TR0 =1;
while(TF0)
{
}
TF0 = 0;
TR0=0;
}
TL0 = 0x66; //timer count set for 5msec
TH0 = 0xFC;
*ptr_8255_ctl =0x08; // make PC4 low
for(i=0;i<on_time;i++)
{
TR0 =1;
R.Y.M.E.C,BELLARY
DEPT OF ECE
44
MICROCONTROLLER LAB
while(TF0)
{
}
TF0 = 0;
TR0=0;
}
}
}
Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
45
MICROCONTROLLER LAB
6. ELEVATOR:
Write C' program to implement Elevator by interfacing Elevator interface
module with 8051
microcontroller.
#include <Intel\8051.h>
#include <standard.h>
#define p8255_ctl
0x2043
#define porta
0x2040
#defineportb
0x2041
#defineportc
0x2042
#define ctl_word
0x82
void floor1(void);
void floor2(void);
void floor3(void);
void floor4(void);
void moveup(void);
void movedown(void);
idata unsigned char req,preq,freq,temp,fg,i,j;
xdata unsigned char *ptr_8255_ctl;
xdata unsigned char *ptr_8255_porta;
xdata unsigned char *ptr_8255_portb;
xdata unsigned char *ptr_8255_portc;
void main ()
{
ptr_8255_ctl = p8255_ctl;
*ptr_8255_ctl = ctl_word;
ptr_8255_porta = porta;
ptr_8255_portb = portb;
ptr_8255_portc = portc;
*ptr_8255_porta = 0x0f;
//porta lower bits for green and amber
delay(50);
//leds,higher bits for enabling 4 flipflops
*ptr_8255_porta = 0x0f0;
// mem. locn to hold adc data to display
freq=0xf0;
//default request is from ground floor
while(1)
{
// floor status 1st floor
if(freq==0xf0)
{
*ptr_8255_porta=0xf0;
req = *ptr_8255_portb; //read the request from portb
temp = req;
req = req & 0x0f;
if(req == 0x0e)
//request is from same floor when it is in flr1
{
floor1();
R.Y.M.E.C,BELLARY
DEPT OF ECE
46
MICROCONTROLLER LAB
}
if(req ==0x0d)
//request from second floor when it is in flr1
{
preq = 0xf0;
//correspondingly update the preq and freq
freq = 0xf3;
//freq holds the future request,
moveup();
//preq holds present stage
floor2();
}
if(req ==0x0b)
//request from third floor when it is in flr1
{
preq = 0xf0;
freq = 0xf6;
moveup();
//blink leds from floor 1 to 3
floor3();
//red led is off and green led is on
}
if(req ==0x07)
{
preq = 0xf0;
freq = 0xf9;
moveup();
floor4();
}
}
R.Y.M.E.C,BELLARY
DEPT OF ECE
47
MICROCONTROLLER LAB
if(req ==0x0b)
//request from third floor when it is flr2
{
preq = 0xf3;
freq = 0xf6;
moveup();
//blink leds from floor 2 to 3
floor3();
//red led is off and green led is on
}
if(req ==0x07)
//request from fourth floor when it is flr2
{
preq = 0xf3;
freq = 0xf9;
moveup();
//blink leds from floor 2 to 4
floor4();
//red led is off and green led is on
}
} //end of for loop
// floor status 3rd floor
if(freq==0xf6)
{
req=*ptr_8255_portb;
temp =req;
req = req & 0x0f;
if(req ==0x0e)
//request from first floor when it is flr3
{
preq = 0xf6;
freq = 0xf0;
movedown();
//blink leds from floor 3 to 1
floor1();
//red led is off and green led is on
}
if(req ==0x0d)
{
preq = 0xf6;
freq = 0xf3;
movedown();
floor2();
}
if(req == 0x0b)
//request from same floor when it is flr3
{
floor3();
//red led is off and green led is on
}
if(req ==0x07)
{
preq = 0xf6;
freq = 0xf9;
R.Y.M.E.C,BELLARY
DEPT OF ECE
48
MICROCONTROLLER LAB
moveup();
floor4();
}
} //end of for loop
}
if(req == 0x07)
{
floor4();
}
} //end of for loop
}// end of while(1)
}
//end of main()
void floor1(void)
R.Y.M.E.C,BELLARY
DEPT OF ECE
49
MICROCONTROLLER LAB
{
*ptr_8255_porta=0xe0;
on
delay_ms(20);
}
void floor2(void)
{
*ptr_8255_porta=0xd3;
delay_ms(20);
}
void floor3(void)
{
*ptr_8255_porta=0xb6;
delay_ms(20);
}
void floor4(void)
{
*ptr_8255_porta=0x79;
delay_ms(20);
}
void moveup(void)
{
// initialise loop to on leds
//
unsigned char i;
for(i=preq;i<freq;i++)
{
delay_ms(250);
*ptr_8255_porta = i; //leds blink from lower to upper value
delay_ms(50);
}
} // end of move up
void movedown(void)
/* initialise loop to on leds */
//
unsigned char j;
for(j =preq;j>freq;j--)
{
delay_ms(250);
*ptr_8255_porta = j;
//leds blink from upper to lower value
delay_ms(50);
}
}
/* end of move down */
Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
50
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
51
MICROCONTROLLER LAB
INDEX
SL
NO.
PROGRAMMING
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
R.Y.M.E.C,BELLARY
DEPT OF ECE
PAGE
NO.
52
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
53
MICROCONTROLLER LAB
SL
NO.
31
32
33
34
35
36
37
38
39
II
INTERFACING
01
02
03
PAGE
NO.
DAC PROGRAMS:
(i) C' program to generate Ramp waveform using DAC
Interface to
8051.
(ii) C' program to generate Square waveform using DAC
Interface to
8051.
(iii) C' program to generate Triangle waveform using DAC
Interface
to 8051.
(iv) C 'program to generate Sinusoidal waveform using DAC
Interface to 8051
04
STEPPER MOTOR:
(i) C' program to rotate Stepper motor in Clockwise direction
by interfacing Stepper motor interface module with 8051
microcontroller.
(ii) C' program to rotate Stepper motor in Clockwise direction
by interfacing Stepper motor interface module with 8051
microcontroller.
05
06
54
MICROCONTROLLER LAB
SL NO.
04
PAGE
NO.
STEPPER MOTOR:
(i) C' program to rotate Stepper motor in Clockwise direction by
interfacing Stepper motor interface module with 8051
microcontroller.
(ii) C' program to rotate Stepper motor in Clockwise direction by
interfacing Stepper motor interface module with 8051
microcontroller.
05
06
R.Y.M.E.C,BELLARY
DEPT OF ECE
55
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
56
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
57
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
58
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
59