找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5229|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

SMT32單片機(jī)的按鍵中斷控制LED燈亮滅的問題

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
使4個按鍵中斷來控制兩個LED的亮滅。
led1 連接     PA(0)      一直閃爍提示系統(tǒng)正常工作
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 滅



編寫的程序編譯后沒有錯誤和警告。下載在開發(fā)板后,led1一直閃爍提示系統(tǒng)正常工作,按一下K_UP鍵led2亮,再按一下K_DOWN鍵led2滅。但是按K_LEFT和K_RIGHT沒反應(yīng),無法控制led3的亮滅。請那位高手幫忙看下下面程序有什么問題?





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

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表