PDA

View Full Version : NOOB in need of help



studysession
- 24th January 2009, 22:10
Hi -
I am new to PICBasic and to the forums. I have PICBasic Pro if that matters, I am unsure. I am having few problems.

I want to control few servos based on if this button is pressed etc... To start figure I would use either buttons or switches (same thing) and light up LED's based on if pressed then do kind of thing. Simple enough but for some reason my LED's blink and not just stay lit and trying to figure out why. Attached is an image of my electronics setup and here is my code. Also I am using a PIC16F84A chip and all the examples in books show an external crystal. If anyone knows how to do this without using the external crystal, that would be nice too.

Please tell me what I am doing wrong and how to do this better. Many thanks!




main

if portb.0 = 1 then gosub pressed0
if portb.0 = 0 then gosub npressed0
if portb.1 = 1 then gosub pressed1
if portb.1 = 0 then gosub npressed1
if portb.2 = 1 then gosub pressed2
if portb.2 = 0 then gosub npressed2
if portb.3 = 1 then gosub pressed3
if portb.3 = 0 then gosub npressed3
goto main

pressed0:
high portb.4
pause 200
return

npressed0:
low portb.4
pause 200
return

pressed1:
high portb.5
pause 200
return

npressed1:
low portb.5
pause 200
return

pressed2:
high portb.6
pause 200
return

npressed2:
low portb.6
pause 200
return

pressed3:
high portb.7
pause 200
return

npressed3:
low portb.7
pause 200
return

mackrackit
- 24th January 2009, 22:56
Welcome.

Looks like your code is working fine, the LEDs blink because if a button is not pressed it GOSUBs to an LED low command.

To make all of the LEDs light, get rid of all


if portb.0 = 0 then gosub npressed0

Make a button when presses GOSUB to an LED_ALL_LOW block.


To run without a crystal you need a chip with a built in OSC like a 16F676.

studysession
- 25th January 2009, 00:33
Sounds good.

How do you make it do one thing when the button is pressed and another when it is not? Like while pressed make a motor run and then stop when not pressed without it being a seprate button?

Thanks for the help

aratti
- 25th January 2009, 00:49
main:

Pause 200

if portb.0 = 1 then gosub pressed0

if portb.1 = 1 then gosub pressed1

if portb.2 = 1 then gosub pressed2

if portb.3 = 1 then gosub pressed3

goto main

pressed0:
toggle portb.4
return

pressed1:
toggle portb.5
return

pressed2:
toggle portb.6
return

pressed3:
toggle portb.7
return

end


Try this way it should do what you need.

See TOGGLE command on manual.

Al.

mackrackit
- 25th January 2009, 04:44
Sounds good.

How do you make it do one thing when the button is pressed and another when it is not? Like while pressed make a motor run and then stop when not pressed without it being a seprate button?

Thanks for the help

Your posted code should do that.???

aratti
- 25th January 2009, 08:57
Dave, let him discover the Toggle instruction.

Al.

mackrackit
- 25th January 2009, 09:06
Dave, let him discover the Toggle instruction.

Al.
Okie Doki :)

studysession
- 25th January 2009, 13:22
The toggle wasn't waht I was looking for. I did try it. When you hold down the button and the toggle was going - on/off/on/off etc.....

Thanks for all the help. I am sure I will be posting more questions as I move forward.

Thanks again -
Keith

Archangel
- 25th January 2009, 17:16
Hi Keith,
Looks like a classic IF THEN ELSE to me.


TRISB = %00001111 ' 4 upper as outputs 4 lower as inputs
IF PortB.0 = 1 THEN ' if true then goto next statement
PortB.4 = 1 ' turn on this port
ELSE ' goes to here if false
PortB.4 = 0 ' Turns this port off
ENDIF ' ends the if then test for true / false and allows code to continue

make sure to use resistors pulling down if you close switches to activate or pulling up if you open switches to activate.

mister_e
- 26th January 2009, 23:01
Not sure at all but...


TRISB=%00001111
PORTB=0

Loop:
PORTB=(PORTB<<4)
PAUSE 200
GOTO Loop