void EXTI_ClearITPendingBit(uint32_t EXTI_Line)void EXTI_ClearFlag(uint32_t EXTI_Line)
时间: 2024-04-25 13:21:40 浏览: 134
这两个函数是用于清除外部中断标志位的函数。
`EXTI_ClearITPendingBit`函数用于清除指定的外部中断线的中断挂起位,参数`EXTI_Line`表示需要清除的外部中断线。该函数将会清除指定外部中断线上的中断挂起位,以便下一次触发中断时能够正确地检测到中断事件。
`EXTI_ClearFlag`函数用于清除指定的外部中断线的中断标志位,参数`EXTI_Line`表示需要清除的外部中断线。该函数将会清除指定外部中断线上的中断标志位,以便下一次触发中断时能够正确地检测到中断事件。
这两个函数通常在外部中断的中断处理函数中使用,用于在处理完中断后清除相应的标志位,以确保下一次触发中断时能够正常工作。
相关问题
声音识别代码stm32
以下是一个基于STM32的语音识别代码示例:
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_exti.h"
#include "misc.h"
#define SAMPLE_RATE 16000
#define BUFFER_SIZE 1600
#define THRESHOLD 500
uint16_t buffer[BUFFER_SIZE];
volatile uint16_t buffer_index = 0;
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET) {
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
buffer[buffer_index++] = ADC_GetConversionValue(ADC1);
if (buffer_index >= BUFFER_SIZE) {
TIM_Cmd(TIM2, DISABLE);
}
}
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) == SET) {
EXTI_ClearITPendingBit(EXTI_Line0);
if (TIM_GetCounter(TIM2) < THRESHOLD) {
buffer_index = 0;
TIM_Cmd(TIM2, ENABLE);
}
}
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
void RCC_Configuration(void)
{
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
NVIC_Configuration();
TIM_Configuration();
while (1) {
// do something
}
}
```
这个代码通过ADC采样声音数据,然后将数据存储到数组中。当声音达到一定阈值时,会触发外部中断,开始采样声音数据。可以根据需要修改阈值和采样率等参数。
STM32F103C8T6单片机NEC红外信号解码程序
好的,这个问题我可以帮你回答。下面是一个简单的STM32F103C8T6单片机NEC红外信号解码程序的示例代码:
```c
#include "stm32f10x.h"
#define IR_PORT GPIOA
#define IR_PIN GPIO_Pin_0
volatile uint16_t ir_code = 0;
volatile uint8_t ir_state = 0;
void EXTI0_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
EXTI_ClearITPendingBit(EXTI_Line0);
if (ir_state == 0) {
TIM2->CNT = 0;
TIM_Cmd(TIM2, ENABLE);
ir_state = 1;
} else if (ir_state == 1) {
ir_code = TIM2->CNT;
TIM_Cmd(TIM2, DISABLE);
ir_state = 2;
}
}
}
void TIM2_IRQHandler(void) {
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
ir_state = 0;
}
}
int main(void) {
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = IR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(IR_PORT, &GPIO_InitStructure);
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);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while (1) {
if (ir_code != 0) {
// decode NEC infrared signal
// ...
ir_code = 0;
}
}
}
```
这个示例程序使用了外部中断和定时器来解码NEC红外信号。当红外接收器接收到一个NEC红外信号的起始脉冲时,会触发外部中断。在中断处理函数中,根据当前状态分别启动和停止定时器,以计算红外信号的脉宽。当定时器溢出时,会触发另一个中断处理函数,重置状态。在主循环中,如果检测到一个完整的红外信号,则进行解码处理。
阅读全文