Skip to content

Commit 5885f6d

Browse files
committed
Initial variable pwm implementation
Added missing bits to update PWM cycle length Fixed several bugs Added low and hight variable pwm rc pulse thresholds Fixed some bugs
1 parent b0ed7db commit 5885f6d

File tree

1 file changed

+73
-30
lines changed

1 file changed

+73
-30
lines changed

Bluejay.asm

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ DEFAULT_PGM_DITHERING EQU 1 ; 0=Disabled, 1=Enabled
157157

158158
DEFAULT_PGM_STARTUP_POWER_MAX EQU 25 ; 0..255 => (1000..2000 Throttle): Maximum startup power
159159
DEFAULT_PGM_BRAKING_STRENGTH EQU 255 ; 0..255 => 0..100 % Braking
160+
DEFAULT_VAR_PWM_LO_THRES EQU 100 ; About 40% rc pulse
161+
DEFAULT_VAR_PWM_HI_THRES EQU 150 ; About 60% rc pulse
160162

161163
;**** **** **** **** ****
162164
; Temporary register definitions
@@ -194,7 +196,7 @@ Flag_Dir_Change_Brake BIT Flags1.5 ; Set when braking before direction change
194196
Flag_High_Rpm BIT Flags1.6 ; Set when motor rpm is high (Comm_Period4x_H less than 2)
195197

196198
Flags2: DS 1 ; State flags. NOT reset upon motor_start
197-
; BIT Flags2.0
199+
Flag_Variable_Pwm_Bits BIT Flags2.0 ; Set when programmed variable pwm
198200
Flag_Pgm_Dir_Rev BIT Flags2.1 ; Set if the programmed direction is reversed
199201
Flag_Pgm_Bidir BIT Flags2.2 ; Set if the programmed control mode is bidirectional operation
200202
Flag_32ms_Elapsed BIT Flags2.3 ; Set when timer2 interrupt is triggered
@@ -330,6 +332,8 @@ _Pgm_Pwm_Dither: DS 1 ; Output PWM dither
330332
Pgm_Brake_On_Stop: DS 1 ; Braking when throttle is zero
331333
Pgm_LED_Control: DS 1 ; LED control
332334
Pgm_Power_Rating: DS 1 ; Power rating
335+
Pgm_Var_PWM_lo_thres: DS 1 ; Variable PWM low rcpulse threshold
336+
Pgm_Var_PWM_hi_thres: DS 1 ; Variable PWM high rcpulse threshold
333337

334338
ISEG AT 0B0h
335339
Stack: DS 16 ; Reserved stack space
@@ -351,7 +355,7 @@ ENDIF
351355
EEPROM_FW_MAIN_REVISION EQU 0 ; Main revision of the firmware
352356
EEPROM_FW_SUB_REVISION EQU 18 ; Sub revision of the firmware
353357
EEPROM_LAYOUT_REVISION EQU 206 ; Revision of the EEPROM layout
354-
EEPROM_B2_PARAMETERS_COUNT EQU 27 ; Number of parameters
358+
EEPROM_B2_PARAMETERS_COUNT EQU 29 ; Number of parameters
355359

356360
Eep_FW_Main_Revision: DB EEPROM_FW_MAIN_REVISION ; EEPROM firmware main revision number
357361
Eep_FW_Sub_Revision: DB EEPROM_FW_SUB_REVISION ; EEPROM firmware sub revision number
@@ -398,6 +402,8 @@ _Eep_Pgm_Pwm_Dither: DB 0FFh
398402
Eep_Pgm_Brake_On_Stop: DB DEFAULT_PGM_BRAKE_ON_STOP ; EEPROM copy of programmed braking when throttle is zero
399403
Eep_Pgm_LED_Control: DB DEFAULT_PGM_LED_CONTROL ; EEPROM copy of programmed LED control
400404
Eep_Pgm_Power_Rating: DB DEFAULT_PGM_POWER_RATING ; EEPROM copy of programmed power rating
405+
Eep_Pgm_Var_PWM_lo_thres: DB DEFAULT_VAR_PWM_LO_THRES ; EEPROM copy of variable PWM low rcpulse threshold
406+
Eep_Pgm_Var_PWM_hi_thres: DB (DEFAULT_VAR_PWM_HI_THRES - DEFAULT_VAR_PWM_LO_THRES) ; EEPROM copy of variable PWM high rcpulse threshold
401407

