Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8534
Скачиваний: 0
Timers and Counters |
263 |
|
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 |
end |
12.5.2 The Timer0 Program
;File: Timer0.ASM
;Date: April 27, 2006
;Author: Julio Sanchez
;Processor: a6F84A
;Description:
;Program to demonstrate programming of the 16F84A
;TIMER0 module. Program flashes eight LEDs in sequence
;counting from 0 to 0xff. Timer0 is used to delay
;the count.
;===========================
;switches ;===========================
;Switches used in __config directive:
; |
_CP_ON |
Code protection ON/OFF |
|
; * |
_CP_OFF |
||
; |
* |
_PWRTE_ON |
Power-up timer ON/OFF |
;_PWRTE_OFF
; |
_WDT_ON |
Watchdog Timer ON/OFF |
||
; * _WDT_OFF |
||||
; |
_LP_OSC |
Low power crystal |
occilator |
|
; * _XT_OSC |
External parallel |
resonator/crystal oscillator |
||
; |
_HS_OSC |
High speed crystal resonator (8 to |
10 MHz) |
|
; |
Resonator: Murate |
Erie CSA8.00MG = |
8 MHz |
|
; |
_RC_OSC |
Resistor/capacitor oscillator (simplest, 20% |
||
error)
;|
;|_____ * indicates set up values
processor |
16f84A |
|
include |
<p16f84A.inc> |
|
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
|
;===================================================== |
||
; |
variables in PIC RAM |
|
;=====================================================
264 |
Chapter 12 |
; None in this application
;
;========================================================
; |
m a i n |
p r o g r a m |
;========================================================
org |
0 |
; start at address 0 |
goto main
;
;=============================
;interrupt handler ;=============================
org 0x08 ;=============================
;main program ;============================= main:
;Clear the Watchdog Timer and reset prescaler clrwdt
;Set up the OPTION register bit map
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 |
||||||
; |
| |
| |
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 is not used in this program mloop:
Timers and Counters |
265 |
|
incf |
PORTB,f |
; Add 1 to register value |
call |
TM0delay |
|
goto |
mloop |
;******************************
;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 |
||
end
12.5.3 The LapseTimer Program
;File: LapseTimer.ASM
;Date: May 1, 2006
;Author: Julio Sanchez
;Processor: 16F84A
;Description:
;Using Timer0 to produce a variable-lapse delay.
;The delay is calculated based on the number of machine
;cycles necessary for the desired wait period. For
;example, a machine running at a 4 Mhz clock rate
;executes 1,000,000 instructions per second. In this
;case a 1/2 second delay requires 500,000 instructions.
;The wait period is passed to the delay routine in three
;program registers which hold the high-, middle-, and
;low-order bytes of the counter. ;===========================
;switches
;===========================
; Switches used in __config directive:
; |
_CP_ON |
Code protection ON/OFF |
|
; * |
_CP_OFF |
||
; |
* |
_PWRTE_ON |
Power-up timer ON/OFF |
266 |
Chapter 12 |
;_PWRTE_OFF
; |
_WDT_ON |
Watchdog Timer ON/OFF |
||
; * _WDT_OFF |
||||
; |
_LP_OSC |
Low power crystal |
occilator |
|
; * _XT_OSC |
External parallel |
resonator/crystal oscillator |
||
; |
_HS_OSC |
High speed crystal resonator (8 to |
10 MHz) |
|
; |
Resonator: Murate |
Erie CSA8.00MG = |
8 MHz |
|
; |
_RC_OSC |
Resistor/capacitor oscillator |
||
;|
;|_____ * indicates set up values
processor 16f84A |
|||
include |
<p16f84A.inc> |
||
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
||
;===================================================== |
|||
; |
variables in PIC RAM |
||
;===================================================== |
|||
; Local variables |
|||
cblock |
0x0d |
; Start of block |
|
; 3-byte auxiliary counter for delay. |
|||
countH |
; High-order byte |
||
countM |
; Medium-order byte |
||
countL |
; Low-order byte |
||
endc |
|||
;========================================================
; m a i n p r o g r a m
;========================================================
org |
0 |
; start at address 0 |
goto |
main |
;
;=============================
; interrupt handler
;=============================
org 0x04
;goto IntServ ;=============================
;main program ;============================= main:
;Clear the Watchdog Timer and reset prescaler clrf TMR0
clrwdt
;Set up the OPTION register bit map
movlw |
b’11010000’ |
|||||
; |
7 |
6 |
5 |
4 |
3 |
2 1 0 <= OPTION bits |
; |
| |
| |
| |
| |
| |
|__|__|_____ PS2-PS0 (prescaler bits) |
; |
| |
| |
| |
| |
| |
Values for Timer0 |
Timers and Counters |
267 |
||||||||
; |
| |
| |
| |
| |
| |
*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 is not used in this program ;============================
;display loop ;============================ mloop:
;Turn on LED
bsf |
PORTB,0 |
; Initialize counters and delay |
|
call |
onehalfSec |
call |
TM0delay |
; Turn off LED |
|
bcf |
PORTB,0 |
; Re-initialize counter and delay call onehalfSec
call TM0delay goto mloop
;==================================
;variable-lapse delay procedure
;using Timer0 ;==================================
;ON ENTRY:
;Variables countL, countM, and countH hold
;the low-, middle-, and high-order bytes
;of the delay period, in timer units
;Routine logic: