UART Remappable Library

UART Remappable hardware module is available with these MCUs: 18F2xJ11, 18F4xJ11, 18F2xJ50 and 18F4xJ50. mikroBasic PRO for PIC UART Remappable Library provides comfortable work with the Asynchronous (full duplex) mode.

You can easily communicate with other devices via RS-232 protocol (for example with PC, see the figure at the end of the topic – RS-232 HW connection). Then, simply use the functions listed below.

  Important : Before using this library, make sure that Peripheral Pin Select Library and Uart Library are checked in the Library Manager, and that appropriate pins were mapped.

Library Dependency Tree

UART Remappable Library Dependency Tree

Library Routines

UART_Remappable_Init

Prototype

sub procedure UART_Remappable_Init(const baud_rate as longint)

Returns

Nothing.

Description

Initializes hardware UART Remappable module with the desired baud rate. Refer to the device data sheet for baud rates allowed for specific Fosc. If you specify the unsupported baud rate, compiler will report an error.

Parameters :

  • baud_rate: requested baud rate

  Note : Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.

Refer to the device data sheet for baud rates allowed for specific Fosc.

Requires

You'll need PIC MCU with hardware UART module and remappable feature.

UART_Remappable_Init needs to be called before using other functions from UART Remappable Library.

  Note : Calculation of the UART baud rate value is carried out by the compiler, as it would produce a relatively large code if performed on the libary level.
Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.
Example

This will initialize hardware UART module and establish the communication at 2400 bps:

UART_Remappable_Init(2400)

UART_Remappable_Data_Ready

Prototype

sub function UART_Remappable_Data_Ready() as byte

Returns

Function returns 1 if data is ready or 0 if there is no data.

Description

The function tests if data in receive buffer is ready for reading.

Requires

MCU with the UART module and remappable feature.

The UART module must be initialized before using this routine. See the UART_Remappable_Init routine.

Example
dim receives as byte
...
' read data if ready
if (UART_Remappable_Data_Ready() = 1) then
  receive = UART_Remappable_Read()
end if

UART_Remappable_Tx_Idle

Prototype

sub function UART_Remappable_Tx_Idle() as byte

Returns

  • 1 if the data has been transmitted
  • 0 otherwise

Description

Use the function to test if the transmit shift register is empty or not.

Requires

UART HW module must be initialized and communication established before using this function. See UART_Remappable_Init.

Example
' If the previous data has been shifted out, send next data:
if (UART_Remappable_Tx_Idle() = 1) then
  UART1_Write(_data)
end if

UART_Remappable_Read

Prototype

sub function UART_Remappable_Read() as byte

Returns

Received byte.

Description

The function receives a byte via UART. Use the UART_Remappable_Data_Ready function to test if data is ready first.

Requires

MCU with the UART module and remappable feature.

The UART module must be initialized before using this routine. See UART_Remappable_Init routine.

Example
dim receive as byte
...
' read data if ready
if (UART_Remappable_Data_Ready() = 1) then
  receive = UART_Remappable_Read()
end if

UART_Remappable_Read_Text

Prototype

sub procedure UART1_Read_Text(dim byref Output as string[255], dim byref Delimiter as string[10], dim Attempts as byte)

Returns

Nothing.

Description

Reads characters received via UART until the delimiter sequence is detected. The read sequence is stored in the parameter output; delimiter sequence is stored in the parameter delimiter.

This is a blocking call: the delimiter sequence is expected, otherwise the procedure exits( if the delimiter is not found). Parameter Attempts defines number of received characters in which Delimiter sequence is expected. If Attempts is set to 255, this routine will continuously try to detect the Delimiter sequence.

Requires

UART HW module must be initialized and communication established before using this function. See UART_Remappable_Init.

Example

Read text until the sequence “OK” is received, and send back what’s been received:

UART_Remappable_Init(4800)                            ' initialize UART module
Delay_ms(100)

while TRUE 
  if (UART_Remappable_Data_Ready() = 1) then          ' if data is received 
    UART_Remappable_Read_Text(output, 'OK', 10)       ' reads text until 'OK' is found
    UART_Remappable_Write_Text(output)                ' sends back text 
  end if
wend

UART_Remappable_Write

Prototype

sub procedure UART_Remappable_Write(dim TxData as byte)

Returns

Nothing.

Description

The function transmits a byte via the UART module.

Parameters :

  • TxData: data to be sent

Requires

MCU with the UART module and remappable feature.

The UART module must be initialized before using this routine. See UART_Remappable_Init routine.

Example
dim data_ as byte
...
data = 0x1E
UART_Remappable_Write(data_)

UART_Remappable_Write_Text

Prototype

sub procedure UART_Remappable_Write_Text(dim byref uart_text as string)

Returns

Nothing.

Description

Sends text (parameter uart_text) via UART. Text should be zero terminated.

Requires

UART HW module must be initialized and communication established before using this function. See UART_Remappable_Init.

Example

Read text until the sequence “OK” is received, and send back what’s been received:

UART_Remappable_Init(4800)                         ' initialize UART module
Delay_ms(100)

while TRUE
  if (UART_Remappable_Data_Ready() = 1) then       ' if data is received 
    UART_Remappable_Read_Text(output, 'OK', 10)    ' reads text until 'OK' is found
    UART_Remappable_Write_Text(output)             ' sends back text 
  end if
wend
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