View previous topic :: View next topic |
Author |
Message |
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
PIC24EP512 - Grabbing serial characters in a loop |
Posted: Fri Oct 25, 2024 6:21 pm |
|
|
Device: PIC24EP512GP806
Compiler: 5.026 (very very old)
Hi guys,
I know this sounds very simple but I've always programmed INT_RDA by grabbing characters upon each interrupt and storing it in a buffer upon every interrupt.
I now am programming a display that sometimes returns plain text characters and sometimes some HEX data like \x00\x00\x00\xff\xff\xff\x88\xff\xff\xff. This is the power-up message for the display.
The problem is that when it's text data, it starts with # and ends with *. That's easy to figure-out... the data gets accumulated as the interrupt is generated then process later when * is found. And it works as expected.
But when it's hex data, it just screws-up everything because, although all responses end with \xFF\xFF\xFF, some may contain other \xFF\xFF\xFF in the middle like the one I indicated above which is 10 bytes long. Others can be 6, others 4 like \x66\x00\x02\xFF\xFF\xFF or \x02\xFF\xFF\xFF.
So I was thinking that perhaps I could use a simple while loop like this:
while( kbhit( UART_DISPLAY ) == TRUE )
{
ReceiveBuffer[RxCharacterCounter ] = (unsigned char) fgetc( UART_DISPLAY );
RxCharacterCounter ++;
}
However, it appears that it grabs something but not what I am expecting.... but I do see the proper data get sent by the display on my logic analyzer....
Therefore, my question is: when using a while loop with kbhit() inside an interrupt, is there anything in particular I should do? Like clear the interrupt or whatever?
I'm a bit confused and been working with these pics for a decade but never attempted to grab all characters in a loop inside the as soon as the first interrupt arrives.
I do disable the interrupt when it is entered and re-enable it when exiting.
Not sure if it makes sense?
Thanks!
Ben |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Fri Oct 25, 2024 7:37 pm |
|
|
Alrighty, if you have an answer, no problem but I found a solution.
When the display powers-up and sends me the long hex string \x00\x00\x00\xff\xff\xff\x88\xff\xff\xff, I just saw that this is actually programmable so I changed it to something simpler within my own protocol #\x99* and it works.
But if you still have input on my question, then maybe it will help others and I may also benefit from it.
Cheers!
Ben |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Sat Oct 26, 2024 4:11 am |
|
|
On the PIC24/30/33 the serial actually has a 4 or 8 character FIFO.
It also will not interrupt again, if you leave the interrupt handler with
characters in this FIFO 'unread'. On the PIC16/18, it will interrupt if
you do this.
So on these later chips you must repeat the read if more characters
are waiting.
Look at:
[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=59548&highlight=pic24+intrda
[/url]
Now you don't test at the start of the loop (this is a waste of time), but
at the end as I show in this thread.
However sepaately, I am worried by what you show. Problem is what
prevents RxCharacterCounter from growing beyond the size of your
buffer?. Nothing shown.
If it does, your write will start to write into storage locations beyond
the buffer. Result disaster. You must always make sure when using
pointers and arrays, that the index is limited to the available space.
No, you do not have to disable/re-enable the interrupt, |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Sun Oct 27, 2024 6:37 pm |
|
|
re: Compiler: 5.026 (very very old)
I guess that's why I'm a 'dinosaur'.....My first PCM was 2.534
Thanks for making an old guy ,feel even older
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Oct 28, 2024 1:48 am |
|
|
Why?.
It was only yesterday...
Must admit I was glancing back at my code archives. Have some code there
dated to just before the millennium, using CCS. The stuff before that used
a different compiler. The really early stuff was all in assembler. I remember
a few years ago, when MicroChip did a history of the PIC, i had to point out
to them that they had their launch date wrong, since I had a project built
nearly a year before they claimed to have launched the processor....
Recent times. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Wed Nov 06, 2024 10:53 pm |
|
|
Ttelmah wrote: | Why?.
I remember a few years ago, when MicroChip did a history of the PIC, i had to point out to them that they had their launch date wrong, since I had a project built nearly a year before they claimed to have launched the processor....
Recent times. |
Speaking of archives -- I recently took a pic of some windows EPROM PICs to share in a FB group...
And we laughed and chuckled at the age of the PICs and us.
[/img] _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|