Файл: 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 |
|||
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 |