正点原子战舰v3笔记(4)串口和中断代码

本文详细介绍了STM32中NVIC_PriorityGroupConfig函数用于设置中断优先级分组,并展示了如何使用NVIC_Init进行中断使能配置。此外,还探讨了串口接收中断的实现,包括接收缓冲区的管理与协议分析,强调了以回车换行作为数据包结束的通信协议重要性。
摘要由CSDN通过智能技术生成

优先级笔记
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
{

assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));

SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;

}

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置优先级分组

//输入参数范围
#define NVIC_PriorityGroup_0 ((uint32_t)0x700) /*!< 0 bits for pre-emption priority
4 bits for subpriority /
#define NVIC_PriorityGroup_1 ((uint32_t)0x600) /
!< 1 bits for pre-emption priority
3 bits for subpriority /
#define NVIC_PriorityGroup_2 ((uint32_t)0x500) /
!< 2 bits for pre-emption priority
2 bits for subpriority /
#define NVIC_PriorityGroup_3 ((uint32_t)0x400) /
!< 3 bits for pre-emption priority
1 bits for subpriority /
#define NVIC_PriorityGroup_4 ((uint32_t)0x300) /
!< 4 bits for pre-emption priority
0 bits for subpriority */

#define IS_NVIC_PRIORITY_GROUP(GROUP) (((GROUP) == NVIC_PriorityGroup_0) ||
((GROUP) == NVIC_PriorityGroup_1) ||
((GROUP) == NVIC_PriorityGroup_2) ||
((GROUP) == NVIC_PriorityGroup_3) ||
((GROUP) == NVIC_PriorityGroup_4))

MDK中NVIC寄存器结构体

typedef struct
{
__IO uint32_t ISER[8]; /*!< Offset: 0x000 Interrupt Set Enable Register /
uint32_t RESERVED0[24];
__IO uint32_t ICER[8]; /
!< Offset: 0x080 Interrupt Clear Enable Register /
uint32_t RSERVED1[24];
__IO uint32_t ISPR[8]; /
!< Offset: 0x100 Interrupt Set Pending Register /
uint32_t RESERVED2[24];
__IO uint32_t ICPR[8]; /
!< Offset: 0x180 Interrupt Clear Pending Register /
uint32_t RESERVED3[24];
__IO uint32_t IABR[8]; /
!< Offset: 0x200 Interrupt Active bit Register /
uint32_t RESERVED4[56];
__IO uint8_t IP[240]; /
!< Offset: 0x300 Interrupt Priority Register (8Bit wide) /
uint32_t RESERVED5[644];
__O uint32_t STIR; /
!< Offset: 0xE00 Software Trigger Interrupt Register */
} NVIC_Type;

void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct); //设置抢占优先级函数 设置中断使能
typedef struct
{

uint8_t NVIC_IRQChannel; //设置中断通道

uint8_t NVIC_IRQChannelPreemptionPriority;//设置响应优先级

uint8_t NVIC_IRQChannelSubPriority; //设置抢占优先级

FunctionalState NVIC_IRQChannelCmd; //使能/使能

} NVIC_InitTypeDef;

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;// 抢占优先级为1

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;// 子优先级位2 响应优先级

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道使能

NVIC_Init(&NVIC_InitStructure); //根据上面指定的参数初始化NVIC寄存器

串口笔记

常用的串口相关寄存器


USART_SR 状态寄存器 

USART_DR 数据寄存器 (发送,读取数据)

USART_BRR 波特率寄存器 

USART_CR 使能寄存器

串口操作相关库函数(省略入口参数):

void USART_Init(); //串口初始化:波特率,数据字长,奇偶校验,硬件流控以及收发使能

void USART_Cmd();//使能串口
void USART_ITConfig();//使能相关中断


void USART_SendData();//发送数据到串口,
DR
uint16_t USART_ReceiveData();//接受数据,从DR读取接受到的数据


FlagStatus USART_GetFlagStatus();//获取状态标志位

void US
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值