STMicroelectronics STM8 Manual page 102

Table of Contents

Advertisement

{
UART1_DeInit();
UART1_Init(9600,
UART1_WORDLENGTH_8D,
UART1_STOPBITS_1,
UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE,
UART1_MODE_TXRX_ENABLE);
UART1_Cmd(ENABLE);
}
Explanation
The peripheral and CPU clocks are set at 2MHz:
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
....
....
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, ENABLE);
The TX-RX GPIO pins are set as output and input respectively:
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);
UART setup is straightforward. We just need to set baud rate, no. of data bits, no. of stop bit, parity
and type of communication (synchronous or asynchronous).
void UART1_setup(void)
{
UART1_DeInit();
UART1_Init(9600,
UART1_WORDLENGTH_8D,
UART1_STOPBITS_1,
UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE,
UART1_MODE_TXRX_ENABLE);
UART1_Cmd(ENABLE);
}
In the main code, we are checking both transmission complete and reception complete flags. With
these flags, we will know if new data arrived and if it is possible to send a new data.
The first part checks if any new data received. That's why the IF condition is checking if the RX buffer
is empty or not. If it is not empty then new data must have arrived. The new data (a character here) is
fetched and displayed on a LCD. Then we clear the RX buffer not empty flag to enable reception of
new data. After that we are sending some data to the host PC over the UART.
if(UART1_GetFlagStatus(UART1_FLAG_RXNE) == TRUE)
{

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the STM8 and is the answer not in the manual?

Questions and answers

Table of Contents

Save PDF