PDA

View Full Version : ON INTERRUPT USAR! variable



egds
- 15th October 2004, 12:43
Hello,

I need to save in a variable some ASCII character.
I´m actually with USAR interrrupt using ON INTERRUPT PIC BASCI COMMAND.

I want to save all serial characters that i received in a variable and after be able to send with hserout command.
My code it here: This code is actually run well, but just send the latest character and not all the characters recieved.
*********************************************
INCLUDE "bs2defs.bas"
DEFINE LOADER_USER 1
DEFINE OSC 20
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600



ADCON1 = 6 ' Set PortA to digital operation
RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag)
OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag)
CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable)
LED VAR PORTB.7 ' Alias LED to PORTB.7

buffer_size CON 32 ' Sets the size of the ring buffer
buffer VAR BYTE[Buffer_size] ' Array variable for holding received characters
index_in VAR BYTE ' Pointer - next empty location in buffer
index_out VAR BYTE ' Pointer - location of oldest character in buffer
bufchar VAR BYTE
bufchar1 VAR BYTE
bufchar2 var byte
posicion VAR BYTE
cont var byte
' Stores the character retrieved from the buffer
i VAR BYTE ' loop counter
errflag VAR BYTE





' Holds error flags

' Initialize variables
cont=0
index_in = 0
index_out = 0


INTCON = %11000000 ' Enable interrupts
On INTERRUPT GoTo serialin ' Declare interrupt handler routine
PIE1.5 = 1 ' Enable interrupt on USART

' Main program starts here - blink an LED at 1Hz

Loop:

High LED ' Turn on LED connected to PORTD.0
For i = 0 To 250 ' Delay for .5 seconds (250*2mS)
Pause 2 ' Use a short pause within a loop
Next i ' instead of one long pause
Low LED ' Turn off LED connected to PORTD.0
For i = 0 To 250 ' Delay for .5 seconds (250*2mS)
Pause 2 ' Use a short pause within a loop
Next i
if porta.2=1 then calle

display:

IF errflag Then error

If index_in = index_out Then Loop ' loop if nothing in buffer



'gosub getbuf ':
'leemos buffer hasta encontrar las primeras "
'gosub getbuf
'hserout[bufchar]
'PAUSE 1000
'hserout[bufchar1]

'GOTO display

' Check for more characters in buffer
' Subroutines

Disable ' Don't check for interrupts in this section

getbuf: ' move the next character in buffer to bufchar
index_out = (index_out + 1) ' Increment index_out pointer (0 to 63)
If index_out > (buffer_size - 1) Then index_out = 0 ' Reset pointer if outside of buffer
bufchar1=buffer[index_out]
goto display
calle:
hserout["AT+CMFG=",34,"67741418",34,bufchar1]
goto display




'Return
error: ' Display error message if buffer has overrun
IF errflag.1 Then error1 ' Determine the error
error1:
hserout["Buffer Overrun"] ' Display buffer error on line-2
'Else
hserout["USART Overrun"] ' Display usart error on line-2
'End If

'LCDOut $fe,2 ' Send the LCD cursor back to line-1 home
'For i = 2 To col ' Loop for each column beyond 1
'LCDOut $fe,$14 ' Move the cursor right to the right column
'Next i

errflag = 0 ' Reset the error flag
CREN = 0 ' Disable continuous receive to clear overrun flag
CREN = 1 ' Enable continuous receive

GoTo display ' Carry on


' Interrupt handler


serialin:
disable
HIGH PORTB.3
' Buffer the character receive
If OERR Then usart_error ' Check for USART errors
index_in = (index_in + 1) ' Increment index_in pointer (0 to 63)
If index_in > (buffer_size - 1) Then index_in = 0 'Reset pointer if outside of buffer
If index_in = index_out Then buffer_error ' Check for buffer overrun
HSerin [buffer[index_in]]
LOW PORTB.3
IF RCIF then serialin ' Read USART and store character to next empty locationIf RCIF Then serialin ' Check for another character while we're here

Resume
enable ' Return to program

buffer_error:
errflag = 1 ' Set the error flag for software
' Move pointer back to avoid corrupting the buffer. MIN insures that it ends up within the buffer.
index_in = (index_in - 1) MIN (buffer_size - 1)
HSerin [buffer[index_in]] ' Overwrite the last character stored (resets the interrupt flag)
usart_error:
errflag = 1 ' Set the error flag for hardware

Resume ' Return to program


End
*************************
So i can´t save more than 1 character.

Some suggestions?

Thanks a lot.

Dwayne
- 15th October 2004, 15:35
Hello Edgs,

Doing a VERY quick lookthrough on your code, I see one major problem (unless I am missing something)

You are calling a routine thorugh a interupt, then using disable. (AS the code below).

then you are looping out of this disable interupt without calling the enable again. Is that what you want??? For a guess, it will only work 1 time (first time) and fail all other times.

Here is the code... Notice the Disable is not commented out, so that tells me the interupt is negated. then the 7th line down you branch out of it to "Display", and from Display you go to "Loop"

Are you sure you don't want to enable it again sometime?

E>>
Disable ' Don't check for interrupts in this section

getbuf: ' move the next character in buffer to bufchar
index_out = (index_out + 1) ' Increment index_out pointer (0 to 63)
If index_out > (buffer_size - 1) Then index_out = 0 ' Reset pointer if outside of buffer
bufchar1=buffer[index_out]
goto display
calle:
hserout["AT+CMFG=",34,"67741418",34,bufchar1]
goto display
<<