一个简单的外部中断的小程序,帮忙看下哪里错了,谢谢
时间:10-02
整理:3721RD
点击:
使用的是STM32f107vc芯片,想要达到的效果是LED1亮,按下Key1,LED1灭,再按下Key1,LED1灯亮,如此循环:
#include "stm32f10x.h"
#define BitBand(addr,n) *(volatile unsigned long*)((addr & 0xf0000000)+0x20000000+(addr & 0xfffff)*32+n*4) //位绑定公式
#define GPIOC_ODR_A (GPIOC_BASE+0x0c)
#define GPIOC_IDR_A (GPIOC_BASE+0x08)
#define GPIOD_ODR_A (GPIOD_BASE+0x0c)
#define GPIOD_IDR_A (GPIOD_BASE+0x08)
#define PCout(n) BitBand(GPIOC_ODR_A,n)
#define PCin(n) BitBand(GPIOC_IDR_A,n)
#define PDout(n) BitBand(GPIOD_ODR_A,n)
#define PDin(n) BitBand(GPIOD_IDR_A,n)
#define Key1 PCin(4)
#define LED1 PDout(2)
void RCC_Configuration(void);
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
EXTI_Configuration();
NVIC_Configuration();
LED1=0; //开始点亮LED灯
while(1);
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource4);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);
if(Key1==0) //判断是否有中断
{
LED1=!LED1; //LEd灯翻转
}
EXTI_ClearITPendingBit(EXTI_Line4); //清空中断标志位
}
#include "stm32f10x.h"
#define BitBand(addr,n) *(volatile unsigned long*)((addr & 0xf0000000)+0x20000000+(addr & 0xfffff)*32+n*4) //位绑定公式
#define GPIOC_ODR_A (GPIOC_BASE+0x0c)
#define GPIOC_IDR_A (GPIOC_BASE+0x08)
#define GPIOD_ODR_A (GPIOD_BASE+0x0c)
#define GPIOD_IDR_A (GPIOD_BASE+0x08)
#define PCout(n) BitBand(GPIOC_ODR_A,n)
#define PCin(n) BitBand(GPIOC_IDR_A,n)
#define PDout(n) BitBand(GPIOD_ODR_A,n)
#define PDin(n) BitBand(GPIOD_IDR_A,n)
#define Key1 PCin(4)
#define LED1 PDout(2)
void RCC_Configuration(void);
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
EXTI_Configuration();
NVIC_Configuration();
LED1=0; //开始点亮LED灯
while(1);
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource4);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);
if(Key1==0) //判断是否有中断
{
LED1=!LED1; //LEd灯翻转
}
EXTI_ClearITPendingBit(EXTI_Line4); //清空中断标志位
}
可能是你IO口配置的问题,模式设为_ipu看下,设为上拉
可能是你IO口配置的问题,模式设为_ipu看下,设为上拉
可能是你IO口配置的问题,模式设为_ipu看下,设为上拉看看
先说明你的问题现象吧,这样好判断,如果连中断都没进入,那讨论中断程序也没什么意义
STM32吗?
是的,stm32f107vc
现象就是没什么反应,按下Key1 没变化,我在主程序在写个其他判断程序在试试,看看进没进中断
试过了,上下拉没用
如果软件没有问题的话,先用万用表测一下你那个设置的中断引脚到底有没有发生中断(电压的变化)。
尼玛。问题找到了,是位绑定公式写错了!0x20000000多写了个0改成0x200000!位绑定函数是粘贴复制之前的,没检查。真是大问题!