Professional Documents
Culture Documents
8051 PROGRAMMING
Example:
MOV A,#88H
ADD A,#93H
88 10001000
+93 +10010011
---- --------------
11B 00011011
CY=1 AC=0 P=0
Example:
MOV A,#38H
ADD A,#2FH
38 00111000
+2F +00101111
---- --------------
67 01100111
CY=0 AC=1 P=1
Example:
MOV A,#9CH
ADD A,#64H
9C 10011100
+64 +01100100
---- --------------
100 00000000
CY=1 AC=1 P=0
MUL AB ;B|A = A*B
MOV A,#25H
MOV B,#65H
MUL AB ;25H*65H=0E99
;B=0EH, A=99H
DIV AB ;A = A/B, B = A mod B
MOV A,#25
MOV B,#10
DIV AB ;A=2, B=5
Looping
AGAIN:
ADD A ,#03 ;add 03 to ACC
DJNZ R2 ,AGAIN ;repeat until R2=0,10 times
MOV R5 ,A ;save A in R5
This program adds value 3 to the ACC ten times
How many times this loop can be repeated?
Nested Loop
MOV A,#55H
BACK: MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
Let P1 toggle.
You can write to P1 directly.
Find the timer’s clock frequency and its period for various 8051-
Solution:
use 8051 XTAL for the clock source, and use instructions to start
and stop the timer.
In the following program, we are creating a square wave of 50% duty cycle (with
equal portions high and low) on the P1.5 bit. Timer 0 is used to generate the time
delay.
DELAY:
SETB TR0 ;start the timer 0
AGAIN:JNB TF0,AGAIN
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
Solution:
Notice that to repeat the process, we must reload the TL and TH registers, and start
the timer again (in the main program).
In the above Example, calculate the amount of time delay in the DELAY subroutine
generated by the timer. Assume that XTAL = 11.0592 MHz.
Solution:
The timer works with the internal system clock.
frequency of internal system clock = 11.0592/12 = 921.6 KHz
machine cycle = 1 /921.6 KHz = 1.085 s (microsecond)
The number of counts = FFFFH – FFF2H +1 = 14 (decimal).
The delay = number of counts × 1.085 s = 14 × 1.085 s = 15.19 s for half the
clock.
For the entire period of a clock, it is T = 2 × 15.19 s = 30.38 s as the time delay
generated by the timer.
In the above Example, calculate the accurate frequency of the square wave
Solution:
In the time delay calculation of Example 9-5, we did not include
the overhead due to instructions in the loop.
To get a more accurate timing, we need to add clock cycles from Table A-1 in
Appendix A, as shown below.
MOV TH0,#0FFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
;----------delay using timer 0
DELAY:
SETB TR0 1
AGAIN: JNB TF0,AGAIN 14
CLR TR0 1
CLR TF0 1
RET 20
Total 28
T = 2 × 28 × 1.085 s = 60.76 s, and F = 16458.196 Hz.
Find the delay generated by timer 0 in the following code, using
both of the methods of Figure 9-4. Do not include the overhead due
to instructions.
Solution:
TH0=TL0=0 means that the timer will count from 0000 to FFFF, and then roll over to
raise the TF0 flag.
As a result, it goes through a total of 65536 states. Therefore, we have delay = (65536
– 0) × 1.085 s = 71.1065 ms.
In mode 1, the program must reload the TH1, TL1 register every timer if we want to
have a continuous wave.
FFFFH – 7634H + 1 = 89CCH = 35276 clock count
Half period = 35276 × 1.085 s = 38.274 ms
Whole period = 2 × 38.274 ms = 76.548 ms
Frequency = 1/ 76.548 ms = 13.064 Hz.
Also notice that the high portion and low portion of the square wave are equal.
In the above calculation, the overhead due to all the instructions in the loop is not
included.
Assume that XTAL = 11.0592 MHz.
What value do we need to load into the timer’s registers if we want to have a time
delay of 5 ms (milliseconds)?
Show the program for timer 0 to create a pulse width of 5 ms on P2.3.
Solution:
XTAL = 11.0592 MHz MC = 1.085 s.
5 ms / 1.085 s = 4608 MCs.
To achieve that we need to load into TL0 and TH0 the value 65536 – 4608 = 60928 =
EE00H.
Therefore, we have TH0 = EE and TL0 = 00.
Assuming that XTAL = 11.0592 MHz, write a program to generate a square wave of
2 kHz frequency on pin P1.5.
Solution:
This is similar to Example 9-10, except that we must toggle the bit to generate the
square wave. Look at the following steps.
(a) The period of square wave = 1 / frequency
= 1 / 2 kHz = 500 s.
(b) The half period = 500 s /2 = 250 s.
(c) 250 s / 1.085 s = 230
65536 – 230 = 65360 = FF1AH.
(d) TL1 = 1AH and TH1 = FFH
Solution:
Look at the following steps.
(a) The period of the square wave = 1 / 50 Hz = 20 ms.
(b) The high or low portion of the square wave = 10 ms.
(c) 10 ms / 1.085 s = 9216
65536 – 9216 = 56320 in decimal = DC00H in hex.
(d) TL1 = 00H and TH1 = DCH.
Examine the following program and find the time delay in seconds.
Exclude the overhead due to the instructions in the loop.
MOV TMOD,#10H
MOV R3,#200
AGAIN: MOV TL1,#08
MOV TH1,#01
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
DJNZ R3,AGAIN
Solution:
TH1-TL1 = 0108H = 264 in decimal
65536 – 264 = 65272.
One of the timer delay = 65272 × 1.085 s = 70.820 ms
Total delay = 200 × 70.820 ms = 14.164024 seconds
• Mode 0 is used to compatible with MSC-48
• Mode 0 is exactly like mode 1 except that it is a 13-bit timer instead of 16-bit.
• The counter can hold values between 0000 to 1FFF
• 213 -1= 2000H-1=1FFFH
• When the timer reaches its maximum of 1FFFH, it rolls over to 0000, and TF0 is
raised.
Assuming that XTAL = 11.0592 MHz, find
(a) the frequency of the square wave generated on pin P1.0 in the following program
(b) the smallest frequency achievable in this program, and the TH value to do that.
Example
Solution:
“MOV TH0,#-150” uses 150 clocks.
The DELAY subroutine = 150 × 1.085 s = 162 s.
The high portion of the pulse is twice that of the low portion (66% duty cycle).
The total period = high portion + low portion
= 325.5 s + 162.25 s = 488.25 s
Frequency = 2.048 kHz.
MOV TMOD,#2H ;Timer 0,mode 2
MOV TH0,#-150 ;Count=150
AGAIN:SETB P1.3
ACALL DELAY
ACALL DELAY
CLR P1.3
ACALL DEALY
SJMP AGAIN
DELAY:SETB TR0 ;start
BACK: JNB TF0,BACK
CLR TR0 ;stop
CLR TF0 ;clear TF
RET
Counter Programming
Assuming that clock pulses are fed into pin T1, write a program for
counter 1 in mode 2 to count the pulses and display the state of the
TL1 count on P2.
Solution:
We use timer 1 as an event counter where it counts up as clock pulses are fed into
pin3.5
MOV TH1,#0
SETB P3.5 ;make T1 input port
AGAIN:SETB TR1 ;start
BACK: MOV A,TL1
MOV P2,A ;display in P2
JNB TF1,BACK ;overflow
CLR TR1 ;stop
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
Notice in the above program the role of the instruction “SETB
P3.5”. Since ports are set up for output when the 8051 is powered
up , we must make P3.5 an input port by making it high.
Assume that a 1-Hz frequency pulse is connected to input pin 3.4.
Solution:
#include <reg51.h>
void T0Delay(void);
void main(void) {
while(1) { //repeat forever
P1=0x55;
T0Delay(); //time delay
P1=0xAA;
T0Delay(); }}
Assume XTML=11.0592MHz for the AT89C51
FFFFH-3500H+1=CB00H=51968
1.085s 51968 56.384ms
void T0Delay() {
TMOD=0x01; //Timer 0, Mode 1
TL0=0x00; TH0=0x35; //initial value
TR0=1; //turn on T0
while (TF0==0); //text TF to roll over
TR0=0; //turn off T0
TF0=0; } //clear TF0
A switch is connected to P1.7. Write an 8051 C program to monitor SW and create
the following frequencies on P1.5.
Solution:
#include <reg51.h>
sbit mybit=P1^5;
sbit SW=P1^7;
void T0Delay(void);
void main(void) {
(b) DS89C420.
Solution:
#include <reg51.h>
void T0Delay(void);
sbit mybit=P1^5;
void main(void) {
while(1) { //repeat forever
mybit=~mybit;
T0Delay(); }}
(a) Assume XTML=11.0592MHz for the AT89C51
FFFFH-4BFDH+1=B403H=46083
1.085s 46083 50ms
void T0Delay() {
TMOD=0x01; //Timer 0, Mode 1
TL0=0xFD; TH0=0x4B; //initial value
TR0=1; //turn on T0
while (TF0==0); //BACK: JNZ TF0,BACK
TR0=0; //turn off T0
TF0=0; } //clear TF0
(b) Assume XTML=11.0592MHz for the DS89C4x0
FFFFH-4BFDH+1=B403H=46083
1.085s 46083 50ms
void T0Delay() {
TMOD=0x01; //Timer 0, Mode 1
TL0=0xFD; TH0=0x4B; //initial value
TR0=1; //turn on T0
while (TF0==0); //BACK: JNZ TF0,BACK
TR0=0; //turn off T0
TF0=0; } //clear TF0
Assume that a 1-Hz external clock is being fed into T1 (P3.5). Write a C program for
counter 1 in mode 2 to count up and display the state of the TL1 count on P1.
Start the count at 0H.
Solution:
P1 is connected to 8 LEDs.
#include <reg51.h>
sbit T1=P3^5;
void main(void) {
T1=1; //make T1 an input pin
TMOD=0x60; //counter 1, mode 2
TH1=0; //reload value
while(1) { //repeat forever
do {TR1=1; P1=TL1;} while (TF1==0);
TR1=0; TF1=0; //clear flags
}}
Tasks
Application software is split up into multitasking blocks called ‘Tasks’.
Timing constraints
SLEEPING
READY
RUNNING
BLOCKED
SLEEPING -> READY through an RTOS system function called from a different
task.
READY -> RUNNING from being selected for execution by the RTOS. There can
only be one RUNNING task at a time.
RUNNING -> BLOCKED when the task is told to wait for an event to occur.
After the event, a task has been waiting for occurs and has higher
priority than current RUNNING task.
RTX51-Tiny
• RTX51 is a commercial RTOS from Keil, available in Full or Tiny version.
• RTX51-Tiny features:
• Task priorities
• Task interrupts
• Messaging routines
Task declaration
#include <rtx51tny.h>
int counter1;
// completed).
}
Tasks do not return or take any parameters except void. RTX51-Tiny always starts
execution at task number 0. RTX51-Full must start the RTOS from main()
Task Scheduling
RTX51 supports preemptive, round-robin, and cooperative task scheduling
algorithm. RTX51 scheduler chooses the task to run by: The highest priority of all
tasks that are READY The task that has been waiting for the longest time if all tasks
have the same priority RTOS only switches tasks if the first rule is violated, except in
round-robin scheduling
Multitasking
1. Round-Robin Multitasking
• Every task is run in turn for an equal length of time called the time-slice
int counter0;
int counter1;
int counter2;
counter1 = 0;
counter2 = 0;
}
}
When there is only one task, the RTOS always chooses job0 to run after its
time-slice. When all three tasks are READY to run, job1 runs after job0, and job2
runs after job1.
TIMESHARING EQU 5
2. Cooperative Multitasking
A type of multitasking in which the process currently controlling the CPU must
offer control to other processes. It is called cooperative because all programs must
cooperate for it to work
Configure RTX51 to schedule in cooperative mode by setting the TIMESHARING
variable in ‘Conf_tny.A51’ to 0.
TIMESHARING EQU 0
Tasks switches are only performed when the running task voluntarily gives up control
of the processor.
Code example
#include <rtx51tny.h>
int counter0;
int counter1;
// increment counter
if (++counter0 == 0) {
// signal task 1
os_send_signal (1);
// wait for signal; releases CPU control
}
os_wait (K_TMO, 1, 0); // wait for 1 machine cycle, give up CPU
// give up CPU
// increment counter 1
counter1++;
}
Task 0 gives up control of processor at ‘os_wait’, where it waits for an event to
occur. Before doing so, task 0 signals task 1 making it READY to execute. Tasks
have to manually control execution order using communication methods like signals.
Preemptive scheduler can switch tasks before the time-slice for the RUNNING task
runs out.
Preemption example
#include <rtx51tny.h>
int counter0;
int counter1;
void job0 (void) _task_ 0 _priority_ 0 {// RTX51 starts off in task 0
// increment counter 0
if (++counter0 == 0)
// signal task 1
os_send_signal (1);
// increment counter 1
counter1++;
}
}
RTOS Events
Events can be used by tasks to communicate between each other or to help
coordinate execution flow.
• Signals
• Messages
• Semaphores
• Interrupts
Signal Event Based Communication
• Signaling is the fastest method of communication.
...
...
• Tasks can send and clear signal for any other task, including itself.
• Tasks waiting for signal are BLOCKED. The RTOS unblocks that task once
the event is received.
• RTX51 has eight mailboxes that are not tied to any single task, numbered 0-7.
• Tasks which send messages are BLOCKED until the message can be sent or
timeout occurs.
int message;
...
...
os_send_token (14);
Task Interrupts
• RTX51-Full supports attaching hardware interrupt vectors to tasks.
os_attach_interrupt (14);
while (1) {
...
}
When to use RTOS
• Large projects benefit from using an RTOS
• Generally when a system has a dozen or more tasks that all have timing
constraints
• Ultra-compact systems may be better off without using an RTOS
• Small systems are easier to code from scratch and can incorporate processor
optimizations, making the system more efficient than one using RTOS
• A full-featured RTOS may not fit within the size limits of an ultra-compact
system.
• RTOS standardizes system functions and makes coding real-time systems
easier.
• Large systems benefit from RTOS, while compact systems can be more
efficient if it is custom-written.
LCD based digital alarm clock with digital thermometer using 8051
microcontroller