2014-04-24 41 views
0

我遇到了與GPIO中斷相關的問題。 任務是製作一個簡單的UI界面,所以我需要使用3個按鈕。 問題是,我不明白如何使用GPIO中斷用於不同的引腳,並且所有的按鈕都以相同的方式工作。針對PSoC 1中不同引腳的GPIO中斷

這裏是代碼:

#include <m8c.h>  // part specific constants and macros 
#include "PSoCAPI.h" // PSoC API definitions for all User Modules 
#include <stdio.h> 
#include <stdlib.h> 

typedef struct { 
    int value; // the actual value which is used in the module 
    char string[16]; // string that is printed in LCD for user 
} UI_ELEMENT; 

#define FIRST_LEVEL 3 
#define SECOND_LEVEL 3 

#define PWM 0 
#define PGA 1 
#define ADC 2 

#define PWM_STATE 0 
#define PWM_PERIOD 1 
#define PWM_WIDTH 2 

#define PWM_STATE_OFF 0 
#define PWM_STATE_ON 1 

volatile int buttonRightPressed = 0; 

#pragma interrupt_handler buttonRightInt 
void buttonRightInt(void){ 
    // disable button interrupt 
    M8C_DisableIntMask(INT_MSK0, INT_MSK0_GPIO); 
    buttonRightPressed = 1; 
} 

void initialize_LCD(void){ 
    LCD_Position(0,0); 
    LCD_PrCString("PWM"); 
    LCD_Position(1,0); 
    LCD_PrCString("< select >"); 
} 

void update_LCD(int* lvl1){ 
    if (*lvl1 == PWM || *lvl1 == 3){ 
     LCD_Position(0,0); 
     LCD_PrCString("PWM"); 
     *lvl1 = 0; 
    } 
    else if (*lvl1 == PGA){ 
     LCD_Position(0,0); 
     LCD_PrCString("PGA"); 
    } 
    else if (*lvl1 == ADC){ 
     LCD_Position(0,0); 
     LCD_PrCString("ADC"); 
    } 
} 

void main(void) 
{ 
    UI_ELEMENT userInterface[FIRST_LEVEL][SECOND_LEVEL]; 
    int level_1_steper = PWM; 
    int i; 

    M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts 
    PWM8_EnableInt(); 
    LCD_Start(); 
    M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO); 

    initialize_LCD(); // set 'PWM' for upper row, '<select>' for lower row 

    while (1){ 
     if (buttonRightPressed == 1){ 
      for (i = 0; i < 350; i++); 
      level_1_steper++; 
      update_LCD(&level_1_steper); 
      buttonRightPressed = 0; 
      // enable button interrupt again 
      M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO); 
     } 

    } 
} 
+0

在沒有指定您正在使用哪個MCU的情況下說GPIO中斷是毫無意義的。我猜這就是所謂的帶有ARM Cortex M0的賽普拉斯PSoC?另外,你需要去掉一個按鈕,你不能像任何I/O一樣簡單地閱讀它。由於按鈕有信號反彈,你很可能不希望邊沿觸發的中斷響應它們,或者你需要一個外部RC濾波器來消除反彈。 – Lundin

+0

問題已解決!通常,解決方案非常簡單:使用GPIO中斷,但測試哪個按鈕已被按下。 GPIO iterrupt:'void buttonInt(void){0}禁用按鈕中斷 \t M8C_DisableIntMask(IN​​T_MSK0,INT_MSK0_GPIO);如果(Right_Data_ADDR&Right_MASK) \t \t buttonRightPressed = 1; \t if(Left_Data_ADDR&Left_MASK) \t \t buttonLeftPressed = 1; (Select_Data_ADDR&Select_MASK) \t if(Select_Data_ADDR&Select_MASK) \t \t buttonSelectPressed = 1; }' – mikedanylov

回答

0

問題解決了!通常,解決方案非常簡單:使用GPIO中斷,但測試哪個按鈕已被按下。 GPIO iterrupt:

void buttonInt(void){ // disable button interrupt 
    M8C_DisableIntMask(INT_MSK0, INT_MSK0_GPIO); 
    if (Right_Data_ADDR & Right_MASK) buttonRightPressed = 1; 
    if (Left_Data_ADDR & Left_MASK) buttonLeftPressed = 1; 
    if (Select_Data_ADDR & Select_MASK) buttonSelectPressed = 1; 
}