402408
Eep_Dummy: DB 0FFh ; EEPROM address for safety reason
403409

@@ -976,6 +982,47 @@ t1_int_zero_rcp_checked:
976982
mov Temp6, Pwm_Limit_By_Rpm
977983

978984

985+
; Check variable pwm
986+
jnb Flag_Variable_Pwm_Bits, t1_int_variable_pwm_done
987+
988+
; If variable pwm, set pwm bits depending on PWM_CENTERED 1 [3-1] or 0 [2-0]
989+
; and 8 bit rc pulse Temp2
990+
clr C
991+
mov A, Temp2 ; Load 8bit rc pulse
992+
993+
t1_int_variable_pwm_lt_lo_rcpulse:
994+
; Compare rc pulse to Pgm_Var_PWM_lo_thres
995+
mov Temp1, #Pgm_Var_PWM_lo_thres ; Load low rc pulse threshold pointer
996+
subb A, @Temp1
997+
jnc t1_int_variable_pwm_gt_lo_rcpulse
998+
999+
; rc pulse <= Pgm_Var_PWM_lo_thres -> choose 96khz
1000+
mov PwmBitsCount, #0
1001+
sjmp t1_int_variable_pwm_centered
1002+
1003+
t1_int_variable_pwm_gt_lo_rcpulse:
1004+
; rc pulse > Pgm_Var_PWM_lo_thres -> choose 48khz or 24khz
1005+
mov Temp1, #Pgm_Var_PWM_hi_thres ; Load high rc pulse threshold pointer
1006+
subb A, @Temp1
1007+
jnc t1_int_variable_pwm_gt_hi_rcpulse
1008+
1009+
; rc pulse <= Pgm_Var_PWM_hi_thres -> choose 48khz
1010+
mov PwmBitsCount, #1
1011+
sjmp t1_int_variable_pwm_centered
1012+
1013+
t1_int_variable_pwm_gt_hi_rcpulse:
1014+
; rc pulse > Pgm_Var_PWM_hi_thres -> choose 24khz
1015+
mov PwmBitsCount, #2
1016+
1017+
t1_int_variable_pwm_centered:
1018+
IF PWM_CENTERED == 0
1019+
; Increment PwmBits count
1020+
inc PwmBitsCount
1021+
ENDIF
1022+
1023+
t1_int_variable_pwm_done:
1024+
1025+
9791026
; Limit PWM and scale pwm resolution and invert (duty cycle is defined inversely)
9801027
; depending on pwm bits count
9811028
mov A, PwmBitsCount
@@ -1045,7 +1092,11 @@ t1_int_pwm_limit_scale_dithering_pwm10bit_limited:
10451092
t1_int_pwm_limit_scale_dithering_pwm10bit_scaled:
10461093
mov A, Temp4 ; 11-bit low byte
10471094
cpl A
1048-
anl A, #((1 SHL (3 - 2)) - 1) ; Get index into dithering pattern table
1095+
anl A, #((1 SHL (3 - 2)) - 1) ; Get index [0,1] into dithering pattern table
1096+
1097+
; Multiplying by 4, select pattern [0, 4] on unified dithering pattern table
1098+
rl A
1099+
rl A
10491100

10501101
add A, #Dithering_Patterns
10511102
mov Temp1, A ; Reuse DShot pwm pointer since it is not currently in use.
@@ -1107,7 +1158,10 @@ t1_int_pwm_limit_scale_dithering_pwm9bit_limited:
11071158

11081159
mov A, Temp4 ; 11-bit low byte
11091160
cpl A
1110-
anl A, #((1 SHL (3 - 1)) - 1) ; Get index into dithering pattern table
1161+
anl A, #((1 SHL (3 - 1)) - 1) ; Get index [0-3] into dithering pattern table
1162+
1163+
; Multiplying by 2, select pattern [0, 2, 4, 6] on unified dithering pattern table
1164+
rl A
11111165

11121166
add A, #Dithering_Patterns
11131167
mov Temp1, A ; Reuse DShot pwm pointer since it is not currently in use.
@@ -1210,6 +1264,11 @@ t1_int_pwm_braking_set:
12101264
ENDIF
12111265

