Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8664
Скачиваний: 0
Supplementary Programs |
683 |
;=============================
;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 |
||
; Setup 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 |
|
;******************************
;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
684 |
Appendix D |
|
; 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
Supplementary Programs |
685 |
;File: TimerTst.ASM
;Date: April 27, 2006
;Author: Julio Sanchez
;Description:
;Using the timer to generate a signal at 1 Mhz ;===========================
;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 setup values
processor 16f84A |
|||
include |
<p16f84A.inc> |
||
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
||
;===================================================== |
|||
; |
PIC register equates |
||
;===================================================== |
|||
porta |
equ |
0x05 |
|
Portb |
equ |
0x06 |
|
Status |
equ |
0x03 |
|
z |
equ |
0x02 |
|
tmr0 |
equ |
0x01 |
|
; |
|||
;===================================================== |
|||
; |
variables in PIC RAM |
||
;===================================================== |
|||
; Local variables |
|||
cblock |
0x0d |
; Start of block |
|
J |
; counter J |
||
K |
; counter K |
||
countL |
; Auxiliary counter |
||
countH |
; ISR counter |
||
686 Appendix D
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 clrwdt
;Set up the OPTION register bit map
movlw |
b'11010011' |
|||||||||
; |
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 |
|||
; Setup 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:
Supplementary Programs |
687 |
|
bsf |
portb,0 |
|
call |
TM0delay |
|
bcf |
portb,0 |
|
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
;xoring with a mask of all ones. The operation sets
;the zero flag if tmr0 is zero.
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