385
The header file also states the communication baud rate and number of bits:
#define baudrate 4800
#define no_of_bits 8
#define one_bit_delay (1000000 / baudrate)
#define half_bit_delay (one_bit_delay / 2)
Based on the baud rate further timing infos are calculated. Software UART is not as reliable as
hardware UART and so it is better to use low baud rates. It is even better if it can be skipped. However,
when there is no other option or when there is a need for additional UART, it must be used.
The UART transmit and receive functions are written using polling methods. External digital I/O
interrupt can be used for receiving data. These functions are created just by studying the signal
patterns and using the same tactics as with other software communication libraries. The trick is to
emulate/receive the signals as a real hardware would do.
void SW_UART_transmit(unsigned char value)
{
unsigned char bits = 0;
SW_UART_TXD_OUT_LOW();
delay_us(one_bit_delay);
for(bits = 0; bits < no_of_bits; bits++)
{
if((value >> bits) & 0x01)
{
SW_UART_TXD_OUT_HIGH();
}
else
{
SW_UART_TXD_OUT_LOW();
}
delay_us(one_bit_delay);
};
SW_UART_TXD_OUT_HIGH();
delay_us(one_bit_delay);
}
unsigned char SW_UART_receive(void)
{
unsigned char bits = 0;
unsigned char value = 0;
while(SW_UART_RXD_INPUT());
delay_us(one_bit_delay);
delay_us(half_bit_delay);
for(bits = 0; bits < no_of_bits; bits++)
{
if(SW_UART_RXD_INPUT())
{
value += (1 << bits);
}