Software UART Library
mikroPascal PRO for PIC 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 |
function Soft_UART_Init(var port : byte; rx_pin, tx_pin, baud_rate : dword, inverted : byte): 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 |
var error : byte; ... // Initialize Software UART communication on pins Rx, Tx, at 14400 bps error := Soft_UART_Init(PORTC, 7, 6, 14400, 0); |
Soft_UART_Read
Prototype |
function Soft_UART_Read(var error: byte): 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: var data : byte; error : byte; ... // wait until data is received repeat data_ := Soft_UART_Read(error); until (error=0); // Now we can work with data: if ( data ) then begin ... end |
Soft_UART_Write
Prototype |
procedure Soft_UART_Write(udata: 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 |
var some_byte : byte; ... // Write a byte via Soft UART some_byte := 0x0A; Soft_UART_Write(some_byte); |
Soft_UART_Break
Prototype |
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 |
var data1, error, counter : byte; procedure interrupt(); begin if (INTCON.T0IF <> 0) then if (counter >= 20) then begin Soft_UART_Break(); counter := 0; // reset counter end else Inc(counter); // increment counter INTCON.T0IF := 0; // Clear Timer0 overflow interrupt flag end; begin counter := 0; OPTION_REG := 0x04; // TMR0 prescaler set to 1:32 ... if (Soft_UART_Init(PORTC, 7, 6, 9600, 0) = 0) then Soft_UART_Write(0x55); ... // try Soft_UART_Read with blocking prevention mechanism INTCON.GIE := 1; // Global interrupt enable INTCON.T0IE := 1; // Enable Timer0 overflow interrupt data1 := Soft_UART_Read(error); INTCON.GIE := 0; // Global 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 mikroPascal PRO for PIC USART Terminal Tool.
program Soft_UART; var error : byte; counter, byte_read : byte; // Auxiliary variables begin ANSEL := 0; // Configure AN pins as digital I/O ANSELH := 0; TRISB := 0x00; // Set PORTB as output (error signalization) PORTB := 0; // No error error := Soft_UART_Init(PORTC, 7, 6, 14400, 0); // Initialize Soft UART at 14400 bps if (error > 0) then begin PORTB := error; // Signalize Init error while (TRUE) do nop; // Stop program end; Delay_ms(100); for counter := 'z' downto 'A' do // Send bytes from 'z' downto 'A' begin Soft_UART_Write(counter); Delay_ms(100); end; while TRUE do // Endless loop begin 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; end.
What do you think about this topic ? Send us feedback!