UART Remappable Library


mikroC PRO for PIC provides a library for the UART Remappable Module and 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

Generic Routines

UART_Remappable_Init

Prototype

void UART_Remappable_Init(const unsigned long baud_rate);

// for MCUs with multiple modules
void UARTx_Remappable_Init(const unsigned long baud_rate);
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.
  • To select the desired UART module for the, simply change the letter x in the prototype for the appropriate module number.

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 library 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);

UARTx_Remappable_Init_Advanced

Prototype

void UARTx_Remappable_Init_Advanced(const unsigned long baud_rate, unsigned char mode, unsigned char stopBits);

Returns

Nothing.

Description

Initializes hardware UART Remappable module with the custom configuration. 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 :

  • mode: parity and data size selector :
    Description Predefined library const
    8-bit mode, even parity UART_MODE_8BIT_EVENPARITY
    8-bit mode, odd parity UART_MODE_8BIT_ODDPARITY
    7-bit mode, no parity UART_MODE_7BIT_NOPARITY
    8-bit mode, no parity UART_MODE_8BIT_NOPARITY

  • stopBits: stop bits mode selector :
    Description Predefined library const
    1 stop bit UART_STOPBITS_1
    1.5 stop bits, verify first UART_STOPBITS_1_5_VERIFYFIRST
    2 stop bits, verify second UART_STOPBITS_2_VERIFYFIRSTSECOND
    2 stop bits, verify first UART_STOPBITS_2_VERIFYFIRST

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired UART module for the, simply change the letter x in the prototype for the appropriate module number.

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

UART1_Remappable_Init_Advanced(9600, UART_MODE_8BIT_EVENPARITY, UART_STOPBITS_1);

UART_Remappable_Data_Ready

Prototype

unsigned short UART_Remappable_Data_Ready();

// for MCUs with multiple modules
unsigned short UARTx_Remappable_Data_Ready();
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.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired UART module, simply change the letter x in the prototype for the appropriate module number.
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
// If data is ready, read it:
if (UART_Remappable_Data_Ready() == 1) { 
 receive = UART_Remappable_Read();
 }

UART_Remappable_Tx_Idle

Prototype

char UART_Remappable_Tx_Idle();

// for MCUs with multiple modules
char UARTx_Remappable_Tx_Idle();
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.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired UART module, simply change the letter x in the prototype for the appropriate module number.
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) { 
  UART_Remappable_Write(_data);
 }

UART_Remappable_Read

Prototype

unsigned short UART_Remappable_Read();

// for MCUs with multiple modules
unsigned short UARTx_Remappable_Read();
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.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired UART module , simply change the letter x in the prototype for the appropriate module number.
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
// If data is ready, read it:
if (UART_Remappable_Data_Ready() == 1) { 
 receive = UART_Remappable_Read();
 }

UART_Remappable_Read_Text

Prototype

void UART_Remappable_Read_Text(char *Output, char *Delimiter, char Attempts);

// for MCUs with multiple modules
void UARTx_Remappable_Read_Text(char *Output, char *Delimiter, char Attempts);
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.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired UART module, simply change the letter x in the prototype for the appropriate module number.
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 (1) {
  if (UART_Remappable_Data_Ready() == 1) {          // if data is received 
    UART_Remappable_Read_Text(output, "OK", 10);    // reads text until 'OK' is found
    UART_Remappable_Write(output);             // sends back text 
 }
}

UART_Remappable_Write

Prototype

void UART_Remappable_Write(unsigned short data_);

// for MCUs with multiple modules
void UARTx_Remappable_Write(unsigned short data_);
Returns

Nothing.

Description

The function transmits a byte via the UART module.

Parameters :

  • TxData: data to be sent

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired UART module, simply change the letter x in the prototype for the appropriate module number.
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
unsigned char _data = 0x1E;
...
UART_Remappable_Write(_data);

UART_Remappable_Write_Text

Prototype

void UART_Remappable_Write_Text (char *uart_text);

// for MCUs with multiple modules
void UARTx_Remappable_Write_Text (char *uart_text);
Returns

Nothing.

Description

Sends text (parameter uart_text) via UART. Text should be limited to 255 characters and zero terminated.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired UART module, simply change the letter x in the prototype for the appropriate module number.
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 (1) {
  if (UART_Remappable_Data_Ready() == 1) {          // if data is received 
    UART_Remappable_Read_Text(output, "OK", 10);    // reads text until 'OK' is found
    UART_Remappable_Write(output);             // sends back text 
 }
}

UART_Set_Active

Prototype

void UART_Set_Active(char (*read_ptr)(), void (*write_ptr)(unsigned char data_), char (*ready_ptr)(), char (*tx_idle_ptr)())

Returns

Nothing.

Description

Sets active UART module which will be used by the UART library routines.

Parameters :

Requires

Routine is available only for MCUs with two UART modules.

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

Example
UART1_Remappable_Init(9600);                    // initialize UART1 module
UART2_Remappable_Init(9600);                    // initialize UART2 module

RS485Master_Init();                  // initialize MCU as Master

UART_Set_Active(&UART1_Remappable_Read, &UART1_Remappable_Write, &UART1_Remappable_Data_Ready, &UART1_Remappable_Tx_Idle); // set UART1 active
RS485Master_Send(dat,1,160);        // send message through UART1

UART_Set_Active(&UART2_Remappable_Read, &UART2_Remappable_Write, &UART2_Remappable_Data_Ready, &UART2_Remappable_Tx_Idle); // set UART2 active
RS485Master_Send(dat,1,160);        // send through UART2

UART_Data_Ready

Prototype

char UART_Data_Ready();

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

Use the function to test 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.

Requires

MCU with the UART module.

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

Example
// If data is ready, read it:
if (UART_Data_Ready() == 1) { 
 receive = UART_Read();
 }

UART_Tx_Idle

Prototype

char UART_Tx_Idle();

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.

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

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_Tx_Idle() == 1) { 
  UART_Write(_data);
 }

UART_Read

Prototype

char UART_Read();

Returns

Returns the received byte.

Description

Function receives a byte via UART. Use the function UART_Data_Ready 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.

Requires

MCU with the UART module.

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

Example
// If data is ready, read it:
if (UART_Data_Ready() == 1) { 
 receive = UART_Read();
 }

UART_Read_Text

Prototype

void UART_Read_Text(char *Output, char *Delimiter, char Attempts);

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

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.

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:

UART1_Init(4800);                         // initialize UART1 module
Delay_ms(100);

while (1) {
  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 
 }
}

UART_Write

Prototype

void UART_Write(char data_);

Returns

Nothing.

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

Requires

MCU with the UART module.

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

Example
unsigned char _data = 0x1E;
...
UART_Write(_data);

UART_Write_Text

Prototype

void UART_Write_Text(char * UART_text);

Returns

Nothing.

Description

Sends text via UART. Text should be zero terminated.

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

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:

UART1_Init(4800);                         // initialize UART1 module
Delay_ms(100);

while (1) {
  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 
 }
}
Copyright (c) 2002-2019 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