button input


Closed Thread
Results 1 to 15 of 15

Thread: button input

  1. #1
    Join Date
    Sep 2006
    Posts
    42

    Default button input

    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.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Code:
    IF (DIP = 0) AND (BUT = 1) THEN SOMETHING
    IF (DIP = 1) AND (BUT = 1) THEN SOMETHING ELSE
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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)...


    Code:
    	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

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    Not as elegant as Dave's...
    Not sure how to take that
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Sep 2006
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Code:
    	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.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Put one dip on a port. Then read the whole PORT into a variable.
    Code:
    dip1=PORTB
    dip2=PORTC
    Read this thread, post #2
    http://www.picbasic.co.uk/forum/show...ight=read+port
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Sep 2006
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Thank you. I tried to understand from the PBP manual but i couldnt succeed. could you pls give some code Example.

    Regards.

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by naga View Post
    could you pls give some code Example.
    The code example is right there in the last post!

  9. #9
    Join Date
    Oct 2008
    Posts
    8


    Did you find this post helpful? Yes | No

    Default HI can we put a PWM command in?

    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

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  11. #11
    Join Date
    Oct 2008
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    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?

  12. #12
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mrhrholden View Post
    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...

  13. #13
    ridzuan_student's Avatar
    ridzuan_student Guest


    Did you find this post helpful? Yes | No

    Default i need help..

    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...

  14. #14
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ridzuan_student View Post
    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?
    Dave
    Always wear safety glasses while programming.

  15. #15
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    Does you instructor know you are trying to cheat?


    That and I'm wondering how post #13 relates to the original thread...nice hi-jack eh?

Similar Threads

  1. RB0 + Internal Pullup + Interrupt
    By Freman in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 10th August 2009, 11:11
  2. button input: what is necessary for a proofed behaviour
    By mischl in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th November 2007, 15:45
  3. Timing input pulses and re-outputting them
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th February 2007, 01:50
  4. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  5. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts