PDA

View Full Version : Button problem.



Modena
- 5th November 2004, 14:45
I'm using the K8084 development board to learn how to use picbasic. The board uses the 16F627 chip with the LED's on port b and buttons on porta. The input from a button is high when pressed.

The following is the code I'm using to try and get the buttons working, but it doesn't work.


B6 var byte
button1 con 1
TRISA = 0

loop:

B6 = 0
button button1, 1, 10, 5, B6, 1, pressed
pause 100
goto loop

pressed:

TRISB = 0 ' PORTB is output
PORTB = %11111111 ' Turn ON diodes on PORTB
PAUSE 1000 ' Wait for 1 second
PORTB = $00000000 ' Turn OFF diodes on PORTB
pause 1000

goto loop

Can anyone see what is wrong.

mister_e
- 5th November 2004, 15:04
in your case i see few mistake.

1. Is your PushButton is connected to PORTA. In case you set TRISA=0... PORTA is now output... so you cannot get from PORTA.

2. PIC16F627 have internal analog comparator that must be turn off when you don't using them CMCON=7

3. i've never use BUTTON statement. I think this fuction eat too much code space for an simple task. i post you my way to handle pushbutton




CMCON=7 ;Disable analog comparator
TRISB=0 ;set PORTB to output
TRISA=255 ;set PORTA to input
PushButton VAR PORTA.0 ;push button connected to RA0

PORTB=0 ;turn off PORTB... in case only

start:
While Pushbutton=0 ;wait for Push button action
;you can also use While NOT PushButton
Wend

While PushButton ; wait for push button release
Wend

pause 50 ; debounce time...cheap but work

;once the debounce time is finish... use your code

PORTB = 255 ' Turn ON diodes on PORTB
PAUSE 1000 ' Wait for 1 second
PORTB = 0 ' Turn OFF diodes on PORTB
pause 1000
GOTO Start



I hope this help you. Sorry if i didn't use BUTTON statement

Modena
- 5th November 2004, 16:43
Buttons now work, thanks.