我需要stm32开发板来写一个程序,达到通过串口向上位机发送数据,数据内容为时间,格式按x:xxcxx显示,时间数据应为实时变化数据。串口功能通过KEYo控制,当KEYo按下时串口开始发送,当再次按下时串口停止发送的效果,代码是怎么样的
时间: 2024-04-29 18:26:38 浏览: 152
上位机的串口发送程序
您可以参考以下代码实现您的需求:
```C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f10x.h"
#define USART1_RX_GPIO GPIOA
#define USART1_RX_PIN GPIO_Pin_10
#define USART1_TX_GPIO GPIOA
#define USART1_TX_PIN GPIO_Pin_9
#define USART1 GPIOA
#define USART1_CLK RCC_APB2Periph_USART1
#define USART1_BAUDRATE 115200
#define LED_GPIO GPIOB
#define LED_PIN GPIO_Pin_1
#define KEY_GPIO GPIOC
#define KEY_PIN GPIO_Pin_13
USART_InitTypeDef USART_InitStructure;
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = USART1_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_RX_GPIO, &GPIO_InitStructure);
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART1_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART1_TX_GPIO, &GPIO_InitStructure);
/* Configure KEY0 (PC13) as input pull-up */
GPIO_InitStructure.GPIO_Pin = KEY_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEY_GPIO, &GPIO_InitStructure);
/* Configure LED (PB1) as output push-pull */
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED_GPIO, &GPIO_InitStructure);
/* Configure USART1 */
USART_InitStructure.USART_BaudRate = USART1_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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART1_Send(const char *data)
{
while (*data != '\0') {
USART_SendData(USART1, *data++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
char* int2str(int num, int radix)
{
static char str[16];
int i = 0;
do {
int t = num % radix;
str[i++] = t > 9 ? t - 10 + 'a' : t + '0';
} while (num /= radix);
str[i] = '\0';
for (int j = 0; j < i / 2; j++) {
char t = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = t;
}
return str;
}
void SysTick_Handler(void)
{
static int count = 0;
static int send = 0;
if (++count == 1000) {
count = 0;
char *hour = int2str(RTC_GetCounter(RTC) / 3600 % 24, 10);
char *minute = int2str(RTC_GetCounter(RTC) / 60 % 60, 10);
char *second = int2str(RTC_GetCounter(RTC) % 60, 10);
char *data = malloc(strlen(hour) + 2 + strlen(minute) + 2 + strlen(second) + 1);
sprintf(data, "%s:%s:%s", hour, minute, second);
if (send) {
USART1_Send(data);
USART1_Send("\r\n");
}
free(data);
}
}
int main(void)
{
SystemInit();
/* Configure SysTick */
SysTick_Config(SystemCoreClock / 1000);
/* Configure RTC */
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();
RTC_InitStructure.RTC_AsynchPrediv = 127;
RTC_InitStructure.RTC_SynchPrediv = 249;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
RTC_SetCounter(0x00000000);
RTC_WaitForLastTask();
USART1_Config();
while (1) {
if (GPIO_ReadInputDataBit(KEY_GPIO, KEY_PIN) == RESET) {
send = !send;
if (send) {
GPIO_SetBits(LED_GPIO, LED_PIN);
USART1_Send("UART start!\r\n");
} else {
GPIO_ResetBits(LED_GPIO, LED_PIN);
USART1_Send("UART stop!\r\n");
}
/* Wait for key release */
while (GPIO_ReadInputDataBit(KEY_GPIO, KEY_PIN) == RESET);
}
}
}
```
这是一个基于STM32的程序,通过串口向上位机发送实时时间数据。其中 KEY0(PC13)控制串口发送任务的启停,当按下KEY0时,LED(PB1)闪烁表示串口开始发送任务,再次按下KEY0时,LED停止闪烁,表示串口停止发送任务。串口发送的时间数据格式为x:xxcxx。
阅读全文