基于stm32f103c8t6的UART驱动代码编写,定时器驱动代码编写,以及按键扫描驱动代码编写
时间: 2023-08-27 19:08:42 浏览: 218
基于STM32F103C8T6的UART驱动代码编写如下:
```c
#include "stm32f10x.h"
#define USART_RX_BUF_SIZE 128
uint8_t USART_RX_BUF\[USART_RX_BUF_SIZE\];
uint8_t USART_RX_CNT = 0;
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
uint8_t r = USART_ReceiveData(USART1); //读取接收到的数据
if (USART_RX_CNT < USART_RX_BUF_SIZE)
{
USART_RX_BUF\[USART_RX_CNT\] = r;
USART_RX_CNT++;
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) //接收完成后进入空闲中断
{
USART1->SR; //先读SR,再读DR才能完成idle中断的清零,否则会一直进入中断
USART1->DR;
if (USART_RX_CNT < USART_RX_BUF_SIZE)
{
// 接收完成标志位
}
else
{
// 升级程序标志
}
USART_RX_CNT = 0;
}
}
```
定时器驱动代码编写如下:
```c
#include "stm32f10x.h"
void TIM6_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; //定时器溢出时间为1ms
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; //定时器时钟频率为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM6, ENABLE);
}
void TIM6_IRQHandler(void)
{
if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)
{
// 定时器中断处理代码
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
}
}
```
按键扫描驱动代码编写如下:
```c
#include "stm32f10x.h"
#define KEY_GPIO_PORT GPIOA
#define KEY_GPIO_PIN GPIO_Pin_0
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
}
uint8_t Key_Scan(void)
{
if (GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN) == RESET)
{
// 按键按下
return 1;
}
else
{
// 按键未按下
return 0;
}
}
```
以上是基于STM32F103C8T6的UART驱动代码、定时器驱动代码和按键扫描驱动代码的编写。请根据实际需求进行适当的修改和调整。
#### 引用[.reference_title]
- *1* *2* [【嵌入式07.1】STM32F103C8T6开发板+CubeMX采用定时器实现周期性串口输出和LED闪烁](https://blog.csdn.net/qq_58869016/article/details/127582403)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [基于STM32F103C8T6串口IAP升级](https://blog.csdn.net/weixin_43527703/article/details/129275189)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文