please dont rip this site

C Serial I/O Software

Here is the simplest code I've found for doing "bit bang" (software only) serial communications in C:

#define BITFIX 1 //fudge to account for extra cycles between delays
#define BITTIME ((CPUHZ / BAUD) - BITFIX) // time needed to send bit
UARTTX_TRIS = 0;
UARTRX_TRIS = 1;

void send_serial_byte(unsigned char data) {
  unsigned char i;
  i=8;                   // 8 data bits to send
  UARTTX_IO = 1;         // send start bit
  DelayMs(BITTIME);
  while(i!=0) {          // send 8 serial bits, LSB first
    if (data & 0x01)
      UARTTX_IO = 1;
    else
      UARTTX_IO = 0;
    data >>= 1;          // rotate left to get next bit
    i--;
    DelayMs(BITTIME);
    }
  UARTTX_IO = 0;
  DelayMs(BITTIME);
  }


You have to play around with FIX until the timing works right. This compensates for the delay introduced by the instructions between the delays. If the processor is very fast, it will be 0 or 1. If it's slow, the value may increase and you may not be able to get it to work at all.

And you have to define or find:

There needs to be a delay routine you can call with millisecond precision. The receive routine in the same except it reads UARTRX_IO and shifts in a 0 or a 1 depending.


file: /Techref/language/ccpp/bitbangserial.htm, 1KB, , updated: 2016/12/1 16:38, local time: 2024/3/28 04:30,
TOP NEW HELP FIND: 
3.238.62.124:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/Techref/language/ccpp/bitbangserial.htm"> C Serial I/O Software</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .