|
|
View previous topic :: View next topic |
Author |
Message |
mdemuth
Joined: 16 Apr 2007 Posts: 71 Location: Stuttgart, Germany
|
#int_rda happens sometimes only once |
Posted: Tue Nov 02, 2010 10:37 am |
|
|
Hi there,
I am using the compiler version 4.114
I am trying to read a stream via the uart. Each received character is supposed to create an IRQ an then it should be written into a stream (array).
The PC sends messages every 0.5s.
Sometimes everything works fine, but sometimes (50%) the programm only gets a single IRQ (I count the IRQs with the LED_3 variable) and then no more characters are received.
Has anybody an idea?
Thanks in advance,
Michael
Code: |
#include <18f2580.h> //
#device ADC=10 //
#device HIGH_INTS=TRUE
// #device ICD=TRUE
// #fuses INTRC_IO,NOWDT,NOPROTECT,BROWNOUT,PUT,MCLR // Kein WDT
#fuses INTRC_IO,NOWDT,NOPROTECT,BROWNOUT,PUT,MCLR, BORV28
#use delay(clock=32000000)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use rs232(stream=COM1, baud=19200,PARITY=N, BITS=8, STOP=1, UART1) //
#use rs232(stream=COM2, baud=9600,XMIT=PIN_C1) //
#use i2c (master, sda=PIN_C4, scl=PIN_C3, FORCE_HW, FAST)
....
// RS232 Schnittstelle
#define RX_BUFFER_LENGTH 35 // STX,ETX,ID,BCC + maximal 30 Byte Nutzdaten
char rx_buffer[RX_BUFFER_LENGTH];
int rx_buffid;
..
setup_oscillator (OSC_32MHZ);
SETUP_ADC_PORTS(AN0_TO_AN1);
SETUP_ADC(ADC_CLOCK_INTERNAL);
SETUP_WDT(WDT_OFF); //WDT
RESTART_WDT(); // Initialisierung
set_tris_a(0b00000011); //
set_tris_b(0b00110000); //
set_tris_c(0b10011000); //
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 250, 1);
seriennummer_LB=read_EEPROM (0);
seriennummer_HB=read_EEPROM (1);
seriennummer = make16(seriennummer_HB,seriennummer_LB);
output_low(LE); // init LED Drivers
output_low(OUT_ENABLE); //
output_low(SDO); //
output_low(SCK); //
...
setup_uart(19200,COM1);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA); // Empfangs IRQ aktivieren
while(1) // Test
{
counter16++;
if (bit_test(counter16,9)) LED_RX = true;
else LED_RX = false;
Set_LEDs();
Read_Keyboard();
if (rx_complete == 2)
{
rx_complete =0;
fputc(0x06,COM1); // Ack senden
// fputc(0x0C,COM2); // Formfeed
// fprintf(com2,"%03u",i);
//for (i=0; i < RX_BUFFER_LENGTH;i++) fprintf(com2," %03u",RX_BUFFER[i]); //
for (i=0; i < 20;i++) fprintf(com2, " %03x" RX_BUFFER[i]);
}
for (i=0; i < 20;i++) RX_BUFFER[i]=0;
}
#INT_RDA
void IRQ_UART(void)
{
character = fgetc(com1); // Character in den Puffer
if (character == 0x02 && rx_complete == 0) rx_buffid = 0; // Bei Empfang von STX Pointer zurücksetzen
else rx_buffid++; // Zeiger inkrementieren
if (rx_buffid >= RX_BUFFER_LENGTH) rx_buffid = RX_BUFFER_LENGTH; // Überlauf abfangen
rx_buffer[rx_buffid]=character;
if (rx_complete == 1) rx_complete = 2; // Empfang von Zeichen nach dem erstem ETX beendet den Telegrammempfang
if (character == 0x03 && rx_complete==0) rx_complete=1; // Empfang von erstem ETX merken
led_3++; // just for debugging....
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Nov 02, 2010 11:07 am |
|
|
You are probably overflowing the buffer.
Your array, has addresses 0 to 34.
You limit the access address to 35.
When you write to location 35, you will overwrite the 'rx_buffid' variable, and failure will result.
You need:
Code: |
if (rx_buffid >= RX_BUFFER_LENGTH) rx_buffid = RX_BUFFER_LENGTH-1; // Überlauf abfangen
|
Note the '-1'.....
Best Wishes |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Wed Nov 03, 2010 8:11 pm |
|
|
You might also want to add ERRORS to your directives:
Code: |
#use rs232(stream=COM1, baud=19200,PARITY=N, BITS=8, STOP=1, UART1,ERRORS) //
#use rs232(stream=COM2, baud=9600,XMIT=PIN_C1,ERRORS) //
|
otherwise your UARTS will halt on errors. |
|
|
mdemuth
Joined: 16 Apr 2007 Posts: 71 Location: Stuttgart, Germany
|
problem solved |
Posted: Thu Nov 04, 2010 5:18 am |
|
|
The "ERRORS" added to the directive solved the problem, thanks for good support!
|
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Nov 04, 2010 12:27 pm |
|
|
ERRORS is almost a required parameter. It is the way it is but you could argue the parameter should have been NO_ERROR_RECOVERY indicating you wanted the interface to crash on any error. The default maybe should have been the same as adding ERRORS since that is what most would want.
RS232 is vulnerable to errors both from the environment and from its asynchronous nature. There isn't much you can do with a RS232 error except log it unless you build in ACK NAK flags so the RS232 sender can resend. |
|
|
|
|
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
|