Improve my code for software UART on a PIC

Status
Not open for further replies.

BD

Member level 1
Joined
Oct 14, 2004
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
321
I wish to implement a sw uart on a pic.
Here is a snippet of the code i am using.

Could anyone explain why 13 and 3 are used in the DLY and TX_OHEAD.

Also why is c bitwise 'orred' with 0x80
"c = (c >> 1) | 0x80;"


#define XTAL 4000000

/* Baud rate */

#define BRATE 9600

#define DLY 3 /* cycles per null loop */
#define TX_OHEAD 13 /* overhead cycles per loop */

#define DELAY(ohead) (((XTAL/4/BRATE)-(ohead))/DLY)

void
putch(char c)
{
unsigned char dly, bitno;

bitno = 11;

INIT_PORT;
TxData = 0; /* start bit */
bitno = 12;
do {
dly = DELAY(TX_OHEAD); /* wait one bit time */
do
/* nix */ ;
while(--dly);
if(c & 1)
TxData = 1;
if(!(c & 1))
TxData = 0;
c = (c >> 1) | 0x80;
} while(--bitno);
}

has anyone got a better routine? that works..
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…