功能:通过串口发送数据给上位机(定时) 涉及知识:串口通信、定时器、中断 参考资料: https://blog.csdn.net/weixin_49079904/article/details/127517855?spm=1001.2014.3001.5502 https://blog.csdn.net/weixin_49079904/article/details/127577210?spm=1001.2014.3001.5502 https://blog.csdn.net/weixin_49079904/article/details/127614041?spm=1001.2014.3001.5502 这几篇看明白了应该问题不大(就是在这个基础上改的)
步骤: 1、一些初始化 2、使能 GPIO USART 定时器 中断 定时器中断 3、外设 引脚重映射 4、初始化 USART 5、初始化 串口设置 6、初始化 定时器 7、配置中断分组NVIC 8、编写中断服务程序
1、一些初始化
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef Usart_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_InitStructure;
2、使能 GPIO USART 定时器 中断 定时器中断
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
3、外设 引脚重映射
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
4、初始化 USART
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Low_Speed;
GPIO_Init(GPIOA , &GPIO_InitStructure);
5、初始化 串口设置
Usart_InitStructure.USART_BaudRate = 9600;
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);
6、初始化 定时器
TIM_InitStructure.TIM_Prescaler = 8400-1;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_Period = 5000-1;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3, &TIM_InitStructure);
7、配置中断分组NVIC
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE);
8、编写中断服务程序
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
{
USART_SendData(USART1,0x55);
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
主函数总代码
#include "stm32f4xx.h"
#include "delay.h"
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef Usart_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Low_Speed;
GPIO_Init(GPIOA , &GPIO_InitStructure);
Usart_InitStructure.USART_BaudRate = 9600;
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);
TIM_InitStructure.TIM_Prescaler = 8400-1;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_Period = 5000-1;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3, &TIM_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE);
while(1);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
{
USART_SendData(USART1,0x55);
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
时间2022/11/02 PS:没有编写上位机基础的姐妹其实可以下载一个“串口猎人”验证正确性
|