Software UART Library

mikroBasic 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.

Important : The Software UART library implements time-based activities, so interrupts need to be disabled when using it.

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

  • 2 - error, requested baud rate is too low
  • 1 - error, requested baud rate is too high
  • 0 - successful initialization

Description

Configures and initializes the software UART module.

Parameters :

  • port: port to be used.
  • rx_pin: sets rx_pin to be used.
  • tx_pin: sets tx_pin to be used.
  • baud_rate: baud rate to be set. Maximum baud rate depends on the MCU’s clock and working conditions.
  • inverted: inverted output flag. When set to a non-zero value, inverted logic on output is used.

Software UART routines use Delay_Cyc routine. If requested baud rate is too low then calculated parameter for calling Delay_Cyc exceeds Delay_Cyc argument range.

If requested baud rate is too high then rounding error of Delay_Cyc argument corrupts Software UART timings.

Requires

Nothing.

Example
dim error as 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

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 :

  • error: Error flag. Error code is returned through this variable.

  • 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 :

  • udata: data to be sent.

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.

  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 interrupt()
  if (INTCON.T0IF <> 0) then
    if (counter >= 20) then
      Soft_UART_Break()
      counter = 0           ' reset counter
    end if
    else
      Inc(counter)          ' increment counter
    INTCON.T0IF = 0         ' Clear Timer0 overflow interrupt flag
  end if
end sub

main:
  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)
  end if

  ...

  ' 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

The example demonstrates simple data exchange via software UART. When PIC MCU receives data, it immediately sends the same data back. If PIC is connected to the PC (see the figure below), you can test the example from mikroBasic PRO for PIC terminal for RS232 communication, menu choice Tools › Terminal.

Copy Code To ClipboardCopy Code To Clipboard
program Soft_UART


dim error_flag as byte
    counter, byte_read as byte           ' Auxiliary variables
main:

  ANSEL  = 0                             ' Configure AN pins as digital I/O
  ANSELH = 0

  TRISB = 0x00                           ' Set PORTB as output (error signalization)
  PORTB = 0                              ' No error
  VDelay_ms(370)
  error_flag = Soft_UART_Init(PORTC, 7, 6, 14400, 0) ' Initialize Soft UART at 14400 bps
  if (error_flag > 0) then
      PORTB = error_flag                 ' 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_flag)    ' Read byte, then test error flag
      if (error_flag <> 0) then                 ' If error was detected
        PORTB = error_flag                      '   signal it on PORTB
      else
        Soft_UART_Write(byte_read)              ' If error was not detected, return byte read
      end if
    wend
end.
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code