刚刚开始学stm32,想用中断(gpioC13)控制led灯(gpioB12)电平翻转。结果自己写了个程序失败了。。。。。。
#include<stm32f10x.h>
#include<stm32f10x_gpio.h>
#include<misc.h>
#include<stm32f10x_rcc.h>
#include<stm32f10x_it.h>
#define LED GPIO_Pin_12
#define Key GPIO_Pin_13
#define LEDCLK RCC_APB2Periph_GPIOB
#define KEYCLK (RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO)
#define ON (BitAction)1
#define OFF (BitAction)0
#define Toggle (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB, LED))
#define KeyPORT GPIO_PortSourceGPIOC
#define KeySOURCE GPIO_PinSource0
#define KeyLINE EXTI_Line0
#define KeyHander EXTI0_IRQHandler
void LEDInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(LEDCLK, ENABLE); // 使能端口时钟
GPIO_InitStructure.GPIO_Pin = LED;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化端口
GPIO_SetBits(GPIOB, LED); // 关闭所有LED
}
void NVICStart()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void InterruptStart()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(KEYCLK, ENABLE); // 使能端口时钟
NVICStart();
GPIO_InitStructure.GPIO_Pin = Key;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_EXTILineConfig(KeyPORT,KeySOURCE);
EXTI_InitStructure.EXTI_Line=KeyLINE;
EXTI_InitStructure.EXTI_Mode= EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger= EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void LEDON()
{
GPIO_WriteBit(GPIOB, LED,ON);
}
void LEDOFF()
{
GPIO_WriteBit(GPIOB, LED,OFF);
}
void LEDToggle()
{
GPIO_WriteBit(GPIOB, LED,Toggle);
}
void delayXus(u16 i)
{
unsigned char t = 0;
for(;i>0;i--)
{
for(t=0;t<2;t++)
{}
}
}
void delayXms(u16 g)
{
while(g--)
{
delayXus(1000);
}
}
void ZD()
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
{
LEDToggle();
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
int main()
{
SystemInit();
LEDInit();
InterruptStart();
LEDOFF();
while(1);
}
|