Documente Academic
Documente Profesional
Documente Cultură
Contents:
I/O Programming; Bit Manipulation Time delay Generation and calculation Timer/Counter Programming -Timers - Counters Interrupts Programming Serial Communication
Bit Manipulation
Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8 bits. This table shows how to name each pin for each I/O port. Example:
Write a program to perform the following. (a) Keep monitoring the P1.2 bit until it becomes high, (b) When P1.2 becomes high, write value 45H to port 0, and (c) Send a high-to-low (H-to-L) pulse to P2.3.
Solution: SETB P1.2 ;make P1.2 an input MOV A,#45H ;A=45H AGAIN:JNB P1.2,AGAIN;get out when P.2=1 MOV P0,A ;issue A to P0 SETB P2.3 ;make P2.3 high CLR P2.3 ;make P2.3 low for H-to-L Note 1. JNB: jump if no bitjump if P1.2 = 0 2. a H-to-L pulse by the sequence of instructions SETB and CLR.
Example: Find the time delay for the following subroutine, assuming a crystal frequency of 11.0592 MHz DELAY: HERE: MOV NOP NOP NOP NOP DJNZ RET R3,#250 ; ; ; ; ; ; ; 1 MC 1 MC 1 MC 1 MC 1 MC 2 MC 1 MC
R3,HERE
2. An event counter.
Timer
Set the initial value of registers Start the timer and then the 8051 counts up. Input from internal system clock (machine cycle) When the registers equal to 0 and the 8051 sets a bit to denote time out 8051
Set Timer 0 P2 P1
TH0 TL0
10
to LCD
Counter
Count the number of events
Show the number of events on registers External input from T0 input pin (P3.4) for Counter 0 External input from T1 input pin (P3.5) for Counter 1 External input from Tx input pin. 8051 We use Tx to denote T0 or T1.
TH0
P1
TL0
to LCD
P3.4 a switch T0
11
12
Each 16-bit timer can be accessed as two separate registers of low byte and high byte.
13
Timer Registers
TH0
D15 D14 D13 D12 D11 D10 D9 D8
TL0
D7 D6 D5 D4 D3 D2 D1 D0
Timer 0
TH1
D15 D14 D13 D12 D11 D10 D9 D8
TL1
D7 D6 D5 D4 D3 D2 D1 D0
Timer 1
14
TMOD Register
Timer mode register: TMOD
MOV TMOD,#21H An 8-bit register Set the usage mode for two timers
Set lower 4 bits for Timer 0 Set upper 4 bits for Timer 1 (Set to 0000 if not used) (Set to 0000 if not used)
Not bit-addressable
(LSB) M0
15
C/T (Clock/Timer)
This bit is used to decide whether the timer is used as a delay generator or an event counter. C/T = 0 : timer C/T = 1 : counter
17
Gate
Every timer has a mean of starting and stopping.
GATE=0
Internal control The start and stop of the timer are controlled by way of software. Set/clear the TR for start/stop timer.
GATE=1
External control The hardware way of starting and stopping the timer by software and an external source. Timer/counter is enabled only while the INT pin is high and the TR control pin is set (TR).
18
M1, M0
M0 and M1 select the timer mode for timers 0 & 1.
M1 M0 Mode 0 0 0 0 1 1 Operating Mode 13-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1) 16-bit timer mode 8-bit THx + 8-bit TLx 8-bit auto reload 8-bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows. Split timer mode
1 0
1 1
19
Example 9-3
Find the value for TMOD if we want to program timer 0 in mode 2, use 8051 XTAL for the clock source, and use instructions to start and stop the timer. Solution:
timer 1 timer 0
TMOD= 0000 0010 Timer 1 is not used. Timer 0, mode 2, C/T = 0 to use XTAL clock source (timer) gate = 0 to use internal (software) start and stop method.
20
IE1
(LSB) IT0
21
IE1
(LSB) IT0
22
= =
= = = =
Timer Mode 1
In following, we all use timer 0 as an example. 16-bit timer (TH0 and TL0) TH0-TL0 is incremented continuously when TR0 is set to 1. And the 8051 stops to increment TH0-TL0 when TR0 is cleared. The timer works with the internal system clock. In other words, the timer counts up each machine cycle. When the timer (TH0-TL0) reaches its maximum of FFFFH, it rolls over to 0000, and TF0 is raised. Programmer should check TF0 and stop the timer 0.
24
TH0
TL0
FFFC
TF = 0
FFFD
TF = 0
TF
FFFE TF = 0
FFFF
TF = 0
0000
TF = 1
26
Mode 1 Programming
XTAL oscillator
12
C/T = 0
TH
TL
TF
TR
overflow flag
28
(FFFF YYXX + 1) 1.085 s where YYXX are TH, TL initial values respectively. Notice that values YYXX are in hex.
Convert YYXX values of the TH, TL register to decimal to get a NNNNN decimal number, then (65536 NNNNN) 1.085 s
29
FFF2
TF0 = 0
FFF3
TF0 = 0
FFF4
TF0 = 0
FFFF
TF0 = 0
0000
TF0 = 1
31
33
34
How to find the inter values needed for the TH, TL?
Divide the desired time delay by 1.085 s. Perform 65536 n, where n is the decimal value we got in Step 1. Convert th result of Step 2 to hex, where yyxx is the initial hex value to be loaded into the timers registers. Set TH = yy and TL = xx. Example 9-10
35
The largest time delay is achieved by making TH=TL=0. What if that is not enough? Example 9-13 show how to achieve large time delay.
38
Example 9-13
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: TH TL = 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
39
Timer Mode 0
Mode 0 is exactly like mode 1 except that it is a 13bit timer instead of 16-bit.
8-bit TH0 + 5-bit TL0
We set the initial values TH0-TL0 to count up. When the timer reaches its maximum of 1FFFH, it rolls over to 0000, and TF0 is raised.
40
Timer Mode 2
8-bit timer.
It allows only values of 00 to FFH to be loaded into TH0.
Auto-reloading TL0 is incremented continuously when TR0=1. In the following example, we want to generate a delay with 200 MCs on timer 0. See Examples 9-14 to 9-16
41
4. After TH0 is loaded with the 8-bit value, the 8051 gives a copy of it to TL0.
TL0=TH0=38H
7. When TL0 rolls over from FFH to 00, the 8051 set TF0=1. Also, TL0 is reloaded automatically with the value kept by the TH0.
TL0= FEH, FFH, 00H (Now TF0=1) The 8051 auto reload TL0=TH0=38H. Go to Step 6 (i.e., TL0 is incrementing continuously).
Note that we must clear TF0 when TL0 rolls over. Thus, we can monitor TF0 in next process. Clear TR0 to stop the process.
43
XTAL oscillator
12
C/T = 0
TL1
reload
TF1
overflow flag
TR1
TH1
Example 9-15
Find the frequency of a square wave generated on pin P1.0. Solution: MOV TMOD,#2H ;Timer 0,mode 2 MOV TH0,#0 AGAIN:MOV R5,#250 ;count 250 times ACALL DELAY CPL P1.0 SJMP AGAIN DELAY:SETB TR0 ;start BACK: JNB TF0,BACK CLR TR0 ;stop CLR TF0 ;clear TF DJNZ R5,DELAY ;timer 2: auto-reload RET T = 2 (250 256 1.085 s) = 138.88 ms, and frequency = 72 Hz.
45
Example 9-16
Assuming that we are programming the timers for mode 2, find the value (in hex) loaded into TH for each of the following cases. (a) MOV TH1,#-200 (b) MOV TH0,#-60 (c) MOV TH1,#-3 (d) MOV TH1,#-12 (e) MOV TH0,#-48 Solution: Some 8051 assemblers provide this way. -200 = -C8H 2s complement of 200 = 100H C8H = 38 H Decimal -200 = - C8H - 60 = - 3CH - 3 - 12 - 48 2s complement (TH value) 38H C4H FDH F4H D0H
46
;start
;stop ;clear TF
48
Counter
These timers can also be used as counters counting events happening outside the 8051. When the timer is used as a counter, it is a pulse outside of the 8051 that increments the TH, TL. When C/T=1, the counter counts up as pulses are fed from
T0: timer 0 input (Pin 14, P3.4) T1: timer 1 input (Pin 15, P3.5)
49
14 15 (MSB) GATE
P3.4 P3.5
T0 T1
C/T=1 M1 Timer 1
M0
GATE
C/T=1 M1 Timer 0
M0
50
Counter Mode 1
16-bit counter (TH0 and TL0) TH0-TL0 is incremented when TR0 is set to 1 and an external pulse (in T0) occurs. When the counter (TH0-TL0) reaches its maximum of FFFFH, it rolls over to 0000, and TF0 is raised. Programmers should monitor TF0 continuously and stop the counter 0. Programmers can set the initial value of TH0-TL0 and let TF0=1 as an indicator to show a special condition. (ex: 100 people have come).
51
52
Counter Mode 2
8-bit counter.
It allows only values of 00 to FFH to be loaded into TH0.
Auto-reloading TL0 is incremented if TR0=1 and external pulse occurs. See Figure 9.6, 9.7 for logic view See Examples 9-18, 9-19
53
54
T1
55
T0
R4
R3
R2
58
A Digital Clock
Example 9-19 shows a simple digital clock.
If we feed an external square wave of 60 Hz frequency into the timer/counter, we can generate the second, the minute, and the hour out of this input frequency and display the result on an LCD.
You might think that the use of the instruction JNB TF0,target to monitor the raising of the TF0 flag is a waste of the microcontrollers time.
The solution is the use of interrupt. See Chapter 11. In using interrupts we can do other things with the 8051. When the TF flag is raised it will inform us.
59
GATE=1 in TMOD
All discuss so far has assumed that GATE=0.
The timer is stared with instructions SETB TR0 and SETB TR1 for timers 0 and 1, respectively.
If GATE=1, we can use hardware to control the start and stop of the timers.
INT0 (P3.2, pin 12) starts and stops timer 0 INT1 (P3.3, pin 13) starts and stops timer 1 This allows us to start or stop the timer externally at any time via a simple switch.
60
61
Timer/Counter 0
XTAL oscillator
12
C/T = 0
C/T = 1 T0 Pin Pin 3.4 Gate INT0 Pin Pin 3.2 TR0
62
Interrupts Programming
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service.
Interrupts vs. Polling A single microcontroller can serve several devices. That are two ways to do that: interrupts or polling. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
63
65
Example:
Write a program using interrupts to simultaneously create 7 kHz and 500 Hz square waves on P1.7 and P1.6.
66
Solution:
ORG LJMP ORG LJMP ORG LJMP ORG MOV MOV SETB SETB MOV MOV SJMP CLR RETI CLR MOV MOV SETB CPL RETI END 0 MAIN 000BH T0ISR 001BH T1ISR 0030H TMOD,#12H TH0,#-71 TR0 TF1 IE,#8AH IE,#8AH $ P1.7 TR1 TH1,#HIGH(-1000) TL1,#LOW(-1000) TR1 P1.6
8051 P1.7
143s 71s
MAIN:
2ms
P1.6
1ms
T0ISR: T1ISR:
67
External Interrupts:
0003
Edge-triggered
Edge-triggered
68
Exercise
We have a motor that send pulses to micro proportional to its r.p.m. write a program that if the number of pulses per 10-second are less than 100, send 1 to P1.0, and if more than 200, send 1 to P1.1 Write a program and design hardware that connect key-pad to micro and identifies which key is pressed.
69
Serial Communication
70
71
72
73
74
MAX232
75
76
77