View Full Version : button input
naga
- 8th September 2008, 10:30
Dear All,Looking for some code help.
portb.0 connected to press button via dip switch. what i want is:
if DIP SW = off and button is pressed then OUTPUT = toggle.(Latch)
if DIP SW = on and button is pressed then OUTPUT = Momentry(as long as the button is pressed)
IS it possible?
Thanks.
mackrackit
- 8th September 2008, 11:26
IF (DIP = 0) AND (BUT = 1) THEN SOMETHING
IF (DIP = 1) AND (BUT = 1) THEN SOMETHING ELSE
Melanie
- 8th September 2008, 11:52
Not as elegant as Dave's... but why do it in one line when you can do it in ten (Ski is not allowed to answer that one)...
PushButton var PortB.0 ' Low when Pressed
DIPSwitch var PortB.1 ' Low when ON
LED var PortB.2
TRISB=%00000011
Low LED ' Start with LED OFF
Loop: If PushButton=0 then ' Check for Button Press
If DIPSwitch=0 then ' Check for Options
High LED ' Turn ON LED with Button Press...
While PushButton=0:Wend
' Wait here as long as Button pressed
Low LED ' then turn OFF LED
else
Toggle LED ' Toggle LED at each Button Press
While PushButton=0:Wend
' Wait here until finger released from Button
endif
endif
Goto Loop
End
mackrackit
- 8th September 2008, 12:06
Not as elegant as Dave's...
Not sure how to take that :)
naga
- 10th September 2008, 15:39
PushButton var PortB.0 ' Low when Pressed
DIPSwitch var PortB.1 ' Low when ON
LED var PortB.2
TRISB=%00000011
Low LED ' Start with LED OFF
Loop: If PushButton=0 then ' Check for Button Press
If DIPSwitch=0 then ' Check for Options
High LED ' Turn ON LED with Button Press...
While PushButton=0:Wend
' Wait here as long as Button pressed
Low LED ' then turn OFF LED
else
Toggle LED ' Toggle LED at each Button Press
While PushButton=0:Wend
' Wait here until finger released from Button
endif
endif
Goto Loop
End
Thanks a lot..
there are 6outputs and 8inputs available in my project. as per above explanation need 12 input ports to enable all.(6for dip and 6 for push buttons) is there any way that to create a dip switch combination, with 2 dip switches..
Regards.
mackrackit
- 10th September 2008, 16:55
Put one dip on a port. Then read the whole PORT into a variable.
dip1=PORTB
dip2=PORTC
Read this thread, post #2
http://www.picbasic.co.uk/forum/showthread.php?t=66&highlight=read+port
naga
- 11th September 2008, 06:01
Thank you. I tried to understand from the PBP manual but i couldnt succeed. could you pls give some code Example.
Regards.
skimask
- 11th September 2008, 06:10
could you pls give some code Example.
The code example is right there in the last post!
mrhrholden
- 21st October 2008, 00:33
Hi this program is great that Mel has writen can we use the same program but soon as the button is pressed led go high for 500 / 1/2 second then PWM 50 duty and 5KHz and then then pwm stop when button is released?
i have the program working but the PWM hangs on after the button is off
PushButton var PortA.1 ' Low when Pressed
DIPSwitch var PortA.1 ' Low when ON
LED var PortB.0
TRISA=%1111
TRISA=%0000
Low LED ' Start with LED OFF
Loop: If PushButton=0 then ' Check for Button Press
If DIPSwitch=0 then ' Check for Options
High LED ' Turn ON LED with Button Press...
PAUSE 500
PWM 0,50,500
While PushButton=0:Wend
' Wait here as long as Button pressed
Low LED ' then turn OFF LED
else
Toggle LED ' Toggle LED at each Button Press
While PushButton=0:Wend
' Wait here until fingerreleased from Button
endif
endif
Goto Loop
End
skimask
- 21st October 2008, 01:35
Straight out of the manual:
5.61. PWM
PWM Pin,Duty,Cycle
Outputs a pulse width modulated pulse train on Pin. Each cycle of PWM consists of 256 steps. The Duty cycle for each PWM cycle ranges from 0 (0%) to 255 (100%). This PWM cycle is repeated Cycle times. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).
The Cycle time of PWM is dependent upon the oscillator frequency. If a 4MHz oscillator is used, each Cycle is about 5ms long. If a 20MHz oscillator is used, each Cycle is about 1ms in length. Defining an OSC value has no effect on PWM. The Cycle time always changes with the actual oscillator speed.
If you want continuous PWM output and the PICmicro MCU has PWM hardware, HPWM may be used instead of PWM. Pin is made an output just prior to pulse generation and reverts to an input after generation stops. The PWM output on a pin looks like so much
garbage, not a beautiful series of square waves. A filter of some sort is necessary to turn the signal into something useful. An RC circuit can be used as a simple D/A converter
PWM is a software, bit-banged, command.
HPWM is a hardware driving command. The PIC's internal PWM module is used.
As the book says, PWM will complete X cycles, then it will move on. You can press buttons all you want, the PIC isn't going to respond to anything (unless you've got some assembly interrupts going) until the PWM command is done.
mrhrholden
- 22nd October 2008, 01:26
Hi Thanks for that yeah that all makes sense now why it hangs on and yeah thats PWM is not what we want. what we want it to do is the LED go high for 500 micro seconds and then PWM at 50% duty 5KHZ - 10KHZ any ideas for can't a pic do this?
skimask
- 22nd October 2008, 01:32
we want it to do is the LED go high for 500 micro seconds and then PWM at 50% duty 5KHZ - 10KHZ any ideas for can't a pic do this?
Re-read the STRAIGHT OUT OF THE MANUAL box above...
ridzuan_student
- 22nd October 2008, 12:42
halo...i i'm beginer in PIC use....so i need somebody to give me a PIC coding to controll bidirectional DC motor for my project...i relly2 need help....for those who can help me...i need a that coding just to simulate in proteus 7...i will represent that dc motor to LED as the outpu...
mackrackit
- 22nd October 2008, 12:58
halo...i i'm beginer in PIC use....so i need somebody to give me a PIC coding to controll bidirectional DC motor for my project...i relly2 need help....for those who can help me...i need a that coding just to simulate in proteus 7...i will represent that dc motor to LED as the outpu...
Does you instructor know you are trying to cheat?
skimask
- 22nd October 2008, 14:24
Does you instructor know you are trying to cheat?
http://www.darreltaylor.com/files/ROFL.gif
That and I'm wondering how post #13 relates to the original thread...nice hi-jack eh?
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.