Software UART Library

The mikroC PRO for PIC 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.

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

Library Routines

Soft_UART_Init

Prototype

char Soft_UART_Init(char *port, char rx_pin, char tx_pin, unsigned long baud_rate, char inverted);

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

This will initialize software UART and establish the communication at 14400 bps:

char error;
...
error = Soft_UART_Init(&PORTC, 7, 6, 14400, 0);   // Initialize Soft UART at 14400 bps

Soft_UART_Read

Prototype

char Soft_UART_Read(char * error);

Returns

Byte received via UART.

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.

Requires

Software UART must be initialized before using this function. See the Soft_UART_Init routine.

Example
char data_, error;
...
// wait until data is received
do
  data = Soft_UART_Read(&error);
while (error);

// Now we can work with data:
if (data_) {...}

Soft_UART_Write

Prototype

void Soft_UART_Write(char udata);

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
char some_byte = 0x0A;
...
// Write a byte via Soft UART
Soft_UART_Write(some_byte);

Soft_UART_Break

Prototype

void Soft_UART_Break();

Returns

Nothing.

Description

Soft_UART_Read is blocking routine and it can block the program flow. Calling this routine from the interrupt will 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
char data1, error, counter = 0;

void interrupt() {

  if (INTCON.T0IF) {
    if (counter >= 20) {
      Soft_UART_Break();
      counter = 0;              // reset counter
    }
    else
      counter++;                // increment counter

    INTCON.T0IF = 0;            // Clear Timer0 overflow interrupt flag

    }
}

void main() {

  OPTION_REG = 0x04;            // TMR0 prescaler set to 1:32

  ...

  if (Soft_UART_Init(&PORTC, 7, 6, 9600, 0) == 0)
    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

}

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 mikroC PRO for PIC USART Terminal Tool.

Copy Code To ClipboardCopy Code To Clipboard
char i, error, byte_read;                 // Auxiliary variables

void main(){

  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) {
    PORTB = error;                        // Signalize Init error
    while(1) ;                            // Stop program
  }
  Delay_ms(100);

  for (i = 'z'; i >= 'A'; i--) {          // Send bytes from 'z' downto 'A'
    Soft_UART_Write(i);
    Delay_ms(100);
  }
   
  while(1) {                              // Endless loop
    byte_read = Soft_UART_Read(&error);   // Read byte, then test error flag
    if (error)                            // If error was detected
      PORTB = error;                      //   signal it on PORTB
    else
      Soft_UART_Write(byte_read);         // If error was not detected, return byte read
    }      
}
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