CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

RCREG not empty

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Shoebie



Joined: 07 Nov 2007
Posts: 5

View user's profile Send private message

RCREG not empty
PostPosted: Wed Nov 28, 2007 3:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 28, 2007 4:09 pm     Reply with quote

See the clear_usart_receiver() function in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=1571&start=3

See the reset_uart() function in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=6649&start=1
Shoebie



Joined: 07 Nov 2007
Posts: 5

View user's profile Send private message

PostPosted: Wed Nov 28, 2007 4:24 pm     Reply with quote

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








PostPosted: Thu Nov 29, 2007 3:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Nov 29, 2007 3:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Nov 29, 2007 9:08 am     Reply with quote

Thanks for the help. Using the RDA interrupt cured the problem. Now I fputc, then clear rcreg. The interrupt captures the characters I need.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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