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

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

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

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

Добавлен: 14.06.2025

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

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

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Communications

387

1.The context is saved. This includes, but is not limited to, the status register, the w register, the PCLATH register, and the FSR register.

2.Code tests for received data by checking the RCIF bit in the PIR1 register. If this bit is clear the interrupt did not originate in received data.

3.Code can also check if the interrupt enable bit (RCIE) is set in the RCIE register. If not enabled the interrupt is related to serial data.

4.The handler usually checks two possible errors during reception: overflow and framing error. The first one by checking the OERR bit and the second one by checking the FERR bit, both in the RCSTA register. If reception errors have taken place, the handler takes appropriate action.

5.If no error is detected then the received data can be retrieved from the RCREG.

6.On exit the interrupt handler restores the context and issues the retfie instruction.

The following code fragment lists the variables and processing routine for an interrupt handler for serial data reception:

=====================================================

; variables in PIC RAM

;===================================================== ; Local variables

cblock 0x20

; Start of block

.

.

.

;Communications variables errorFlags

;Temporary storage used by interrupt handler tempW

tempStatus tempPclath tempFsr endc

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

;============================================================ ; interrupt handler for received characters ;============================================================ ;============================================================ IntServ:

movwf

tempW

;

Save W

movf

STATUS,W ;

Store STATUS

in W

clrf

STATUS

;

Select

bank0

movwf

tempStatus

; Save STATUS

movf

PCLATH,W ;

Store PCLATH

in W

movwf

tempPclath

; Save PCLATH

clrf

PCLATH

;

Select

program memory page 0

movf

FSR,W

;

Store FSR in

W


388

Chapter 14

movwf

tempFsr

;

Save FSR value

;

Test for received

data interrupt

Bank0

;

select bank0

;

7 6 5 4 3

2

1 0

<= PIR1

;|__________________ (RCIF) USART receive interrupt

;

flag

Btfsc

PIR1,RCIF

; Test bit 5

bsf

STATUS,RP0

; Bank 1 if RCIF set

;

7 6 5 4

3 2 1 0

<= PIE1

;|__________________ (RCIE) Receive interrupt enable

;

bit

btfss

PIE1,RCIE

; Test if interrupt is enabled

goto

IntExit ; Go if not enabled

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

;received data ;==============================

;Routine to handler received data. Overrun and framing

;errors are detected and remembered in the variable

;errorFlags, as follows:

;

7

6

5 4

3

2

1 0

<== errorFlags

;

not

used

——

|

|___

overrun error

;

|______ framing error

Bank0

; Select bank 0

;Test for overrun and framing errors.

;Bit 1 (OERR) of the RCSTA register detects overrun

;Bit 2 (FERR) of the RCSTA register detects framing error

btfsc

RCSTA,OERR

;

Test

for overrun error

goto

OverErr ;

Error handler

btfsc

RCSTA,FERR

;

Test

for framing error

goto

FrameErr ;

Error handler

;At this point no error was detected

;Received data is in the USART RCREG register

movf

RCREG,w ; Received data into w

; Clear error flags

clrf

errorFlags

goto

IntExit

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

;error handlers ;==========================

;Errors are returned as bits in the errorFlags register

;

7 6

5 4 3

2

1 0 <= errorFlags

;

—- not used —-

|

|____ overrun error

;

|_______ framing error

; Error

responses to

be made by main code

OverErr:

bsf

errorFlags,0

; Bit 0 is overrun error

; Reset

system

bcf

RCSTA,CREN

; Clear continuous receive bit


Communications

389

bsf

RCSTA,CREN

; Set to re-enable reception

goto

IntExit

FrameErr:

bsf

errorFlags,1; Bit 1 is framing error

movf

RCREG,W ; Read and throw away bad data

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

;

interrupt handler exit

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

IntExit:

Bank0

movf

tempFsr,w

; Recover FSR value

movwf

FSR

; Restore in register

movf

tempPclath,w

; Recover PCLATH value

movwf

PCLATH

; Restore in register

movf

tempStatus,W

; Recover STATUS

movwf

STATUS

; Restore in register

swapf

tempW,F

; Swap file register in itself

swapf

tempW,W

; Restore in register

retfie

The program SerIntLCD in the book’s online software is an interrupt-driven demonstration for the circuit in Figure 14-18.

14.5 Sample Programs

The sample programs listed in the following sections refer to the programming discussed in this chapter.

14.5.1 SerialSnd Program

;File name: SerialSnd.asm

;Date: May 5, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Description:

;Two programs to exercise serial communications between

;two PIC 16F84A both running at 4Mhs. One program sends

;data through a single line and the other one receives

;it. This program is the sender.

;

;Circuit:

;Port A1 is the serial transmission line

;Port A2 is an active-low pushbutton switch that

;

serves to initiate communications.

;Port A3 is a LED that is ON when the program is

;

ready

to send data. Once data starts

;

being

sent the LED

is turned OFF.

;

Port-B0-B7 is

a 8 x toggle

switch that provides


390

Chapter 14

;

the data byte to be sent

;A pushbutton switch is in the 16F84 RESET line

;

and serves to restart the program

;

;Communications parameters:

;Timer channel TMR0 is used for synchronizing data

;transmission. The timer runs at the maximum rate of

;256 cycles per iteration. In a 4Mhz system the

;timer rate is 1Mhz, thus the bit rate is

;

1,000,000/256

;which is approximately 3,906 microseconds per bit.

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

;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 presently selected

;========================= ; setup and configuration ;=========================

processor

16f84A

include

<p16f84A.inc>

__config

_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

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

; M A C R O S

;============================================================ ; Macros to select the register banks

Bank0

MACRO

; Select RAM bank 0

bcf

STATUS,RP0

ENDM

Bank1

MACRO

; Select RAM bank 1

bsf

STATUS,RP0