View previous topic :: View next topic |
Author |
Message |
sjharris
Joined: 11 May 2006 Posts: 78
|
Serial buffer overrun |
Posted: Mon Aug 13, 2007 7:52 am |
|
|
Hello, I am using a PIC 16F688 with the hardware UART and I am trying to read some information in to the serial port, but only at a specific time.
I am using the interrupt RDA routine to read in data to a variable.
I only want to read in data from the serial port after I have sent something to it, at all other times there is data coming from it but I dont want to read it.
After I have sent data to the serial port I am intitating the interrupt routine, if there has not been any data received before this point it will receive data OK.
But if any data has been received before I start the interrupt then it will not receive any more data.
I think it is the Buffer filling prematurely and causing the UART to 'crash' if it can.
Is there any way to stop anything from being put into the buffer?
Is there a way of clearing the buffer if it gets full?
Thanks in advance
Stephen Harris
PS I am unable to post code sue to security risks. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Aug 13, 2007 8:33 am |
|
|
Quote: | 1).Is there any way to stop anything from being put into the buffer?
2).Is there a way of clearing the buffer if it gets full?
|
1.) disable_interrupts(INT_RDA);//this way data is at the pin. but never
enters the chip.
2.)reset the pointers. my buffer looks like this
int8 rx_buf[BUFF_SZ];
int8 rx_i = 0;
int8 rx_o = 0;
with an in((rx_i)) pointer and and out((rx_o)) pointer.
to reset the input just set it = to out.
rx_i=rx_o;
but thats not the way I'd do it.
I think first you should build a nice state machine that will follow your spec.
Then make that code without adding in the rx buffer stuff.
then add in the rx buffer stuff. |
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Mon Aug 13, 2007 9:00 am |
|
|
As I recall, if the uart gets an overrun error, it quits until the error is cleared - there's an option in #use rs232 that handles this. Or, you might clear this right after you send data, then it should receive ok. I think I would just disable the receiver until I wanted to receive.
Ken |
|
|
Ttelmah Guest
|
|
Posted: Mon Aug 13, 2007 9:23 am |
|
|
Yes. You need to add 'ERRORS' to the RS232 define, to make the code automatically clear buffer overrun errors.
Remember when the interrupt is enabled, there will be an immediate interrupt, and possibly two characters will be received in turn in the interrupt handler, which will need to be disposed of.
Why not handle the whole thing differently? Just set a flag, and have the interrupt handler left enabled, but throw away any characters received. This way you won't get overruns, and because the earlier characters will have been thrown away, no extra handling will be needed. So:
Code: |
int1 receive_enabled=true;
#int_rda
void serial_isr() {
int t;
//read the waiting character
t=getc();
if (receive_enabled) {
buffer[next_in]=t;
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
}
|
Simply set 'receive_enabled' to false, to turn off serial reception.
Best Wishes |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Tue Aug 14, 2007 2:33 am |
|
|
I have now just set a flag in the int routine. It ignores any characters that it receives until I want to receive them.
It seems to work.
Thanks everyone |
|
|
|