Software UART Library
mikroBasic PRO for AVR provides library which implements Software UART communication. These routines are hardware independent and can be used with any MCU. The Software UART Library provides easy communication with other devices via the RS232 protocol.
![]() |
Library Routines
Soft_UART_Init
Prototype |
sub procedure Soft_UART_Init(dim byref port as byte, dim rx_pin, tx_pin, baud_rate, inverted as byte) as byte |
---|---|
Returns |
|
Description |
Configures and initializes the software UART module. Parameters :
Software UART routines use Delay_Cyc routine. If requested baud rate is too low then calculated parameter for calling If requested baud rate is too high then rounding error of |
Requires |
Nothing. |
Example |
dim error as byte ... ' Initialize Software UART communication on pins Rx, Tx, at 14400 bps error = Soft_UART_Init(PORTB, 1, 2, 9600, 0) |
Soft_UART_Read
Prototype |
sub function Soft_UART_Read(dim byref error as byte) as byte |
---|---|
Returns |
Returns a received byte. |
Description |
The function receives a byte via software UART. This is a blocking function call (waits for start bit). Programmer can unblock it by calling Soft_UART_Break routine. Parameters :
Values : |
Requires |
Software UART must be initialized before using this function. See the Soft_UART_Init routine. |
Example |
Here’s a loop which holds until data is received: error = 1 do data = Soft_UART_Read(error) loop until error = 0 |
Soft_UART_Write
Prototype |
sub procedure Soft_UART_Write(dim data as byte) |
---|---|
Returns |
Nothing. |
Description |
This routine sends one byte via the Software UART bus. Parameters :
|
Requires |
Software UART must be initialized before using this function. See the Soft_UART_Init routine. Be aware that during transmission, software UART is incapable of receiving data – data transfer protocol must be set in such a way to prevent loss of information. |
Example |
Soft_UART_Write($0A) |
Soft_UART_Break
Prototype |
sub procedure Soft_UART_Break() |
---|---|
Returns |
Nothing. |
Description |
Soft_UART_Read is blocking routine and it can block the program flow. Call this routine from interrupt to unblock the program execution. This mechanism is similar to WDT. ![]() |
Requires | Nothing. |
Example |
dim data1, error_, counter as byte sub procedure Timer0Overflow_ISR() org 0x12 counter = 0 if (counter >= 20) then Soft_UART_Break() counter = 0 ' reset counter end if else Inc(counter) ' increment counter end if end sub main: TOIE0_bit = 1 ' Timer0 overflow interrupt enable TCCR0_bit = 5 ' Start timer with 1024 prescaler SREG_I_bit = 0 ' Interrupt disable ... Soft_UART_Init(9600) Soft_UART_Write(0x55) ... ' try Soft_UART_Read with blocking prevention mechanism SREG_I_bit = 1 ' Interrupt enable data1 = Soft_UART_Read(&error_) SREG_I_bit = 0 ' Interrupt disable ... end. |
Library Example
This example demonstrates simple data exchange via software UART. If MCU is connected to the PC, you can test the example from the mikroBasic PRO for AVR USART Terminal Tool.
program Soft_UART dim error_, counter, byte_read as byte ' Auxiliary variables main: DDRB = 0xFF ' Set PORTB as output (error signalization) PORTB = 0 ' No error error_ = Soft_UART_Init(PORTD,0,1,9600,0) ' Initialize Soft UART at 14400 bps if (error_ > 0) then PORTB = error_ ' Signalize Init error while TRUE nop ' Stop program wend end if Delay_ms(100) for counter = "z" to "A" step -1 ' Send bytes from 'z' downto 'A' Soft_UART_Write(counter) Delay_ms(100) next counter while TRUE ' Endless loop byte_read = Soft_UART_Read(error_) ' Read byte, then test error flag if (error_ <> 0) then ' If error was detected PORTB = error_ ' signal it on PORTB else Soft_UART_Write(byte_read) ' If error was not detected, return byte read end if wend end.
What do you think about this topic ? Send us feedback!