PDA

View Full Version : Button help



darklight
- 19th July 2006, 06:12
Hey people.

I'm new to microcontrolers and picbasic.
I was just wondering if anyone could help me with switchs.
I can already output leds from portB.

PIC = 16F627

I'm using a programmer / test board (VELLEMAN VM111) to test buttons and leds.

The leds = 6 on portB
Buttons = 4 on portA

Thanks for any help.

blainecf
- 19th July 2006, 19:50
Make sure:

1. ADCON1 = 7 so that your PORTA pins are digital

2. Set the appropriate TRISA.x = %1 for Digital Input

3. Wire a pull-up resistor (10K from V+ to each pin that will sense the switch)

4. Wire each switch, one side ground, the other to the pin from step #3

5. Set up a loop for polling the ports; High (%1) will be normal, and Low (%0) will show when the switch is held closed.

If you're not used to digital signals, it seems weird to ground out a resistor thats hooked to V+, but just accept it.

Regards,

mister_e
- 20th July 2006, 01:59
ADCON1=7 have nothing usefull to do with the 16F627 but compilation error. There's no ADCs in the PIC16F627, just analog comparator.

CMCON=7

homebrewer
- 21st August 2006, 23:51
ADCON1=7 have nothing usefull to do with the 16F627 but compilation error. There's no ADCs in the PIC16F627, just analog comparator.

CMCON=7

I'm a beginner too. I had also problems to query PORTA. The tip with CMON=7 was the solution. Steve, thank you very much!
_______________
Regards
Johann

Archangel
- 22nd August 2006, 02:38
Attention fellow newbies:
Here's what I learned the hard way, twice!
When your button code calls a LABEL,
put that labeled code in the very next line after your button code,
or you get nasty error codes assuring you that it is not really a label.

I ran into this in a program with several sub routines and several buttons
and the only way it would compile was if I put the subs directly after the button.

flotulopex
- 30th August 2006, 22:12
I can't make it work with the BUTTON command.

So I tried the other way and use the simple IF..THEN command.

But again, I have the same unexpected result. The Led starts to blink when I press the button. As I release the button, it never "toggles" as I want it to do.

Since this program behavior is the same in both cases (BUTTON and IF..THEN commands), I assume something is wrong with some register settings.

I was thinking about a bouncing problem, but I'm not sure about that.

Here is my code; where do I miss something?

'PIC 16F88
'Button is on PORTB.3 (Pin 9) Pressed = 0V
'Led is on PORTB.4 (Pin 10)

OSCCON = %01100000 'Internal RC set to 4MHZ
TRISB = %00001000 'RB3 is an Input, all others are Outputs
'----------------------------------------------------------------

' IF..THEN
MAIN:
IF PORTB.3 = 0 then SWITCH_LED
goto main

SWITCH_LED:
pause 100 'Debounce
toggle PORTB.4
goto main

end

'----------------------------------------------------------------
' BUTTON

'BVar var byte
'BVar = 0
'
'MAIN:
' BUTTON 9, 0, 255, 255, BVar, 0, SWITCH_LED
' goto main
'
'SWITCH_LED:
' pause 100 'Debounce
' toggle PORTB.4
' goto main

END

Melanie
- 31st August 2006, 00:02
MAIN:
IF PORTB.3 = 0 then SWITCH_LED
goto main

SWITCH_LED:
pause 100 'Debounce
toggle PORTB.4
goto main

So what happens above when you keep your finger pressed on the button for longer than 100mS? Your LED will keep toggling every 100mS...

Try this...

MAIN:
IF PORTB.3 = 0 then SWITCH_LED
goto main

SWITCH_LED:
pause 100 'Debounce
toggle PORTB.4
While PortB.3=0:Wend ' Wait here for Button to be released
goto main

homebrewer
- 31st August 2006, 08:26
' Try CMCON=7 as mister_e has stated in reply#3
' now you can query input pins
' from this point it's up to you, how your led should work

CMCON=7

MAIN:
IF PORTB.3 = 0 then SWITCH_LED
goto main

SWITCH_LED:
pause 100 'Debounce
toggle PORTB.4
goto main

end


' Regards
' Johann

Melanie
- 31st August 2006, 08:31
CMCON has no effect on PortB on this PIC.

homebrewer
- 31st August 2006, 09:09
Hello Melanie,
you are very right. I have looked into the datasheet after my reply.
I was too overhasty - sorry!

Melanie
- 31st August 2006, 09:24
Appologies are unnescessary.

