PDA

View Full Version : programming error.. plz help



Srigopal007
- 4th November 2004, 17:28
here is my code and my objective is to make all the LEDs turn on and off at the same time, but when a switch is pressed and let go, the PWM should pause, and when you press the switch again it should resume where it left off. I have tried to implement this on my program, I konw that the theory is valid, but the output is not working. any help or suggestions would be greatly appreciated.

here is where I have so far:
==============================================
include "modedefs.bas"
'Objective to turn LEDS on and off at the same time
Duty VAR Word
Cycle0 VAR BYTE
Cycle1 VAR BYTE
cycles VAR Word
Switch Var PORTC.6
TRISB = %00000000
TRISC = %01000000

DEFINE OSC 20
start:
Cycle1 = 2



Loop:
'sequence 1
For Duty = 0 to 2000 'ALL OFF ---> All ON
if(Switch = 0) then pause 100 'waiting for debounce to stop
if(Switch = 1) then Duty = Duty - 1 'now the pause to PWM appears when switch is high

For cycles = 0 to Cycle1
Portb = %00000000 Pauseus 2000 - Duty
Portb = %11110000
Pauseus Duty
Next cycles

Next Duty
Pause 1000
'sequence 0 (t = 1)
For Duty = 2000 to 0 step -1 'ALL ON--> ALL OFF
if(Switch = 0) then pause 100
if(Switch = 1) then Duty = Duty - 1
For cycles = 0 to Cycle1
Portb = %11110000 Pauseus Duty Portb = %00100000
Pauseus 2000 - Duty
Next cycles
Next Duty
Pause 1000

Goto Loop

mister_e
- 5th November 2004, 00:17
Here's my version... we will check the switch action inside delay procedure.




'Objective to turn LEDS on and off at the same time

DEFINE OSC 20


Duty VAR Word
Cycle0 VAR BYTE
Cycle1 VAR BYTE
cycles VAR Word
Switch Var PORTC.6
DelayV VAR BYTE

TRISB = %00000000
TRISC = %01000000

start:

Cycle1 = 2



Loop:

;sequence 1
;==========

For Duty = 0 to 2000 'ALL OFF ---> All ON
For cycles = 0 to Cycle1
Portb = %00000000
DelayV=2000-Duty
Gosub DelayUs
Portb = %11110000
DelayV=Duty
Gosub DelayUs
Next cycles

Next Duty

DelayV=1000
Gosub DelayUs

;sequence 0 (t = 1)
;=================
For Duty = 2000 to 0 step -1 'ALL ON--> ALL OFF
For cycles = 0 to Cycle1
Portb = %11110000
DelayV=Duty
Gosub DelayUs
Portb = %00100000
DelayV=2000-Duty
Gosub DelayUs
Next cycles
Next Duty

DelayV=1000
Gosub DelayUs

Goto Loop



;I will assume the minimum uSec pause is 4 (3 by the book)

DelayUs:
DelayV=DelayV/4
while DelayV !=0
Pauseus 4
If switch then
gosub StopEverything
ENDIF
DelayV=DelayV-1
WEND
return

StopEverything:
While switch ;wait untill switch is release
Wend
PAUSE 100 ; cheap debounce time

While switch=0 ;wait for switch press
Wend
return



what about now srig ?

regards

mister_e
- 5th November 2004, 00:24
i presume light will turn off or totaly on when switch is pressed.

oops i still have to work on it... see you later

mister_e
- 5th November 2004, 00:50
'Objective to turn LEDS on and off at the same time

DEFINE OSC 20

TRISB = %00000000
TRISC = %01000000

StopAll VAR bit
Duty VAR Word
Cycle0 VAR BYTE
Cycle1 VAR BYTE
cycles VAR Word
Switch Var PORTC.6
DelayV VAR BYTE

Cycle1 = 2
stopall = 0

Loop:

;sequence 1
;ALL OFF ---> All ON
;====================

duty=0
while duty !=2000
For cycles = 0 to Cycle1
Portb = %00000000
DelayV=2000-Duty
Gosub DelayUs
Portb = %11110000
DelayV=Duty
Gosub DelayUs
Next cycles
if StopAll=0 then duty=duty+1
Wend

DelayV=1000
Gosub DelayUs


;sequence 0 (t = 1)
;ALL ON--> ALL OFF
;===================

duty=2000
For Duty = 2000 to 0 step -1
For cycles = 0 to Cycle1
Portb = %11110000
DelayV=Duty
Gosub DelayUs
Portb = %00100000
DelayV=2000-Duty
Gosub DelayUs
Next cycles
if stopall=0 then duty=duty-1
Next Duty

DelayV=1000
Gosub DelayUs

Goto Loop



;I will assume the minimum uSec pause is 4 (3 by the book)
;================================================= ========
DelayUs:
DelayV=DelayV/4
while DelayV !=0
Pauseus 4
If switch then
gosub StopEverything
ENDIF
DelayV=DelayV-1
WEND
return

StopEverything:
While switch ;wait untill switch is release
Wend
PAUSE 50 ; cheap debounce time
If stopall=0 then
stopall=1
else
stopall=0
endif
return


now it's working... i hope

regards

mister_e
- 5th November 2004, 15:28
Oups forget the above.... damn

'Objective to turn LEDS on and off at the same time

DEFINE OSC 20

TRISB = %00000000
TRISC = %01000000

StopAll VAR bit
Duty VAR Word
Cycle0 VAR BYTE
Cycle1 VAR BYTE
cycles VAR Word
Switch Var PORTC.6
DelayV VAR BYTE

Cycle1 = 2
stopall = 0

Loop:

;sequence 1
;ALL OFF ---> All ON
;====================

cycles=0
for duty=0 to 2000
while cycles != Cycle1
Portb = %00000000
DelayV=2000-Duty
Gosub DelayUs
Portb = %11110000
DelayV=Duty
Gosub DelayUs
if stopall=0 then cycles = cycles +1
Wend
Next

DelayV=1000
Gosub DelayUs


;sequence 0 (t = 1)
;ALL ON--> ALL OFF
;===================

cycles=0

For duty=2000 to 0 step -1
while cycles != Cycle1
Portb = %11110000
DelayV=Duty
Gosub DelayUs
Portb = %00100000
DelayV=2000-Duty
Gosub DelayUs
if StopAll=0 then cycles = cycles + 1
wend
next

DelayV=1000
Gosub DelayUs

Goto Loop

;I assume minimum uSec pause is 4 (3 by the book)
;=========================================
DelayUs:
DelayV=DelayV/4
while DelayV !=0
Pauseus 4
If switch then
gosub StopEverything
ENDIF
DelayV=DelayV-1
WEND
return

StopEverything:
While switch ;wait untill switch is release
Wend
PAUSE 50 ; cheap debounce time
If stopall=0 then
stopall=1
else
stopall=0
endif
return
Better use this one... was missing my pillow when i post the one before...