PDA

View Full Version : Help with code, button



xobx
- 14th July 2007, 17:41
When I give PORTA.1 +5V, LED1 dosent shine..

Whats wrong with my code?



LED1 var PORTB.1
PRESS var PORTA.1

loop:

If press = 1 then
high led1
else
low led1
endif
goto loop
end

mackrackit
- 14th July 2007, 17:45
Does your PIC have ADC?
If it does...
http://www.picbasic.co.uk/forum/showthread.php?t=562

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

xobx
- 14th July 2007, 17:56
I dont know its a PIC16F648A

mackrackit
- 14th July 2007, 18:09
According to the data sheet it has two comparators.
The above links will explain why
CMCON=7
is needed.

xobx
- 15th July 2007, 10:43
Ok, thanks.

But it still dosent work as I want it :/


@ device pic16F648A, hs_osc, wdt_off, mclr_on, lvp_off, protect_off
DEFINE OSC 20

LED1 var PORTB.1
PRESS var PORTA.1
CMCON=7


loop:

If press=1 then
high led1
else
low led1
endif
goto loop
end

When i transfer it LED1 starts to shine directly even if PortA.1 doesnt have +5V

:/

mackrackit
- 15th July 2007, 11:02
Maybe the LED is backwards:)

Do you have PORTA.1 tied to ground with a resistor (10K) so it is not floating?

xobx
- 15th July 2007, 11:13
Floating ?

xobx
- 15th July 2007, 11:22
Should it be like this?

http://img168.imageshack.us/img168/3927/floatej7.jpg

mackrackit
- 15th July 2007, 13:45
Yes, that is correct.

xobx
- 15th July 2007, 14:19
I added it and it worked!

Thanks.

xobx
- 15th July 2007, 14:57
If I make the code like tis:



@ device pic16F648A, hs_osc, wdt_off, mclr_on, lvp_off, protect_off
DEFINE OSC 20

LED1 var PORTB.1
PRESS var PORTA.1
CMCON=7


loop:

If press=0 then
high led1
else
low led1
endif
goto loop
end

Should it be like this?

http://img528.imageshack.us/img528/1509/floatqs4.jpg

mackrackit
- 15th July 2007, 17:52
If I make the code like tis:



@ device pic16F648A, hs_osc, wdt_off, mclr_on, lvp_off, protect_off
DEFINE OSC 20

LED1 var PORTB.1
PRESS var PORTA.1
CMCON=7


loop:

If press=0 then
high led1
else
low led1
endif
goto loop
end

Should it be like this?

http://img528.imageshack.us/img528/1509/floatqs4.jpg

No. The first way is what you want. When the switch is open the pin is "0"LOW. When the switch is closed the pin is "1"HIGH.

If you are using push buttons, you will need to use either a normally open "NO" or a normally closed "NC".

The resistor keeps the pin LOW (no float). High resistance is used to minimize the current through the switch (just a little short).

Depending on how you look at it this is called a pull up or a pull down.