在线时间587 小时
UID3067179
ST金币3115
蝴蝶豆191
注册时间2014-10-14
该用户从未签到
论坛元老
- 最后登录
- 2024-1-26
|
本帖最后由 TLLED 于 2019-2-16 11:14 编辑
使用STM8L050芯片串口输出数据,使用半双工模式。
一、引脚配置
使用STM8CubeMX来配置串口引脚。
二、硬件部分
使用USB-TTL模块连接PA0引脚和地引脚。
三、程序部分
3.1、串口部分程序
- <font size="3">
- //uart.c
- #include "stm8l15x.h"
- #include "stm8l15x_usart.h"
- //串口半双工通信
- void uart_init_halfduplex(void)
- {
- //设置串口端口
- GPIO_Init(GPIOA, GPIO_Pin_0, GPIO_Mode_Out_OD_HiZ_Fast);
-
- CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
- SYSCFG_REMAPPinConfig(REMAP_Pin_USART1TxRxPortA,ENABLE);
- //配置串口
- USART_DeInit(USART1);
- USART_Init(USART1, (uint32_t)115200, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx));
-
- //使能半双工模式
- USART_HalfDuplexCmd(USART1,ENABLE);
-
- USART_Cmd(USART1, ENABLE);
- }
- void Uart_SendDat( uint8_t dat)
- {
- USART_SendData8(USART1, dat);
- while(USART_GetFlagStatus( USART1, USART_FLAG_TC) == RESET);
- }
- void UART_SendString ( uint8_t *s)
- {
- while (*s != 0)
- {
- Uart_SendDat(*s++);
- }
- }
- //uart.h
- #ifndef __UART_H
- #define __UART_H
- void uart_init_halfduplex(void);
- void Uart_SendDat(uint8_t dat);
- void UART_SendString ( uint8_t *s);
- #endif
- </font>
复制代码
3.2、主程序
- <font size="3">void main(void)
- {
- CLK_Config(); //时钟初始化
-
- Delay(0x2fffff); //延时5S
-
- led_init(); //led初始化
- //swim_init();
- uart_init_halfduplex();
-
- while (1)
- {
- led_tog(); //LED取反
- //swim_tog(); //SWIM引脚取反
- UART_SendString ( "www.stmcu.org.cn\r\n");
- UART_SendString ( "stm8l050 串口测试\r\n");
-
- Delay(0xffff);
- }
- }</font>
复制代码
四、程序执行结果
|
|