Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
if(buff[0]=='O' && buff[1]=='K'){ //are they OK?
flag=1; //yes so set flag
}else{ //no so deal with it
if(buff[0]=='O' && buff[1]=='K'){ //are they OK?
flag=1; //yes so set flag
count=0;
}else{ //no so deal with it
If you don't reset 'count' to 0 then on the next keypress it'll just continue to increment and overrun the char buff[2], likely trashing the 'count' and 'flag' variables.on second thoughts this shouldn't mater. The received values will just scroll through the buffer and get recognised by the test.
thats what i noticed some kind of overflow.. but i thought that when he used count-- he was decreasing the counter back to its initial position .. hmmIf you don't reset 'count' to 0 then on the next keypress it'll just continue to increment and overrun the char buff[2], likely trashing the 'count' and 'flag' variables.
The isr should check to make sure 'count' is < 2 before adding the char to 'buff', otherwise bad things will happen.
Yes, but only if count == 2, and if you don't reset count then that only happens once every 256 key presses (but since 'count' gets trashed then who knows when that actually happens).but i thought that when he used count-- he was decreasing the counter back to its initial position
i see, how you would suggest fixing this issue in the existing code ? should I declade count=0 at the end or something ? .Yes, but only if count == 2, and if you don't reset count then that only happens once every 256 key presses (but since 'count' gets trashed then who knows when that actually happens).
Rewrite the isr so that count never exceeds the size of the input buffer, and if you decrement count make sure it's not 0 before you decrement it. If you want to use the 'flag' variable, then post #22 should work ok. I'm not sure about bothering to decrement it at all...how you would suggest fixing this issue in the existing code
void interrupt ISR()
{
if(RCIF)
{
buff[a] = RCREG; /* read received byte from serial buffer */
a++;
if(RCSTAbits.OERR) /* check if any overrun occur due to continuous reception */
{
CREN = 0;
NOP();
CREN=1;
}
status_flag=1; /* use for new message arrival */
}
}
That's basically the start of all usart ISR's... If using a FIFO just change the RCREG read to the next available space in the FIFO..Code:void interrupt ISR() { if(RCIF) { buff[a] = RCREG; /* read received byte from serial buffer */ a++; if(RCSTAbits.OERR) /* check if any overrun occur due to continuous reception */ { CREN = 0; NOP(); CREN=1; } status_flag=1; /* use for new message arrival */ } }
I have been searching and I found this model, what do you think about this one? guys ?
thanks sir. the only thing i think i should fix its to add the null caracter to the end to prevent trash information. also. the error check is necesary ? ..That's basically the start of all usart ISR's... If using a FIFO just change the RCREG read to the next available space in the FIFO..
When you get your head around using FIFO buffers, you'll wonder why you hadn't done it sooner!
#define FifoLength 32
unsigned char FifoBuffer[FifoLength];
unsigned char *FifoStart;
unsigned char *FifoEnd;
unsigned char count;
void InitFifo(void){
FifoStart=FifoBuffer;
FifoEnd=FifoBuffer;
count=0;
}
void PutFifo(unsigned char chr){
*FifoEnd++=chr;
count++;
if(FifoEnd==FifoBuffer+FifoLength) //reached end
FifoEnd=FifoBuffer; //yes so wrap
if(FifoStart==FifoEnd){ //is head eating tail?
//fifo full so deal with it!!
}
}
unsigned char GetFifo(void){
unsigned char chr;
while(FifoStart==FifoEnd); //if fifo empty then wait
chr=*FifoStart++;
count--;
if(FifoStart==FifoBuffer+FifoLength) //wrapped?
FifoStart=FifoBuffer; //yes
return(chr);
}
unsigned char FifoCount(void){
return(count);
}