使用uart1串口,需要用到stm8s_uart1.c和stm8s_uart1.h两个文件 1、建立工程目录结构如下:
2、编写uart.h文件如下: #ifndef __UART_H #define __UART_H
#include "stm8s.h" #include "stm8s_clk.h"
void USART_Configuration(void); //串口配置函数 void UART_send_string(uint8_t *Buffer);//发送一个字符串函数 #endif /* __UART_H */
3、编写uart.c文件如下: #include "uart.h"
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_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++; } }
4、编写主函数如下: #include "stm8s.h" #include "stm8s_clk.h" #include "uart.h" static void delay (int cnt) { while (cnt--); }
int main(void) { CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); USART_Configuration();//串口配置 while (1) { UART_send_string("LIKE"); UART1_SendData8('\n'); while (UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET); delay(30000); delay(30000); delay(30000); } }
运行结果:
|