#include <nvic.h> void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the USARTy Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
时间: 2024-02-26 11:53:23 浏览: 176
这是一个配置STM32F4微控制器中NVIC(Nested Vectored Interrupt Controller)的函数。在STM32F4中,NVIC用于管理中断,并且可以配置中断优先级。在这个函数中,首先通过调用NVIC_PriorityGroupConfig函数配置NVIC的优先级分组,然后通过配置NVIC_InitStructure结构体中的各个参数来初始化NVIC。在这个函数中,配置了USART1_IRQn和TIM3_IRQn两个中断的优先级和使能状态。USART1_IRQn用于USART1串口通信的中断处理函数,而TIM3_IRQn用于定时器3的中断处理函数。配置NVIC后,就可以启用这两个中断,并且可以在中断处理函数中进行相应的操作。
相关问题
You are required to write a C program to: • Initialize GPIO peripherals • Initialise UART peripheral for receiving ASCII characters ‘A’ to ‘Z’ at baud 9600 • Initialise an internal array to hold 10 characters with head and tail: CharBuff • Repeat the following:o When data is received on the serial communication port, read ASCII character X, o If received character X is a capital letter add it to CharBuff, else ignore. o While CharBuff is not empty, transmit the morse code of the oldest stored character by blinking the LED (code provided for you). o When CharBuff is full, disable UART RX. o If UART RX is disabled, pushing the button P_B1 will activate it; otherwise, pushing the button does not affect your programme. You are recommended to use interrupt to control UART receiving data and coordinate the operation between CharBuff and P_LD2. 在我的代码基础上完成以上任务#include <platform.h> #include <gpio.h> #include "delay.h" #include "uart.h" #include <stm32f4xx.h> /* NOTE******** YOU CAN USE THE IN-UILT FUNCTION delay_ms(HOW_LONG) TO CAUSE A DELAY OF HOW_LONG MILLI SECONDS
由于您的要求涉及到硬件操作和中断控制,需要在具体的嵌入式系统上进行编程。下面是一个基于 STM32F4xx 的 C 代码示例,可以实现您所要求的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_usart.h"
#define BUFFER_SIZE 10
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
volatile char CharBuff[BUFFER_SIZE];
volatile int head = 0;
volatile int tail = 0;
volatile int buffer_full = 0;
volatile int uart_rx_enabled = 1;
void init_gpio(void) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
}
void init_uart(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx;
USART_Init(USART2, &USART_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void) {
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
char data = USART_ReceiveData(USART2);
if (data >= 'A' && data <= 'Z' && !buffer_full && uart_rx_enabled) {
CharBuff[tail] = data;
tail = (tail + 1) % BUFFER_SIZE;
if (tail == head) {
buffer_full = 1;
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
}
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
void blink_led(char data) {
int i, j;
int dot_time = 200;
int dash_time = 500;
switch (data) {
case 'A':
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
delay_ms(dash_time);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
break;
case 'B':
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dash_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
break;
// Add more cases for the rest of the alphabet...
}
}
int main(void) {
init_gpio();
init_uart();
while (1) {
if (head != tail) {
blink_led(CharBuff[head]);
head = (head + 1) % BUFFER_SIZE;
if (buffer_full) {
buffer_full = 0;
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
}
if (!UART_RX_ENABLED && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) {
UART_RX_ENABLED = 1;
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
}
}
```
该程序使用 STM32F4xx 的 GPIO 和 USART 模块来实现所需的功能。其中,`init_gpio()` 函数用于初始化 GPIO 端口,`init_uart()` 函数用于初始化 USART 端口。在中断服务函数 `USART2_IRQHandler()` 中,程序会检测是否有数据通过 USART 接收到,如果是大写字母且 CharBuff 没有满,那么将其添加到 CharBuff 中。如果 CharBuff 已满,则禁用 UART RX 中断。在主循环中,程序会周期性地检查 CharBuff 是否有数据,如果有则将其转换为 Morse 代码并通过 LED 闪烁输出。当 CharBuff 已满时,程序会禁用 UART RX 中断,直到 CharBuff 又有空间可用。同时,程序会检测 P_B1 按键是否按下,如果按下且 UART RX 已禁用,则启用 UART RX 中断。
为下面每一行代码添加注释:#include "stm32f10x.h" void RCC_Configuration(void) { /* Enable GPIOA, GPIOC and AFIO clocks / RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); / Enable SYSCFG clock / RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; / Configure PA0 pin as input floating / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); / Configure PC13 pin as output push-pull / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure / Configure the NVIC Preemption Priority Bits / NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); / Enable the EXTI0 Interrupt / NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void EXTI_Configuration(void) { EXTI_InitTypeDef EXTI_InitStructure; / Configure EXTI Line0 to generate an interrupt on falling edge / EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); / Connect EXTI Line0 to PA0 pin / GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); } void SysTick_Configuration(void) { / Configure SysTick to generate an interrupt every 1ms / if (SysTick_Config(SystemCoreClock / 1000)) { / Capture error / while (1); } } void Delay(__IO uint32_t nTime) { / Wait for nTime millisecond / TimingDelay = nTime; while (TimingDelay != 0); } void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } int main(void) { RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); EXTI_Configuration(); SysTick_Configuration(); / Infinite loop / while (1) { / Toggle PC13 LED every 500ms / GPIOC->ODR ^= GPIO_Pin_13; Delay(500); } } void EXTI0_IRQHandler(void) { / Check if PA0 button is pressed / if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET) { / Reset MCU / NVIC_SystemReset(); } / Clear EXTI Line0 pending bit */ EXTI_ClearITPendingBit(EXTI_Line0); }
// 引入STM32F10x库
#include "stm32f10x.h"
// 配置RCC
void RCC_Configuration(void) {
// 使能GPIOA, GPIOC和AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
// 使能SYSCFG时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
}
// 配置GPIO
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// 配置PA0引脚为浮空输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置PC13引脚为推挽输出模式,速率50MHz
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
// 配置NVIC
void NVIC_Configuration(void) {
NVIC_InitTypeDef NVIC_InitStructure;
// 配置NVIC抢占优先级位数
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
// 使能EXTI0中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
// 配置EXTI
void EXTI_Configuration(void) {
EXTI_InitTypeDef EXTI_InitStructure;
// 配置EXTI Line0在下降沿触发中断
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 将EXTI Line0连接至PA0引脚
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
}
// 配置SysTick
void SysTick_Configuration(void) {
// 配置SysTick使其每1ms产生一次中断
if (SysTick_Config(SystemCoreClock / 1000)) {
// 发生错误
while (1);
}
}
// 延时函数
void Delay(__IO uint32_t nTime) {
// 等待nTime毫秒
TimingDelay = nTime;
while (TimingDelay != 0);
}
// 延时计数器递减函数
void TimingDelay_Decrement(void) {
if (TimingDelay != 0x00) {
TimingDelay--;
}
}
// 主函数
int main(void) {
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
EXTI_Configuration();
SysTick_Configuration();
// 无限循环
while (1) {
// 每500ms交替改变PC13引脚的电平
GPIOC->ODR ^= GPIO_Pin_13;
Delay(500);
}
}
// EXTI0中断处理函数
void EXTI0_IRQHandler(void) {
// 检查PA0引脚的按键是否被按下
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET) {
// 复位MCU
NVIC_SystemReset();
}
// 清除EXTI Line0的中断挂起位
EXTI_ClearITPendingBit(EXTI_Line0);
}
阅读全文