Project

General

Profile

Work with UART (C)

Observe transmit and receive messages via UART more details (in С).

Configuration UART on transmission and reception message in C looks like simple as in Assembler of multicellular processor.
Besides this in C for working with UART available usefull functions and macros, such as sending value int type, sending string,
sending definition array of data memory.

In code examples use UART library, which must required:

#include <uart.h>

а) Transmit via UART

1. Configurate UART on transmission

First step - create structure (all elements of structure see in uart.h).

UART_InitTypeDef UART_InitStructure;

Initialize needed parameters (other parameters initialize as null).

    UART_InitStructure.BaudRate = 38400; //baudrate
    UART_InitStructure.TypeParity = 0x00000000; //type parity control
    UART_InitStructure.Parity = 0x00000000; //allow parity control
    UART_InitStructure.FlowControl = 0x00000000; //allow dataflow control
    UART_InitStructure.Mode = 0x00000002; //allow transmit  

Initialize for UART0:
uart_init(UART0, &UART_InitStructure);

For UART3:
uart_init(UART3, &UART_InitStructure);

2. Allow alternative port functions

For UART0:

GPIOB->BPS = 0x00000F00;

For UART3:

GPIOD->BPS = 0x00000F00;

3. Check transmitter FIFO on full

For UART0:

while(UART_FIFO_TX_FULL(UART0) == 1);

For UART3:

while(UART_FIFO_TX_FULL(UART3) == 1);

4. Transmit byte via UART

For UART0:

UART_SEND_BYTE(0xAB, UART0);

For UART3:

UART_SEND_BYTE(0xAB, UART3);

As a result you can get simple example - transmission byte via UART with baudrate 38400 kbit/s:

For UART0:

#include <HDL51001_ccf.h>
#include <uart.h>

void main()
{
   UART_InitTypeDef UART_InitStructure;

    UART_InitStructure.BaudRate = 38400; //baudrate
    UART_InitStructure.TypeParity = 0x00000000; //type parity control
    UART_InitStructure.Parity = 0x00000000; //allow parity control
    UART_InitStructure.FlowControl = 0x00000000; //allow dataflow control
    UART_InitStructure.Mode = 0x00000002; //allow transmit

    uart_init(UART0, &UART_InitStructure);  

    GPIOB->BPS = 0x00000F00;

    while(UART_FIFO_TX_FULL(UART0) == 1);
    UART_SEND_BYTE(0xAB, UART0);
}

For UART3 example is analogie, replace port B on D and UART0 on UART3.

Transmitted byte you can receive with PC application - any COM port monitor, setting on data with 1 stop-bit,
baudrate 38400 kbit/s, data word - 8 bit.

б) Receive via UART

For reception via UART:

1. Execute 1,2 under letter а)

2. Check receiver FIFO for new data

For UART0:

while(UART_NEW_DATA(UART0) == 0);

For UART3:

while(UART_NEW_DATA(UART3) == 0);

3. Get data

For UART0:

rbyte = UART_GET_BYTE(UART0);

For UART3:

rbyte = UART_GET_BYTE(UART3);

API functions UART:

Observe every functions, which declared in uart.h

1) void uart_init(UART_TypeDef *UART, UART_InitTypeDef* UART_InitStruct)

Function uart_init() initialize definition number of UART.
UART - available value: UART0, UART1, UART2, UART3
UART_InitStruct - structure, contain configuration elements UART

2) void DM2UART(UART_TypeDef *UART, int start_address, int size_block)

Функция DM2UART () output sector data memory via UART in a little endian format.
UART - available value: UART0, UART1, UART2, UART3
start_address - start data memory address
size_block - size data sector

3) void uart_send_str(char *str, UART_TypeDef *UART)

Function uart_send_str() output string via UART.
UART - available value: UART0, UART1, UART2, UART3
str - string

4) void uart_send_int(int str, UART_TypeDef *UART)

Function uart_send_int() output variable int type via UART.
UART - available value: UART0, UART1, UART2, UART3
str - variable int type

Note:

In library versions before 01.04.13 needed before DM2UART (), uart_send_int()
use consructions:

while(UART_FIFO_TX_EMPTY(UART0) == 0);

In library versions after 01.04.13 you can use functions independently.