Get Key pressed from keypad and save it for later use

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

Hi Team,

I'm not sure what exactly wrong Im doing but I know that the code doesn't make sense. I love that I'm learning and understanding myself but I have spend the last 48 hours just trying the problem in my code below. I'm scanning the keypad (using col, and row) when I find the key I'm jumping to "convert" and see there what is the value of the key pressed. I want to make it as simple as possible by just pressing 1 key then "*" then second key then "#" and compute the multiplication of key 1 times key 2 and display it to LED.

If you could just help me with understanding how do I save the value of the key pressed to use it again when the * is pressed or when the # is pressed. It doesnt seem that the value for the key pressed is being saved.

Appreciate it if anyone can have a quick look and guide me.



 .include ""
 .def row = r16 ; current row number
 .def col = r17 ; current column number
 .def rmask = r18 ; mask for current row during scan
 .def cmask = r19 ; mask for current column during scan
 .def temp1 = r20
 .def temp2 = r21
 .def temp = r22
 .def count = r23
 .equ PORTFDIR = 0xF0 ; PF7-4: output, PF3-0, input
 .equ INITCOLMASK = 0xEF ; scan from the leftmost column,
 .equ INITROWMASK = 0x01 ; scan from the top row
 .equ ROWMASK =0x0F ; for obtaining input from Port F

 .org 0x200
 rjmp RESET

 ldi temp1, PORTFDIR ; PF7:4/PF3:0, out/in
 out DDRF, temp1
 ser temp1 ; PORTC is output
 out DDRC, temp1
 out PORTC, temp1
 ldi    temp, high(RAMEND) // Main program start
 out    SPH,  temp         // Set Stack Pointer to top of RAM
 ldi    temp, low(RAMEND)
 out    SPL,  temp
 clr count

 ldi cmask, INITCOLMASK ; initial column mask
 clr col ; initial column

 cpi col, 4
 breq main ; if all keys are scanned, repeat.
 out PORTF, cmask ; otherwise, scan a column
 ldi temp1, 0xFF ; slow down the scan operation.
  dec temp1
  brne delay
  in temp1, PINF ; read PORTF
  andi temp1, ROWMASK ; get the keypad output value
  cpi temp1, 0xF ; check if any row is low
  breq nextcol ; if yes, find which row is low
  ldi rmask, INITROWMASK ; initialize for row check
  clr row ;

cpi row, 4
breq nextcol ; the row scan is over.
mov temp2, temp1
and temp2, rmask ; check un-masked bit
breq convert ; if bit is clear, the key is pressed
inc row ; else move to the next row
lsl rmask
jmp rowloop
nextcol: ; if row scan is over
lsl cmask
inc col ; increase column value
jmp colloop ; go to the next column

    inc count
    cpi col, 3 ; If the pressed key is in col. 3
    breq letters ; we have a letter ; If the key is not in col. 3 and

    cpi row, 3 ; if the key is in row3,
    breq symbols ; we have a symbol or 0
    push row
    push col
    push temp1
    rcall getNumber ; Otherwise we have a number in 1-9
    jmp main

    cpi count, 1
    breq FirstNumber
    cpi count, 3
    breq SecondNumber

    pop temp1
    pop col
    pop row
    mov temp1, row
    lsl temp1
    add temp1, row ;
    add temp1, col ; temp1 = row*3 + col
    inc temp1
    mov r7, temp1
    push r7

    mov temp1, row ; Otherwise we have a number in 1-9
    lsl temp1
    add temp1, row ;
    add temp1, col ; temp1 = row*3 + col
    inc temp1
    mov r8, temp1
    push r8


    ldi temp1, 'A'
    add temp1, row ; Get the ASCII value for the key
    jmp convert_end

    cpi col, 0 ; Check if we have a star
    breq star
    rcall hash
    cpi col, 1 ; or if we have zero
    breq zero
    rcall hash
    jmp main

    pop r8
    pop r7
    mul r7, r8
    mov temp1, r0
    out portc, temp1
    ldi temp1, '*' ; Set to star
    jmp convert_end
   ldi temp1, '0' ; Set to zero

   out PORTC, temp1 ; Write value to PORTC
   jmp main ; Restart main loop

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

Hi All, 


I was just searching and reading in avrfreaks I fount the below code to save to SRAM 

keypress: .byte 1

....more code

sts keypress, r0  -> store value in R0 to keypress
lds r1, keypress   -> load value from keypress into r1


I will try to implement it in my code instead of the weird push and pop I used, I'm just thinking of the below logic: 

keypress1: .byte 1
keypress2: .byte 1

When key is pressed, and jumped to "convert", once the checking is "number" then I get the value and do sts keypress1, temp1(which is the value of the number pressed)
Finally, when "#" is pressed, I do lds r2, keypress1, lds r3, keypress2, mul r2, r3, out portc r0 (because the results of the multiplication will be stored in r1:r0)


Will check and hopefully hopefully that finally give me results. will let you know. 




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

Write a function to accumulate a number. given an input of 0-9, multiply your accumulated number by ten then add the new value. You might also test for a CLR key that zeroes out your accumulated number.