CONTENTS
Experiments Date of
experimentation
Data Transfer
Arithmetic Operations
Code Conversion
Array Handling
1/O Programming
LED & Toggle Switch Interface
DC Motor Control System
Stepper Motor Control System
Waveform Generation
LED Display Interface
Viva Questions
References
Evaluation Plan:
Continuous Evaluation - 50%
a Regularity, Preparation, Documentation, Viva and Performance.
> Mini Project - 10%
> Lab test - 40%MICROPROCESSORS & MICROCONTROLLERS Lab
1. Data Transfer
1. Program to transfer 5 bytes of data from internal memory location starting from 20h to
internal memory location starting from 40h.
Program:
ong tan + Start of program
MOV R2, #0SH Initialize the counter
Initialize the source memory
MOV RO, #20H
snd ‘location to 20h
Initialize the destination memory
bast atc aal ‘location to 40h
Copy a byte of data 20h to the
AGAIN: | MOV A, @RO ee
:Copy content of accumulator to
MOV @RI, A
a 40h
INC RO sIncrement the source memory
slocation by 1
Increment the destination memory
aie slocation by 1
DJNZ R2, AGAIN Decrement the counter,
if coum #0, jump w AGAIN
; End of program.
Dept. of ICE, MIT, Manipal Page 1MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Result:
Before Execution.
oxo 0x00
0x02 4 0x00
0x03 0x00
ox04 0x00
0x05 0x00
Ox01
0x02
0x03
Ox04
0x05
Dept. of ICE, MIT, Manipal Page 2MICROPROCESSORS & MICROCONTROLLERS Lab
2. Program to interchange two blocks of data residing at internal memory locations starting
from 20h and 40h.
Program:
ORG oor ; Start of program
MOV R2, #0SH 3 Initialize the counter
MOV RO, #20H + Initialize the source memory location
MOV RI, #40H ; Initialize destination memory location
AGAIN: [| MOV A, @RO
XCH A, @RI 3 Exchange A with data pointed to by RO
MOV @RO, A 3 Copy the contents of A to the address in
:RO
INC RO 5 Increment the source memory pointer
INCRI + Increment the destination memory pointer.
TaN RE AGAIN 3 Decrement the counter,
Jitcount £0, jump to AGAIN
END + End of program
Dept. of ICE, MIT, Manipal Page 3MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Result:
Before Execution.
Source Address | om |
| sins” ||
Ox02
0x05
Dept. of ICE, MIT, Manipal Page 4MICROPROCESSORS & MICROCONTROLLERS Lab
3. Program to transfer 5 bytes of data from external memory location starting from 9400h to
external memory location starting from 9500h.
ORG 00H ; Start of program
START: | MOV_ RO, #05H 3 Initialize the counter.
MOV DPTR, #9400H sInitialize memory location 9400h
MOV RI1,82H 3Save the address in RI and R2
MOV R2,83H
MOV DPTR, #9500H sInitialize memory location 9500h
MOVX @DPTR, A Copy data from A to the external
saddress in DPTR:
INC DPTR
PUSH 82H ; Push the address onto the stack
PUSH 83H
MOV 82H, RI 3 Save the address in DPL and DPH
MOV 83H, R2
INC DPTR + Increment the memory location by 1
MOVX A, @DPTR Copy the data byte in accumulator
sto destination memory location
MOV R18:
MOV R2,83h
POP 83h ; Retrieve the destination memory
slocation address from the stack
Dept. of ICE, MIT, ManipalMICROPROCESSORS & MICROCONTROLLERS Lab
POP 82h
DINERO AGAIN ; Decrement the counter,
sif count #0, jump to AGAIN
END End of program
Result:
Before Execution.
9400h, 9500h 0x00
9401h
9501h 0x00
9402h 9502h 0x00
9403h, 9503h 0x00
9404h, 9504h 0x00
After execution.
[sewer ssion | mn |
94008, 9500h
40th 9501h
9402h 9502h
0403h, 9503h
9404h, 9504h
Dept. of ICE, MIT, Manipal Page 6MICROPROCESSORS & MICROCONTROLLERS Lab
4, Program to interchange 5 bytes of data residing at external memory locations starting from
9400h and 9500h,
Program:
ORG 00h
START:
MOV RO, #05H
MOV DPTR, #94001
MOV R1, 83H
MOV R2, 82H
MOV DPTR, #95001
AGA
PUSH 82H
PUSH 83H
MOVX A, @DPTR
MOV R3,
MOV 83H, Ri
MOV 82H, R2
MOVX A, @DPTR
XCH A, R3
MOVX @DPTR, A
INC DPTR
MOV RI, 83H
MOV R2, 82H
Dept. of ICE, MIT, Manipal
; Start of program
5 Initialize the counter
+ Initialize memory location 9400h
Save the address in RI and R2.
5 Initialize memory location 9500h
+ Push the address onto the stack
3 Copy a byte of data from 9500h to
surnulator
sSave in R3
3 Copy a byte of data from 9400h to
saccumulator
3 Exchange data byte between R3 and
A
5 Increment the memory location
94x
; Save the address in R1 and R2
Page 7MICROPROCESSORS & MICROCONTROLLERS Lab
POP 83H
POP 82H
MOV A, R3
MOVX @DPTR, A
INC DPTR
DJNZ RO, AGAIN
END
HICE-3111]
+ Retrieve the destination memory
slocation address from the stack
+: Copy the data byte in accumulator to
:destination memory location
+ Increment the memory location 95x
+ Decrement the counter,
sif count 40, jump to AGAIN
; End of program
Result:
Before Execution
[sowie | oa |
94008, OxOL
940ih 0x02
o4o2h, 0x03
9403h, 0x04
9404h, 0x05
Dept. of ICE, MIT, Manipal
Address
9500h
9501h
9502h
9503h
9504h
Page 8MICROPROCESSORS & MICROCONTROLLERS Lab
After execution.
‘94008,
9401h
9402h
9403h
9404b,
Dept. of ICE, MIT, Manipal
95008
9501h
9502h
9503h
9504h
HICE-3111]MICROPROCESSORS & MICROCONTROLLERS Lab
2. Arithmetic Operations
1, Program to add two 16-bit numbers residing at internal memory locations.
Program:
LABELS M COMM!
ORG 00h ; Start of program
CLRC Clear carry
MOV R2, #02. + Initialize the counter
MOV RO, #20H + Initialize the pointer to first number
MOV RI, #40H + Initialize the pointer to second.
snumber
MOV A, @RO
ADDC A, @R1 + Perform addition with carry
MOV @RO, A ; Save the result in address pointed by
sO
INCRO :Increment the pointer by one
INCRI
DJNZ R2, LOOP + Decrement count if count #0, jump
tw LOOP
CLRA
ADDC A, #00H Perform if there is a carry
MOV @RO, A ; Storing the number to next location
END ; End of program
Dept. of ICE, MIT, Manipal Page 10MICROPROCESSORS & MICROCONTROLLERS Lab
Result:
Input: Output
Number 1 = 0A85h (16 bit) Addition Result
Number 2 = 1234h (16 bit)
a
1CB9h
HICE-3111]
2. Program to multiply a 16 bit number by an 8 bit number residing at internal memory
locations.
Program:
:ABELS MNEUMONICS COMMENTS
ORG 00H Start of program
MOV RO, 30H +: Load the 8 bit number in RO
MOV A, 31H 5 Load the lower byte of 16 bit
number in A.
MOV B, RO 3 Move the lower byte of 16 bit
number to A
MUL AB s Perform multiplication
MOV 50H, A 3 Store the lower byte result in 50h
MOV RI, B 5 Store the higher byte of result in RI
MOV A, 32H 3 Load the higher byte of 16 bit
snumber to A
Dept. of ICE, MIT, Manipal
Page 11MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
MOV B, 30H 3 Move 8 bit number in to B
MUL AB + Perform multiplication.
MOV R2, B 5 Store the higher byle of result in R2
ADD A, RI 3 Perform addition
JNC FINISH 3 Check if C=1, If not jump to
SFINISH
INC R2 3 IF CY =I, increment higher byte by
cone.
FINISH: | MOV 51H, A 3 Store the middle byte result in ST.
MOV 52H, R2 3 Store the higher byte result in 52h,
END End of program
Result:
Input: Output:
Number 1=11h (8 Multiplication Result= 036542h
Number 2=3322h (16 bit)
30h, 50h
3ih Sih
32h 52h
Dept. of ICE, MIT, Manipal Page 12MICROPROCESSORS & MICROCONTROLLERS Lab
3. Program to find the square root of a number residing at register R3.
Program:
ORG 00H
MOV R34 36
MOV R0,#00H
MOV R1,01H
CLRC
MOV A, R3
SUBB A, RI
MOV R3, A
JNCSQR
MOV A, RO
MOV PO, A
SUMP LOUP
INCRO
MOY A, RI
ADD A, #02H
MOV RI, A
SJMP LOOPL
END
Result:
Input: Load number 36 in R3.
Ouiput: Send result (06) to PO.
Dept. of ICE, MIT, Manipal
:Start of program
Load decimal number 36 in R3
Clear RO
Initialize with RI=1(First odd
number)
Load a with the number 36
;Caleulate(36-odd number)
sLoad (36-odd number) in A
sCheck if C=1, If not jump to SQR
sf C=1 load square root in A
;Send the square root answer to PO
Loop here torever
«Increment RO by 1, if 36>odd
snumber and C=0
sLoad ARI
Calculate next odd number
Load RI with next odd number
Repeat the process until C=1
End of program.
Page 13MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
1. Analyze the following code and show the results on the registers A and B after each
step.
ORG 00H
MOV A, #30H.
SJMP 100H
MOV A, #401
ORG 100H
MOV B, #0CFIL
ANL A,B
SJMP 200H
ORG 200H
XRL A, #0FFH
SJMP 200H
2. Write an 8051 ALP to compute the number of zeros and ones in the following 8 bit-stream.
1011 1100
3. Write an 8051 ALP to add five 8-bit numbers.
Dept. of ICE, MIT, Manipal
Page 14MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
3. Code Conversion
1, Program to convert Packed BCD to ASt
I numbers.
Program:
LABELS MNEUMONICS COMMENTS
ORG 00H ;Start of program
MOV A, #25 :A=25H, packed BCD
MOV R2, A :Keep a copy of BCD data IN R2
ANL A, #0FH :Mask the upper nibble (A=05)
ORLA, #301 :Make an ASCH, A=35H
MOV R6. A Save it in R6
MOV A, R2 :A=25H, Get the original data
ANL A, #0FOH Mask the lower nibble (A=20)
RRA Rotate right
RRA Rotate right
RRA Rotate right
RRA
ORL A, #30H Make an ASCII, A=32H
MOV R2, A Save it in R2
END +: End of program
Result:
Input Output
Dept. of ICE, MIT, Manipal Page 18MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
2. Program to form a 2’s complement number given the 7-bit magnitude and sign information
separately.
ORG 00H Start of program
MOV PO, #00H
MOV RO, #3FH ; Load RO with 3Fh
MOV A,RI ‘Move assign bit to A
JB ACC.0, CONV Check A for sign bit, if | branch
sto CONV
MOV A, RO Move the value in RO to A
MOV P0, A Send 2°s complement number to.
PO
SJMP STOP
MOV A, RO sMove magnitude in A
CPLA ;Complement A.
INCA Add I to A
RO
END ; End of program
Result:
Input:
Assume 7-bit magnitude information is available in RO and sign bit is available in RI.
Output:
Send 2’s complement number to Port 0.
Dept. of ICE, MIT, Manipal Page 16MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
3. Program to convert hexadecimal to its equivalent decimal number.
Program:
LABELS MNEUMONICS COMMENTS,
ORG 00H ; Start of program
MOV RO, #5011
MOV A, PI ; Read data from PI
MOV B, #10 ; B-0AH
DIV AB ; Divide by 10
MOV @RO, B Save lower digit
INC RO
MOV B, #10
DIV AB + Divide by 10 once more
MOV @RO, B + Save the next digit
INC RO
MOV @RO, A ; Save the last digit
END ; End of program
Result:
Input:
Read the data from Port P1 (FEH).
Output
Converted data is available
internal memory locations starting from 30H.
Dept. of ICE, MIT, Manipal Page 17MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
1, Write an 8051 ALP to convert ASCII numbers to packed BCD number.
2. Write an 8051 ALP to convert a decimal number to its equivalent hexadecimal number.
3. Write an 8051 ALP to separate an & bit 2s complement number into magnitude and sign
bit.
Dept. of ICE, MIT, Manipal Page 18MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
4. Array Handling
1, Program to find the smallest number in a given array residing at external memory locations
starting from 9400h,
Program:
LABELS [NEMONICS COMMENTS
ORG 00H ; Start of program
MOV RO, #091 + Initialize the counter
MOV DPTR, #9400H + Initialize the array
MOVX A, @DPTR +; Get a byte of data from the array
MOV RI, A ; Save the number in RI
AGAIN: | INC DPTR ; Increment data pointer
MOVX A, @DPTR + Get the next number
MOV R2. A : Save it in R2
CLRC Clear carry flag
SUBB ARI ; Subtract the firet mumber from the
second number
JNC OVER 5 1fCarry =0 , RI contains the
smallest number
MOV Ri, 02 ; If carry = 1, R2 contains the
smallest number
DJNZ RO, AGAIN ;feount 40 jump to AGAIN
MOV DPTR, #9500H
MOV A,RI
MOVX @DPTR, A : Save the result
END End of program
Dept. of ICE, MIT, Manipal Page 19MICROPROCESSORS & MICROCONTROLLERS Lab
Result:
Before Execution. After Execution:
} smwcaairss [poe |
940th
9402h
9403h
9404h
9408h
94064
9407h
9408h
9409h,
9400h
Dept. of ICE, MIT, Manipal Page 20MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
1. Write an 8051 ALP to find the largest number in a given array.
2. Write an 8051 ALP to sort the given array in ascending order
3. Write an 8051 ALP to sort the given array in descending order.
Dept. of ICE, MIT, Manipal Page 21MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
5. /O Programming
1. Program to toggle all bits of Port 1 by send
lay of 0.6 ms.
the values 55h & AAh continuous!
Puta time
ORG 00H :Start of program
MOV PI, #55H Send 55h to Port 1
LCALL DELAY ; Time delay
MOV P1, #0AAH ;Send Ah to Port |
LCALL DELAY Call time delay
SJMP REPEAT + Repeat toggling
DELAY: MOV RO, #0FFH : Routine for 0.6 m sec delay
AGAIN: DJNZ RO, AGAIN
RET
sip stop
END ; End of program
Result:
The ouput Is observed on Port 1.
Dept. of ICE, MIT, Manipal Page 22MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
2. Program to get the x value from P1 and send x? to P2 continuously. Access the x2 values
from lookup table located in the ROM space of 8051.
Program:
MNEUMONICS COMMENTS
ORG 00H Start of program
MOV DPTR, #XSQR 3 Load the look-up table
caddress
MOV A, PI 5 Get x value from Port |
MOVC A, @A+DPTR 3 Get x squared from table
MOV P2, A 3 Issue it to Port 2
SJMP BACK 3 Keep doing it
ORG 100
DB 0, 1, 4, 9, 16, 25, 36, 49
DB 64, 81, 100, 121, 144, 169, 196, 225
; End of program
Result:
The output is observed on Port 2 based on the status of Port 1.
Dept. of ICE, MIT, Manipal Page 23MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
3. A washing machine is designed for voltage range of 180-240 V. If the voltage is above 240V
or below 180V, the washing machine will shut down by turning off a relay connected to
1.0. Assume that the voltage can be read at Port 0 in the range 0-25SV. Write a program to
LABELS MNEUMONICS COMMENTS
ORG 00H Start of program
SIMP 10H Jump to location 100H.
ORG 100H
INPUT: ] MOV P0, #0FFH ‘Configure PO as input port
MOV A, PO ‘Input the voltage value to A
SUBB A, #180 A=A-180
JC OFF ZaU
SJMP_INPU Voltage in range check the
‘nput again
CLRELO sTurn off the relay
SJMP INPUT Check the input voltage
again
; End of program
Result:
The output is observed on Port 1.
Dept. of ICE, MIT, Manipal Page 24MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
1. Write a program generate an 8 bit BCD up counter counting from 00h to 99h. Store the
result in memory location starting from 9400h. Introduce a time delay of 0.8 ms between
‘h count.
2. Generate the following waveform on P1.3.
3. Generate two square waves — one of 2KHz frequency at pin P1.3 and another of frequency
4KHz at pin P:
Dept. of ICE, MIT, Manipal Page 25MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
6. LED and Toggle Switch Interface
|. Program to blink LEDs using ARM LPC 2148.
Line# Program Comments
1 #include
2 void DELAY (unsigned long VALUE);
3 unsigned char A = 0x01;
4 int main)
5 t
6 JOODIR = 0,000000FF; /* Port0 00-07 as output*/
7 while(1) /* Infinite loop */
8 t
9 IO0SET |= A; 7 Port 00-07 High*/
10 DELAY(1000000);
i TOOCLR |= A; # Port0) 00.07 Low!
2 As<=1;
13 if(A=0500) A=0x01;
4 DELAY(1000000);
15 :
16 }
7 void DELAY (unsigned long VALUE)
18 t
19 while(VALUE>0)
Dept. of ICE, MIT, Manipal Page 26MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
20 t
2 VALUE-;
2 }
B }
Circuit Configuration:
3300)
P00 7 18 ky
ULN2803
330
PO.7 8 u kK]
9 10
ARM LPC 2148 10%
Pod
sl)
10k
P02
Li
lo
Dept. of ICE, MIT, Manipal Page 27MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
2. Program to read the status of toggle switch using ARM LPC 2148.
Line# Program Comments
1 #include
2 int main,
3 t
4 int temp ~ 05
5 TOODIR = 0x000000FF; /* Port0 00-07 as output*/
6 while(1) /* Infinite loop */
7 t
8 temp = IOOPIN
9 temp = temp>>16;
10 TOSETO = temp;
i TOCLRO =~temp;
2 }
1B }
Result:
Dept. of ICE, MIT, Manipal Page 28MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
I. Generate an asymmetrical square wave at the lowest four pins of LPC2148.
Dept. of ICE, MIT, Manipal Page 29MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
7. DC MOTOR CONTROL SYSTEM
1. Program to control the direction and speed of the DC motor using ARM LPC 2148.
Line# Program. Comments
1 #include
2
3 #define 01 0X00E00000
4 #define 02 0X00D00000
5 #define 03 0X00B00000
6 #define 04 0X00700000
7
8 define 11 0x000E0000
9 f#define 12 0x000D0000
10 #define 13 0x000B0000
i #define 14 0x00070000
2
B Hacfine CLR 0x00F 00000
4
15 unsigned int PWMI = 0x00000001; f PWMI
16 unsigned int PWM2 = 0x00000080; y PWM2
7 unsigned int ENBL = 0x00000002; u ENABLE
18
19 char scan (int);
Dept. of ICE, MIT, Manipal Page 30MICROPROCESSORS & MICROCONTROLLERS Lab
20 void delay(int);
2
2 int main(void)
B t
or IOODIR = OXO0F 00083;
25 while(1)
26 t
7 JOOSET |= ENBL;
28 IOOCLR = CLR;
29
30
31 while(sean(11)) dW
32 {
33 1O0SET |= PWM1;
34 IOOCLR |= PWM2;
35 }
36
37 while(scan({I2)) aw
38 {
39 TOOCLR |= PWM2;
40 TOOSET |= PWMI;
at delay(15);
Dept. of ICE, MIT, Manipal
si
HICE-3111]
Page 31MICROPROCESSORS & MICROCONTROLLERS Lab
2 JOOCLR [= PWM;
B delay(5);
44 4
45
46 IOOCLR = CLR;
47 IOOSET = 02;
48
49 while(sean(I1)) 4
50 {
5 IOOSET |= PWM2;
52 IOOCLR |= PWMI;
53 }
54
55 while(sean(12)) i
56 t
37 TOOCLR |= PWM2:
58 TOOSET |= PWMI;
9 delay(10);
60 IOOCLR |= PWM1;
61 delay(10);
a )
6
Dept. of ICE, MIT, Manipal
s2
S6
HICE-3111]
Page 32MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
64 IOOCLR = CLR;
65 IOOSET = 03;
66
67 while(scan(12)) 4 87
68 {
0 TOOCLR [= PWM2;
0 JOOSET |= PWM;
1 delay(5);
n IOOCLR |= PWM;
B delay(15);
™ }
8 }
16 }
”
B char scan(int keystatus) /* scanning a key*/
9 cl
80 (IOOPIN & 0X000F0000)==keystatus)
sl t
2 return(1);
83 )
4 return(0);
85 )
Dept. of ICE, MIT, Manipal Page 33MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
86,
87
id delay(int n) /* generates one
millisecond delay */
88 t
89.
a1 for(§=0; j<=10000; j++);
92 }
Circuit Configuration:
Cloctowise rotation anticlockwise rotation
Dept. of ICE, MIT, Manipal Page 34MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
vee
PO. 1 8/16
P00 2 3
Por 7 1293p (n)
—
413
5/12 s
vec
ARM LPC 2148 2, VT ark
PO.20 tate
PO.21
15) WU AT) <3,
¥ Y
me 16) 12, ¥ 4
PO.23
PO.18
0.17 }}#$————————~
P0.16 }$
Dept. of ICE, MIT, Manipal Page 35MICROPROCESSORS & MICROCONTROLLERS Lab
2. Program to understand Matrix Keyboard interface to ARM LPC 2148,
Line # Program Comments
1 #include
2
3 #define O1 OX00E00000.
4 #define 02 0X00D00000
5 #define 03 OX0OB00000
6 #define 04 0X00700000
7
8 #define 11 0x000E0000
9 ‘#define 12 0x000D0000
10 define 13 0x000B0000
i ‘#define 14 0x00070000
R
B #define CLR 0x00F00000,
4
1s char scan (int);
16 void delay(int);
17
18 int main(void)
19
20 {
Dept. of ICE, MIT, Manipal
HICE-3111]
Page 36MICROPROCESSORS & MICROCONTROLLERS Lab
a
2
B
m4
25
26
7
28
29
30
31
32
33
ery
35
26
37
38.
39,
40
41
a
looiR =
)XOOFOFFFF:
while(1)
{
IOOCLR = CLR,
IOOSET = O1;
delay(10);
iffscan(I1))IOOSET = 000000001;
iffscan(12) IOOSET = 0x00000010;
iffscan(13))IOOSET = 0x00000100;
iffscan(14))IOOSET = 0x00001000;
IOOCLR = CLR,
IOOSET = 02;
if{scan(11) )IOOSET = 0x00000002;
if{scan(12))IOOSET = 0x00000020;
iffscan(13))IOOSET = 0x00000200;
if{eean(14))OOSET ~ 000002000;
IOOCLR = CLR;
IOOSET = 03;
if{sean(11))100SET = 0x00000004;
iffscan(12))IOOSET = 0x00000040;
iffscan(13)IOOSET = 0x00000400;
if{scan(I4))IOOSET = 000004000;
Dept. of ICE, MIT, Manipal
wu
W
W
Ww
W
"
”
W
W
W
W
HICE-3111]
sl
ss
so
S13
s2
S6
s10
su
S3
ST
sil
sis
Page 37MICROPROCESSORS & MICROCONTROLLERS Lab
4B IOOCLR = CLR:
4 IOOSET = 04;
45 iff'scan(11))IOOSET = 000000008;
46 iflscan(12))IOOSET = 000000080;
a7 ifl'scan(13))IOOSET = 000000800;
48 iffscan(14))]O0SET = 0x00008000;
49 )
50 }
sl
52 char scan(int keystatus)
33 {
54 while(IOOPIN & 0X000F0000)
55 {
56 delay(50);
37 if(1OOPIN &
0X000F0000)==0X000F0000)retumn(I);,
58 )
59 return(0) ;
60 }
61
2 void delay(int n)
Dept. of ICE, MIT, Manipal
HICE-3111]
Ho 84
i SS
Wo Siz
Hl $16
/*scanningaakey */
generates one
millisecond delay */
Page 38MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
“8 {
64 int is
65 for (i=1; i<=n; i++)
66 for(§H0; j<=10000; j++);
07 }
Dept. of l&CE, MIT, Manipal Page 39MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
a program to rotate the DC motor for vai
switches $10, S11, and S12 are pressed.
ble speed using LPC2148, when
Dept. of ICE, MIT, Manipal Page 40MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
8. STEPPER MOTOR CONTROL SYSTEM
1. Program to control the direction and speed of the stepper motor using ARM LPC 2148.
Line# Program ‘Comments
1 #include
2
3 #define 01 0X00E00000
4 #define 02 0X00D00000
5 #define 03 0X00B00000
6 #aefime O4 0X00700000
7
8 #define 11 0x000E0000
9 #define 12.0x000D0000
10 #define 13 0x000B0000
n define 14 0500070000
2
B #define CLR 0x00F 00000
M4 unsigned char CLOCK[4] = {0x08,0x04,0x02,0x01};
15 unsigned char ANTI_CLOCK(4] =
{0x01,0x02,0x04,0x08};
16
7 char seam (int);
18 void delay(int);
19
Dept. of ICE, MIT, Manipal Page 41MICROPROCESSORS & MICROCONTROLLERS Lab
20 int main(void)
2 t
2 TOODIR = OXOOFO0O)
B while(1)
or {
25 unsigned char I;
26 IOOCLR = CLR;
7 IO0SET= 01;
28 while(scan(11)) u
29 {
30 for(I=0;1<4;1++)
31 t
32 JOOSET |= CLOCK];
33 JOOCLR |= ~CLOCK I];
34 delay(5);
35 }
36 }
37 while(scan({2)) aw
38 {
39
40 t
at IO0SET |= CLOCK];
Dept. of ICE, MIT, Manipal
sl
ss
HICE-3111]
Page 42MICROPROCESSORS & MICROCONTROLLERS Lab
2
43
45
46
47
48
49
50.
31
52
33
58
56
7
58
61
02
68
TOOCLR [= ~CLOCK {I};
delay(10);
}
}
IOOCLR = CLR;
IOOSET = 02;
while(sean(1)) av
{
for(I=0;1<4;1++)
{
IOOSET |= ANTI_CLOCKII;
TOOCLR | ~ANTI_CLOCK [I];
delay(S);
}
)
while(scan(12)) HW
{
TOOSET |= CLOCK[I];
IOOCLR |= ~CLOCK[I];
delay(S0);
Dept. of ICE, MIT, Manipal
s2
S6
HICE-3111]
Page 43MICROPROCESSORS & MICROCONTROLLERS Lab
65 }
66 1OOCLR = CLR;
or IO0SET = 03;
68 while(scan(12)) 4 87
0 {
70 for(I=0;1<431+)
7 {
n IOOSET |= CLOCK{I];
B TOOCLR [= ~CLOCK{N];
"4 delay(90);
5 }
16 )
1 }
B }
9
80 char scanint keystatus) /* scanning aa key
81 t
82 while((IOOPIN & 0X000F0000)==keystatus)
83 t
84 return(1);
Dept. of ICE, MIT, Manipal
HICE-3111]
4
Page 44MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
85 }
86 return(0);
87 }
88
89 void delay(int n) /* generates one
millisecond delay */
a1 int ijs
92 for (G=15 i=; i++)
93 for(j=0; j<=10000; j++);
Resul
Dept. of ICE, MIT, Manipal Page 45MICROPROCESSORS & MICROCONTROLLERS Lab
Circuit Configuration:
HICE-3111]
P0.0 T 7
~E+ «30
Pu2 3 ULN 2803 15
4
P03
1B
vec
47k:
ARM LPC 2148 13 9, 1F ak
Pozo 14] tol 6] 2.
Po2t TRGES
onl is) 2 8 V4
0.23 Lt
P0.19}}-——4
Po.18 #1
P0.17}¢-————+
Po,16 }# |
4-Step Sequence:
Step # Winding A Winding B Winding C Winding D
T 7 7 a 7
2 7 7 7 a
3 7 7 7 a
4 7 7 a 7
Dept. of ICE, MIT, Manipal
Page 46MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
1. Write a program to rotate the stepper motor clockwise and anticlockwise 90 degree
when switches $8 and $9 are pressed respectively.
2, Write a program to rotate the stepper motor clockwise 135 degree when switch S12 is
pressed .
Dept. of ICE, MIT, Manipal Page 47MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
9. WAVEFORM GENERATION
1 Program to generate different waveforms by pressing various keys using ARM LPC
2148.
Line # Program ‘Comments
1 Hinclude
2
3 Hdefine O1 0X00E00000
4 Hdefine 02 0X00D00000
5 Adefine 03 0X00B00000
6 define 04 0X00700000
1
8 define 11 0x000E0000
9 Adefine 12 0x000D0000
10 define 13 0x000B0000
u define 14 0x00070000
2
1B define CLR 0x00F00000
4
1s char scan (int);
16 void DELAY (int);
7
18 int main(void)
19 {
Dept. of ICE, MIT, Manipal Page 48MICROPROCESSORS & MICROCONTROLLERS Lab
20
”
2B
m4
25
26
30
unsigned char table{180] =
{128,132,136,141,154,150,154,158,163,167,171,175,18,
0,184,188,192,195,199,203,206,210,213,216,220,223,22
6,228,231,234,236,238,241,243,244,246,247,248,249,25
(0,251,252,253,254,255,255,255,255,255,254,254,253,25
2,281,249,246,244,243,241,238,236,234,231,228,226,22
3,220,216,213,210,206,203,199,195,192,188,184,180,17
5,171,167,163,158,154,150,145,141,136,132,128,123,11
9,114,110,105,101,97,92,88,84,80,75,71,67,64,60,56,52,
49,45 42,39,35,32,29,27,24,21,19,17,14,12,11,9,7,6,4,3,
2,1,1,0,0,0,0,0,0,0,0,1,1,2,3,4,6,7,9,11,12,14,17,19,21,24
527,29,32,35,39,42,45,49,52,56,60,64,67,71,75,80,84,88,
92,97,101,105,110,114,119,123,128};
IOODIR = OXOOFOFFO0;
while(t)
t
TOOCLR = CLR;
IO0SET = 01;
while(sean(I1))
{
unsigned char I;
unsigned int SINE,A;
for (I=0;1<1805
+4)
Dept. of ICE, MIT, Manipal
HICE-3111]
H!S1 SINE WAVE.
Page 49MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
a t
2 DELAY (5);
33 SINE = (table{t));
34 for(A=0;A0;5--)
60 t
61 DELAY (2);
e TRI=J;
6 for(B-0;B-8;B+4)
64 t
65 TRI=TRI<<1;
66 }
67 IOOSET [= TRI:
68 TOOCLR |= ~TRI;
co }
10 }
1 TOOCLR = CLR;
n IO0SET = 03;
B while(sean(11)) S3. SQUARE WAVE
Dept. of ICE, MIT, Manipal Page StMICROPROCESSORS & MICROCONTROLLERS Lab
"a
5
16
7
8
9
85
86
87
88
89
90
a1
IOOSET |= 0x0000F F00;
DELAY (500);
TOOCLR |= 0x0000FF00;
DELAY (500);
}
3
char scan(int keystatus) / scanning aa key
{
while((IOOPIN & 0X000F0000)—keystatus)
{
return(1);
5
return(0);
3
void DELAY (int n) generates
milisecond delay */
{
int i,j
Dept. of ICE, MIT, Manipal
HICE-3111]
Page S2MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
98 for (i=13 i
2
3 unsigned int srek = 0x00000002;
4 unsigned int rek = 0x00000001;
5 unsigned int ser = 0x00000004;
6
7 id delay int);
8 void convert_display(unsigned char);
9 void Clear (void);
10 int display(unsigned char);
u unsigned char disp{10] =
{0x40,Oxcf,0xa4,0x30,0x19,0x12,0x02,0x18,0x00,0x10};
2 ‘unsigned char count = 0;
13
4
15 {
16 TOODIR |= 0x00000007;
7 Clear()s
18 while(1)
Dept. of ICE, MIT, Manipal Page 56MICROPROCESSORS & MICROCONTROLLERS Lab
19
20
2
B
m4
25
26
28
29
30
31
33
4
38
36
37
38,
39
convert_display(count);
count++;
delay(100);
Clears
3
3
void Clear (void)
{
unsigned int x5
for (x-O5x-6:x+)
{
display(OxFF);
void delay(int n) generates
millisecond delay */
Dept. of ICE, MIT, Manipal
HICE-3111]
Page $7MICROPROCESSORS & MICROCONTROLLERS Lab
40
41
2
B
45
46
47
48
49
53
54
55
56
59
60
61
for(j=0; j<=10000; j++);
int display(unsigned char value)
{
char m;
int buffer;
buffer = value;
For(m=0;m<83m++)
{
if(buffer&0x80) IOOSET |= ser;
else TOOCLR [= sers
IOOSET |= srek;
buffer<<=1;
TOOCLR [= seeks
y
IO0SET
TOOCLR
ek;
return 0;
Dept. of ICE, MIT, Manipal
HICE-3111]
Page 58MICROPROCESSORS & MICROCONTROLLERS Lab
0
63
65
66
67
68
69
70
1
n
B
Result:
void convert display(unsigned char value)
unsigned char x, dl, d2, d3;
x= value / 105
d1 = value % 10;
d2=x% 10;
d3=x/105
display(disp[d3]);
display(displa2))s
display(displat)s
return;
Dept. of ICE, MIT, Manipal
HICE-3111]
HW divide by 10
W save low digit
(reminder of division)
divide by 10 once
‘more
Page 59MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
Exercise Program
|. _ Write a program to display pressed key on LED disp]
Dept. of ICE, MIT, Manipal Page 60MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
~PADAARYN
10.
ll.
12.
13.
15.
16,
17.
18.
19,
20.
21
a
2
24.
25.
26.
27.
28.
29.
Viva Questions
Answer the following:
i) — Onenibble = bits
ii) One byte bits
iii) How many K is 1 giga?
. Convert decimal numbers to hex numbers.
Perform addition of hex numbers.
. Give the example for microcontrollers and microprocessors.
. The 8081 is an -bit microcontroller.
. List the features of 8051.
. Name any three 16-bit microcontrollers.
. What are PSW and SFRs?
What is simulator/debugger?
What are 8051 data types and directives?
What is the size of SP register?
. SP is incremented by 1 for each operation.
. On power-up , The 8051 uses, bank.
14.
List the 16 -bit registers in the 8051.
What are the four fields of assembly language instructions?
MOV A, #40 h requires bytes
What is machine cycle for 8051?
What is delay? Why do we need it?
In what ways instructions LIMP and SJMP are different?
Why do we need subroutines?
What is the difference between RET and RET! instructions?
How many ports are available in 8051?
Which 8051 ports need pull-up resistors to function as an /O ports?
Write simple instruction to send 99h to ports P1 and P2.
Which pins of port 3 cater to interrupts?
Port 2 is also designated as of 8051.
List the addressing modes provided by 8051 with examples.
What is done by the following instructions?
MOV A, PI
JB ACC.1, HERE
Is following instruction valid?
MOVC, A
Dept. of ICE, MIT, Manipal Page 61MICROPROCESSORS & MICROCONTROLLERS Lab HICE-3111]
30.
a
32.
3.
&
35.
36,
37.
38.
39,
4
s
4
46.
47.
48.
49,
50.
uy.
(1.
GB]
What is the difference between the operations of a timer and a counter?
What is the use of TMOD register?
. What is the difference between mode | and mode 2 operations of timer?
33.
What is the address of TCON register?
Distinguish between half duplex and full duplex mode of communication
with an example.
What is the function of MAX 232 chip?
State the most important signals of RS232.
. What is baud rate?
What is the roll of SBUF in serial communication?
Show the interrupt vector table for the 8051.
. What is ARM7TDMI.
41.
42.
43.
44,
List the features of ARM microcontroller.
ARM has registers each of which is, bit long.
What is the difference between little endian and big endian format.
What is done by the following instruction of the ARM?
MYN RO, R9
. What are the types of the stacks available in ARM?
List any three features of LPC2148
What is the use of PWM unit?
Whaat is the difference between CCLK and PCLK?
How to set up the communication between PC and microcontroller?
How does the prescaler in time unit function?
References
Mazidi M. A. & J. G. Mazidi, The 8051 Microcontroller and embedded systems,
Pearson. 2002
LPC21XX User Manual. 2007
Ramani Kalpathi Microcontrollers & Applications, Sanguine Technical Publishers, 2008
Dept. of ICE, MIT, Manipal Page 62