Juan Esteban Paz
Published © CC BY

LCD Menu Interface with Buttons

A great way to save resources (pins) and make a device with plenty of configurations and information in different screens.

BeginnerProtip30 minutes2,670
LCD Menu Interface with Buttons

Things used in this project

Story

Read more

Schematics

PSOC Workspace

Code

Main.c

C/C++
#include <device.h>
#include <stdlib.h> 

//LED
#define Green() {PrISM_WritePulse0(0x0u);PrISM_WritePulse1(0x00u);}
#define Red()   {PrISM_WritePulse0(0xFFu); PrISM_WritePulse1(0xFFu);}
#define Yellow(){PrISM_WritePulse0(0xFFu); PrISM_WritePulse1(0x00u);}
#define NOC()   {PrISM_WritePulse0(0x0u);PrISM_WritePulse1(0xFFu); }
//LCD
#define clr() LCD_ClearDisplay();

//EEPROM
cystatus status;
uint8 ram_array[10]; // size of this array and the 
static const uint8 CYCODE eepromArray[]=  { 0,0,0,0,0,0,0,0,0,0 };
uint16 user_array[5];


//HMI VARIABLES
uint8 whichkey,menuoffset=1,set=0,run=0,fill=0;
//BUTTONS
CY_ISR(ENTER){whichkey=1;}//ok
CY_ISR(ESC)  {whichkey=2;}//ok
CY_ISR(UP)   {whichkey=3;}//ok
CY_ISR(DWN)  {whichkey=4;}//ok

double SPspeedA,SPspeedB,angleA,angleB,time;//machine or device variables
char msg[4];

//EEPROM TRANSFER
void user_eeprom(uint8 * dest, uint8 *  src,uint8 size){// size - size of the src and dest in bytes.
     uint8 i=0;
     for(i=0;i<size;i++){ *(dest+i) = *(src + i); }
}
void Storeparam();
void Menu(void);

void rundisable(void){
run=0;   
}
void Init(void){
    EEPROM_Start();    
    CyDelay(100);    
   if (*(volatile uint8 *) &eepromArray[0] == 0){//First RUN write Default values        
        user_array[0]= 300;//g
        user_array[1]= 1000;//ms
        user_array[2]= 100;//%
        user_array[3]= 1;//gainA
        user_array[4]= 0;//count
        user_eeprom((uint8*)&ram_array,(uint8*)(&user_array), 10);
        status = EEPROM_Write(ram_array,eepromArray,10);
        if (CYRET_SUCCESS != status){}
        return;
    }
    else{//2nd run
        user_eeprom((uint8*)(&user_array), (uint8*)&eepromArray, 10);
        SPspeedA=user_array[0];
        SPspeedB=user_array[1];
        angleA=user_array[2];
        angleB=user_array[3];
        time=user_array[4];
        user_eeprom((uint8*)&ram_array,(uint8*)(&user_array), 10);
        status = EEPROM_Write(ram_array,eepromArray,10);       
    }
    //LED test sequence
    PrISM_Start();
    Green();CyDelay(80);
    Red();CyDelay(80);
    Yellow();CyDelay(80);
    NOC();
       
    set=0;
    //I2C and LCD startup
    CyDelay(200u);
    I2C_Start();
    CyDelay(200u);
    LCD_Start();
    CyDelay(1000u);
    clr();Menu();
   //ISR Buttons
   upISR_StartEx(UP);
   dwnISR_StartEx(DWN);
   escISR_StartEx(ESC);
  entrISR_StartEx(ENTER);
    whichkey=0;   
}

