PDA

View Full Version : Addition and subtraction



Christopher4187
- 25th January 2008, 01:13
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

skimask
- 25th January 2008, 03:54
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

Jerson
- 25th January 2008, 05:10
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.




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



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


and here


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


Good luck
JF

Archangel
- 25th January 2008, 07:30
Never mind . . .

Christopher4187
- 25th January 2008, 13:19
Thanks Jerson! Sometimes the copy and paste function coupled with no sleep can be a real problem! But, you've helped me once again!

skimask
- 25th January 2008, 13:53
How true, See the highlights :o
DOH!!!!
Missed that myself...and I was looking for it!

Jerson
- 25th January 2008, 14:24
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