UART Library

The UART hardware module is available with a number of FT90x MCUs. The mikroBasic PRO for FT90x UART Library provides comfortable work with the Asynchronous (full duplex) mode.

  Important :

Library Routines

Generic Routines

UARTx_Init

Prototype

sub procedure UARTx_Init(dim baud_rate as longword)

Description

Configures and initializes the UART module.

The internal UART module module is set to :

  • 8-bit data, no parity bit.
  • 1 STOP bit.
Parameters
  • baud_rate: requested baud rate.
Returns

Nothing.

Requires

Routine requires the UART module.

Example
' Initialize hardware UART1 module and establish communication at 2400 bps
UART1_Init(2400)
Notes
  • Refer to the device data sheet for baud rates allowed for specific peripheral clock values.
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Switching between the UART modules in the UART library is done by the UART_Set_Active function (UART modules have to be previously initialized).
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

UARTx_Init_Advanced

Prototype

sub procedure UARTx_Init_Advanced(dim baud_rate as longword, dim data_bits as word, dim parity as word, dim stop_bits as word)

Description

Configures and initializes the UART module with user defined settings.

Parameters
  • baud_rate: Requested baud rate.
  • data_bits: Data bits selection parameter. Valid values :

    Data Bits
    Description Predefined library const
    5 data bits _UART_DATA_BITS_5
    6 data bits _UART_DATA_BITS_6
    7 data bits _UART_DATA_BITS_7
    8 data bits _UART_DATA_BITS_8
  • parity: Parity selection parameter. Valid values :

    Parity Mode
    Description Predefined library const
    No parity _UART_PARITY_NONE
    Even parity _UART_PARITY_EVEN
    Odd parity _UART_PARITY_ODD
    Parity bit is one _UART_PARITY_MARK
    Parity bit is zero _UART_PARITY_SPACE
  • stop_bits: Stop bit selection parameter. Valid values :

    Stop bits
    Description Predefined library const
    One stop bit _UART_STOP_BITS_1
    One and a half stop bits _UART_STOP_BITS_1_5
    Two stop bit _UART_STOP_BITS_2
Returns

Nothing.

Requires

Routine requires UART module.

Example
' Initialize hardware UART1 module at 115200 bps with 8 data, no parity and 1 stop bit :
UART1_Init_Advanced(115200, _UART_DATA_BITS_8, _UART_PARITY_NONE, _UART_STOP_BITS_1)
Notes
  • Refer to the device data sheet for baud rates allowed for specific peripheral clock values.
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Switching between the UART modules in the UART library is done by the UART_Set_Active function (UART modules have to be previously initialized).
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • For available working modes for a specific MCU please read the appropriate datasheet.

UARTx_Data_Ready

Prototype

sub function UARTx_Data_Ready() as word

Description

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

Parameters

None.

Returns
  • 1 if data is ready for reading.
  • 0 if there is no data in the receive register.
Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
dim receive as word
...
' read data if ready
if (UART1_Data_Ready() = 1) then
  receive = UART1_Read()
end if
Notes
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

UARTx_Tx_Idle

Prototype

sub function UARTx_Tx_Idle() as word

Description

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

Parameters

None.

Returns
  • 1 if the data has been transmitted.
  • 0 otherwise.
Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
' If the previous data has been shifted out, send next data:
if (UART1_Tx_Idle() = 1) then
  UART1_Write(_data)
end if
Notes
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

UARTx_Read

Prototype

sub function UARTx_Read() as word

Description

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

Parameters

None.

Returns

Received byte.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
dim receive as word
...
' read data if ready
if (UART1_Data_Ready() = 1) then 
  receive = UART1_Read()
end if
Notes
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

UARTx_Read_Text

Prototype

sub procedure UARTx_Read_Text(dim byref output, delimiter as string; dim Attempts as byte)

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 sub procedure exits (if the delimiter is not found).

Parameters
  • Output: received text.
  • Delimiter: sequence of characters that identifies the end of a received string.
  • 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.
Returns

Nothing.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example

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

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

 while TRUE
   if (UART1_Data_Ready() = 1)             ' if data is received   
     UART1_Read_Text(output, "OK", 10)  ' reads text until 'OK' is found
     UART1_Write_Text(output)              ' sends back text 
   end if
 wend.
Notes
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

UARTx_Write

Prototype

sub procedure UARTx_Write(dim data_ as word)

Description

The function transmits a byte via the UART module.

Parameters
  • _data: data to be sent.
Returns

Nothing.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
dim data_ as byte
...
data_ = 0x1E
UART1_Write(data_)
Notes
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

UARTx_Write_Text

Prototype

