Need help with a little optimization
I have been playing around with this code for a while, I'm slowly getting it down in size and I want to work on the Change Direction section of the Code.
Currently these pins control the motor directions of both M1 and M2 motors, One is high while the other is low.
M1_INA Pin RB.7 Output
M1_INB Pin RB.2 Output
M2_INA Pin RC.3 Output
M2_INB Pin RC.1 Output
Looking below does anyone have any suggestion on how to make this code more optimized?
Thanks in Advance,
Eric
Currently these pins control the motor directions of both M1 and M2 motors, One is high while the other is low.
M1_INA Pin RB.7 Output
M1_INB Pin RB.2 Output
M2_INA Pin RC.3 Output
M2_INB Pin RC.1 Output
Looking below does anyone have any suggestion on how to make this code more optimized?
Thanks in Advance,
Eric
DEVICE SX48, OSCHS3 FREQ 50_000_000 ID "MotorV.1" Baud CON "T19210" DataInPin Pin RB.0 Input DataOutPin Pin RB.1 OutPut MOTOR Var Byte Direction Var Byte(2) Speed Var Byte(2) MarkSpeed Var Byte Delay Var Byte Index Var Byte tmpB1 Var byte tmpB2 Var Byte Temp Var byte MOTOR Var Byte Direction Var Byte(2) Command Var byte LED0 Pin RD.0 LED1 Pin RD.1 LED2 Pin RD.2 LED3 Pin RD.3 LED4 Pin RE.0 LED5 Pin RE.1 LED6 Pin RE.2 LED7 Pin RE.3 M1_INA Pin RB.7 Output M1_INB Pin RB.2 Output M2_INA Pin RC.3 Output M2_INB Pin RC.1 Output M1_PWM Pin RB.6 Output M2_PWM Pin RC.2 Output ' ========================================================================= INTERRUPT 1000 TIMER1 PWM,Speed(1),256 TIMER2 PWM,Speed(2),256 ' ========================================================================= RETURNINT ACK Sub 0 SendData Sub 1 SET_SPEED Sub 0 Change_Direction Sub 0 Get_Info Sub 0 GetData Func 1 RB=%0100_0000 RC=%0000_1000 PROGRAM Start NoStartup Start: Tris_C=%00000000 Tris_E=%00000000 Tris_D=%00000000 Tris_B=%00000000 Main: Do DO Command=GetData LOOP UNTIL Command = "!" DO Command=GetData LOOP UNTIL Command = "M" DO Command=GetData LOOP UNTIL Command = "C" Command=GetData ON Command = "S", "D", "I" GOSUB SET_SPEED , Change_Direction, Get_Info Loop SET_SPEED: High LED0 MOTOR=GetData Speed(MOTOR)=GetData ACK Speed(MOTOR)=Speed(MOTOR) Max 128 Low LED0 Return Change_Direction: MOTOR=GetData Temp=GetData ACK High LED1 If Direction(MOTOR)= Temp Then E_X_I_T Direction(MOTOR)= Temp MarkSpeed=Speed(MOTOR) Do DEC Speed(MOTOR) Speed(MOTOR) = Speed(MOTOR) Min 1 Pause 5 LOOP Until Speed(MOTOR)=1 If MOTOR=1 Then If Direction(1)=1 Then High M1_INA Low M1_INB ElseIf Direction(1)=2 Then High M1_INB Low M1_INA EndIF EndIf If MOTOR=2 Then If Direction(2)=1 Then High M2_INA Low M2_INB ElseIf Direction(2)=2 Then High M2_INB Low M2_INA EndIF EndIf Do Inc Speed(MOTOR) Speed(MOTOR) = Speed(MOTOR) Max MarkSpeed Pause 5 LOOP Until Speed(MOTOR)=MarkSpeed Low LED1 E_X_I_T: Return Get_Info: Return Fault: HIGH LED3 PAUSE 10 LOW LED3 Return ACK: SendData "A" SendData "C" SendData "K" Return Return SendData: SerOut DataOutPin,Baud,__Param1 Return GetData: SerIn DataInPin,Baud,__Param1,100,FAULT Return __Param1
Comments
If you wish to reduce codespace you can remove a few of your IF and ELSE statements while still maintaining nearly the same functionality. For example, you check to see if you are addressing motor #1 and if so you handle changes to its data. Next you check to see if you are addressing motor #2 and if you are you handle changes to its data. I do not think you expect to change both motors during the same pass through the subroutine. You could therefore assume that if you are not processing data for motor #1 then you must be processing data for Motor #2. You could handle direction changes in a similar fashion. This will result in a codespace reduction. The drawback is that you must insure elsewhere (wherever these commands are being generated) that the assumptions being made will always be true! The subroutine as you have it written now will ignore commands for motor numbers larger than two. If you reduce some of the logic statements to save some space, any motor number greater than one will be treated as though it ought to be addressing motor two. If it is unlikely that invalid commands will be sent then you can save a few bytes of codespace by assuming that if condition one is not met than condition two probably was.
I realize this may not be the type of “optimization” you desired; but it is what came to my mind!
- Sparks
Eric
·