Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8517
Скачиваний: 0
248 |
Chapter 12 |
PB SW
R=10K
R=10K
+5v
1 |
16F84 |
18 |
||||||
RA2 |
RA1 |
|||||||
2 |
17 |
Osc |
||||||
RA3 |
RA0 |
|||||||
3 |
16 |
|||||||
RA4/TOCKI |
OSC1 |
|||||||
4 |
15 |
|||||||
MCLR |
OSC2 |
|||||||
5 |
14 |
+5v |
+5v |
|||||
Vss |
Vdd |
|||||||
6 |
13 |
7-segment |
||||||
RB0/INT |
RB7 |
|||||||
7 |
12 |
LED |
||||||
RB1 |
RB6 |
f |
a |
|||||
8 |
11 |
|||||||
RB2 |
RB5 |
|||||||
9 |
g |
a |
b |
|||||
10 |
||||||||
RB3 |
RB4 |
|||||||
f |
b |
|||||||
g |
||||||||
220 R |
PWR |
|||||||
X 7 |
e |
e |
c |
|||||
ON |
||||||||
d |
d |
c |
||||||
Figure 12-2 Test Circuit for Timer/Counter Program
A Timer/Counter Test Circuit
The circuit shown in Figure 12-2 contains a pushbutton switch wired to port RA4/TOCKI and a seven-segment LED display wired to Port-B lines 0 to 6.
The Tmr0Counter Program
The program named Tmr0Counter in the book’s online software package uses the circuit in Figure 12-2 to demonstrate the programming of the Timer0 module in the counter mode. The program detects and counts action on the pushbutton switch wired to port RA4/TOCKI. The value of the count in hex digits ranging 0x00 to 0x0f is displayed in the seven-segment LED connected to Port-B.
The following code fragment shows the program’s initialization routine to set up the ports and the timer:
main:
;Clear the Watchdog Timer and reset prescaler clrwdt
;Set up the OPTION regiser bit map
movlw |
b’10111000’ |
|||||
; |
7 |
6 |
5 |
4 |
3 |
2 1 0 <= OPTION bits |
; |
| |
| |
| |
| |
| |
|__|__|_____ PS2-PS0 (prescaler bits) |
; |
| |
| |
| |
| |
| |
Values for Timer0 |
Timers and Counters |
249 |
||||||||
; |
| |
| |
| |
| |
| |
*000 |
= 1:2 |
001 = 1:4 |
|
; |
| |
| |
| |
| |
| |
010 |
= 1:8 |
011 = 1:16 |
|
; |
| |
| |
| |
| |
| |
100 |
= 1:32 |
101 = 1:64 |
|
; |
| |
| |
| |
| |
| |
110 |
= 1:128 |
*111 = 1:256 |
|
; |
| |
| |
| |
| |
|______________ |
PSA |
(prescaler assign) |
||
; |
| |
| |
| |
| |
*1 |
= |
to WDT |
||
; |
| |
| |
| |
| |
0 |
= |
to Timer0 |
||
; |
| |
| |
| |
|_________________ |
TOSE (Timer0 edge select) |
||||
; |
| |
| |
| |
0 |
= |
increment on low-to-high |
|||
; |
| |
| |
| |
*1 |
= |
increment in high-to-low |
|||
; |
| |
| |
|____________________ |
TOCS (TMR0 clock source) |
|||||
; |
| |
| |
0 |
= |
internal clock |
||||
; |
| |
| |
*1 |
= |
RA4/TOCKI bit source |
||||
; |
| |
|_______________________ |
INTEDG (Edge select) |
||||||
; |
| |
*0 |
= |
falling edge |
|||||
;|__________________________ RBPU (Pullup enable)
; |
0 = enabled |
|
; |
*1 = disabled |
|
option |
||
; Set up ports |
||
movlw |
0x00 |
; Set Port-B to output |
tris |
portb |
|
clrf |
portb |
; All Port-B to 0 |
; Port-A. Five low-order lines set for input |
||
movlw |
B’00011111’ ; w = 00011111 binary |
|
tris |
porta |
; Port-A (lines 0 to 4) to input |
Once the hardware is initialized, program operation consists of reading the value stored in TMR0, scaling this value to the display range 0 to 15, and displaying it on the seven-segment LED. Processing is as follows:
;=================================
;Check value in TMR0 and display ;=================================
;Every press of the pushbutton switch connected to line
;RA4/TOCKI adds one to the value in the TMR0 register.
;Loop checks this value, adjusts to the range 0 to 15
;and displays the result in the seven-segment LED on
;Port-B
;
checkTmr0:
movf |
mr0,w |
; Timer register to w |
; Eliminate four high order bits
andlw |
b’00001111’ ; Mask off high bits |
;At this point the w register contains a 4-bit value
;in the range 0 to 0xf. Use this value (in w) to
;obtain seven-segment display code
;
call |
segment |
250 |
Chapter 12 |
|||
movwf |
portb |
; |
Display |
switch bits |
goto |
checkTmr0 |
; |
Endless |
loop |
; |
;================================
;routine to return 7-segment
;codes ;================================ segment:
addwf |
PCL,f |
; PCL is program counter latch |
retlw |
0x3f |
; 0 code |
retlw |
0x06 |
; 1 |
retlw |
0x5b |
; 2 |
retlw |
0x4f |
; 3 |
retlw |
0x66 |
; 4 |
retlw |
0x6d |
; 5 |
retlw |
0x7d |
; 6 |
retlw |
0x07 |
; 7 |
retlw |
0x7f |
; 8 |
retlw |
0x6f |
; 9 |
retlw |
0x77 |
; A |
retlw |
0x7c |
; B |
retlw |
0x39 |
; C |
retlw |
0x5b |
; D |
retlw |
0x79 |
; E |
retlw |
0x71 |
; F |
retlw |
0x7f |
; Just in case all on |
The programming of seven-segment LEDs was discussed in Chapter 10.
12.3.2 Timer0 as a Simple Delay Timer
Perhaps the simplest use of the Timer0 module is to implement a delay loop. In this application, the Timer0 module is initialized to use the internal clock by setting the TOSE bit of the OPTION register. If the prescaler is to be used, as is most likely, the PSA bit is cleared and the desired prescaling is entered in bits PS2 to PS0 of the OPTION register. The circuit in Figure 12-3 allows testing several timer-related programs developed in this chapter.
The program named Timer0, in the book’s online software package, uses a timer-based delay loop to flash in sequence eight LEDs that display the binary values from 0x00 to 0xff. The delay routine executes in the foreground, so that processing is suspended while the count is in progress. The initialization requires clearing the TOCS bit in the OPTION register to select the internal clock. The prescaler is assigned to Timer0 by clearing the PSA bit and bits PS2 to PS0 are set to assign a 1:256 prescale to the timer. The following code fragment shows the processing.
Timers and Counters |
251 |
|||||||||||||||||||||
+5v |
||||||||||||||||||||||
1 |
16F84 |
|||||||||||||||||||||
2 |
RA2 |
RA1 |
Osc |
|||||||||||||||||||
R=10K |
RA3 |
RA0 |
||||||||||||||||||||
3 |
RA4/TOCKI |
OSC1 |
||||||||||||||||||||
4 |
MCLR |
OSC2 |
||||||||||||||||||||
5 |
Vss |
Vdd |
+5v |
|||||||||||||||||||
6
RB0/INT RB7
7
RB1 RB6
8
RB2 RB5
9
RB3 RB4
R=330x8 Ohm
Figure 12-3 Circuit for Testing Several Timer Programs
main:
;Clear the Watchdog Timer and reset prescaler clrwdt
;Set up the OPTION register
movlw |
b’11010111’ |
|||||||||||
; |
7 |
6 |
5 |
4 |
3 |
2 1 0 <= OPTION bits |
||||||
; |
| |
| |
| |
| |
| |
|__|__|_____ |
PS2-PS0 (prescaler |
bits) |
||||
; |
| |
| |
| |
| |
| |
Values for Timer0 |
||||||
; |
| |
| |
| |
| |
| |
000 |
= 1:2 |
001 |
= |
1:4 |
||
; |
| |
| |
| |
| |
| |
010 |
= 1:8 |
011 |
= |
1:16 |
||
; |
| |
| |
| |
| |
| |
100 |
= 1:32 |
101 |
= |
1:64 |
||
; |
| |
| |
| |
| |
| |
110 |
= 1:128 *111 |
= |
1:256 |
|||
; |
| |
| |
| |
| |
|______________ |
PSA |
(prescaler assign) |
|||||
; |
| |
| |
| |
| |
1 |
= |
to WDT |
|||||
; |
| |
| |
| |
| |
*0 |
= |
to Timer0 |
|||||
; |
| |
| |
| |
|_________________ |
TOSE (Timer0 edge select) |
|||||||
; |
| |
| |
| |
0 |
= |
increment on |
low-to-high |
|||||
; |
| |
| |
| |
*1 |
= |
increment in |
high-to-low |
|||||
; |
| |
| |
|____________________ |
TOCS (TMR0 clock |
source) |
|||||||
; |
| |
| |
*0 |
= |
internal clock |
|||||||
252 |
Chapter 12 |
|||||
; |
| |
| |
1 |
= RA4/TOCKI bit source |
||
; |
| |
|_______________________ INTEDG (Edge select) |
||||
; |
| |
0 |
= |
falling |
edge |
|
; |
| |
*1 |
= |
raising |
edge |
|
;|__________________________ RBPU (Pullup enable)
; |
0 = enabled |
|
; |
*1 = disabled |
|
option |
||
; Set up ports |
||
movlw |
0x00 |
; Set Port-B to output |
tris |
portb |
|
clrf |
portb |
; All Port-B to 0 |
; Port-A is not used in this program |
||
mloop: |
||
incf |
portb,f |
; Add 1 to register value |
call |
TM0delay |
|
goto |
mloop |
|
The delay procedure named TM0delay provides the necessary time lapse between successive increments in the count displayed. The code is as follows:
;******************************
;delay sub-routine
;uses Timer0 ;****************************** TM0delay:
;Initialize the timer register
clrf |
tmr0 |
; Clear SFR for Timer0 |
;Routine tests the value in the TMR0 register by
;subtracting 0xff from the value in TMR0. The zero flag
;is set if TMR0 = 0xff
cycle: |
||
movf |
tmr0,w |
; Timer to w |
; w has TMR0 register value |
||
sublw |
0xff |
; Subtract max value |
; Zero flag is set if value in TMR0 = 0xff |
||
btfss |
status,z ; Test for zero |
|
goto |
cycle |
; Repeat |
Return |
||
12.3.3 Measured Time Lapse
A variable time-lapse routine that can be edited or adjusted to produce delays within a specific time range is a useful tool in any programmer’s library. In previous sections, we developed delay routines that do so by counting timer pulses. This same idea can be used to develop a routine that produces accurate delays within a range.