// 11.0592MHz XTAL #include "sdccF000.h" //register definitions #define TRUE 1 #define FALSE 0 #define TX2 P0_6 //bit bashed UART2 TX #define RX2 P0_7 //bit bashed UART2 RX //Prototypes void config_mcu(void); void UART1_TX(char character); void UART1_send_string(unsigned char string[]); //Global variables int test = 0; int i = 0; int k = 0; unsigned char l; unsigned int j; char n = 0; __bit UART1_sample = FALSE; unsigned char bit_mask = 0x01; void main(void) { //setup the MCU's registers config_mcu(); UART1_send_string("Hello World!"); //send a message out our bit bashed 9600 UART while(TRUE) { if (RI == 1) { UART1_TX(SBUF); //send what ever we receive on the built in UART to our bit bashed UART RI = 0; } } } void config_mcu() { int i = 0; //Disable the WatchDog WDTCN = 0xDE; WDTCN = 0xAD; //UART_init // setup the UART for 4800, 8, N, 1 SCON = 0x52; CKCON = 0x24; T2CON = 0x34; RCAP2H = 0xFF; RCAP2L = 0xB8; TH2 = 0xFF; TL2 = 0xB8; //Timer 0 - use for 9600 baud bit-bashed UART TMOD = 0x01; TCON = 0x10; //enable timer0 TH0 = 0xFF; //these valuse give a 4.8kHz clock which gives a 9600 baud rate TL0 = 0xA1; //port_IO_init // P0.0 - SCL (I2C), Open-Drain // P0.1 - SDA (I2C), Open-Drain // P0.2 - TX (UART), Push-Pull // P0.3 - RX (UART), Push-Pull XBR0 = 0x05; //UART assigned to Port0.2 and 0.3 XBR1 = 0x00; XBR2 = 0x40; //Weak pull ups enabled for Open Drain outputs PRT0CF = 0xFC; PRT1CF = 0xF0; //0-3 Open-Drain, 4-7 Push-Pull PRT2CF = 0xFF; PRT3CF = 0xFF; //oscillator_init OSCXCN = 0x67; for (i = 0; i < 3000; i++); // Wait 1ms for initialization while ((OSCXCN & 0x80) == 0); OSCICN = 0x08; //interrupts_init IE = 0x82; IP = 0x02; //Timer 0 high priority } void UART1_send_string(unsigned char string[]) { l = 0; while (string[l] != 0x00) { UART1_TX(string[l]); l++; } l = 0; } void UART1_TX(char character) { //convert character to serial, and send it out TX2 //This routine is, basically, a parallel to serial converter //Characters are sent LSB first, 9600 baud bit_mask = 0x01; // Send a start bit - first we'll keep the line idle for one more clock cycle - I found this stops glitches //I was getting occasionally with the first byte sent out while (UART1_sample == FALSE); TX2 = TRUE; UART1_sample = FALSE; //Now we'll start... This is the start bit while (UART1_sample == FALSE); TX2 = FALSE; UART1_sample = FALSE; //send the byte - LSB first for (k = 0; k < 8; k++) { // determine whether to put a 0 or 1 on to TX2 while (UART1_sample == FALSE); if ((character & bit_mask) > 0) TX2 = TRUE; else TX2 = FALSE; UART1_sample = FALSE; // Shift bit_mask right 1 bit 0x01->0x02->0x04->0x08->0x10->0x20->0x40->0x80 bit_mask = bit_mask << 1; } //send a stop bit while (UART1_sample == FALSE); TX2 = TRUE; UART1_sample = FALSE; } /********** INTERRUPT ROUTINES ****************/ void Timer0_Isr (void) __interrupt (1) //interrupt every 418uS - { //TX2 = !TX2; //used to test the timer period is correct - if it is correct, you'll see a whole bunch of "U" characters in your terminal window UART1_sample = TRUE; TH0 = 0xFF; TL0 = 0xA1; }