sub procedure UARTx_Write_Text(dim byref uart_text as string)

Description

Sends text via UART. Text should be zero terminated and is limited to 255 characters.

Parameters
  • UART_text: text to be sent.
Returns

Nothing.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example

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

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

 while TRUE
   if (UART1_Data_Ready() = 1)             ' if data is received   
     UART1_Read_Text(output, 'OK', 10)  ' reads text until 'OK' is found
     UART1_Write_Text(output)              ' sends back text 
   end if
 wend.
Notes
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 0 to 1.
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

UART_Set_Active

Prototype

sub procedure UART_Set_Active (dim read_ptr as ^TUART_Rd_Ptr, dim write_ptr as ^TUART_Wr_Ptr, dim ready_ptr as ^TUART_Rdy_Ptr, dim tx_idle_ptr as ^TUART_TX_Idle_Ptr)

Description

Sets active UART module which will be used by UARTx_Data_Ready, UARTx_Read and UARTx_Write routines.

Parameters

Parameters :

Returns

Nothing.

Requires

Routine is available only for MCUs with multiple UART modules.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
UART1_Init(9600)                    ' initialize UART1 module
UART2_Init(9600)                    ' initialize UART2 module

RS485Master_Init()                  ' initialize MCU as Master

UART_Set_Active(@UART1_Read, @UART1_Write, @UART1_Data_Ready, @UART1_Tx_Idle) ' set UART1 active
RS485Master_Send(dat,1,160)        ' send message through UART1

UART_Set_Active(@UART2_Read, @UART2_Write, @UART2_Data_Ready, @UART2_Tx_Idle) ' set UART2 active
RS485Master_Send(dat,1,160)        ' send through UART2
Notes

None.

UART_Data_Ready

Prototype

sub function UART_Data_Ready() as word

Description

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

This is a generic routine which uses the active UART module previously activated by the UART_Set_Active routine.

Parameters

None.

Returns
  • 1 if data is ready for reading.
  • 0 if there is no data in the receive register.
Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
dim receive as word
...
' read data if ready
if (UART_Data_Ready() = 1) then
  receive = UART_Read()
end if
Notes

None.

UART_Tx_Idle

Prototype

sub function UART_Tx_Idle() as word

Description

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

This is a generic routine which uses the active UART module previously activated by the UART_Set_Active routine.

Parameters

None.

Returns
  • 1 if the data has been transmitted.
  • 0 otherwise.
Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

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

None.

UART_Read

Prototype

sub function UART_Read() as word

Description

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

This is a generic routine which uses the active UART module previously activated by the UART_Set_Active routine.

Parameters

None.

Returns

Received byte.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
dim receive as word
...
' read data if ready
if (UART_Data_Ready() = 1) then 
  receive = UART_Read()
end if
Notes

None.

UART_Read_Text

Prototype

sub procedure UART_Read_Text(dim byref output, delimiter as string; dim Attempts as byte)

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 sub procedure exits (if the delimiter is not found).

This is a generic routine which uses the active UART module previously activated by the UART_Set_Active routine.

Parameters
  • Output: received text.
  • Delimiter: sequence of characters that identifies the end of a received string.
  • 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.
Returns

Nothing.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example

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

 while TRUE
   if (UART_Data_Ready() = 1)             ' if data is received   
     UART_Read_Text(output, "OK", 10)  ' reads text until 'OK' is found
     UART_Write_Text(output)              ' sends back text 
   end if
 wend.
Notes

None.

UART_Write

Prototype

sub procedure UART_Write(dim data_ as word)

Description

The function transmits a byte via the UART module.

This is a generic routine which uses the active UART module previously activated by the UART_Set_Active routine.

Parameters
  • _data: data to be sent.
Returns

Nothing.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example
dim data_ as byte
...
data_ = 0x1E
UART_Write(data_)
Notes

None.

UART_Write_Text

Prototype

sub procedure UART_Write_Text(dim byref uart_text as string)

Description

Sends text via UART. Text should be zero terminated and is limited to 255 characters.

This is a generic routine which uses the active UART module previously activated by the UART_Set_Active routine.

Parameters
  • UART_text: text to be sent.
Returns

Nothing.

Requires

Routine requires at least one UART module.

Used UART module must be initialized before using this routine. See UARTx_Init and UARTx_Init_Advanced routines.

Example

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

 while TRUE
   if (UART_Data_Ready() = 1)             ' if data is received   
     UART_Read_Text(output, 'OK', 10)  ' reads text until 'OK' is found
     UART_Write_Text(output)              ' sends back text 
   end if
 wend.
Notes

None.

Copyright (c) 2002-2015 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