View previous topic :: View next topic |
Author |
Message |
Shoebie
Joined: 07 Nov 2007 Posts: 5
|
RCREG not empty |
Posted: Wed Nov 28, 2007 3:38 pm |
|
|
I'm using a PIC16F876 with CCS ver 4.023. I am trying to capture a string from another source. I have found that the RCREG register is not empty. I try a read and still get a "Wrong" byte. Here is my code.
Code: | fputc('V',HW_RS232);
string_conversion();
char Hex_Dec(void)
{
char chr;
Rx_Tmr_On();
chr = getg();
if(chr <= (0x39) && chr >= (0x30))
chr=(chr-(0x30));
else
chr=(chr-(0x37));
Rx_Tmr_Off();
return chr;
}
void string_conversion(void)
{
unsigned char ch;
DUT_monitor = 0;
Count = 0;
Read_Rx();
set_tris_c(0xff);
if(Count<=6)
ch = Hex_Dec();
DUT_monitor = ch;
DUT_monitor = (DUT_monitor << 4);
if(Count<=6)
ch = Hex_Dec();
DUT_monitor = DUT_monitor + ch;
DUT_monitor = (DUT_monitor << 4);
if(Count<=6)
ch = Hex_Dec();
DUT_monitor = DUT_monitor + ch;
DUT_monitor = (DUT_monitor << 4);
if(Count<=6)
ch = Hex_Dec();
DUT_monitor = DUT_monitor + ch;
}
|
If I leave out the fputc function, the uart will rx the correct character. I have to send out this character to recieve data. I tried reading RCREG and RCSTA before the fputc and it didn't help. For this debugging, I used hyperterminal to send data.(hex numbers). Count is used in the interrupt routine, so I don't hang with lack of data. Is there another way I can insure the RX buffer is empty? Read_Rx is the routine I use to read RCREG. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Shoebie
Joined: 07 Nov 2007 Posts: 5
|
|
Posted: Wed Nov 28, 2007 4:24 pm |
|
|
Thank you for the very quick reply.
This is my clear function:
Code: | void Read_Rx(void)
{
char u;
u=RCREG;
u=RCREG;
u=RCREG;
}
|
Somehow, after a printf, a character sneeks into the RCREG. Not all the time. I have to send a character, then RX. I don't have time to run Read_Rx or I might miss a character. Is there a safer function that fputc that I should consider using ? |
|
|
Guest
|
|
Posted: Thu Nov 29, 2007 3:33 am |
|
|
Seriously, have you tried putting a logic analyser/storage scope onto the serial receive line, and triggering it off the data being sent?. A character appearing in the receive buffer, really suggests that you may have some noise present on this line, that is just occasionally resulting in a 'low' being seen, and triggering a 'receive'. I really would suggest that you have a hardware issue, and that it should be rectified...
Best Wishes |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Thu Nov 29, 2007 3:55 am |
|
|
I would suggest using an RDA interrupt. This code may be unreliable if you are not using the hardware uart since it doesn't look like you're polling it fast enough. |
|
|
Shoebie
Joined: 07 Nov 2007 Posts: 5
|
|
Posted: Thu Nov 29, 2007 9:08 am |
|
|
Thanks for the help. Using the RDA interrupt cured the problem. Now I fputc, then clear rcreg. The interrupt captures the characters I need. |
|
|
|