Keypad Interfacing with LCD in ATmega328P

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Having a Problem where the LCD Doesn't show anything and the Keypad only takes 0 3 and 7 but doesn't display them. 

 

 

.EQU KEY_PC = PORTC; For Rows
.EQU KEY_PIN = PINC; For Columns
.EQU KEY_PD = PORTD;
.EQU KEY_PB = PORTB;
.EQU KEY_C = DDRC;
.EQU KEY_D = DDRD;
.EQU KEY_B = DDRB;
.EQU RS = 0;
.EQU EN = 1;
.DEF KEYVAL = R16;
.DEF TEMP = R17;

Main:
        CALL LCD;

Start:
        ; Initialize Stack Pointer
        LDI KEYVAL,LOW(RAMEND);
        OUT SPL,KEYVAL;
        LDI KEYVAL,HIGH(RAMEND);
        OUT SPH,KEYVAL; 

        LDI TEMP,0XFF;
        OUT KEY_D,TEMP; Make PortD Output
        OUT KEY_B,TEMP;

Key_Init:
        ; Make Columns as Inputs and Rows as Outputs
        LDI KEYVAL,0XF0; 
        OUT KEY_C,KEYVAL; 

Pullups:
        ; Enable Pullups On Columns
        LDI KEYVAL,0X0F;
        OUT KEY_PC,KEYVAL;    

Key_Pressed:
        NOP;
        IN TEMP,KEY_PIN; Read Key Pins
        ANDI TEMP,0X0F; Mask Unused Bits
        CPI TEMP,0X0F; ("R17" == 0x0F) If No Key Is Pressed
        BRNE Key_Pressed; Check If a Key Is Released

Get_Key:
        NOP;
        IN TEMP,KEY_PIN; Read Key Pins
        ANDI TEMP,0X0F; Mask Unused Bits
        CPI TEMP,0X0F; ("R17" == 0x0F) If No Key Is Pressed
        BREQ Get_Key; Check If a Key Is Pressed
        CALL Delay_15ms;

        IN TEMP,KEY_PIN; Read Key Pins
        ANDI TEMP,0X0F; Mask Unused Bits
        CPI TEMP,0X0F; ("R17" = 0x0F) If No Key Is Pressed
        BREQ Get_Key; Check If a Key Is Pressed

        LDI TEMP,0X7F; Make Row1 Low
        OUT KEY_PC,TEMP; Send To PORTC
        NOP;
        IN TEMP,KEY_PIN; Read All Columns
        ANDI TEMP,0X0F;
        CPI TEMP,0X0F;
        BRNE Column1; 

        LDI TEMP,0XBF; Make Row2 Low
        OUT KEY_PC,TEMP; Send To PORTC
        NOP;
        IN TEMP,KEY_PIN; Read All Columns
        ANDI TEMP,0X0F;
        CPI TEMP,0X0F;
        BRNE Column2; 

        LDI TEMP,0XDF; Make Row3 Low
        OUT KEY_PC,TEMP; Send To PORTC
        NOP;
        IN TEMP,KEY_PIN; Read All Columns
        ANDI TEMP,0X0F;
        CPI TEMP,0X0F;
        BRNE Column3; 

        LDI TEMP,0XEF; Make Row4 Low
        OUT KEY_PC,TEMP; Send To PORTC
        NOP;
        IN TEMP,KEY_PIN; Read All Columns
        ANDI TEMP,0X0F;
        CPI TEMP,0X0F;
        BRNE Column4; 
        RJMP Get_Key;

Column1:
        LDI R30,LOW(Key_Code1 << 1);
        LDI R31,HIGH(Key_Code1 << 1);
        RJMP Find;
Column2:
        LDI R30,LOW(Key_Code2 << 1);
        LDI R31,HIGH(Key_Code2 << 1);
        RJMP Find;
Column3:
        LDI R30,LOW(Key_Code3 << 1);
        LDI R31,HIGH(Key_Code3 << 1);
        RJMP Find;
Column4:
        LDI R30,LOW(Key_Code4 << 1);
        LDI R31,HIGH(Key_Code4 << 1);
        RJMP Find;

