1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

lcd menu

Discussion in 'Microcontrollers' started by dawson, Oct 23, 2009.

  1. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    Hi, thanks for all the help again. Have just tried your last suggestion and now get:

    "waiting"
    press menu
    ">option 1"
    "option 2"
    press down
    "option 3"
    "option 3"
    press down
    "|:;;\\\ 3"
    "op:\|| 3"
    press up
    "option 1"
    "option 1"
     
  2. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Can u post ur current full code so I can see what I might have misses
     
  3. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    sure here it is:

    Code (text):

    #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;
    unsigned char PAGE=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+PAGE)]);
        }

        switch(WaitForInput()){
            case UP:
                if(POS>0){
                    POS--;
                    ITEM--;
                } else {
            if(PAGE != 0) PAGE--;
          }
                break;
            case DOWN:
                if(POS<1){
                    POS++;
                    ITEM++;
                } else {
            PAGE++;
          }
                break;
            case MENU:
                done = 0xFF;
                break;
        }
    }
        // ITEM contains what item is selected
        // POS isnt used incase larger then 2 menu...
      ITEM+=PAGE;
        switch(ITEM){
            case 0:
                    Delay_ms(2000);
                break;
            case 1:
                    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);
    }
     
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US

    hey try this lol sorry:
    Code (text):

    #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;
    unsigned char PAGE=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+PAGE)]);
        }

        switch(WaitForInput()){
            case UP:
                if(POS>0){
                    POS--;
                    ITEM--;
                } else {
            if(PAGE != 0) PAGE--;
          }
                break;
            case DOWN:
                if(POS<1){
                    POS++;
                    ITEM++;
                } else {
    [B]        PAGE=1;[/B]
          }
                break;
            case MENU:
                done = 0xFF;
                break;
        }
    }
        // ITEM contains what item is selected
        // POS isnt used incase larger then 2 menu...
      ITEM+=PAGE;
        switch(ITEM){
            case 0:
                    Delay_ms(2000);
                break;
            case 1:
                    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);
    }

     
     
  6. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    Almost there :)

    now it displays:

    "Waiting"
    press menu
    ">Option 1"
    "Option 2"
    press down
    "Option 2"
    ">Option 3"
    press UP
    ">Option 1"
    "Option 2"

    so Option 2 can never be selected
     
  7. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    try:
    Code (text):

    #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--;
                } else {
            if(PAGE != 0) PAGE--;
          }
                break;
            case DOWN:
                if(POS<2){
                    POS++;
                    ITEM++;
                } else {
            if(PAGE<1){
              ITEM--;
              POS--;
              PAGE=1;
            }
          }
                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);
    }

     
     
  8. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    The above code should do it. I even changed my code to mimic it in my menu:
    Code (text):

    /* *****************************************************************************
    ;                                                                             *
    ;    Filename:                                                                *
    ;    Date:                                                                    *
    ;    File Version: 001                                                        *
    ;                                                                             *
    ;    Author:   Jason Lopez                                                    *
    ;    Company:  AtomSoft                                                       *
    ;                                                                             *
    ;***************************************************************************** */

    #include <p18F4620.h>
    #include <delays.h>
    #include <string.h>
    #include "lcd.h"
    #include "keys.h"

    #pragma config WDT = OFF, LVP = OFF, OSC = HS, XINST = OFF

    unsigned rom char MainMenuLIST[2][16] = {
    {"Change View "},
    {"Change Speed"}
    };

    unsigned rom char SpeedMenuLIST[3][16] = {
    {"500 mS   "},
    {"1 Second "},
    {"2 Seconds"}
    };
    unsigned rom char Company[16]="  AtomSoftTech  ";
    unsigned rom char SpeedLIST[3][16] = {
    {"Update: 500 mS"},
    {"Update:  1 sec"},
    {"Update:  2 sec"}
    };
    /************************************
    Prototypes
    *************************************/
    void main(void);
    unsigned char DECtoBCD(unsigned char DEC);
    unsigned char DECtoASCII(unsigned char DEC,char part);
    /************************************
    Variables and Defines
    *************************************/
        #define ADCPin TRISAbits.TRISA0 // RA0/AN0 TRIS (DATA DIRECTION REGISTER)

        unsigned int oldresult; //Old Result Variable
        unsigned int result;    //Result Variable
        unsigned int DegC;      //Result in Celsius
        unsigned int DegF;      //Result in Fahrenheit
        unsigned int DelayTime; //Delay Length
        unsigned char MyView;
        unsigned char lcdtmp[10];
        unsigned char avg[10];
        unsigned char CompSpeed;

    /************************************
    Main
    *************************************/
    void main(void){
        unsigned char tmp;
        unsigned long tmp1;
        TRISD = 0x00;
        KeyDIR = 0x0F;

        ADCPin = 1;                     //AN0 = Input
        ADCON0 = 0x00;                  //BIT 7:6 Always 00, BIT 5:2 is 0000 for Channel 0 (AN0),
                                        //BIT 1:0 is 00 (A/D Idle and Module is off) until we are done setting up
        ADCON1 = 0x0E;                  //BIT 7:6 Always 00, BIT 5 is 0 for VSS, BIT 4 is 0 for VDD
                                        //BIT 3:0 is 1110 for all digital expect AN0. We need it analog so we can convert it.
        ADCON2 = 0b10001101;            //BIT 7 is 0 for Left Justified, BIT 6 Always 0, BIT 5:3 001 for 2 TAD (A/D Acquisition Time)
                                        //BIT 2:0 is 100 for FOSC/4 for A/D Conversion Clock
        ADCON0bits.ADON = 1;            //Turn on the A/D module
       
        LCD_Init();
        DelayMS(100);

        LCD_LINE(1);
        LCD_STR(Company);

        LCD_LINE(2);
        LCD_STR((unsigned rom char*)"Temp: ");
        //DEFAULTS
        DelayTime = 500;
        MyView = 'F';
        CompSpeed = 0; //Company = 0, Speed = 1
        while(1){

          for(tmp=0;tmp<5;tmp++){
            ADCON0bits.GO = 1;          //Start the conversion
            while(ADCON0bits.DONE);     //Wait until it’s done
            result = ADRESH;            //Load ADRESH into result
            result <<= 8;               //Shift over Result 8 bits to the left
            result |= ADRESL;           //OR in our LOW byte to finish off out INT
            avg[tmp] = result;  
            DelayMS(40);
          }      
            result = 0;
            for(tmp=0;tmp<5;tmp++){
                result += avg[tmp];
            }
            result /= 5;
            if(result==oldresult) goto tSame;
           
            DegC = (result*5)/10;
            DegC--;

            DegF = DegC * 9;
            DegF/=5;
            DegF+=32;
            //clear LCD temp buffer
            for(tmp=0;tmp<16;tmp++)
                lcdtmp[tmp] = ' ';

        if(MyView == 'F'){
            lcdtmp[0] = DECtoASCII(DegF,'h');
            lcdtmp[1] = DECtoASCII(DegF,'l');
            lcdtmp[2] = 0xDF;
            lcdtmp[3] = 'F';
            lcdtmp[4] = ' ';
        } else {
            lcdtmp[0] = DECtoASCII(DegC,'h');
            lcdtmp[1] = DECtoASCII(DegC,'l');
            lcdtmp[2] = 0xDF;
            lcdtmp[3] = 'C';
            lcdtmp[4] = ' ';
        }

            LCD_LINE(2);
            LCD_STR((unsigned rom char*)"Temp: ");

            LCD_STR2(lcdtmp);
            DelayMS(DelayTime);

            oldresult = result;

    tSame:
           
            if(KeyPORT == MENU){
                DelayMS(10); //Extra Debounce
                MainMenu();
                while(KeyPORT == MENU);
            }
            if(KeyPORT == UP){
                LCD_LINE(1);
                if(CompSpeed == 1){
                    CompSpeed = 0;
                    LCD_STR(Company);
                } else {
                    CompSpeed = 1;
                    switch(DelayTime){
                        case 500:
                            LCD_STR(SpeedLIST[0]);
                            break;
                        case 1000:
                            LCD_STR(SpeedLIST[1]);
                            break;
                        case 2000:
                            LCD_STR(SpeedLIST[2]);
                            break;
                    }
                }
                while(KeyPORT == UP);
            }

        }
    }

    unsigned char DECtoASCII(unsigned char DEC,char part){
    unsigned char temp;
    unsigned char myData;
        temp = DECtoBCD(DEC);

        if(part == 'l')
            myData = temp & 0x0F;

        if(part == 'h')
            myData = (temp >> 4) & 0x0F;
       
        myData += 0x30;
        return myData;
    }
    unsigned char DECtoBCD(unsigned char DEC){
    unsigned char temph;
    unsigned char templ;
    unsigned char myData;
        templ = DEC % 10;
        temph = DEC / 10;
        myData = (temph << 4) | templ;
       
        return myData; //Return myData
    }

    ////////////////////////////////////////////////////////////////////////////
    //// MENU SYSTEM
    ////////////////////////////////////////////////////////////////////////////

    void MainMenu(void){
    unsigned char POS = 0;  //Used for cursor ONLY!
    unsigned char done = 0;
    unsigned char x;
    unsigned char ITEM=0;

    LCD_CLEAR(5);

    while(KeyPORT == MENU);
    while(!done){
        for(x=0;x<2;x++){
            LCD_LINE(x+1);

            if(x == POS)
                LCD_DATA('>',1);
            else
                LCD_DATA(' ',1);

            LCD_STR(MainMenuLIST[x]);
        }

        switch(WaitForInput()){
            case UP:
                if(POS>0){
                    POS--;
                    ITEM--;
                }
                break;
            case DOWN:
                if(POS<1){
                    POS++;
                    ITEM++;
                }
                break;
            case ENTER:
                done = 0xFF;
                break;
        }
    }
        // ITEM contains what item is selected
        // POS isnt used incase larger then 2 menu...

        switch(ITEM){
            case 0:
                //Code to toggle C or F
                if(MyView == 'F')
                    MyView = 'C';
                else
                    MyView = 'F';
                break;
            case 1:
                //Code to jump to new menu system
                SpeedMenu();
                break;
        }
        LCD_LINE(1);
        if(CompSpeed == 0){
            LCD_STR(Company);
        } else {
            switch(DelayTime){
                case 500:
                    LCD_STR(SpeedLIST[0]);
                    break;
                case 1000:
                    LCD_STR(SpeedLIST[1]);
                    break;
                case 2000:
                    LCD_STR(SpeedLIST[2]);
                    break;
            }
        }
        oldresult = 0;
    }
    ////////////////////////////////////////////////////////////////////////////
    //// SPEED MENU SYSTEM
    ////////////////////////////////////////////////////////////////////////////

    void SpeedMenu(void){
    unsigned char POS = 0;  //Used for cursor ONLY!
    unsigned char START = 0;
    unsigned char done = 0;
    unsigned char x;
    unsigned char ITEM=0;
    LCD_CLEAR(5);

    while(!done){
        for(x=0;x<2;x++){
            LCD_LINE(x+1);

            if(x == POS)
                LCD_DATA('>',1);
            else
                LCD_DATA(' ',1);

            LCD_STR(SpeedMenuLIST[(x+START)]);
        }

        switch(WaitForInput()){
            case UP:
                if(POS>0){
                    POS--;
                    ITEM--;
                } else {
                    POS++;
                    ITEM++;
                    START = 0;
                }
                break;
            case DOWN:
                if(POS<1){
                    POS++;
                    ITEM++;
                } else {
                    if(START<1){
                        START = 1;
                        POS--;
                        ITEM--;
                    }
                }
                break;
            case ENTER:
                done = 0xFF;
                break;
        }
    }
        // ITEM contains what item is selected
        // POS isnt used incase larger then 2 menu...
        //START is used to determine which number we are on plus the LCD #
        ITEM+=START;

        switch(ITEM){
            case 0:
                //Code to set 500mS
                DelayTime = 500;
                break;
            case 1:
                //Code to set 1 Second
                DelayTime = 1000;
                break;
            case 2:
                //Code to set 2 Seconds
                DelayTime = 2000;
                break;
        }
    }
    ////////////////////////////////////////////////////////////////////////////
    //// BUTTON SYSTEM
    ////////////////////////////////////////////////////////////////////////////

    unsigned char WaitForInput(void){

        unsigned char temp1;    //TEMP DATA HOLDER
        unsigned char done = 0; //DONE Var will determine if done

        while(!done){                 //while done == 0
            if(KeyPORT>0){            //if KeyPORT aka PORTB&0x0F.. the &0x0f will make sure we
                                      //return the lower part of port only RB0 to RB3
                temp1 = KeyPORT;      //Place the lower part of port into temp variable
                while(KeyPORT==temp1);//Like a debounce for button press
                done = 0xFF;          //Set DONE Var so we know we are complete
            }
        }
        return temp1;                 //Return our temp1 var
    }


     
    I even added the option to show the current update speed by pressing up
     
  9. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    That makes it worse, it now does:

    "waiting"
    press MENU
    ">option 1"
    "option 2"
    Press DOWN
    "Option 1"
    "option 2"

    but it made me notice that I had declared POS and X twice when I removed it from this code it did nothing but when I removed it from the previous revision, it was a little bit better:

    Code (text):

    #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;
    unsigned char PAGE=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+PAGE)]);
        }

        switch(WaitForInput()){
            case UP:
                if(POS>0){
                    POS--;
                    ITEM--;
                } else {
            if(PAGE != 0) PAGE--;
          }
                break;
            case DOWN:
                if(POS<1){
                    POS++;
                    ITEM++;
                } else {
            PAGE=1;
          }
                break;
            case MENU:
                done = 0xFF;
                break;
        }
    }
        // ITEM contains what item is selected
        // POS isnt used incase larger then 2 menu...
      ITEM+=PAGE;
        switch(ITEM){
            case 0:
                    Delay_ms(2000);
                break;
            case 1:
                    Delay_ms(2000);
                break;
        }

        //Bring our title back.. cuz we are done with the menu...


    }


    void main() {
      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);
    }

     
    produces:
    "Waiting"
    press MENU
    ">Option 1"
    "Option 2"
    Press DOWN
    "Option 2"
    ">Option 3"
    press UP
    ">Option 2"
    "Option 3"
    press UP
    ">Option 1"
    "Option 2"

    So it seems to work on the UP key press but not the DOWN
     
  10. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    For the up part try

    <2

    Not <1
     
  11. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    I have also noticed:

    TRISA = (0x0F<<2); // RA5,RA4,RA3,RA2 are set to input

    which means that RA5 is set as an input but is not being used in the code.

    I am using a LVP so RA5 is pulled high all the time would this make a difference?
     
  12. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    Code (text):
    #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;
    unsigned char PAGE=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+PAGE)]);
        }

        switch(WaitForInput()){
            case UP:
                if(POS>0){
                    POS--;
                    ITEM--;
                } else {
            if(PAGE != 0) PAGE--;
          }
                break;
            case DOWN:
                if(POS<2){
                    POS++;
                    ITEM++;
                } else {
            PAGE=1;
          }
                break;
            case MENU:
                done = 0xFF;
                break;
        }
    }
        // ITEM contains what item is selected
        // POS isnt used incase larger then 2 menu...
      ITEM+=PAGE;
        switch(ITEM){
            case 0:
                    Delay_ms(2000);
                break;
            case 1:
                    Delay_ms(2000);
                break;
        }

        //Bring our title back.. cuz we are done with the menu...


    }


    void main() {
      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);
    }
    produces
    ">Option 1"
    "Option 2"
    "Option 1"
    "option 2"
     
  13. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    If u want try...

    TRISA = 0b00011100;
     
  14. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    no that made no difference. I am totally lost now lol
     
  15. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    i think its a hardware issue. Since it works on my side so well..

    Can you post a full schematic and if possible zip your entire code and post it here
     
  16. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    that is my entire code, since we got the buttons working i havent tried to integrate the ds1820 yet. will draw up a schematic and post
     
  17. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    i meant like the project files. Like can you post a zip or something with all files in that folder so i can see how everything is setup
     
  18. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    Here is my project files and schematic
     

    Attached Files:

    • menu.zip
      File size:
      89.2 KB
      Views:
      139
  19. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    You are using MikroC ? Or MikroC Pro?
     
  20. dawson

    dawson New Member

    Joined:
    Sep 21, 2009
    Messages:
    97
    Likes:
    0
    Location:
    UK, north east
    mikroC PRO sorry didnt realise there was a difference
     
  21. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Try This pal:
    Code (text):

    //REV2
     #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 A"},
    {"Option B"},
    {"Option C"}
    };

    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;
    unsigned char PAGE=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+PAGE)]);
        }

        switch(WaitForInput()){
            case UP:
                if(POS>0){
                    POS--;
                    ITEM--;
                } else {
                    if(PAGE>0){
                        POS++;
                        ITEM++;
                        PAGE=0;
                    }
                }
                break;
            case DOWN:
                if(POS<1){
                    POS++;
                    ITEM++;
                } else {
                    if(PAGE<1){
                        PAGE = 1;
                        POS--;
                        ITEM--;
                    }
                }
                break;
            case MENU:
                done = 0xFF;
                break;
        }
    }
            // ITEM contains what item is selected
            // POS isnt used incase larger then 2 menu...
        ITEM+=PAGE;
        switch(ITEM){
            case 0:
                Delay_ms(2000);
                break;
            case 1:
                Delay_ms(2000);
                break;
        }
    }


    void main() {
      CMCON  |= 7;                       // Disable Comparators
      TRISA = 0b00011100;                 // 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);
    }
     
     
    Last edited: Oct 25, 2009

Share This Page