PDA

View Full Version : NEWB Help HPWM coding



Seafurymike
- 1st January 2008, 11:28
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.

Seafurymike
- 1st January 2008, 12:49
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

mister_e
- 1st January 2008, 18:38
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...



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

Seafurymike
- 1st January 2008, 21:52
thanks for your help, greatly appreciated.

I will look through your changes to understand what you did

/M