Electronic – Bit Banging UART

cmicrocontrollerpicserialuart

My full code is located here.

Basically, I'm transmitting to an Arduino, and I'm not receiving the correct value (you'll notice I'm trying to send d'22'). Here's the method where I actually bit bang the transmission:

// Method for transmitting value using serial bit banging
void uart_tx_bit_bang(unsigned char val) {
    unsigned char i;
    Tx_Pin = 0;                         // Start bit
    uart_time_delay();
    for ( i = 8 ; i != 0 ; --i ) {
        if (val & 0x01) Tx_Pin = 1;   // Begin with LSB
        else            Tx_Pin = 0;
        val >>= 1;
        uart_time_delay();
        }
    Tx_Pin = 1;                         // Stop bit
    uart_time_delay();
}

Since the delay should be 1/baud, uart_time_delay() should be a 104us delay. I'm using __delay_us(104) from the PIC libraries. Any help on this is greatly appreciated.

I'm positive that I'm using the correct baud rate on both ends.

Best Answer

While your calculation of 104uS is correct for 9600BPS your loop and the various operations it performs will be adding an additional delay. There are a few ways you could go about tuning the timing:

  • Subtract a constant from your uS delay until it starts working. It'd probably be best to determine the minimum / maximum number it works with and pick the middle value.

  • Do something similar using a scope to check the final timing if you have one available.

  • Look at the assembler output from the compiler and determine how many cycles the loop takes.

I also see you're using the RC clock. I normally like to keep my serial timing within 2% for reliable operation so also check to make sure the part has that much stability when using the RC clock for reliable operation.