flotulopex reported that his LED blinks when he pushes the button, that's because there's only a 100mS Pause in the loop and you toggle the LED-ON, and with your finger still on the button, 100mS later you toggle it OFF, and with your finger still on the button, the LED toggles again 100mS later... etc etc until the end of time.

You need to ensure that once the LED toggles, it doesn't do so again until the Button is pressed anew. There are MANY, MANY ways of doing this. The example I posted simply waits for the user to remove his finger from the button before proceeding further.

flotulopex
- 31st August 2006, 20:06
Thank you Melanie,

Actually, it still doesn't work.

Each time I press the button, the Led lights up and as soon as I release the button, the Led switches off. At least, it doesn't blink anymore while the button is pressed.

It just doesn't want to toggle!

To make sure my button is correctly connected, is this schematic okay?
> Vdd-R10k-PORTB.3 and the button (normally open) is derived from PORTB.3 to Vss.

Since I can't make it work with this simple program, my last guess would be that my PIC is defect. I tried with other ports but there is no change. I shall receive another PIC tomorrow.

By the way, is there any utility tool to check the PIC's health?

Melanie
- 31st August 2006, 20:18
MyButton var PortB.3
LED var PortB.4

TRISB = %00001000

Loop:
If MyButton=0 then
Toggle LED
While MyButton=0:Pause 100:Wend
endif
Goto Loop

End

Try the above and report back what the result is...

precision
- 1st September 2006, 11:05
'PIC 16F84A
DEFINE OSC 4
TRISA = %000001
TRISB = %11111100
I VAR BYTE
PAUSE 200

MAIN:
IF PORTA.0 = 1 then SWITCH_LED
goto main

SWITCH_LED: IF PORTA.0 = 0 THEN SWITCH_LED_INSTANT
FOR I = 0 TO 10
IF I < 10 AND PORTA.0 = 0 THEN SWITCH_LED_INSTANT
PAUSE 100
NEXT I
IF PORTA.0 = 1 THEN SWITCH_LED_DELAY
goto main

SWITCH_LED_DELAY: TOGGLE PORTB.0
PAUSE 10

AVOID_LED_BLINK: IF PORTA.0 = 1 THEN AVOID_LED_BLINK
PAUSE 10
GOTO MAIN


SWITCH_LED_INSTANT: TOGGLE PORTB.1
PAUSE 10
GOTO MAIN

JARI NETWORKS
- 1st September 2006, 13:36
I'm also a new one with de pic chips, can I use this also for a 16F628A chip??

grz Jan

flotulopex
- 1st September 2006, 18:40
Hello Melanie,

Your piece of code didn't work until I... grounded PORTB.2.

So I tried my code and it worked well too.

I didn't expect this component to be so sensible to the electric field around the pins.

I won't forget this lesson, for sure ;-)

JARI NETWORKS
- 2nd September 2006, 21:24
so many viewers, and nobody can give me an answer??

malc-c
- 2nd September 2006, 21:54
I'm also a new one with de pic chips, can I use this also for a 16F628A chip??

grz Jan

If you are meaning "can you use the button command with the 16f628a chip, then the answer is yes ;)

JARI NETWORKS
- 2nd September 2006, 22:01
ok thanks,that was all I want to know at this moment

med2007
- 5th March 2008, 09:12
On the side, could you tell me the correct syntax to poll portb in a for..next loop?
viz
FOR i = 0 to 7
IF portb.[i]=0 THEN GOSUB alarm
NEXT i
This doesn't work and I can't find the answer in the book



Make sure:

1. ADCON1 = 7 so that your PORTA pins are digital

2. Set the appropriate TRISA.x = %1 for Digital Input

3. Wire a pull-up resistor (10K from V+ to each pin that will sense the switch)

4. Wire each switch, one side ground, the other to the pin from step #3

5. Set up a loop for polling the ports; High (%1) will be normal, and Low (%0) will show when the switch is held closed.

If you're not used to digital signals, it seems weird to ground out a resistor thats hooked to V+, but just accept it.

Regards,

skimask
- 5th March 2008, 14:01
On the side, could you tell me the correct syntax to poll portb in a for..next loop?
viz
FOR i = 0 to 7
IF portb.[i]=0 THEN GOSUB alarm
NEXT i
This doesn't work and I can't find the answer in the book

http://www.picbasic.co.uk/forum/showthread.php?t=544

med2007
- 6th March 2008, 09:01
Thanks for the link...so my syntax should be?

FOR i = 0 to 7
IF portb.0(i) = 0 THEN GOSUB alarm
NEXT i

I'll give it a whirl.
Rgds
MED2007