BD
Member level 1
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..
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..