Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top