12121266

1267+
; Update pwm cycle length (8-11 bits)
1268+
mov A, #80h
1269+
add A, PwmBitsCount
1270+
mov PCA0PWM, A
1271+
12131272
; Note: Interrupts are not explicitly disabled
12141273
; Assume higher priority interrupts (Int0, Timer0) to be disabled at this point
12151274
; Set power and damp pwm auto-reload registers
@@ -3942,6 +4001,8 @@ set_default_parameters:
39424001
imov Temp1, #DEFAULT_PGM_BRAKE_ON_STOP ; Pgm_Brake_On_Stop
39434002
imov Temp1, #DEFAULT_PGM_LED_CONTROL ; Pgm_LED_Control
39444003
imov Temp1, #DEFAULT_PGM_POWER_RATING ; Pgm_Power_Rating
4004+
imov Temp1, #DEFAULT_VAR_PWM_LO_THRES ; Pgm_Var_PWM_lo_thres
4005+
imov Temp1, #(DEFAULT_VAR_PWM_HI_THRES - DEFAULT_VAR_PWM_LO_THRES) ; Pgm_Var_PWM_hi_thres
39454006

39464007
ret
39474008

@@ -4092,31 +4153,7 @@ decode_pwm_dithering:
40924153
add A, #0FFh ; Carry set if A is not zero
40934154
mov Flag_Dithering, C ; Set dithering enabled
40944155

4095-
; Decode dithering pattern depending on PwmBitsCount
4096-
mov A, PwmBitsCount
4097-
4098-
decode_pwm_dithering_pwm10bit:
4099-
cjne A, #2, decode_pwm_dithering_pwm9bit
4100-
4101-
; Initialize pwm dithering bit patterns
4102-
mov Temp1, #Dithering_Patterns ; 1-bit dithering (10-bit to 11-bit)
4103-
mov @Temp1, #00h ; 00000000
4104-
imov Temp1, #55h ; 01010101
4105-
sjmp decode_pwm_dithering_done
4106-
4107-
decode_pwm_dithering_pwm9bit:
4108-
cjne A, #1, decode_pwm_dithering_pwm8bit
4109-
4110-
mov Temp1, #Dithering_Patterns ; 2-bit dithering (9-bit to 11-bit)
4111-
mov @Temp1, #00h ; 00000000
4112-
imov Temp1, #11h ; 00010001
4113-
imov Temp1, #55h ; 01010101
4114-
imov Temp1, #77h ; 01110111
4115-
sjmp decode_pwm_dithering_done
4116-
4117-
decode_pwm_dithering_pwm8bit:
4118-
cjne A, #0, decode_pwm_dithering_done
4119-
4156+
; Initialize unified dithering pattern table
41204157
mov Temp1, #Dithering_Patterns ; 3-bit dithering (8-bit to 11-bit)
41214158
mov @Temp1, #00h ; 00000000
41224159
imov Temp1, #01h ; 00000001
@@ -4127,7 +4164,7 @@ decode_pwm_dithering_pwm8bit:
41274164
imov Temp1, #77h ; 01110111
41284165
imov Temp1, #7fh ; 01111111
41294166

4130-
decode_pwm_dithering_done:
4167+
; All decoding done
41314168
ret
41324169

41334170
;**** **** **** **** **** **** **** **** **** **** **** **** ****
@@ -4147,6 +4184,12 @@ calculate_pwm_bits:
41474184
; Let Temp1 = Pgm_Pwm_Freq / 24
41484185
mov Temp1, #Pgm_Pwm_Freq
41494186

4187+
calculate_pwm_bits_variable_pwm_bits:
4188+
; If pwm is variable do not substract and set variable pwm flag
4189+
cjne @Temp1, #192, calculate_pwm_bits_pwm96bits
4190+
setb Flag_Variable_Pwm_Bits
4191+
sjmp calculate_pwm_bits_pwm_decoded
4192+
41504193
calculate_pwm_bits_pwm96bits:
41514194
; If pwm is 96 khz substract 2
41524195
cjne @Temp1, #96, calculate_pwm_bits_pwm48bits

0 commit comments

Comments
 (0)