The use of STM8S003F3 uart

I will organize the use of uart here to facilitate my subsequent use

bsp_uart1.h

#ifndef _BSP_UART1_H_
#define _BSP_UART1_H_


#include "stm8s.h"
#include "stm8s_clk.h"


void USART_Configuration(void);   //串口配置函数
void UART_send_string(uint8_t *Buffer);//发送一个字符串函数
#endif /* _BSP_UART_H_ */

bsp_uart1.c

void USART_Configuration(void)//串口初始化函数
{  
    UART1_DeInit(); //清除之前的串口配置
    UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, \
    UART1_PARITY_NO , UART1_SYNCMODE_CLOCK_DISABLE , UART1_MODE_TXRX_ENABLE);
    //串口配置:波特率115200,字节数8,1个停止位,无奇偶效验位,非同步模式,允许接受和发送  
    UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
    UART1_Cmd(ENABLE );  //启用串口
}

void UART_send_string(uint8_t *Buffer) //发送一个字符
{
    uint8_t *String;
    String=Buffer;
	  while(*String!='\0')
    {
        UART1_SendData8(*String);
        while (UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET);
		    String++;
    }
    UART1_SendData8(0x0d);
    UART1_SendData8(0x0a);
}

In the interrupt handling function, the code is as follows. What is realized here is to send the received data again, which can be done according to your actual situation.

stm8_it.c

INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */

    if(UART1_GetITStatus(UART1_IT_RXNE) != RESET)        //检查指定的UART1中断是否发生。 
    {
        UART1_SendData8(UART1_ReceiveData8());             //将接收的数据再用串口发送出去
        UART1_ClearITPendingBit(UART1_IT_RXNE);            //清除UART1挂起标志
     }
}

Reference blog: https://blog.csdn.net/qinrenzhi/article/details/80894508

Reference blog: https://www.cnblogs.com/zhenghaoyu/p/10698471.html

Guess you like

Origin blog.csdn.net/smile_5me/article/details/114880396