找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4820|回复: 0
收起左侧

SMT32单片机的按键中断控制LED灯亮灭的问题

[复制链接]
ID:462723 发表于 2019-4-3 15:22 | 显示全部楼层 |阅读模式
使4个按键中断来控制两个LED的亮灭。
led1 连接     PA(0)      一直闪烁提示系统正常工作
led2 连接     PA(1)
led3 连接      PA(2)

K_UP        连接    PB(0)  按一下控制led2 亮
K_DOWN  连接    PB(1)  按一下控制led2 灭
K_LEFT    连接     PB(2)   按一下控制led3 亮
K_RIGHT  连接    PB(3)   按一下控制led3 灭



编写的程序编译后没有错误和警告。下载在开发板后,led1一直闪烁提示系统正常工作,按一下K_UP键led2亮,再按一下K_DOWN键led2灭。但是按K_LEFT和K_RIGHT没反应,无法控制led3的亮灭。请那位高手帮忙看下下面程序有什么问题?

20190403_145046.jpg
1.jpg


MAIN.C

#include "system.h"
#include "led.h"
#include "SysTick.h"
#include "key.h"
#include "exti.h"

int main()
{
        u8 i=0;
        SysTick_Init(72);
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        LED_Init();
  KEY_Init();
        My_EXTI_Init();
        
        while(1)
        {
               
                i++;
                if(i%20==0)
                {
                        led1=!led1;
                }
                delay_ms(10);
        }
        
}


EXTI.C
#include "exti.h"
#include "Systick.h"
#include "led.h"
#include "key.h"



void My_EXTI_Init()
{
        NVIC_InitTypeDef NVIC_InitStructure;
        EXTI_InitTypeDef EXTI_InitStructure;


        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1)
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource2)
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource3)




        NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=3
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);
        


        NVIC_InitStructure.NVIC_IRQChannel=EXTI1_IRQn
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=2
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);
        


        NVIC_InitStructure.NVIC_IRQChannel=EXTI2_IRQn
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);


        NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;


        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);
        
        EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line1|EXTI_Line2|EXTI_Line3;
        EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
        EXTI_InitStructure.EXTI_LineCmd=ENABLE;
        EXTI_Init(&EXTI_InitStructure);
        
}





void EXTI0_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line0)==1)
        {
                delay_ms(10);
                if(K_UP==0)
                {
                        led2=1;
                }
               
        }
        EXTI_ClearITPendingBit(EXTI_Line0);
}





void EXTI1_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line1)==1)
        {
                delay_ms(10);
                if(K_DOWN==0)
                {
                        led2=0;
                }
               
        }
        EXTI_ClearITPendingBit(EXTI_Line1);
}



void EXTI2_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line2)==1)
        {
                delay_ms(10);
                if(K_LEFT==0)
                {
                        led3=1;
                }
               
        }
        EXTI_ClearITPendingBit(EXTI_Line2);
}





void EXTI3_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line3)==1)
        {
                delay_ms(10);
                if(K_RIGHT==0)
                {
                        led3=0;
                }
               
        }
        EXTI_ClearITPendingBit(EXTI_Line3);
}


EXTI.H
#ifndef _exti_H
#define _exti_H

#include "system.h"



void My_EXTI_Init(void);

#endif


LED.C
#include "led.h"
#include "system.h"

void LED_Init()
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        
        GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
        
}

LED.H
#ifndef _led_H
#define _led_H


#include "system.h"


void LED_Init(void);


#define led1 PAout(0)
#define led2 PAout(1)
#define led3 PAout(2)


#endif


KEY.C
#include "key.h"
#include "SysTick.h"


void KEY_Init()
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
        
        
}

u8 KEY_Scan(u8 mode)
{
        static u8 key=1;
        if(key==1&&(K_UP==0||K_DOWN==0||K_LEFT==0||K_RIGHT==0))
        {
                delay_ms(10);
                key=0;
                if(K_UP==0)
                {
                        return KEY_UP;
                }
                else if(K_DOWN==0)
                {
                        return KEY_DOWN;
                }
                else if(K_LEFT==0)
                {
                        return KEY_LEFT;
                }
                else if(K_RIGHT==0)
                {
                        return KEY_RIGHT;
                }
        }
        else if(K_UP==1&&K_DOWN==1&&K_LEFT==1&&K_RIGHT==1)
        {
                key=1;
        }
        if(mode==1)
        {
                key=1;
        }
        return 0;
}

KEY.H
#ifndef _key_H
#define _key_H

#include "system.h"

void KEY_Init(void);
u8 KEY_Scan(u8 mode);

#define K_UP    PBin(0)
#define K_DOWN  PBin(1)
#define K_LEFT  PBin(2)
#define K_RIGHT PBin(3)

#define KEY_UP                 1
#define KEY_DOWN         2
#define KEY_LEFT         3
#define KEY_RIGHT 4

#endif

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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