programming error.. plz help


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78

    Default programming error.. plz help

    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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Here's my version... we will check the switch action inside delay procedure.

    Code:
    '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
    Last edited by mister_e; - 5th November 2004 at 00:22.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    i presume light will turn off or totaly on when switch is pressed.

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

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

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

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Oups forget the above.... damn
    Code:
    '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...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Data Programming Error at 0000
    By cpatnoi in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 22nd May 2009, 03:37
  2. Problems programming
    By Lionheart in forum General
    Replies: 4
    Last Post: - 7th December 2008, 16:51
  3. Who can help me checking my Programming
    By slimpeng in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 23rd January 2008, 15:08
  4. PIC programming algorithm - where is it to find?
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th June 2007, 18:31
  5. MELab Programming
    By tarr in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd March 2006, 13:36

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