1. I have a application in which uart is done by software since it has only one uart & it is used for another purpose.
MCU is 16Mhz with 62.5ns instruction time. Goal is to continuously receive data on software uart at 9600 baud & transmit it on hardware uart at 57600 baud.
2. Below is code for above task. Delay I have made from timer to be as accurate as possible.
3. Is there any way I can best optimize code for this task. One thing I am tryign to do is to move tranmit section on hardware uart in interrupt to have least possible time in main.
4. Is this code ok for software uart?
MCU is 16Mhz with 62.5ns instruction time. Goal is to continuously receive data on software uart at 9600 baud & transmit it on hardware uart at 57600 baud.
2. Below is code for above task. Delay I have made from timer to be as accurate as possible.
3. Is there any way I can best optimize code for this task. One thing I am tryign to do is to move tranmit section on hardware uart in interrupt to have least possible time in main.
4. Is this code ok for software uart?
Code:
#define SYSTEM_FREQ_HZ 16000000
#define BAUD_RATE 9600
#define DELAY_US_1_5_BIT ((16000000 / 9600) * 1.5) //approx 2500us
#define DELAY_US_1_0_BIT ((16000000 / 9600) * 1.0) //approx 1666.67us
#define DELAY_US_0_5_BIT ((16000000 / 9600) * 0.5) //approx 833.33us
void rx_byte(void)
{
uint8_t data_val;
uint8_t cnt;
uint8_t err;
/* pin as input pull up */
RX_PIN_INPUT_PULLUP();
while(1)
{
data_val = 0U;
err = 0U;
/* while pin is high */
while(1U == READ_PIN());
/* 1.5 bit delay */
delay(DELAY_US_1_5_BIT);
/* get byte */
for(cnt = 0U ; cnt < 8U ; cnt++)
{
if(1U == READ_PIN())
{
data_val = data_val | (1U << cnt);
}
/* 1 bit delay */
delay(DELAY_US_1_0_BIT);
}
/* stop bit error low */
if(0U == READ_PIN())
{
err = 1U;
}
/* 0.5 bit delay */
delay(DELAY_US_0_5_BIT);
/* send byte on another uart */
if(0U == err)
{
uart_tx_data(data_val);
}
}
}