#define MENU 2
#define UP 3
#define DOWN 4
// LCD connections definitions
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD connections definitions
unsigned char MainMenuLIST[3][16] = {
{"Option 1"},
{"Option 2"},
{"Option 3"}
};
unsigned char WaitForInput(void){
unsigned char done = 0; //DONE Var will determine if done and hold key
while(!done){ //while done == 0
if (Button(&PORTA, MENU, 1, 0))
done = MENU;
if (Button(&PORTA, UP, 1, 0))
done = UP;
if (Button(&PORTA, DOWN, 1, 0))
done = DOWN;
}
return done; //Return our done/key var
}
void MainMenu(void){
unsigned char POS = 0; //Used for cursor ONLY!
unsigned char done = 0;
unsigned char x;
unsigned char ITEM=0;
Lcd_Cmd(_LCD_CLEAR);
while(!done){
for(x=0;x<2;x++){
if(x == POS)
Lcd_Out((x+1),1,">");
else
Lcd_Out((x+1),1," ");
Lcd_Out((x+1),2,MainMenuLIST[x]);
}
switch(WaitForInput()){
case UP:
if(POS>0){
POS--;
ITEM--;
}
break;
case DOWN:
if(POS<1){
POS++;
ITEM++;
}
break;
case MENU:
done = 0xFF;
break;
}
}
// ITEM contains what item is selected
// POS isnt used incase larger then 2 menu...
switch(ITEM){
case 0:
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 3, "temp ");
Delay_ms(2000);
break;
case 1:
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 3, "Time");
Delay_ms(2000);
break;
}
//Bring our title back.. cuz we are done with the menu...
}
void main() {
unsigned char POS = 0;
unsigned char x;
CMCON |= 7; // Disable Comparators
TRISA = (0x0F<<2); // RA5,RA4,RA3,RA2 are set to input
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1, 3, "Waiting");
do {
switch(WaitForInput()){
case MENU:
Delay_ms(100);
MainMenu();
break;
}
} while (1);
}