How To Store Multiple Keys in Arrays?

Thread Starter

markyoon

Joined May 12, 2020
6
Hi all,

I'm new to microcontroller programming. I would like to ask how do I display multiple keys on my LCD display. An example is I want to show "123456". Below is the codes I'm using. I am using PIC18F4520 and this is my LCD datasheet. Thanks in advance!

2020-07-20 (1).png

Code:
#include <xc.h>

void Init_LCD(void); // Initialize the LCD
void LCD_sendCW(char); // 8-bit Control word for LCD
void LCD_sendData(char); // 8-bit Text Data for LCD
char key_set();

#define LCD_DATA PORTD"
#define LCD_RS PORTBbits.RB1 // RS signal for LCD
#define LCD_E PORTBbits.RB2 // E signal for LCD

#define A PORTAbits.RA0 
#define B PORTAbits.RA1
#define C PORTAbits.RA2 
#define D PORTAbits.RA3

#define MAX_DATA_LENGTH 6

char DataEntered[MAX_DATA_LENGTH];
char DataPointer = 0;



void main()
{
    int i,x;
    ADCON1 = 0x0F;
    TRISA = 0xFF;
    TRISD = 0;
    Init_LCD(); // Initialize LCD 8-bit interface,multiple line

    
    while (1)
    {
        for (x=0;x<6;x++)
        {
        DataEntered[DataPointer] = get_key();
        if (DataPointer < MAX_DATA_LENGTH)
        {
            DataPointer++;
        }
        }
        LCD_sendCW(0b11000000);
        for (i=0; DataEntered[DataPointer]!=0; i++)
        LCD_sendData(DataEntered[DataPointer]);
                    
    }
}

char key_set(){
    
    char dataIn;
    
    if (PORTAbits.RA4==1)
    {
        
        dataIn = PORTA;
        
        switch(dataIn)
        { 
        case 0: LCD_sendData('1');
           break;
          
        case 1: LCD_sendData('2');
           break;
          
        case 2: LCD_sendData('3');
           break;
        
        case 3: LCD_sendData('A');
           break;
          
        case 4: LCD_sendData('4');
           break;   
          
        case 5: LCD_sendData('5');
           break;
          
        case 6: LCD_sendData('6');
           break;
          
        case 7: LCD_sendData('B');
           break;
          
        case 8: LCD_sendData('7');
           break;
          
        case 9: LCD_sendData('8');
           break;
          
        case 10: LCD_sendData('9');
           break;
          
        case 11: LCD_sendData('C');
           break;
          
        case 12: LCD_sendData('*');
           break; 
          
        case 13: LCD_sendData('0');
           break;
          
        case 14: LCD_sendData('#');
           break; 
          
        case 15: LCD_sendData('D');
        }
    }
}



void Init_LCD(){
LCD_sendCW(0b00111000); // Function Set - 8-bit, 2 lines, 5X7
LCD_sendCW(0b00001100); // Display on, cursor on
LCD_sendCW(0b00000111); // Entry mode - inc addr, no shift
LCD_sendCW(0b00000001); // Clear display
LCD_sendCW(0b00000010); // Return cursor to home position
}
/* Write control word to LCD */
void LCD_sendCW(char x){
 LCD_RS = 0;
 LCD_E = 1;
 LCD_DATA = x;
 LCD_E = 0;
 _delay(1000); // 2ms delay
}
/* Write text data to LCD */
void LCD_sendData(char x){
 LCD_RS = 1;
 LCD_E = 1;
 LCD_DATA = x;
 LCD_E = 0;
 _delay(500); // 1ms delay
}
 

jpanhalt

Joined Jan 18, 2008
10,068
It looks like you are trying to initialize an HD44780 (Hitachi) compatible character display. Are you using 4-bit or 8-bit mode?

If that is completely off base, please give more infromation about yout display and how you are communicating with it ( e.g., parallel, serial, if serial, protocol).
 

Ian Rogers

Joined Dec 12, 2012
792
LCD+sendCW(b1100000); is placing your print location at the same point so the data is overwritten each key press.

Lose this command until all the array is printed.

EDIT.. Scratch that.... following the main isn't simple....
for (i=0; DataEntered[DataPointer]!=0; i++)
This looks dubious though.... I hope your array contains a 0 somewhere..
 
Last edited:

AlbertHall

Joined Jun 4, 2014
10,226
Try these changes starting at line 34:
Code:
        DataPointer = 0;                                             // <= added line
        for (x=0;x<MAX_DATA_LENGTH;x++)            // <= modified line
        {
        DataEntered[DataPointer] = get_key();
        //if (DataPointer < MAX_DATA_LENGTH)      // <= line deleted
        //{                                                                  // <= line deleted
            DataPointer++;
        //}                                                                  // <= line deleted
        }
        LCD_sendCW(0b11000000);
        DataPointer = 0;                                            // <= line added
        for (i=0; i,MAX_DATA_LENGTH; i++)             // <= line modified
        LCD_sendData(DataEntered[DataPointer]);
 
Top