Need help with PIC16F877 USART

Started by farroos March 7, 2003
Hi again,

I wrote this small program to test PIC's USART (Synchronous Slave
reception). The program gives port A the value 1010 1010 and port B
the value 0000 0000. But when an interrupt happens (enterrupt happens
when the data starts comming in the DT line, pin 26) the value of
port B should change from 0000 0000 to 1010 1010. The clock comes in
the CK line, pin 25.

The problem is that port B stays low and doesn't change, this also
means that I have failed to make an interrupt.

I have attached a copy of my test program so that people can comment
on it and maybe point out my mistakes.

please help..
Farroos

PIC16F877 program: list pf877 ; set processor type
include <P16f877.INC __CONFIG _XT_OSC & _PWRTE_ON & _BODEN_OFF & _CP_OFF & _WDT_ON

;-----------------------
; Reset and Interrupt Vectors

org 00000h ; Reset Vector

goto Initialization

org 00004h ; Interrupt vector
goto InterruptServiceRoutine

;-----------------------
; start program by enabling USART

Initialization

bsf TXSTA, SYNC ; enable SLAVE SYNCHRONOUS
; RECEIVER
bsf RCSTA, SPEN
bcf TXSTA, CSRC
bsf PIE1, RCIE ; enable interrupts
bsf INTCON, GIE
bsf INTCON, PEIE
bcf RCSTA, RX9 ; 8 bit reception
bcf RCSTA, CREN ; just get 1 byte

;------------------------------
; set ports A and B as outputs

banksel TRISA
clrf TRISA
clrf TRISB
goto Main ;------------------------------
; main program just sets port A to AA(hex) and port B to 00(hex)

Main
movlw 0AAh
movwf PORTA
movlw 0AAh
movwf PORTB
SLEEP
goto Main

;-----------------------------
; first thing we do in InterruptServiceRoutine is disable interrupts

InterruptServiceRoutine
bcf PIE1, RCIE
bcf RCSTA, SPEN
bcf INTCON, GIE
goto PutValOnPort

;------------------------------
; set port B to AA(hex) in an endless loop

PutValOnPort
movlw 0AAh
movwf PORTB
goto PutValOnPort

;------------------------------
; The End (this part will never be reached)

END




It doesn't look like you are handling the bank select bits
correctly. You select bank 1 just before the TRIS stuff but you
don't change back in 'main'. There is a problem with this in the
interrupt handler as well.

One rule handed down by others - leave the setting for bank 0. If a
subroutine changes the bank it should restore it.

--- In , farroos <no_reply@y...> wrote:
> Hi again,
>
> I wrote this small program to test PIC's USART (Synchronous Slave
> reception). The program gives port A the value 1010 1010 and port B
> the value 0000 0000. But when an interrupt happens (enterrupt
happens
> when the data starts comming in the DT line, pin 26) the value of
> port B should change from 0000 0000 to 1010 1010. The clock comes
in
> the CK line, pin 25.
>
> The problem is that port B stays low and doesn't change, this also
> means that I have failed to make an interrupt.
>
> I have attached a copy of my test program so that people can
comment
> on it and maybe point out my mistakes.
>
> please help..
> Farroos
>
> PIC16F877 program: > list pf877 ; set processor type
> include <P16f877.INC > __CONFIG _XT_OSC & _PWRTE_ON & _BODEN_OFF & _CP_OFF & _WDT_ON
>
> ;-----------------------
> ; Reset and Interrupt Vectors
>
> org 00000h ; Reset Vector
>
> goto Initialization
>
> org 00004h ; Interrupt vector
> goto InterruptServiceRoutine
>
> ;-----------------------
> ; start program by enabling USART
>
> Initialization
>
> bsf TXSTA, SYNC ; enable SLAVE SYNCHRONOUS
> ; RECEIVER
> bsf RCSTA, SPEN
> bcf TXSTA, CSRC
> bsf PIE1, RCIE ; enable interrupts
> bsf INTCON, GIE
> bsf INTCON, PEIE
> bcf RCSTA, RX9 ; 8 bit reception
> bcf RCSTA, CREN ; just get 1 byte
>
> ;------------------------------
> ; set ports A and B as outputs
>
> banksel TRISA
> clrf TRISA
> clrf TRISB
> goto Main > ;------------------------------
> ; main program just sets port A to AA(hex) and port B to 00(hex)
>
> Main
> movlw 0AAh
> movwf PORTA
> movlw 0AAh
> movwf PORTB
> SLEEP
> goto Main
>
> ;-----------------------------
> ; first thing we do in InterruptServiceRoutine is disable interrupts
>
> InterruptServiceRoutine
> bcf PIE1, RCIE
> bcf RCSTA, SPEN
> bcf INTCON, GIE
> goto PutValOnPort
>
> ;------------------------------
> ; set port B to AA(hex) in an endless loop
>
> PutValOnPort
> movlw 0AAh
> movwf PORTB
> goto PutValOnPort
>
> ;------------------------------
> ; The End (this part will never be reached)
>
> END





--- In , "rtstofer" <rstofer@p...> wrote:
>
> It doesn't look like you are handling the bank select bits
> correctly. You select bank 1 just before the TRIS stuff but you
> don't change back in 'main'. There is a problem with this in the
> interrupt handler as well.
>
> One rule handed down by others - leave the setting for bank 0. If
a
> subroutine changes the bank it should restore it.


This the the modified version of the interrupt test program with bank
selects all over. And it still does not interrupt, please help

Test program:

list pf877 ; set processor type
include <P16f877.INC __CONFIG _XT_OSC & _PWRTE_ON & _BODEN_OFF & _CP_OFF & _WDT_ON

;-----------------------
; Reset and Interrupt Vectors

org 00000h ; Reset Vector

goto Initialization

org 00004h ; Interrupt vector
goto InterruptServiceRoutine

;-----------------------
; start program by enabling USART

Initialization

banksel TXSTA
bsf TXSTA, SYNC
bcf TXSTA, CSRC
banksel RCSTA
bsf RCSTA, SPEN
banksel PIE1
bsf PIE1, RCIE ; enable interrupt
banksel INTCON
bsf INTCON, GIE
bsf INTCON, PEIE
banksel RCSTA
bcf RCSTA, RX9 ;
bcf RCSTA, CREN ; just get 1 byte ;------------------------------
; set ports A and B as outputs

banksel TRISA
clrf TRISA
clrf TRISB

BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0
goto Main ;------------------------------
; main program just sets port A to AA(hex) and port B to 00(hex)

Main

BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0

movlw 0Ah
banksel PORTA
movwf PORTA
SLEEP
goto Main

;-----------------------------
; first thing we do in InterruptServiceRoutine is disable interrupts

InterruptServiceRoutine

banksel PIE1
bcf PIE1, RCIE ; disable interrupts
banksel RCSTA
bcf RCSTA, SPEN
banksel INTCON
bcf INTCON, GIE

BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0

goto PutValOnPort

;------------------------------
; set port B to AA(hex) in an endless loop

PutValOnPort

BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0

movlw 0AAh
banksel PORTB
movwf PORTB
goto PutValOnPort

;------------------------------
; The End (this part will never be reached)

END