IAR FOR ARM编程,外部中断下降沿触发按键导致LED1亮灭
时间: 2023-10-04 07:09:56 浏览: 87
以下是IAR FOR ARM编程的代码,实现外部中断下降沿触发按键导致LED1亮灭:
```c
#include "stm32f10x.h"
void Delay(uint32_t nCount);
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB12 as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Connect EXTI Line12 to PB12 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12);
/* Configure EXTI Line12 to generate an interrupt on falling edge */
EXTI_InitStructure.EXTI_Line = EXTI_Line12;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI Line12 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure PC13 as output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
while (1)
{
/* Do nothing */
}
}
void EXTI15_10_IRQHandler(void)
{
/* Clear the EXTI line 12 pending bit */
EXTI_ClearITPendingBit(EXTI_Line12);
/* Toggle PC13 */
GPIO_WriteBit(GPIOC, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)));
/* Delay */
Delay(0xFFFFF);
}
void Delay(uint32_t nCount)
{
uint32_t i;
for (i = 0; i < nCount; i++)
{
}
}
```
在这个例程中,我们首先启用了GPIOB和GPIOC的时钟,并将PB12配置为输入浮动,并将PC13配置为输出推挽。然后,我们启用了AFIO时钟,并将EXTI线12连接到PB12引脚。接下来,我们配置了EXTI线12以在下降沿触发时生成中断,并将中断优先级设置为最低。最后,我们在while循环中等待中断的发生。
在EXTI15_10_IRQHandler中,我们首先清除了EXTI线12的挂起位,然后切换PC13的状态,并进行延迟。这样,当我们按下PB12时,PC13会亮起,当我们放开PB12时,PC13会熄灭。
阅读全文