PDA

View Full Version : help!! With Keypad change



clare1
- 12th December 2006, 09:31
Hello All

I am a newbie when it comes to picbasic i am still learning the basics of it
so please dont be too hard on me.
I writting to find out if there is any examples of interupt on portb change
i am using pic16f877 and LAB-X1 Experimenter Board
i have got an example of using the key pad as below and it works fine
But the problem is that i have already sent it out to my brother in africa
before he told me that they are only going to be using SW1,SW2 & SW3
Wired as nornally open contacts .
The problem with that is the way the example problem works is that
when the using presses the button, it only activates after you have released the button.
Question :
How can i modified the code so that it works when the button is pressed
the different test1 , test2 & test3 to activate
would i have to use portb change

Please Help
Clare1

' PicBasic Pro program to display display the following when the
' SW1 = EARTH BOND
' SW2 = FLASH TEST
' SW3 = IR TEST


DEFINE LOADER_USED 1 ' uses a bootloader

@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H
' @ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H
INCLUDE "Modedefs.Bas"


Define OSC 20
clear

' Setup Hardware for uart/RS232
'--------------------------------
DEFINE HSER_BAUD 9600
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1
' End of Setup Hardware for uart/RS232
'-------------------------------------
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'Start of LCD Setup
'--------------------

DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line

DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)

' ** Define LCD Control Constants **

I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2
Line3 Con 148 ' Point to beginning of line 3
Line4 Con 212 ' Point to beginning of line 4
'End of LCD Setup
'--------------------
' ** Setup the ADCIN Defines **

DEFINE ADC_BITS 10 ' Set resolution of conversion
DEFINE ADC_CLOCK 2 ' Set clock source (x/FOSC or FRC)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time (in uS)
'
' Define program variables
col VAR BYTE ' Keypad column
row VAR BYTE ' Keypad row
key VAR BYTE ' Key value
disp_key VAR BYTE ' Key ASCII value



INTCON2.7 = 0 ' Enable PORTB pullups (18F452)

ADCON1 = 7 ' Make PORTA and PORTE digital
TRISB=0
portd=0
Pause 1000 ' Wait for LCD to start
loopp:
lcdout I,Clr:Pause 30
LCDOut I,Line1," Press A key To Test" ' Display sign on message
lcdout I,Line2," S1= Earth Bond Test"
lcdout I,Line3," S2= Flash Test "
lcdout I,Line4," S3= DC IR Test"
loop:
GoSub getkey ' Get a key from the keypad
IF key != $FF Then ' Check to see if a key was pressed
IF (disp_key <>"1") and (disp_key <>"2") and (disp_key <>"3")then loop
'Block response from other key press outside S1-S3
' Loop back to loop if other keys are pressed
LCDOut $fe, 1, disp_key ' Display ASCII key label
if disp_key = 49 then Earth_Bond 'dec value for ascii 1
if disp_key = 50 then Flash_Test 'dec value for ascii 2
if disp_key = 51 then IR_Test 'dec value for ascii 3
EndIF
'if disp_key > "3" then key = $FF
'if disp_key < "1" then key = $FF
' <> or !=


GoTo loop ' Do it forever


' Subroutine to get a key from keypad
getkey:
' Check for keypress
For row = 0 TO 3 ' Loop for 4 rows in keypad
PORTB = 0 ' All output pins low
TRISB = ~(DCD row) ' Set one row pin to output
@ NOP ' Fudge for 18F452
col = PORTB >> 4 ' Read column pins

IF col != $0F Then ' Check if key is pressed

GoTo gotkey ' If any keydown, exit
EndIF
Next row ' Repeat for the next row

key = $FF ' No keys down, set key to $FF
Return ' Return to main loop

gotkey: ' Change row and column to key number 0 - 15
Pause 50 ' Debounce


' Wait for all keys up
PORTB = 0 ' All output pins low
TRISB = $F0 ' Least significant 4 pins out, top 4 pins in
IF ((PORTB >> 4) != $0F) Then gotkey ' If any keys down, loop
key = (row * 4) + (NCD (col^$0F)) - 1 ' Combine row and column into numeric value


' Translate key to display label:
' 1 2 3 A
' 4 5 6 B
' 7 8 9 C
' * 0 # D
LookUp key,["123A456B789C*0#D"],disp_key

Return ' Return to main loop

Earth_Bond:
'==========
high portd.0
lcdout I,Clr:Pause 30
lcdout I,Line1," Earth_Bond "
lcdout I,Line2," Test "
hserout [" EarthBond Test ",10,13]
pause 500
low portd.0
return

Flash_Test:
'===========
high portd.1
lcdout I,Clr:Pause 30
lcdout I,Line1," Flash_Test "
lcdout I,Line2," HV "
hserout [" Flash_Test",10,13]
pause 500
low portd.1
return

IR_Test:
'=======
high portd.2
lcdout I,Clr:Pause 30
lcdout I,Line1," Insulation_Test "
lcdout I,Line2," HV "
hserout [" IR_Test",10,13]
pause 500
low portd.2
return
goto loop
End

isaac
- 12th December 2006, 14:20
You would have to use portb change interupt.
You could even use DT instant interuptS

Isaac

sayzer
- 12th December 2006, 17:30
....
The problem with that is the way the example problem works is that
when the using presses the button, it only activates after you have released the button.
Question :
How can i modified the code so that it works when the button is pressed
the different test1 , test2 & test3 to activate
would i have to use portb change
....


There is something called "Rising Edge" and also "Falling Edge".

You check what these are first.

Then you will know how to set a button when it is pressed or after it is released.

----------------------

isaac
- 12th December 2006, 21:13
Hi Clare

Just a thought . For your application you said your were using the LAB-X1 Experimenter Board and that you are only interested in SW1-SW3
if that is the case you don't even need to use the PortB change interupt
you can do something like this and it should work give it a go


Isaac


' PicBasic Pro program to show button press on LED

' Define LOADER_USED to allow use of the boot loader.

Define LOADER_USED 1

OPTION_REG = $7f ' Enable PORTB pull-ups


loop:
PORTB = 0 ' PORTB lines low to read buttons
TRISB = $f0 ' Enable all buttons



' Check any button pressed to turn on LED
If PORTB.4 = 0 Then ' If 4th button pressed...
pause 50
if PORTB.4=0 Then
gosub Earth_Bond
Endif
Endif


If PORTB.5 = 0 Then ' If 3rd button pressed...
pause 50
if PORTB.5=0 Then
gosub Flash_Test
Endif

Endif
If PORTB.6 = 0 Then ' If 2nd button pressed...
pause 50
if PORTB.6=0 Then
gosub IR_Test
Endif

Endif

Goto loop ' Do it forever

End

Earth_Bond:
'==========
'your stuff here
return

Flash_Test:
'===========
'your stuff here
return

IR_Test:
'=======
'your stuff here
return