PDA

View Full Version : Keypad input test



Kalind
- 7th October 2008, 15:16
Please could you debug my program. It displays strange lines across the LCD at the start then it prompt the user to enter high setpoint but the keypad doesnt seem to work and the program just halts at that point. It's purpose is to measure and display temperature. I've added a keypad to enable the user to set upper and lower setpoints. At the start up of my design, i would like to prompt the user to enter setpoints. Is my code doing what i want it to do? please let me know. Thank you!

' Define LCD registers and bits

Define LCD_DREG PORTB
Define LCD_DBIT 0
Define LCD_RSREG PORTB
Define LCD_RSBIT 5
Define LCD_EREG PORTB
Define LCD_EBIT 4
dEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE KEYPAD_ROW 4 ' 4 ROW keypad
DEFINE KEYPAD_ROW_PORT PORTC ' ROW port = PORTC
DEFINE KEYPAD_ROW_BIT 4 ' ROW0 = PORTC.4
DEFINE KEYPAD_COL 4 ' 4 COL keypad
DEFINE KEYPAD_COL_PORT PORTC ' COL port = PORTC
DEFINE KEYPAD_COL_BIT 0 ' COL0 = PORTC.0

Pause 1000
LCDOut $FE,1,"Initializing ...."
pause 1000
gosub setuppersp
gosub setlowersp

Pause 1000

adval var word ' Create adval to store result
temp var word ' Create temp to store result
temp1 var word ' Create temp1 to store result
col VAR BYTE ' Keypad column
row VAR BYTE ' Keypad row
key VAR BYTE ' Key value
MyOtherVar var word
uppersp var word
lowersp var word

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and RIGHT justify result
ADCON0 = %11000001 ' Configure and turn on A/D Module
Pause 100 ' Wait 0.1 second


loop: ADCON0.2 = 1 ' Start Conversion

AGAIN: Pause 1
If ADCON0.2 = 1 Then AGAIN ' Wait for low on bit-2 of ADCON0, conversion finished

adval.highbyte = ADRESH ' Move HIGH byte of result to adval
adval.lowbyte = ADRESL ' Move LOW byte of result to adval

Lcdout $fe, 1 ' Clear screen
temp=50*adval ' Conversion to Degrees
temp=temp/100
Lcdout "TEMP = ",DEC temp,$DF,"C" ' Display the value of temp
temp1 = temp*18 ' Conversion to Fahrenheit
temp1 = temp1+320
temp1 = temp1/10
lcdout $FE,$C0, "TEMP = ",dec temp1,$DF,"F" ' Display the value of temp
Pause 1000 ' Wait 1 second

Goto loop ' Do it forever
End

' Keypad Subroutine

scan:
Gosub getkey ' Get a key from the keypad
PAUSE 200
Goto scan ' Do it forever

' Subroutine to get a key from keypad
getkey:
Pause 50 ' Debounce

getkeyu:
' Wait for all keys up
PORTC = 0 ' All output pins low
TRISC = $f0 ' Bottom 4 pins out, top 4 pins in
IF ((PORTC >> 4) != $f) Then getkeyu ' If any keys down, loop

Pause 50 ' Debounce

getkeyp:
' Wait for keypress
For col = 0 to 3 ' 4 columns in keypad
PORTC = 0 ' All output pins low
TRISC = (DCD col) ^ $ff ' Set one column pin to output
row = PORTC >> 4 ' Read row
IF row != $f Then gotkey ' If any keydown, exit
Next col

GoTo getkeyp ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16
key = (col * 4) + (NCD (row ^ $f))
Return ' Subroutine over

lookup key,[0,"123A456B789C*0#D"],MyOtherVar

End

setuppersp:
LCDOut $FE,1,"Enter High Setpoint:"
gosub getkey
LOOKUP key,["123A456B789C*0#D"],uppersp
RETURN

setlowersp:
LCDOut $FE,1,"Enter Low Setpoint:"
gosub getkey
LOOKUP key,["123A456B789C*0#D"],lowersp
return

mackrackit
- 7th October 2008, 16:22
There is no need to send multiple PMs, when someone gets the time, they will help.

First thing I see, it looks like you copy/pasted from at least two different programs, but that was it. You did not do any re-write to make them work. The duplicate "END"s is what I am getting at.

Next,
then it prompt the user to enter high setpoint
You say the program does this, but I do not see where.

Then I do not see where the code goes from the main loop to the keypad routine.

Without doing a complete re-write for you, I will suggest that you work on one thing at a time. Maybe start with the keypad. Get it working, then move on to saving a value.

Do not try to write the whole thing at once and then try to figure out where it went wrong.

I normally use a shift register for keypads, so if you want to do it the way you have it, go back and read http://www.picbasic.co.uk/forum/showthread.php?t=3250

That should get you going there. Then we can work on saving a value.

skimask
- 7th October 2008, 16:30
3 buttons instead of the keypad
One to drive the value up (up arrow?), one to drive the value down (down arrow?), one to save the value and move on (enter?).
maybe even a 4th button to reset the value back to a default value (ESC?).
A lot easier than 'punching in numbers'.
Overdesigning a project sometimes really sucks...

Archangel
- 7th October 2008, 17:30
Are you trying to use Mister_e's Keypad.bas program ? You will need the statement INCLUDE "keypad.bas" early in your code. You will need interrupts, and it will require you to use MPASM as the assembler unless you use Darrel's modified version.

Kalind
- 7th October 2008, 17:34
no im using another keypad routine. can u help me out on this one joe?

mackrackit
- 7th October 2008, 18:31
no im using another keypad routine.
I thought you were using Steve's for some reason.
Where did you get the code and how is the keypad connected to the pic.

Kalind
- 7th October 2008, 18:46
connected to the pic via port c.

skimask
- 7th October 2008, 18:53
I thought you were using Steve's for some reason.
Where did you get the code and how is the keypad connected to the pic.

I did a search on KEYPAD_ROW_PORT and the only places I could find that PARTICULAR define is connected with Steve's keypad routine...
hmmmm........

mackrackit
- 7th October 2008, 19:41
This looks like part of you code.
http://www.melabs.com/resources/samples/x1/pbp/keyx.bas
and here is the schematic for it
http://www.melabs.com/downloads/labx1sch.pdf
Make the necessary hardware changes and get the above code working. Do not add anything else yet, just the keypad code. (maybe a blinky to see if thing are running) but nothing else.

Then we will go from there.

Archangel
- 8th October 2008, 04:00
no im using another keypad routine. can u help me out on this one joe?

All I can do to get Steve's routine to work, I will be posting something using it when I kill off a couple of bugs in my code, Finally with some help from Jeremy got my interrupts to work :D