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.

See also:


file: /Techref/language/ccpp/bitbangserial.htm, 1KB, , updated: 2016/12/1 15:38, local time: 2017/11/23 06:58,
TOP NEW HELP FIND: 
54.196.127.107:LOG IN

 ©2017 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://www.piclist.com/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 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?

  PICList 2017 contributors:
o List host: MIT, Site host massmind.org, Top posters @20171123 RussellMc, Van Horn, David, James Cameron, Sean Breheny, alan.b.pearce, IVP, Neil, David C Brown, Bob Blick, Denny Esterline,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
 

Welcome to www.piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .