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
' 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