PDA

View Full Version : Weird PWM Behaviour on 16F1825



RossWaddell
- 17th October 2012, 02:35
It's possible I'm losing my mind, but why would this code make my motor spin faster than the revised code below? Everything else is the same (code, config, circuit connections).

Sample code 1 (motor spins up to correct RPM, from Test_SN754410_Reverse_0):


HIGH MOTOR_2_DIR ' reverse
' Spin up motors to saved value of _MotorRPM
IF MotorRPM > 66 THEN
FOR I = (255-65) to (255-MotorRPM) step -1
pause 30
HPWM 2, I, 20000 ' port engine (CCP2)
NEXT I
EndIf
HPWM 2, 255-MotorRPM, 20000' port engine (CCP2)


Sample code 2 (motor spins up to a lower RPM regardless of initial value of PortEngDir, from Test_SN754410_Reverse_1):


PortEngDir VAR BYTE
PortEngDir = 1

if PortEngDir = 1 then
HIGH MOTOR_2_DIR ' reverse
IF MotorRPM > 66 THEN
FOR I = (255-65) to (255-MotorRPM) step -1
pause 30
HPWM 2, I, 20000 ' port engine (CCP2)
NEXT I
EndIf
HPWM 2, 255-MotorRPM, 20000' port engine (CCP2)
else
LOW MOTOR_2_DIR ' Forward
IF MotorRPM > 66 THEN
FOR I = 65 to (MotorRPM - 1)
pause 30
HPWM 2, I, 20000 ' port engine (CCP2)
NEXT I
EndIf
HPWM 2, MotorRPM, 20000 ' port engine (CCP2)
endif


Is there something about nested IF blocks I don't know about?

RossWaddell
- 18th October 2012, 16:51
What's weird is that the code in the Main loop is the same, but if I program the same PIC16F1825 chip with the 2nd file (Test_SN754410_Reverse_1) then the motor spins at the lower RPM as it does in the IF..ELSE..ENDIF block before Main.

Anyone have any ideas? Ultimately, I'm modifying the program to allow using a switch to control motor direction but I don't want to lose how the motors spun with the previous code (as in Test_SN754410_Reverse_0)

RossWaddell
- 21st October 2012, 15:40
Anyone? A correction to the above is that the faster RPM is **not** what I want - I think this a cause of my EEPROM variable getting a weird initial setting of greater than what I have in the code (possibly linked to my post here (http://www.picbasic.co.uk/forum/showthread.php?t=2444&page=2&p=116771#post116771)).

mackrackit
- 21st October 2012, 17:22
In both cases it defaults to

IF MotorRPM > 66 THEN

So it will always go to the second IF in the nest.??

RossWaddell
- 21st October 2012, 18:31
But the direction of the motor should be different (if you're talking about the 2nd sample code). Could it be that with additional code lines somehow the chip isn't being completely erased before re-programming so the EEPROM variable is some number > 255?

Demon
- 23rd October 2012, 05:22
You should check the settings on your programmer.

Robert

RossWaddell
- 23rd October 2012, 12:52
I set them back to default after reading a post from Darrel on the EE_Var thread, but that hasn't helped. Which settings in particular are you thinking of?

Demon
- 23rd October 2012, 14:42
Erase code, erase eeprom settings for starters.

RossWaddell
- 23rd October 2012, 14:50
I had checked those earlier, Demon, and also selected the 'Set to default'. I'll look over those again tonight.

RossWaddell
- 24th October 2012, 00:47
Here's a screenshot of my settings:

6715

Demon
- 24th October 2012, 02:19
I think you are interested in 3 things in particular: erasing before programming, programming and erase options.

Here are my settings:

6717

6718

Robert

Demon
- 24th October 2012, 02:21
... Could it be that with additional code lines somehow the chip isn't being completely erased before re-programming so the EEPROM variable is some number > 255?


What if you display the eeprom value to an LCD or send it out to PC via RS232?

It would help to eliminate that as the source of your problem.

Demon
- 24th October 2012, 17:20
Ross, what about your Program settings?

6720

RossWaddell
- 24th October 2012, 18:41
I have the same settings except I also have the 'User ID' checked/enabled.

RossWaddell
- 26th October 2012, 21:59
Problem resolved. I was updating DT_INST-14.bas directly for different chips based on instructions for earlier versions of the file; now that I've downloaded the latest version and added the variables to my main program as needed, it seems to work fine.