找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1791|回复: 3
收起左侧

stm32中断用不了怎么回事

[复制链接]
ID:466381 发表于 2019-2-24 00:25 | 显示全部楼层 |阅读模式
刚刚开始学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);
}


回复

使用道具 举报

ID:433680 发表于 2019-2-24 07:51 | 显示全部楼层
stm32中断应使用在 stm32f10x_it.c
Main Interrupt Service Routines

评分

参与人数 1黑币 +60 收起 理由
admin + 60 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:466381 发表于 2019-2-26 23:42 | 显示全部楼层
laigs218 发表于 2019-2-24 07:51
stm32中断应使用在 stm32f10x_it.c
Main Interrupt Service Routines

谢谢帮忙!!!!
回复

使用道具 举报

ID:298850 发表于 2019-2-27 08:34 | 显示全部楼层
一脸懵逼。中断函数名写错了吧?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表