PDA

View Full Version : HSEROUT with Timer2 Interrupt



MarkEngineer
- 12th June 2015, 18:10
I am using a PIC18F87K22 micro. I have timer2 setup with PR2 to generate an interrupt 250 times per second
'setup timer2
'Fosc/4 with prescaler=16 AND postscaler of 16 yields 62,500 Hz, count of 250 gives interrupt every 250th of a second
'64mhz/4 = 4 mhz............16mhz/256 = 62500 hz.....use for 64 Mhz clock
PR2 = 249 'Timer2 Period Register
T2CON = 111110
PIE1.1 = 1 'enable the timer2 interrupt

Both UARTS are set to 19,200 baud (actually 19,230)

Each UART will generate an interrupt if a character arrives and I can read both serial ports without any issues whatsoever.

It is when I send characters where it appears that the UART timing seems to go screwy and the receiving
computer gets strings with incorrect or missing characters

If I transmit an array this way
HSEROUT2 [STR SENDBUFF\30]
It does not work. But if I disable the timer2 interrupt it works fine.

However if I use assembly the timer2 interrupt does not affect it at all.

FOR I = 0 TO 29
SENDCHAR = SENDBUFF(I)
GOSUB SENDCOM2
NEXT I

SENDCOM2:
;MOVFF source, destination = move source reg to dest reg
@MOVFF _SENDCHAR,TXREG2
CHECKCOM2:
@MOVFF TXSTA2,_SENDCHAR
IF SENDCHAR.1 = 0 THEN GOTO CHECKCOM2 'wait until transmit buffer is empty
RETURN

Suggestions?

MarkEngineer
- 16th June 2015, 20:34
Finally found out the answer, needed to save and restore the Bank Select Register in the assembly interrupt routine.
Upon entering the interrupt:
;save critical registers
MOVFF STATUS,_SSAVE ;save STATUS
MOVFF WREG,_WSAVE ;save W
MOVFF FSR0L,_FSAVELOW ;save FSR0 lowbyte
MOVFF FSR0H,_FSAVEHIGH ;save FSR0 highbyte
MOVFF BSR,_BSRSAVE ;save bank select register

Upon exiting:
;restore critical registers
MOVFF _FSAVELOW,FSR0L ;restore FSR0 lowbyte
MOVFF _FSAVEHIGH,FSR0H ;restore FSR0 highbyte
MOVFF _BSRSAVE,BSR ;restore bank select register
MOVFF _WSAVE,WREG ;restore W
MOVFF _SSAVE,STATUS ;restore STATUS
RETFIE ;return from the interrupt