Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8556

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
; XORing with a 1-bit produces ; the complement
; Complement bit 2, Port-B

258

Chapter 12

;=======================================================

;Interrupt Service Routine ;=======================================================

;Service routine receives control when there the timer

;register TMR0 overflows, that is, when 256 timer beats

;have elapsed

IntServ:

; First test if source is a Timer0 interrupt

btfss

INTCON,toif

; TOIF is Timer0 interrupt

goto

notTOIF

; Go if not RB0 origin

; If so clear the timer interrupt flag so that count continues

bcf

INTCON,toif

; Clear interrupt flag

; Save context

movwf

old_w

;

Save w register

swapf

STATUS,w ; STATUS to w

movwf

old_status

;

Save STATUS

;=========================

;interrupt action ;=========================

;Subtract 256 from beat counter by decrementing the

;mid-order byte

decfsz countM,f

goto

exitISR

; Continue if mid-byte not

; zero

;At this point the mid-order byte has overflowed.

;High-order byte must be decremented.

decfsz countH,f

goto exitISR

;At this point count has expired so the programmed time

;has elapsed. Service routine turns the LED on line 0,

;Port-B on and off at every conclusion of the count.

;This is done by XORing a mask with a one-bit at the

;Port-B line 0 position

movlw b’00000001’

xorwf portb,f

; Reset one-half second counter call onehalfSec

;=========================

;exit ISR ;========================= exitISR:

;Restore context

swapf

old_status,w ; Saved status to w

movfw

STATUS

; To STATUS

register

swapf

old_w,f

;

Swap file

register in itself

swapf

old_w,w

;

re-swap back to w

; Return from interrupt


Timers and Counters

259

notTOIF:

retfie

One of the initial operations of the service routine is to clear the TOIF bit in the INTCON register. This action re-enables the timer interrupt and prevents counting cycles to be lost. Since the interrupt is generated every 256 beats of the timer, there is no risk that by enabling the timer interrupt flag a re-entrant interrupt will take place.

The interrupt-based timer program named LapseTmrInt can be tested on the same circuit shown in Figure 12-3.

12.4 The Watchdog Timer

The 16F84 contains an independent timer with its own clock source called the Watchdog Timer, or WDT. The Watchdog Timer provides a way for the processor to recover from a software error that impedes program continuation, such as an endless loop. The

Watchdog Timer is not designed to recover from hardware faults, such as a brown-out.

The Watchdog Timer hardware is independent of the PIC’s internal clock. Its time-out period lasts approximately 18ms to 2.3s, depending on whether the prescaler is used and on its setting. It is not very accurate due to its sensitivity to temperature. According to Microchip’s documentation, under worst-case conditions, its time-out period can take up to several seconds. The following program elements relate to Watchdog Timer operation:

1.Configuration bit 2, labeled WDTE, enables and disables the Watchdog Timer during system configuration. The WDT cannot be set or reset at runtime. It is enabled and disabled during programming.

2.The PSA bit in the OPTION register selects whether the prescaler is assigned to the

Watchdog Timer or to the Timer0 module.

3.Bits PS2 to PS0 in the OPTION register allow assigning eight rates to the Watchdog Timer, from 1:1 to 1:128.

4.Bit 4 of the STATUS register, named the TO bit, is cleared when a time-out condition occurred that originated in the WDT.

5.The power-down bit (PD) in the STATUS register is set after the execution of the clrwdt instruction.

6.The clrwdt instruction clears the Watchdog Timer. It also clears the prescaler count (if the prescaler is assigned to the Watchdog Timer) and sets STATUS bits TO and PD.

The WDT provides a recovery mechanism for software errors. When the WDT times-out, the TO flag in the STATUS register is cleared and the program counter is reset to 0000 so that the program restarts. Applications can prevent the reset by issuing the clrwdt instruction before the time-out period ends. When clrwdt executes the WDT time-out period restarts.

260

Chapter 12

12.4.1 Watchdog Timer Programming

Not much information is available regarding the details of operation of the Watchdog Timer in the 16F84. Using the WDT in applications is not just a simple matter of restarting the counter with the clrwdt instruction. The timer is designed to detect software errors that can hang up a program, but how it detects these errors and which conditions trigger the WDT operation are not clear from the information provided by Microchip. For example, an application that contains a long delay loop may find that the Watchdog Timer forces an untimely break out of the loop. The Watchdog Timer provides a powerful error-recovery mechanism, but its use requires careful consideration of program conditions that could make the timer malfunction.

12.5 Sample Programs

The following programs demonstrate the programming discussed in this chapter.

12.5.1 The Tmr0Counter program

;File name: Tmr0Counter.asm

;Date: April 30, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Reference: SevenSeg Circuit and Board

;Description:

;Test program for the Timer0 counter. The program counts

;the number of presses of the pushbutton switch on port

;RA4/TOCKI and displays the count on a seven segment LED.

;Switch is wired active low.

;

; 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

;|

;|_____ * indicates set up values

;=========================

;set up and configuration ;=========================


Timers and Counters

261

processor 16f84A

include

<p16f84A.inc>

__config

_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

;

;=====================================================

;

constant definitions

;

(per circuit wiring diagram)

;=====================================================

#define Pb_sw 4 ; Port-A line 4 to push button switch

;

;============================

;

local variables

;============================

cblock

0x0c

; Start of block

J

; counter J

K

; counter K

endc

;============================================================

;

program

;============================================================

org

0

; start at address 0

goto

main

;

; Space for interrupt handlers

org

0x08

main:

;Clear the timer and the watchdog clrf TMR0

clrwdt

;Set up the OPTION register bit map

movlw

b’10111000’

;

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


262

Chapter 12

; | |_______________________ 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 for input

movlw

B’00011111’

; w = 00011111 binary

tris

PORTA

; Port-A (lines 0 to 4) to

; input

;=================================

;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

TMR0,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

movwf

PORTB

; Display switch bits

goto

checkTmr0

; Endless loop

;

;================================

; routine to returns 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