In: Electrical Engineering
How would I code the following in assembly language?
Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence:
1. On for 50mS,
2. Off for 400mS,
3. On for 50mS,
4. Off for 1.5S,
5. Repeat the sequence from step 1.
Answer :- Assuming LED is connected at port1 pin zero. Write 10111100 = 0xBC at SFR address 0xE3 to set 72 kHz with divider 8 i.e. 9 kHz frequency.
T = 1/9 ms. For 50 ms delay, count = 50*9 = 450
For 400 ms delay, count = 400*9 = 3600 and for 1.2 s, count =
1200*9 = 10800.
In 8051 MC we have 8-bit GPR, so max count can be 256 only i.e. 0
to 255. The code can be written as-
-------------------------------------------------------------------------------------
P1 EQU 90H
OSCLCN EQU E3H
MOV OSCLCN, #BCH ;set controller frequency to work at 9
kHz
LoopInf : infinite loop starts here
MOV P1, #01H ;make LED on
CALL Delay1 ;keep On for 50 ms
MOV P1, #00H ;make LEd off
CALL Delay2 ;keep LED off for 400 ms
MOV P1, #01H ;make LED on
CALL Delay3 ;Keep LED on for 1.2 s
SJMP LoopInf ;go to label LoopInf to repeat
Delay1: ;delay loop for 50 ms
MOV R1, #10 ;register R1 = 10
Loop1:
MOV R2, #45 ;register R2 = 45
Loop2:
DJNZ R2, Loop2 ;decrease R2 and goto label Loop2 if not zero
DJNZ R1, Loop1 ;decrease R1 and goto label Loop1 if not zero
RET
Delay2: ;delay loop for 400 ms
MOV R1, #36 ;register R1 = 36
Loop3:
MOV R2, #100 ;register R2 = 100
Loop4:
DJNZ R2, Loop4 ;decrease R2 and goto label Loop4 if not zero
DJNZ R1, Loop3 ;decrease R1 and goto label Loop3 if not zero
RET
Delay3: ;delay loop for 1.2 s
MOV R1, #100 ;register R1 = 100
Loop5:
MOV R2, #108 ;register R2 = 108
Loop6:
DJNZ R2, Loop6 ;decrease R2 and goto label Loop6 if not zero
DJNZ R1, Loop5 ;decrease R1 and goto label Loop5 if not zero
RET
Note:- Dear student, while creating the project in keil, please select the above mentioned MCU. Also be sure that port and register address defind in first two lines are defined for the MCU. For this you can see the datasheet of C8051F330.