stm32 串口485通信发送字符串,最后总是0xff

stm32 串口485通信发送字符串,最后总是0xff

解决办法:在读取TXE标志后再读取TC标志,解决

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以帮你实现一个基于 STM32 的数字时钟,通过串口发送当前时间。首先,你需要准备一块 STM32 开发板和一个带有串口功能的显示屏。 下面是实现步骤: 1. 配置 STM32 的时钟和串口,使其能够正常工作。 2. 编写代码,获取当前时间,并将其转换为字符串格式。 3. 使用串口发送字符串格式的当前时间到显示屏上。 以下是示例代码,你可以参考一下: ```c #include "stm32f10x.h" #include "stdio.h" #include "string.h" #define USART USART1 #define BAUDRATE 9600 void init_USART(void); void USART_send(uint16_t ch); void USART_puts(char *str); void init_clock(void); void init_GPIO(void); void init_timer(void); void RTC_Configuration(void); void delay_ms(uint16_t ms); RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; int main(void) { char time_str[20]; init_clock(); init_GPIO(); init_USART(); init_timer(); RTC_Configuration(); while(1) { RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct); RTC_GetDate(RTC_Format_BIN,&RTC_DateStruct); sprintf(time_str, "%02d:%02d:%02d", RTC_TimeStruct.RTC_Hours, RTC_TimeStruct.RTC_Minutes, RTC_TimeStruct.RTC_Seconds); USART_puts(time_str); USART_puts("\r\n"); delay_ms(1000); } } void init_USART(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //使能USART和GPIO的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); //USART1_TX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //USART1_RX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); //USART初始化 USART_InitStructure.USART_BaudRate = BAUDRATE; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART, &USART_InitStructure); //使能USART USART_Cmd(USART, ENABLE); } void USART_send(uint16_t ch) { while (USART_GetFlagStatus(USART, USART_FLAG_TXE) == RESET); USART_SendData(USART, (uint8_t)ch); } void USART_puts(char *str) { while(*str) { USART_send(*str); str++; } } void init_clock(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); RCC_WaitForHSEStartUp(); RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); SysTick_Config(SystemCoreClock / 1000); } void init_GPIO(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); } void init_timer(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_LSEConfig(RCC_LSE_ON); while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); } void RTC_Configuration(void) { RTC_InitTypeDef RTC_InitStructure; //使能PWR和BKP的时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能RTC PWR_BackupAccessCmd(ENABLE); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); //RTC初始化 RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); //设置时间 RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; RTC_TimeStruct.RTC_Hours = 0x12; RTC_TimeStruct.RTC_Minutes = 0x00; RTC_TimeStruct.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BIN,&RTC_TimeStruct); RTC_DateStruct.RTC_WeekDay = RTC_Weekday_Monday; RTC_DateStruct.RTC_Month = RTC_Month_January; RTC_DateStruct.RTC_Date = 0x01; RTC_DateStruct.RTC_Year = 0x21; RTC_SetDate(RTC_Format_BIN,&RTC_DateStruct); } void delay_ms(uint16_t ms) { volatile uint32_t nCount; RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); nCount = (RCC_Clocks.HCLK_Frequency / 10000) * ms; for (; nCount != 0; nCount--); } void SysTick_Handler(void) { static uint32_t ticks = 0; ticks++; if(ticks >= 1000) { ticks = 0; GPIO_WriteBit(GPIOC, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13))); } } ``` 在这个代码中,我们使用 RTC(Real-Time Clock)模块获取当前时间,并将其转换为字符串格式,然后使用 USART 发送到显示屏上。同时,我们还使用 SysTick 定时器来控制 LED 灯的闪烁,以证明程序在运行。 这只是一个简单的示例,你需要根据你的具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值