| ??? 01/24/03 09:40 Read: times |
#37068 - Software UART 9600 baud in C |
Hi,
I was always thinking, that a software UART in C on the 8051 was impossible. Now I needed a transmitter on the AT89C2051, but already all 3 timers are in use. So I tried it and must revise my former opinion: <pre> /************************************************************************/ /* */ /* Software UART transmitter */ /* */ /* Author: Peter Dannegger */ /* danni@specs.de */ /* */ /************************************************************************/ #include < reg51.h > #define XTAL 24e6 #define BITWAIT (uchar)(XTAL / 12.0 / 19200 + 0.5) // Attention !!! // watch this condition: // at max interrupt time = 35 cycle: 255 - 35 - 7 > BITWAIT > 35 + 7 #define BITSLICE 2 // 9600 baud //#define BITSLICE 16 // 1200 baud sbit STXD = P3^5; char putchar( char c ) { unsigned char i, j, t; t = TH0; // T0 in mode 3 ! STXD = 0; // 0 = start bit for( i = 10; i; i-- ){ for( j = BITSLICE; j; j-- ){ // wait bit time while( TH0 - t < BITWAIT ); // need 7 cycle t += BITWAIT; } STXD = c & 1; // send bit c = (unsigned char)c >> 1 | 0x80; // shift 1 in } return c; } Peter |
| Topic | Author | Date |
| Software UART 9600 baud in C | 01/01/70 00:00 | |
RE: Software UART 9600 baud in C | 01/01/70 00:00 |



