NEWB Help HPWM coding


Closed Thread
Results 1 to 4 of 4
  1. #1

    Default NEWB Help HPWM coding

    Hi,

    I am trying write some code to get the HPWM to drive a motor in forward and reverse.
    I have trawled the forums and google for ages and I have come up with this, but it doesn't want to compile.I get Syntax erros and bad expressions.

    Can I write If .. then loops like this??
    -----------------------------------------------------------------------------------------
    WHILE 1 = 1 'Infinite loop
    IF S1 = LOW THEN
    IF SPEED = 0 THEN DIRECTION = REVERSE 'If motor stopped, set reverse direction

    IF DIRECTION = REVERSE THEN 'If engine in reverse, then..
    IF SPEED < 255 THEN INC SPEED 'If Speed less than Maximum, then increase

    ELSE 'Else goto FORWARD loop
    IF SPEED > 0 THEN DEC SPEED 'If speed not yet 0, then decrease speed

    ENDIF
    ENDIF

    IF S2 = LOW THEN 'If S2 pressed, then
    IF SPEED = 0 THEN DIRECTION = FORWARD 'If motor stopped, set forward direction

    IF DIRECTION = FORWARD THEN 'If engine in reverse, then..
    IF SPEED < 255 THEN INC SPEED 'If speed not yet 255, then increase speed

    ELSE 'Else goto REVERSE loop)
    IF SPEED > 0 THEN DEC SPEED 'If speed not yet 0, then reduce until 0

    ENDIF
    ENDIF

    FORWARD = DIRECTION 'As DIRECTION = HIGH then
    'FORWARD = HIGH
    REVERSE = ~FORWARD 'REVERSE is negated to FORWARD

    HPWM 1, SPEED, 2000 'Alloctae 'SPEED' value to the CCP module

    PRINT AT 1, 13, DEC (SPEED * 100) / 255, "% " 'Reflect speed in %

    DELAYMS 10 'Increase SPEED
    IF SPEED = 0 THEN DELAYMS 700 'Wait longer when speed =0
    WEND 'To While
    -----------------------------------------------------------------------------------------

    Could someone please steer me in the right direction.

  2. #2


    Did you find this post helpful? Yes | No

    Default

    I'm not sure how this works, but when I compile the code with Proton_DS_Lite is compiles without errors.

    Can someone explain this to me please?

    /Michael

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


    Did you find this post helpful? Yes | No

    Default

    well, because
    1)PBP don't have PRINT but LCDOUT,
    2)PBP don't allow to use LOW as Variable, should be the same with PDS
    3)PBP don't use DEC to decrement a variable, not sure in PDS
    4)PBP don't use INC to increment a variable, not sure in PDS
    5)There's no DelayMS in PBP but PAUSE
    6)REVERSE is a reserved word in PBP

    Now if you use PDS, we can't really help, www.picbasic.org/forum will.

    you may try this one...
    Code:
    WHILE 1 = 1 'Infinite loop
        IF S1 = 0 THEN
            IF SPEED = 0 THEN DIRECTION = REVERSE 'If motor stopped, set reverse direction
            
            IF DIRECTION = REVERSE THEN 'If engine in reverse, then..
                IF SPEED < 255 THEN 
                    SPEEED= SPEED +1 'If Speed less than Maximum, then increase
                    ENDIF
                    
                ELSE 'Else goto FORWARD loop
                    IF SPEED > 0 THEN 
                        SPEED = SPEED -1 'If speed not yet 0, then decrease speed
                        ENDIF
                ENDIF
            ENDIF
        
        IF S2 = 0 THEN 'If S2 pressed, then
            IF SPEED = 0 THEN DIRECTION = FORWARD 'If motor stopped, set forward direction
            
            IF DIRECTION = FORWARD THEN 'If engine in reverse, then..
                IF SPEED < 255 THEN 
                    SPEED=SPEED+1 'If speed not yet 255, then increase speed
                    ENDIF
                                
                ELSE 'Else goto REVERSE loop)
                    IF SPEED > 0 THEN 
                        SPEED= SPEED-1 'If speed not yet 0, then reduce until 0
                        ENDIF            
                ENDIF
            ENDIF
        
        FORWARD = DIRECTION 'As DIRECTION = HIGH then
        'FORWARD = HIGH
        REVERSE = ~FORWARD 'REVERSE is negated to FORWARD
        
        HPWM 1, SPEED, 2000 'Alloctae 'SPEED' value to the CCP module
        
        PRINT AT 1, 13, DEC (SPEED * 100) / 255, "% " 'Reflect speed in %
        
        DELAYMS 10 'Increase SPEED
        IF SPEED = 0 THEN 
            DELAYMS 700 'Wait longer when speed =0
            endif
            
        WEND 'To While
    HTH
    Last edited by mister_e; - 1st January 2008 at 18:44.
    Steve

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

  4. #4


    Did you find this post helpful? Yes | No

    Default

    thanks for your help, greatly appreciated.

    I will look through your changes to understand what you did

    /M

Similar Threads

  1. HPWM coding
    By civictypeR in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 29th January 2008, 14:54
  2. need help on hpwm
    By helmut in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th August 2007, 15:49
  3. HPWM of the port of two HPWM
    By ja2rqk in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th April 2007, 15:05
  4. sound command & music
    By trying in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th May 2006, 14:14
  5. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43

Members who have read this thread : 1

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