|
|
View previous topic :: View next topic |
Author |
Message |
Jasen
Joined: 31 Aug 2009 Posts: 6
|
String lost |
Posted: Fri Sep 18, 2009 8:52 am |
|
|
Hi !!!I took a look at program EX_SISR and found this problem. I Make some modification to better follow the problem (see the program bellow)
After certain constant string received (about 11 strings ) , its print out data invalid !!!! After this, its back print data OK. Again, after about 11 strings received , its print out data invalid and repeat the loop.
My question is why its "lost" the correct string some times ? The string test is been send every 500ms with 10 char each string.
Code: | #define BUFFER_SIZE 11
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
int data2,x,j,y,z;
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main() {
enable_interrupts(global);
enable_interrupts(int_rda);
data2 = 0;
z = 1;
y = 1;
printf("\r\n\Running...\r\n");
// The program will delay for 10 seconds and then display
// any data that came in during the 10 second delay
do {
delay_ms(500);
while(bkbhit)
{x = bgetc();
j++;
if (j == 2) data2 = x;}
j = 0;
if ( data2 == 0x69) printf ("data OK%u\n\r", y++);
if ( data2 != 0x69)
{printf ("data inválido !!!%u\n\r", z++);
y = 1; }
} while (TRUE);
} |
|
|
|
Ttelmah Guest
|
|
Posted: Fri Sep 18, 2009 9:47 am |
|
|
First thing, don't use a buffer size of 11. Choose a size that is a binary multiple (8, 16, 32). If you use a 'non binary' size, the compiler has to switch to using division, rather than just shifting, and this results in much slower ISR response, and the probability of interrupts being disabled in the external code, when the same arithmetic is used. The code really ought to come with a 'health warning' about this....
Second comment though, there is no guarantee about the timings in your code. Your routine will take 500mSec, plus the loop time, plus the time to send the data out on the serial. If the data is arriving 'every 500mSec', each time round, the loop _will_ fall further and further behind the incoming data. As written, the code throws away data if the buffer overflows, so you _will_ see data loss...
Get rid of the delays in your 'main'. You can tell if ten characters have been received, by subtracting 'next_in' from 'next_out'. this result % BUFFER_SIZE, will give the number of characters in the buffer. Use this to trigger your response code.
Best Wishes |
|
|
|
|
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
|