Connect with us

Need help storing keypad input - Hi-Tech C

Discussion in 'Microcontrollers, Programming and IoT' started by Sadlercomfort, Apr 8, 2017.

Scroll to continue with content
  1. Sadlercomfort

    Sadlercomfort Ash

    424
    53
    Feb 9, 2013
    Hi Guys,

    I'm trying to write a program which will record the pressed keys on the keypad, after the person hits enter from an external standard push button.

    As the user presses buttons, the numbers get written on an LCD display one by one.


    This works nicely, but I now need to record all the digits pressed.. and store them into one variable. I'm finding this very difficult..

    heres the code so far (without recording anything, just writing the digits to LCD):


    Code:
    unsigned char i = 0;    //Variable for LCD char position
    unsigned char value;
    
    Increment ()
    {
        if (i<19)
        {
            i = i++;
        }
    }
    
    Keypad_init()
    {
    TRISD = 0b00000000;        //Keypad Rows set as inputs  
    
    TRISCbits.TRISC5 = 1;
    TRISCbits.TRISC6 = 1;        //Keypad Columns set as outputs
    TRISCbits.TRISC7 = 1;
    }
    
    Keypad_scan()
    {
        PORTD=0b10000000;
            __delay_us(1);      
            if (RC7==1 && RC6==0 && RC5==0) {    //#
            i = 0;
            LCD_cmd(LCD_LINE4);                        //* Clear all
            LCD_string("                    ");
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==1 && RC5==0) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110000);                //0
            Increment();
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==0 && RC5==1) {  
            i = 0;
            LCD_cmd(LCD_LINE4);                        //* Clear all
            LCD_string("                    ");
            __delay_ms(250);
            }
    
            PORTD=0b01000000;
            __delay_us(1);
            if (RC7==1 && RC6==0 && RC5==0) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00111001);                //9
            Increment();
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==1 && RC5==0) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00111000);                //8
            Increment();
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==0 && RC5==1) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110111);                //7
            Increment();
            __delay_ms(250);
            }
    
    
            PORTD=0b00100000;
            __delay_us(1);
            if (RC7==1 && RC6==0 && RC5==0) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110110);                //6
            Increment();
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==1 && RC5==0) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110101);                //5
            Increment();
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==0 && RC5==1) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110100);                //4
            Increment();  
            __delay_ms(250);  
            }
    
            PORTD=0b00010000;
            __delay_us(1);
            if (RC7==1 && RC6==0 && RC5==0) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110011);                //Write 3
            Increment();
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==1 && RC5==0) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110010);                //Write 2 to LCD
            Increment();
            __delay_ms(250);
            }
    
            if (RC7==0 && RC6==0 && RC5==1) {
            LCD_cmd(LCD_LINE4 + i);
            LCD_data(0b00110001);                //Write 1 to LCD
            Increment();
            __delay_ms(250);
            }
    }
     
  2. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    Hi Ash. Where do you want to store the values? What micro are you using?
    Adam
     
  3. Sadlercomfort

    Sadlercomfort Ash

    424
    53
    Feb 9, 2013
    Hi Adam,

    PIC16F877A

    The above code is in a header file where the Keypad_scan() function is called from the main program. I would like the above program to combine each pressed key, into one value, then maybe return it. I will probably store this in the eeprom.

    The person can write up too 20 characters, which is probably too much.. so I will be reducing this to 4.
     
  4. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    Hi Ash
    have you had a look on the MC website. They have lots of apps. If not I can send you some code from a kindle book I have but might not be until Monday. I am on my tablet and not sure if I can copy text. Let me see if I can.
    Adam
     
  5. Sadlercomfort

    Sadlercomfort Ash

    424
    53
    Feb 9, 2013
    I'm not sure which website your talking about..
     
  6. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    Microchip...you know the ones that make the chip lol
     
  7. Sadlercomfort

    Sadlercomfort Ash

    424
    53
    Feb 9, 2013
    lol, yeah i know microchip.. been a long day
     
    Arouse1973 likes this.
  8. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    Just tried and I cant copy easily from my tablet. I will try from my PC latér.
    Adam
     
  9. Sadlercomfort

    Sadlercomfort Ash

    424
    53
    Feb 9, 2013
    Okay, thanks.

    What does the microchip website have? Don't think I've ever used there resources.
     
  10. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    Why dont you have a look :) I think you will find it interesting.
    Adam
     
  11. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    As promised, this may help.
    Adam

    Eprom1.PNG
    Eprom2.PNG
    Eprom3.PNG

    Eprom4.PNG
    Eprom5.PNG
     
  12. Sadlercomfort

    Sadlercomfort Ash

    424
    53
    Feb 9, 2013
    Thanks, Hi-tech C has a "eeprom_routines.h" file which can be used quite easily, similar to what you have shown. I haven't attempted the eeprom yet.

    I'm now having even more issues with my microcontroller, which goes into unexpected reset.
     
  13. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    If you want a hand to sort the reset problem out. Post your circuit here and explain in detail what's happening and when.
    Cheers
    Adam
     
  14. Sadlercomfort

    Sadlercomfort Ash

    424
    53
    Feb 9, 2013
    I seem to have fixed the reset problem, I checked everything from the master clear circuit to the decoupling capacitors but everything was fine.

    I'm guessing it was something to do with my interrupt routines, causing a reset.

    I decided not to use my keypad, It's just to difficult to display the typed characters.. record each typed character and concatenate them together. I'm using a rotary encoder instead.. much easier to record a value since its a single variable thats incremented or decremented depending on the rotation.


    P.S Learnt the word concatenate today and just had to use it!
     
  15. Arouse1973

    Arouse1973 Adam

    5,164
    1,080
    Dec 18, 2013
    Ok Cool
    Adam
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-