Jump to content
Sign in to follow this  
doozer

Software Vs Hardware Usart

Recommended Posts

Hi All,

 

I am currently writing some control software using a 16F628. It needs to communicate with a display module that expects TTL (0/5v) serial at 4800 baud, 8 data bits, 1 stop bit, inverted data - as can be seen here:

 

icurcuits website

 

For testing purposes I use the Olimex range of prototyping boards that have a MAX232 chip onboard.

 

Originall I used code such as this:

void SetupUSART(void) {
// rs232 communications setup
txsta=0;    // Transmit Status Register - init everything to 0
rcsta=0;    // Receive Status Register - init everything to 0
#ifdef CLOCK_4_MHZ
 set_bit(txsta,BRGH);    // High Baud Rate Select
 spbrg=51; 	 // 4MHz: 25=9600, 51=4800
#endif
#ifdef CLOCK_20_MHZ
 clear_bit(txsta,BRGH);    // Low Baud Rate Select
 spbrg=64; 	 // 20MHz: 64=4800
#endif
clear_bit(txsta,SYNC);  // SYNC=0;  // Async Mode
set_bit(rcsta,SPEN);    // SPEN=1;  // Serial Port Enable
set_bit(txsta,TXEN);    // Transmit Enable
set_bit(pie1,TXIE);     // Transmit Interrupt Enable
clear_bit(rcsta,CREN);    // Constant Receive disable
clear_bit(pie1,RCIE);     // Receive Interrupt disable
set_bit(intcon,PEIE);   // PEripherial Interrupt Enable
trisb = 00000110b;
return;
}

/** \brief Loads TXREG with data.

 This function can be used to send data using the PIC's AUSART.
 \param char to be loaded into TXREG.
*/
void txreg_putch( char c )
{
   if( ! test_bit(txsta, TXEN) ){
       set_bit(txsta, TXEN );
   }

   while( ! test_bit(pir1, TXIF) ){
       nop();
   }
   txreg=c;
}

Using Hyperterm on the PC set to 4800 8-N-1 it happily outputs chars. The problem is when I try to connect directly to the TTL device (no MAX232). I just end up with garbage on the screen.

 

To prove the device was working correctly I used Basic (Serout B2,n4800,["Hello world"]). This works fine.

 

I noticed that C2C has the TRUE_RS232 pragma. I have tried this set to 0 and 1, but it doesn't seem to make any difference to the .hex file generated. I suspect it only affects the built in software putchar() function.

 

My question (yes I know its rather long!) is how do I perform this "inverting" using a hardware USART, or have I missed something more fundamental?

 

Thanks,

Matt.

Share this post


Link to post
Share on other sites

Doozer,

 

I noticed that C2C has the TRUE_RS232 pragma. I have tried this set to 0 and 1, but it doesn't seem to make any difference to the .hex file generated. I suspect it only affects the built in software putchar() function.

 

 

Yes you are correct. The pragma only affects software generated serial comms.

 

When you remove the max 232 you lose the inversion of the signal, plus all the voltage levels will no longer be within the RS232 specification.

 

I would say don't do this - sounds like very bad practice.

 

If you do want to do this, then invert the signal using an external NOT gate.

 

If you are eventually going to connect two PICs like this then no problem, an invertor will be missing at both ends, so it will work.

 

Regards

Dave

Share this post


Link to post
Share on other sites
...

 

To prove the device was working correctly I used Basic (Serout B2,n4800,["Hello world"]).  This works fine.

....

 

My question (yes I know its rather long!) is how do I perform this "inverting" using a hardware USART, or have I missed something more fundamental?

 

You cannot "invert" when using the hardware UART - in this case you need to do the inversion external to the PIC in hardware. If the target device uses TTL levels for the serial interface then all you need is one gate of an inverter. Alternatively use a software uart (bit bashing) in this case you can invert in software

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...