|
|
View previous topic :: View next topic |
Author |
Message |
sorin
Joined: 14 Mar 2005 Posts: 2
|
Yet another RS232 problem |
Posted: Mon Mar 14, 2005 12:23 pm |
|
|
Hi there.
I have the following code for serial interrupt testing. It works fine, but when I copy/paste alot of data the PIC hangs and then is restarted by WDT. I assume it's some kind of RS232 buffer overflow, but I thought ERRORS should take care of this... any thoughts ?
Code: |
#include <16F876.h>
#use delay(clock=12000000)
#fuses WDT, HS, NOPROTECT, NOLVP, NOBROWNOUT
#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, RESTART_WDT, ERRORS)
//serial data
#define RX_BUFFER_SIZE 70
static char rx_buffer [ RX_BUFFER_SIZE ];
static char rx_buffer_count;
static int1 process_data = 0;
#DEFINE FLASH_A0 pina0 = !pina0; output_bit(PIN_A0, pina0); delay_ms(250); restart_wdt();
#DEFINE FLASH_A1 pina1 = !pina1; output_bit(PIN_A1, pina1); delay_ms(250); restart_wdt();
static int1 pina0;
static int1 pina1;
//serial interrupt
#INT_RDA
void serial_isr() {
unsigned int8 received_char;
pina0 = !pina0;
output_bit(PIN_A0, pina0);
if(!process_data) {
received_char = getchar();
if( (received_char == 10) || (received_char == 13) ){
process_data = 1;
} else {
rx_buffer[rx_buffer_count] = received_char;
rx_buffer_count = (rx_buffer_count + 1) % RX_BUFFER_SIZE;
};
};
}
void main() {
setup_wdt(WDT_1152MS);
FLASH_A0;
FLASH_A1;
FLASH_A0;
FLASH_A1;
FLASH_A0;
FLASH_A1;
FLASH_A0;
FLASH_A1;
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
//main loop
while(1) {
restart_wdt();
if(process_data) {
pina1 = !pina1;
output_bit(PIN_A1, pina1);
rx_buffer[rx_buffer_count] = 0;
printf("%s\r\n", rx_buffer);
process_data = 0;
rx_buffer_count = 0;
};
};
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 14, 2005 2:53 pm |
|
|
I didn't look closely at your code, but my very first thought is to
add the line shown in bold, below:
#include <16F876.h>
#use delay(clock=12000000, restart_wdt)
#fuses WDT, HS, NOPROTECT, NOLVP, NOBROWNOUT |
|
|
Ttelmah Guest
|
|
Posted: Mon Mar 14, 2005 4:39 pm |
|
|
If a serial interrupt has occured, there is always a character 'waiting'. So move the line to read the buffer outside the test part of the routine. Effectively you are stopping the routine from handling characters if the 'process data' flag is set. The 'errors' flag, only operates when a character is read. It tells the code to clear the UART error flags if they are set at this point, but you are preventing this from happening...
You can also get rid of the 'restart_wdt', in the RS232 setup. This is only needed if you are sitting 'waiting' in getc, and with the interrupt driven comms this should never happen.
Beware the order you do things. You should reset the buffer counter before resetting the 'process data' flag, otherwise if a character arrives between these two statements, it will be appnded to the already processed buffer.
Best Wishes |
|
|
sorin
Joined: 14 Mar 2005 Posts: 2
|
|
Posted: Tue Mar 15, 2005 5:48 am |
|
|
you are both absolutely right, thanks, it's ok now.
the reason I am stopping the buffer from being added to in the serial interrupt while processing is that I am afraid the buffer might get overwritten by new data, and it will not be able to process correctly the old data. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|