Software UART Library
The mikroBasic PRO for 8051 provides routines for implementing 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.
Note: The Software UART library implements time-based activities, so interrupts need to be disabled when using it.
External dependencies of Software UART Library
| The following variables must be defined in all projects using Software UART Library: | Description: | Example : |
|---|---|---|
dim Soft_Uart_RX as sbit bdata sfr external |
Receive line. | dim Soft_Uart_RX as sbit at P3_0_bit |
dim Soft_Uart_TX as sbit bdata sfr external |
Transmit line. | dim Soft_Uart_TX as sbit at P3_1_bit |
Library Routines
Soft_UART_Init
| Prototype |
sub function Soft_UART_Init(dim baud_rate as longword, dim inverted as byte) as word |
|---|---|
| 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 |
Global variables:
|
| Example |
' Initialize Software UART communication on pins Rx, Tx, at 9600 bps Soft_UART_Init(9600, 0) |
Soft_UART_Read
| Prototype |
sub function Soft_Uart_Read(dim byref error as byte) as byte |
|---|---|
| Returns |
Byte received via UART. |
| Description |
The function receives a byte via software UART. This is a blocking function call (waits for start bit). Parameters :
|
| Requires |
Software UART must be initialized before using this function. See the Soft_UART_Init routine. |
| Example |
dim data as byte
error as byte
...
' wait until data is received
do
data = Soft_Uart_Read(error)
loop until (error = 0)
' Now we can work with data:
if (data) then
...
end if
|
Soft_UART_Write
| Prototype |
sub procedure Soft_Uart_Write(dim udata 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 |
dim some_byte as byte ... ' Write a byte via Soft Uart some_byte = 0x0A Soft_Uart_Write(some_byte) |
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. Note: Interrupts should be disabled before using Software UART routines again (see note at the top of this page). |
| Requires | Nothing. |
| Example |
dim data1, error, counter as byte
sub procedure Timer1InterruptHandler() org IVT_ADDR_ET1
counter = 0
if (counter >= 20) then
Soft_UART_Break()
counter = 0 ' reset counter
else
Inc(counter) ' increment counter
end if
end sub
main:
TR1_bit = 0 ' Stop Timer1
ET1_bit = 1 ' Enable Timer1 interrupt
TH1 = 0x00 ' Set Timer1 high byte
TL1 = 0x00 ' Set Timer1 low byte
TR1_bit = 1 ' Run Timer1
EA_bit = 0 ' Interrupt disable
...
Soft_UART_Init(9600)
Soft_UART_Write(0x55)
...
' try Soft_UART_Read with blocking prevention mechanism
EA_bit = 1 ' Interrupt enable
data1 = Soft_UART_Read(&error)
EA_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 8051 USART Terminal Tool.
program Soft_UART
' Soft UART connections
dim Soft_Uart_RX as sbit at P3_0_bit
dim Soft_Uart_TX as sbit at P3_1_bit
' End Soft UART connections
dim counter, error_, byte_read as byte ' Auxiliary variables
main:
P2 = 0 ' P2 is used for error signalization as No error
error_ = Soft_UART_Init(4800, 0) ' Initialize Soft UART at 4800 bps
if (error_ > 0) then
P2 = 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
P2 = 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!



