Switch Input Code Help

Status
Not open for further replies.

Suraj143

Active Member
Hi I have connected 4 switches to PORTA0, 1, 2, 3.They are active low types.

I need to detect if any key is pressed then the function must be called. I think there are some syntax errors. Please correct them. I use mikroC.
Code:
while(1){
          temp= 0b00001111 & PORTA;
          temp = temp ^ 0b00001111;
          if(temp>0){             // new key found
          Key_Scan();		  // do action on each switch
          Update_LCD();          // if new key found update the display
     }
  }
 
Last edited:
I have never used mikroC but I will give you an example of how I would do it in Microchip C32. You should be able to work it out from there. If you have any questions just ask


Code:
#define      sw1      !PORTAbits.RA0
#define      sw2      !PORTAbits.RA1
#define      sw3      !PORTAbits.RA2
#define      sw4      !PORTAbits.RA3

while(1){
      if(sw1){
            // do action for switch number 1
      }
      if(sw2){
            // do action for switch number 2
      }
      if(sw3){
            // do action for switch number 3
      }
      if(sw4){
            // do action for switch number 4
      }
      
      UpdateLCD();
}
 
Last edited:
Hi Gobbledok

I need to keep the new button press routine separate from other functions.In other words I need to detect a new button pressed before do action to the particular button.

The red colour area is the other functions.

while(1){
temp= 0b00001111 & PORTA;
temp = temp ^ 0b00001111;
if(temp>0){ // new key found
Key_Scan(); // do action on each switch
Update_LCD(); // if new key found update the display

}
}
}[/CODE]
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…