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 15:38, local time: 2024/12/22 18:17,
18.119.119.191: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? <A HREF="http://techref.massmind.org/techref/language/ccpp/bitbangserial.htm"> C Serial I/O Software</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to techref.massmind.org! |
.