Find:
        LSR TEMP; Logical Shift Right For "R17"
        BRCC Match; Branch If Carry Cleared
        LPM KEYVAL,Z+; Increment Z and Load Program Memory
        RJMP Find;

Match:
        LPM KEYVAL,Z; Load Program Memory
        CALL Data; Send "R16" Value To PORTD
        RJMP Pullups;

Delay1:
        NOP;
        NOP;
        RET;

Delay_750us:
        PUSH R19;
        LDI R19,250;
    F1:
        CALL Delay1;
        DEC R19;
        BRNE F1;
        POP R19;
        RET;

Delay_15ms:
        PUSH R19;
        LDI R19,150;
    F2:
        CALL Delay_750us;
        DEC R19;
        BRNE F2;
        POP R19;
        RET;

.ORG 0X300;
Key_Code1: .DB '0','1','2','3'; Row 1
Key_Code2: .DB '4','5','6','7'; Row 2
Key_Code3: .DB '8','9','A','B'; Row 3
Key_Code4: .DB 'C','D','E','F'; Row 4

LCD:
        CBI KEY_PB,EN;
        CALL Delay_Func_2ms;
        LDI R16,0x38;
        CALL Command;
        CALL Delay_Func_2ms;
        LDI R16,0x0E;
        CALL Command;
        LDI R16,0x01;
        CALL Command;
        CALL Delay_Func_2ms;
        RET;

Command:
        OUT KEY_PD,KEYVAL;
        CBI KEY_PB,RS;
        SBI KEY_PB,EN;
        CALL Delay;
        CBI KEY_PB,EN;
        CALL Delay_100us;
        RET;

Data:
        OUT KEY_PD,KEYVAL;
        SBI KEY_PB,RS;
        SBI KEY_PB,EN;
        CALL Delay;
        CBI KEY_PB,EN;
        CALL Delay_100us;
        RET;

Delay:
        NOP;
        NOP;
        RET;

Delay_100us:
        PUSH R19;
        LDI R19,60;
    L1:
        CALL Delay;
        DEC R19;
        BRNE L1;
        POP R19;
        RET;

Delay_Func_2ms:
        PUSH R19;
        LDI R19,20;
    L2:
        CALL Delay_100us;
        DEC R19;
        BRNE L2;
        POP R19;
        RET;

        
 

M

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mhmd wrote:

.EQU KEY_PC = PORTC; For Rows
.EQU KEY_PIN = PINC; For Columns
.EQU KEY_PD = PORTD;
.EQU KEY_PB = PORTB;
.EQU KEY_C = DDRC;
.EQU KEY_D = DDRD;
.EQU KEY_B = DDRB;
.EQU RS = 0;
.EQU EN = 1;
.DEF KEYVAL = R16;
.DEF TEMP = R17;

Main:
        CALL LCD;

Start:
        ; Initialize Stack Pointer
        LDI KEYVAL,LOW(RAMEND);
        OUT SPL,KEYVAL;
        LDI KEYVAL,HIGH(RAMEND);
        OUT SPH,KEYVAL; 

        LDI TEMP,0XFF;
        OUT KEY_D,TEMP; Make PortD Output
        OUT KEY_B,TEMP;

 

You are trying to initialise the LCD before you set the port direction.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay thank you, i just fixed it now when i press on the keypad the lcd only shows spaces should i insert ASCII Converter.

M

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Forget your keypad code for the moment and just get the LCD working. Can you print "Hello World" on it?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

no i can't, the LCD isn't displaying anything just tried to print letter A the LCD showed a space like the letter is written on it but not displayed. 

M

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does the LCD have a contrast control?  Perhaps that needs adjustment to see the characters.

post a photo of your setup we may be able to spot something.

 

jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Sun. Jan 12, 2020 - 03:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Consider making the code more modular and concentrate on implementing and testing just one small part at a time. I'd get the LCD init and character output implemented and tested first so you can rely on it and use it as a debug tool for testing the implementation of the other code.

 

Just throwing everything together and hoping it all works is seldom very successful.