文章目录[隐藏]
1. 配置 GPIO 时钟、串口时钟、复用时钟
2. 配置 GPIO 结构体
3. 配置串口
1. 2. 3. 步骤基本框架在 5. STM32——串口发送字符、字符串 + printf 的重定向
4. 配置中断 NVIC 抢占优先级
在 misc.h 头文件中查找相关函数函数
USART1_IRQn 在 stm32f10x.h 头文件中查找相关函数函数
//4. 配置中断抢占优先级
NVIC_InitTypeDef nvicInitStructure
//4.1 配置优先级组
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2)
//4.2 配置结构体
nvicInitStructure.NVIC_IRQChannel = USART1_IRQn;
nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 1;
nvicInitStructure.NVIC_IRQChannelSubPriority = 1;
nvicInitStructure.NVIC_IRQChannelCmd = ENABLE;
//4.3 初始化结构体
NVIC_Init(&nvicInitStructure);
5. 搭建串口中断服务函数
USART1_IRQHandler 在 startup_stm32f10x_hd.s 文件中查找
//5. 搭建串口中断服务函数
void USART1_IRQHandler(void)
{
char temp;
if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET ) //发生中断
{
temp=USART_ReceiveData(USART1);
if(temp=='O')
{
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
USARTSendStr(USART1, "LED is open");
}
else if(temp=='C')
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
USARTSendStr(USART1, "LED is close");
}
}
}
用串口发送数据点亮LED
usart.c
#include "usart.h"
#include "stm32f10x.h"
void usart_init()
{
GPIO_InitTypeDef gpioInitStructure;
USART_InitTypeDef usartInitStructure;
NVIC_InitTypeDef nvicInitStructure;
//1.配置时钟
//1.1配置GPIOA、串口时钟、复用时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//2.配置GPIOA结构体
//2.1 A9 TX
gpioInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStructure.GPIO_Pin = GPIO_Pin_9;
gpioInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioInitStructure);
//2.2 A10 RX
gpioInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &gpioInitStructure);
//3.配置串口结构体
usartInitStructure.USART_BaudRate = 115200;
usartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartInitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
usartInitStructure.USART_Parity = USART_Parity_No;
usartInitStructure.USART_StopBits = USART_StopBits_1;
usartInitStructure.USART_WordLength = USART_WordLength_8b;
//3.1 初始化串口
USART_Init(USART1, &usartInitStructure);
//3.2 配置串口中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//3.3 使能串口
USART_Cmd(USART1, ENABLE);
//4.配置NVIC中断优先级
//4.1 配置优先级组
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//4.2 配置NVIC优先级结构体
nvicInitStructure.NVIC_IRQChannel = USART1_IRQn;
nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 1;
nvicInitStructure.NVIC_IRQChannelSubPriority = 1;
nvicInitStructure.NVIC_IRQChannelCmd = ENABLE;
//4.3 初始化NVIC优先级
NVIC_Init(&nvicInitStructure);
}
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData( USARTx, Data);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
}
void USARTSendStr(USART_TypeDef* USARTx, char* str)
{
uint16_t i=0;
while( *(str+i) != '\0' )
{
/*
USART_SendData( USARTx, *(str+i));
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
*/
USARTSendByte( USART1, *(str+i) );
i++;
}
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
usart.h
#include "stm32f10x.h"
#include <stdio.h>
void usart_init(void);
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data);
void USARTSendStr(USART_TypeDef* USARTx, char* str);
main.c
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "shake.h"
#include "relay.h"
#include "exti.h"
#include "usart.h"
#include "tim.h"
void delay(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i=10000;
while(i--);
}
}
int main()
{
// usart_init();
LED_Init();
tim_Init();
GPIO_SetBits(GPIOC, GPIO_Pin_13);
// USARTSendByte(USART1, 'O');
// USARTSendByte(USART1, 'K');
// printf("Yinyuer is a pretty girl!");
while(1)
{
}
}
//5. 搭建串口中断服务函数
void USART1_IRQHandler(void)
{
char temp;
if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET )
{
temp=USART_ReceiveData(USART1);
if(temp=='O')
{
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
USARTSendStr(USART1, "LED is open");
}
else if(temp=='C')
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
USARTSendStr(USART1, "LED is close");
}
}
}
点灯成功
版权声明:本文为CSDN博主「ZCY(Yinyuer1)」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46105931/article/details/120393624
暂无评论