[STM8] STM8学习笔记---串口uart1

[复制链接]
 楼主| grfqq325 发表于 2021-6-22 23:32 | 显示全部楼层 |阅读模式

使用uart1串口,需要用到stm8s_uart1.c和stm8s_uart1.h两个文件

1、建立工程目录结构如下:

5990560d202a073b9f.png

Center.jpg


 楼主| grfqq325 发表于 2021-6-22 23:34 | 显示全部楼层
2、编写uart.h文件如下:
  1. #ifndef __UART_H
  2. #define __UART_H


  3. #include "stm8s.h"
  4. #include "stm8s_clk.h"


  5. void USART_Configuration(void);   //串口配置函数
  6. void UART_send_string(uint8_t *Buffer);//发送一个字符串函数
  7. #endif /* __UART_H */
 楼主| grfqq325 发表于 2021-6-22 23:35 | 显示全部楼层
3、编写uart.c文件如下:
  1. #include "uart.h"

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


  11. void UART_send_string(uint8_t *Buffer) //发送一个字符
  12.     {
  13.        uint8_t *String;
  14.         String=Buffer;
  15.         while(*String!='\0')
  16.        {
  17.           UART1_SendData8(*String);
  18.           while (UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET);
  19.           String++;
  20.         }
  21.     }
 楼主| grfqq325 发表于 2021-6-22 23:36 | 显示全部楼层
4、编写主函数如下:

  1. #include "stm8s.h"
  2. #include "stm8s_clk.h"
  3. #include "uart.h" 
  4.    
  5. static void delay (int cnt) 
  6. {
  7.   while (cnt--);
  8. }


  9. int main(void)

  10.   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  11.    
  12.   USART_Configuration();//串口配置
  13.   while (1)
  14.   {
  15.      UART_send_string("LIKE");
  16.      UART1_SendData8('\n');
  17.      while (UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET);
  18. delay(30000);
  19.                 delay(30000);
  20.                 delay(30000);
  21.   }
  22. }
 楼主| grfqq325 发表于 2021-6-22 23:37 | 显示全部楼层
运行结果: 3837760d2036ed7644.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

64

主题

686

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部

64

主题

686

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部