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

RS232 reception on PIC18F (SOLVED)

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



Joined: 04 May 2007
Posts: 4

View user's profile Send private message

RS232 reception on PIC18F (SOLVED)
PostPosted: Tue Aug 25, 2009 10:09 am     Reply with quote

Hi,

I have trouble receiving data using the USART on a PIC18F1320.

Main routine:
Code:

   char string[30];

   for(;;)
   {
      fgets(string, ser);       
      fprintf(ser, "%s", string);
   }


Setup
Code:

#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_B1,rcv=PIN_B4,bits=8,stop=1,stream=ser)


Often when I transmit a string it will miss the end of line (and mix up other characters as well). The next time I transmit the string the result looks like this on a logic analyzer:


The baud-rate seems to be very exact to 9600bps when measuring using the logic analyzer. I measure directly on the PIC pins.

Transmission from the PIC is always perfect. Only reception fails.


Solution to the mystery:
The hardware USART configures the RX pin as Schmitt Trigger and requires >4.0V. The output from the RS232 level converter barely gives out 4.0V for a logic one for some reason.


Last edited by strombom on Thu Aug 27, 2009 1:41 am; edited 2 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 25, 2009 10:56 am     Reply with quote

Try a more simple test program. See if it works. If it doesn't work,
post your compiler version. Also post a description of your hardware.
Code:
#include <18F1320.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B4, ERRORS)

//======================================
void main(void)
{
int8 c;

while(1)
  {
   c = getc();
   putc(c);
  }
 
}
strombom



Joined: 04 May 2007
Posts: 4

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 3:30 am     Reply with quote

Hi,

Unfortunately that code does not make any difference. Still there are often this type of errors:

The first byte is always correct but some of the last ones usually have errors.

When I change to software uart however (FORCE_SW), it works perfectly.

The hardware consists of a PIC18F1320 running from a 20MHz crystal, I have stripped it down to a minimum, I measure directly on TX/RX pins. The TX/RX go through a MAX232 level converter and then the PC serial port. The 5V power supply is very clean.

PCH version 4.093

/Johan
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 6:32 am     Reply with quote

You don't perform any additional, interrupt base action while echoing the UART data? You can clearly see, that the receiver looses part of the UART bitstream but reproduces it correctly from the erroneously assumed start bit, it's the case in both tests.

If no interrupts are disturbing the reception, it may be the case, that the receiver assumes an overrun error and restarts the reception. You may want to check (if you already didn't) the Rx signal directly at the processor pin, also if it has a correct logic level. The logic analyzer must not necessarily use the same threshold as the PIC.
Ttelmah
Guest







PostPosted: Wed Aug 26, 2009 7:14 am     Reply with quote

Interrupt actions would not cause a mid character break in the data on the hardware UART.

I must admit, I'd be most suspicious of the second proposed idea, the signal level.

RB4, uses TTL signal levels, _when used as a conventional input_, so would use these levels when you program a software UART,explaining why this works. It uses Schmitt input levels, when you use the hardware UART. try the simple expedient, of adding a 2K2 resistor, from this pin, to +5v. If it starts working, you have found the problem. Then, look at your MAX232, and check the version. The original MAX232, only warranted TTL outputs. The latter MAX232A, has CMOS outputs, which provided they are only driving a single input, should easily meet the Schmitt input thresholds.
If your analyser supports variable input thresholds try at 0.8*Vcc, and see if it now duplicates the problem.
Obviously then investigate the wiring to this pin. One 'problem' with logic analysers, is that if a signal does not cross their threshold, you will see lovely 'square' edges and tops, even with a signal that is perhaps not making good contact....

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 8:43 am     Reply with quote

Quote:
Interrupt actions would not cause a mid character break in the data on the hardware UART.
Yes. Also an overrun can't occur at this time.
strombom



Joined: 04 May 2007
Posts: 4

View user's profile Send private message

PostPosted: Thu Aug 27, 2009 1:40 am     Reply with quote

Ttelmah: You were right about the level problem.

It seems the hardware USART configures the RX pin as Schmitt Trigger and requires >4.0V. The output from the RS232 level converter barely gives out 4.0V for a logic one for some reason. I have no more time to look into this, but using software rs232 solved the problem so I stuck with that.
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