Addition and subtraction


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Addition and subtraction

    I don't know how to do that quote thing or I'd post my code in a thread. I started out writing a simple program to increase and decrease a timer based upon button inputs. When I do the increase, everything works great. When I hit the button to do the decrease, instead of adding 50ms, it adds a lot more then that. I don't have a display attached to it so I can't debug it but it's strange. Maybe a fresh set of eyes will be able to find the problem.

    @ DEVICE PIC16F688, INTRC_OSC_NOCLKOUT, WDT_ON, PWRT_OFF, MCLR_OFF, BOD_ON,PROTECT_OFF

    DEFINE OSC 4
    TRISA = %00111011
    TRISC = %00111110
    ANSEL = %00000000
    CMCON0=7
    PULSEOFFDEC VAR PORTC.4 'PULSE OFF DECREASE BUTTON
    PULSEOFFINC VAR PORTC.3 'PULSE OFF INCREASE BUTTON
    RELAY VAR PORTA.2 'RELAY OUTPUT
    PULSEONINC VAR PORTC.1 'PULSE ON INCREASE BUTTON
    PULSEONDEC VAR PORTC.2 'PULSE ON DECREASE BUTTON
    ONOFFBUT VAR PORTC.5 'ONOFFBUTTON
    ONTIME VAR WORD
    OFFTIME VAR WORD
    RELAY=0

    ONTIME=500
    OFFTIME=500


    '------------------------------START OF PROGRAM---------------------------------
    PAUSE 1000
    START1:
    IF ONOFFBUT=1 THEN
    GOTO START1
    RELAY=0
    PAUSE 500
    ENDIF


    START:

    '-------------------------PULSEONINCREASE---------------------------------------
    IF PULSEONINC=1 THEN
    ONTIME=(ONTIME+50)
    PULSEONINCPAUSE:
    IF PULSEONINC=1 THEN
    GOTO PULSEONINCPAUSE
    ENDIF
    ENDIF

    '-------------------------PULSEONDECREASE---------------------------------------
    IF PULSEONDEC=1 THEN
    ONTIME=(ONTIME-50)
    PULSEONDECPAUSE:
    IF PULSEONINC=1 THEN
    GOTO PULSEONDECPAUSE
    ENDIF
    ENDIF

    '-------------------------PULSEOFFINCREASE---------------------------------------
    IF PULSEOFFINC=1 THEN
    OFFTIME=(OFFTIME+50)
    PULSEOFFINCPAUSE:
    IF PULSEOFFINC=1 THEN
    GOTO PULSEOFFINCPAUSE
    ENDIF
    ENDIF

    '-------------------------PULSEOFFDECREASE---------------------------------------
    IF PULSEOFFDEC=1 THEN
    OFFTIME=(OFFTIME-50)
    PULSEOFFDECPAUSE:
    IF PULSEOFFINC=1 THEN
    GOTO PULSEOFFDECPAUSE
    ENDIF
    ENDIF

    '-------------------------ON/OFF BUTTON-----------------------------------------
    IF ONOFFBUT=1 THEN GOTO MAIN_SEQUENCE
    GOTO START




    '------------------------- MAIN SEQUENCE----------------------------------------
    MAIN_SEQUENCE:
    RELAY=1
    PAUSE ONTIME
    RELAY=0
    PAUSE OFFTIME
    IF ONOFFBUT=1 THEN GOTO START1
    GOTO MAIN_SEQUENCE

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Christopher4187 View Post
    I don't know how to do that quote thing or I'd post my code in a thread. I started out writing a simple program to increase and decrease a timer based upon button inputs. When I do the increase, everything works great. When I hit the button to do the decrease, instead of adding 50ms, it adds a lot more then that. I don't have a display attached to it so I can't debug it but it's strange. Maybe a fresh set of eyes will be able to find the problem.
    Could be a switch debouncing problem.
    Every time you hit a button, whether you realize it or not, the switch bounces on/off a bunch of times for a few ms. So, in that nice tight loop you got that detects a switch press, the code sees the press, changes the time, then right away sees the switch not pressed and jumps back to the main loop, but the switch is still bouncing, so it jumps to the time changing bit, and so on and so on...
    Try putting a simple 'PAUSE 10' every time you detect a switch has been pressed and see what happens. If that doesn't work, try 'PAUSE 50' and try again.

    Try this bit instead:

    @ DEVICE PIC16F688, INTRC_OSC_NOCLKOUT, WDT_ON, PWRT_OFF, MCLR_OFF, BOD_ON,PROTECT_OFF

    DEFINE OSC 4
    TRISA = %00111011 : TRISC = %00111110 : ANSEL = %00000000 : CMCON0=7
    PULSEOFFDEC VAR PORTC.4 'PULSE OFF DECREASE BUTTON
    PULSEOFFINC VAR PORTC.3 'PULSE OFF INCREASE BUTTON
    RELAY VAR PORTA.2 'RELAY OUTPUT
    PULSEONINC VAR PORTC.1 'PULSE ON INCREASE BUTTON
    PULSEONDEC VAR PORTC.2 'PULSE ON DECREASE BUTTON
    ONOFFBUT VAR PORTC.5 'ONOFFBUTTON
    ONTIME VAR WORD : OFFTIME VAR WORD : RELAY=0 : ONTIME=500 : OFFTIME=500
    '------------------------------START OF PROGRAM---------------------------------
    PAUSE 1000
    START1: IF ONOFFBUT=1 THEN START1
    RELAY=0 : PAUSE 500

    START:
    '-------------------------PULSEONINCREASE---------------------------------------
    IF PULSEONINC=1 THEN
    ONTIME=(ONTIME+50) : pause 50
    PULSEONINCPAUSE: IF PULSEONINC=1 THEN PULSEONINCPAUSE
    ENDIF

    '-------------------------PULSEONDECREASE---------------------------------------
    IF PULSEONDEC=1 THEN
    ONTIME=(ONTIME-50) : pause 50
    PULSEONDECPAUSE: IF PULSEONINC=1 THEN PULSEONDECPAUSE
    ENDIF

    '-------------------------PULSEOFFINCREASE---------------------------------------
    IF PULSEOFFINC=1 THEN
    OFFTIME=(OFFTIME+50) : pause 50
    PULSEOFFINCPAUSE: IF PULSEOFFINC=1 THEN PULSEOFFINCPAUSE
    ENDIF

    '-------------------------PULSEOFFDECREASE---------------------------------------
    IF PULSEOFFDEC=1 THEN
    OFFTIME=(OFFTIME-50) : pause 50
    PULSEOFFDECPAUSE: IF PULSEOFFINC=1 THEN PULSEOFFDECPAUSE
    ENDIF

    '-------------------------ON/OFF BUTTON-----------------------------------------
    IF ONOFFBUT=1 THEN pause 50 : goto MAIN_SEQUENCE
    GOTO START

    '------------------------- MAIN SEQUENCE----------------------------------------
    MAIN_SEQUENCE:
    RELAY=1 : PAUSE ONTIME : RELAY=0 : PAUSE OFFTIME
    IF ONOFFBUT=1 THEN START1
    GOTO MAIN_SEQUENCE

    END
    Last edited by skimask; - 25th January 2008 at 03:58.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Christopher4187 View Post
    I don't know how to do that quote thing or I'd post my code in a thread. I started out writing a simple program to increase and decrease a timer based upon button inputs.
    to quote, use the word quote enlosed in square brackets and end the quote with /quote in square brackets. Same applies for code.


    Quote Originally Posted by Christopher4187 View Post
    When I do the increase, everything works great. When I hit the button to do the decrease, instead of adding 50ms, it adds a lot more then that. Maybe a fresh set of eyes will be able to find the problem.
    How true, See the highlights

    Code:
    '-------------------------PULSEONDECREASE---------------------------------------
    IF PULSEONDEC=1 THEN
    ONTIME=(ONTIME-50)
    PULSEONDECPAUSE:
    IF PULSEONINC=1 THEN 
    GOTO PULSEONDECPAUSE
    ENDIF
    ENDIF
    and here
    Code:
    '-------------------------PULSEOFFDECREASE---------------------------------------
    IF PULSEOFFDEC=1 THEN
    OFFTIME=(OFFTIME-50)
    PULSEOFFDECPAUSE:
    IF PULSEOFFINC=1 THEN 
    GOTO PULSEOFFDECPAUSE
    ENDIF
    ENDIF
    Good luck
    JF

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Also . . .

    Never mind . . .
    Last edited by Archangel; - 25th January 2008 at 07:33.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default

    Thanks Jerson! Sometimes the copy and paste function coupled with no sleep can be a real problem! But, you've helped me once again!

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    How true, See the highlights
    DOH!!!!
    Missed that myself...and I was looking for it!

  7. #7
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Christopher4187
    Thanks Jerson! Sometimes the copy and paste function coupled with no sleep can be a real problem! But, you've helped me once again!
    My pleasure


    JF

Similar Threads

  1. Mathematical Precedence
    By keithdoxey in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th October 2006, 21:44

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