int main(){	 
    uint8 conf=0,mode=0;  
    CyGlobalIntEnable;
    Init();  
    for(;;){         
        switch(whichkey) {
            case 0:{//no pressed key 
                if((run==1)&&(menuoffset==2)){ //RUN
                    CyDelay(175);
                    LCD_Position(0u,10u);LCD_PrintString("    "); //clear the previous data
                    LCD_Position(0u,10u);LCD_PrintNumber(SPspeedA);//update
                    LCD_Position(1u,10u);LCD_PrintString("    ");//clear the previous data
                    LCD_Position(1u,10u);LCD_PrintNumber(SPspeedB);                 
                }
                if((set>0)&&(set<4)){//Parameter Change
                    LCD_Position(1,9);LCD_PrintString("    ");Menu();CyDelay(50);Yellow();//blink the data that is being set              
                }  //parameters     
                break;
            }//No key         
            case 1:{
                switch(menuoffset){               
                    case 2:{mode=1;run=1;Yellow();upISR_Disable();dwnISR_Disable();clr();Menu();CyDelay(600);break;}
                    case 3:{set=1;Yellow();CyDelay(300);break;}//set SpeedA
                    case 4:{set=2;Yellow();CyDelay(300);break;}//set SpeedB
                    case 5:{set=3;Yellow();CyDelay(300);break;}//set Time
                    case 6:{upISR_Disable();dwnISR_Disable();break;}//test
                    case 9://confirm menu
                        {if(conf==1){mode=0;run=0;menuoffset=2;rundisable();conf=0;upISR_Enable();dwnISR_Enable();NOC();clr();Menu();CyDelay(400);}break;}           
                }                             
                clr();Menu();                
                whichkey=0;
                break;}//Enter
            case 2:{             
                switch(menuoffset){
                    case 2:{if(run==1){CyDelay(400);menuoffset=9;conf=1;clr();}}break;
                    case 3 ... 5:{
                        if((set>0)&&(set<4)){set=0;Storeparam();NOC();}                    
                        if(menuoffset==5){set=0;Storeparam();NOC();}break; }                   
                    case 6:{CyDelay(50);}break;
                    case 9:{if(conf==1){CyDelay(300);menuoffset=2;clr();conf=0;}}break;
                }
                Menu();
                whichkey=0;                     
                break;}//ESC
            case 3:{ //up/
                if((run==0)&&(set>0)){
                    switch(set){
                        case 1:{SPspeedA++; break;}//SpeedA+
                        case 2:{SPspeedB++; break;}//SpeedB+
                        case 3:{time++;     break;}//Time+                        
                    }
                    Green();CyDelay(75);NOC();CyDelay(25);clr();
                }else if(menuoffset<7){menuoffset++;CyDelay(300);clr();}
                Menu();    
                whichkey=0;
                break;}//UP            
            case 4:{//down
                if((run==0)&&(set>0)){
                    switch(set){
                        case 1:SPspeedA--;  break;//SpeedA-
                        case 2:SPspeedB--;  break;//SpeedB-
                        case 3:{time--;     break;}//Time-
                    }
                    Green();CyDelay(75);NOC();CyDelay(25);clr();
                    
                }else if ((menuoffset>2)&&(menuoffset<7)){menuoffset--;CyDelay(500);clr();}                
                Menu();
                whichkey=0;
                break;}//Down     
            }            
        }
}
void Menu(void){     
    switch(menuoffset){
        case 1:{
            LCD_Position(0u,4u); LCD_PrintString("Welcome");
            LCD_Position(1u,0u); LCD_PrintString("press Up . . ."); 
            break;}//MainScreen
        case 2:{
            LCD_Position(0u,0u); LCD_PrintString("RUN:");
            LCD_Position(0u,14u);LCD_PrintString("m/s");            
            LCD_Position(1u,14u);LCD_PrintString("m/s");  
            break;}//RunScreen
        case 3:{                         
            LCD_Position(0u,0u); LCD_PrintString("Config. Speed A:");        	
            LCD_Position(1u,14u);LCD_PrintString("m/s"); 
            LCD_Position(1u,9u); LCD_PrintNumber(SPspeedA);
            break;}//Set SPEEDA 
        case 4:{               
            LCD_Position(0,0); LCD_PrintString("Config. Speed A:");
            LCD_Position(1,14);LCD_PrintString("m/s"); 
        	LCD_Position(1,9); LCD_PrintNumber(SPspeedB);   
            break;}//Set SPEEDB 
        case 5:{               
            LCD_Position(0,0); LCD_PrintString("time:");
        	LCD_Position(1,14);LCD_PrintString("s");
            LCD_Position(1,10);LCD_PrintNumber(time);
        break;}//Set TIME
        case 6:{ 
            LCD_Position(0,10);LCD_PrintString("TESTING..."); 
            LCD_Position(1,9); LCD_PrintNumber(angleA); 
            LCD_Position(1,9); LCD_PrintNumber(angleB); 
        break;}//Testing
        case 9:{
            LCD_Position(0,0);LCD_PrintString("Sure want to Exit?:");            
            LCD_Position(1,0);LCD_PrintString("Press Enter");            
            break;}//Confirmar
    } 
}

void Storeparam(){//emeeprom store param
    user_array[0]= SPspeedA;
    user_array[1]= SPspeedB;
    user_array[2]= angleA;
    user_array[3]= angleB;
    user_array[4]= time;
    user_eeprom((uint8*)&ram_array,(uint8*)(&user_array), 10);
    status = EEPROM_Write(ram_array,eepromArray,10);
}

Credits

Juan Esteban Paz

Juan Esteban Paz

13 projects • 27 followers
Mechanical and Electrical Engineer enjoy working with PSOC

Comments