You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7234 lines
215 KiB

  1. $NOMOD51
  2. ;**** **** **** **** ****
  3. ;
  4. ; BLHeli program for controlling brushless motors in helicopters and multirotors
  5. ;
  6. ; Copyright 2011, 2012 Steffen Skaug
  7. ; This program is distributed under the terms of the GNU General Public License
  8. ;
  9. ; This file is part of BLHeli.
  10. ;
  11. ; BLHeli is free software: you can redistribute it and/or modify
  12. ; it under the terms of the GNU General Public License as published by
  13. ; the Free Software Foundation, either version 3 of the License, or
  14. ; (at your option) any later version.
  15. ;
  16. ; BLHeli is distributed in the hope that it will be useful,
  17. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ; GNU General Public License for more details.
  20. ;
  21. ; You should have received a copy of the GNU General Public License
  22. ; along with BLHeli. If not, see <http://www.gnu.org/licenses/>.
  23. ;
  24. ;**** **** **** **** ****
  25. ;
  26. ; The software was initially designed for use with Eflite mCP X, but is now adapted to copters/planes in general
  27. ;
  28. ; The software was inspired by and started from from Bernard Konze's BLMC: http://home.versanet.de/~bkonze/blc_6a/blc_6a.htm
  29. ; And also Simon Kirby's TGY: https://github.com/sim-/tgy
  30. ;
  31. ; This file is best viewed with tab width set to 5
  32. ;
  33. ; The input signal can be positive 1kHz, 2kHz, 4kHz, 8kHz or 12kHz PWM (e.g. taken from the "resistor tap" on mCPx)
  34. ; And the input signal can be PPM (1-2ms) or OneShot125 (125-250us) at rates up to several hundred Hz.
  35. ; The code adapts itself to the various input modes/frequencies
  36. ; The code ESC can also be programmed to accept inverted input signal.
  37. ;
  38. ; The first lines of the software must be modified according to the chosen environment:
  39. ; Uncomment the selected ESC and main/tail/multi mode
  40. ; BESCNO EQU "ESC"_"mode"
  41. ;
  42. ;**** **** **** **** ****
  43. ; Revision history:
  44. ; - Rev1.0: Initial revision based upon BLHeli for AVR controllers
  45. ; - Rev2.0: Changed "Eeprom" initialization, layout and defaults
  46. ; Various changes and improvements to comparator reading. Now using timer1 for time from pwm on/off
  47. ; Beeps are made louder
  48. ; Added programmable low voltage limit
  49. ; Added programmable damped tail mode (only for 1S ESCs)
  50. ; Added programmable motor rotation direction
  51. ; - Rev2.1: (minor changes by 4712)
  52. ; Added Disable TX Programming by PC Setup Application
  53. ; therfore changed EEPROM_LAYOUT_REVISION = 8
  54. ; Added Vdd Monitor as reset source when writing to "EEProm"
  55. ; Changed for use of batch file to assemble, link and make hex files
  56. ; - Rev2.2: (minor changes by 4712)
  57. ; Added Disable Throttle Re-Arming every motor start by PC Setup Application
  58. ; - Rev2.3: (minor changes by 4712)
  59. ; Added bugfixed (2x CLR C before j(n)c operations)thx Steffen!
  60. ; - Rev2.4: Revisions 2.1 to 2.3 integrated
  61. ; - Rev3.0: Added PPM (1050us-1866us) as accepted input signal
  62. ; Added startup rpm as a programming parameter
  63. ; Added startup acceleration as a programming parameter
  64. ; Added option for using voltage measurements to compensate motor power
  65. ; Added governor target by setup as a governor mode option
  66. ; Governor is kept active regardless of rpm
  67. ; Smooth governor spoolup/down in arm and setup modes
  68. ; Increased governor P and I gain programming ranges
  69. ; Increased and changed low voltage limit programming range
  70. ; Disabled tx programming entry for all but the first arming sequence after power on
  71. ; Made it possible to skip parameters in tx programming by setting throttle midstick
  72. ; Made it default not to rearm for every restart
  73. ; - Rev3.1: Fixed bug that prevented chosen parameter to be set in tx programming
  74. ; - Rev3.2: ...also updated the EEPROM revision parameter
  75. ; - Rev3.3: Fixed negative number bug in voltage compensation
  76. ; Fixed bug in startup power calculation for non-default power
  77. ; Prevented possibility for voltage compensation fighting low voltage limiting
  78. ; Applied overall spoolup control to ensure soft spoolup in any mode
  79. ; Added a delay of 3 seconds from initiation of main motor stop until new startup is allowed
  80. ; Reduced beep power to reduce power consumption for very strong motors/ESCs
  81. ; - Rev3.4: Fixed bug that prevented full power in governor arm and setup modes
  82. ; Increased NFETON_DELAY for XP_7A and XP_12A to allow for more powerful fets
  83. ; Increased initial spoolup power, and linked to startup power
  84. ; - Rev4.0: Fixed bug that made tail tx program beeps very weak
  85. ; Added thermal protection feature
  86. ; Governor P and I gain ranges are extended up to 8.0x gain
  87. ; Startup sequence is aborted upon zero throttle
  88. ; Avoided voltage compensation function induced latency for tail when voltage compensation is not enabled
  89. ; Improved input signal frequency detection robustness
  90. ; - Rev4.1: Increased thermal protection temperature limits
  91. ; - Rev5.0: Added multi(copter) operating mode. TAIL define changed to MODE with three modes: MAIN, TAIL and MULTI
  92. ; Added programmable commutation timing
  93. ; Added a damped light mode that has less damping, but that can be used with all escs
  94. ; Added programmable damping force
  95. ; Added thermal protection for startup too
  96. ; Added wait beeps when waiting more than 30 sec for throttle above zero (after having been armed)
  97. ; Modified tail idling to provide option for very low speeds
  98. ; Changed PPM range to 1150-1830us
  99. ; Arming sequence is dropped for PPM input, unless it is governor arm mode
  100. ; Loss of input signal will immediately stop the motor for PPM input
  101. ; Bug corrected in Turnigy Plush 6A voltage measurement setup
  102. ; FET switching delays are set for original fets. Stronger/doubled/tripled etc fets may require faster pfet off switching
  103. ; Miscellaneous other changes
  104. ; - Rev6.0: Reverted comparator reading routine to rev5.0 equivalent, in order to avoid tail motor stops
  105. ; Added governor range programmability
  106. ; Implemented startup retry sequence with varying startup power for multi mode
  107. ; In damped light mode, damping is now applied to the active nfet phase for fully damped capable ESCs
  108. ; - Rev6.1: Added input signal qualification criteria for PPM, to avoid triggering on noise spikes (fix for plush hardware)
  109. ; Changed main and multi mode stop criteria. Will now be in run mode, even if RC pulse input is zero
  110. ; Fixed bug in commutation that caused rough running in damped light mode
  111. ; Miscellaneous other changes
  112. ; - Rev7.0 Added direct startup mode programmability
  113. ; Added throttle calibration. Min>=1000us and Max<=2000us. Difference must be >520us, otherwise max is shifted so that difference=520us
  114. ; Added programmable throttle change rate
  115. ; Added programmable beep strength, beacon strength and beacon delay
  116. ; Reduced power step to full power significantly
  117. ; Miscellaneous other changes
  118. ; - Rev8.0 Added a 2 second delay after power up, to wait for receiver initialization
  119. ; Added a programming option for disabling low voltage limit, and made it default for MULTI
  120. ; Added programable demag compensation, using the concept of SimonK
  121. ; Improved robustness against noisy input signal
  122. ; Refined direct startup
  123. ; Removed voltage compensation
  124. ; Miscellaneous other changes
  125. ; - Rev9.0 Increased programming range for startup power, and made its default ESC dependent
  126. ; Made default startup method ESC dependent
  127. ; Even more smooth and gentle spoolup for MAIN, to suit larger helis
  128. ; Improved transition from stepped startup to run
  129. ; Refined direct startup
  130. ; - Rev9.1 Fixed bug that changed FW revision after throttle calibration or TX programming
  131. ; - Rev9.2 Altered timing of throttle calibration in order to work with MultiWii calibration firmware
  132. ; Reduced main spoolup time to around 5 seconds
  133. ; Changed default beacon delay to 3 minutes
  134. ; - Rev9.3 Fixed bug in Plush 60/80A temperature reading, that caused failure in operation above 4S
  135. ; Corrected temperature limit for HiModel cool 22/33/41A, RCTimer 6A, Skywalker 20/40A, Turnigy AE45A, Plush 40/60/80A. Limit was previously set too high
  136. ; - Rev9.4 Improved timing for increased maximum rpm limit
  137. ; - Rev10.0 Added closed loop mode for multi
  138. ; Added high/low BEC voltage option (for the ESCs where HW supports it)
  139. ; Added method of resetting all programmed parameter values to defaults by TX programming
  140. ; Added Turnigy K-force 40A and Turnigy K-force 120A HV ESCs
  141. ; Enabled fully damped mode for several ESCs
  142. ; Extended startup power range downwards to enable very smooth start for large heli main motors
  143. ; Extended damping force with a highest setting
  144. ; Corrected temperature limits for F310 chips (Plush 40A and AE 45A)
  145. ; Implemented temperature reading average in order to avoid problems with ADC noise on Skywalkers
  146. ; Increased switching delays for XP 7A fast, in order to avoid cross conduction of N and P fets
  147. ; Miscellaneous other changes
  148. ; - Rev10.1 Relaxed RC signal jitter requirement during frequency measurement
  149. ; Corrected bug that prevented using governor low
  150. ; Enabled vdd monitor always, in order to reduce likelihood of accidental overwriting of adjustments
  151. ; Fixed bug that caused stop for PPM input above 2048us, and moved upper accepted limit to 2160us
  152. ; - Rev10.2 Corrected temperature limit for AE20-30/XP7-25, where limit was too high
  153. ; Corrected temperature limit for 120HV, where limit was too low
  154. ; Fixed bug that caused AE20/25/30A not to run in reverse
  155. ; - Rev10.3 Removed vdd monitor for 1S capable ESCs, in order to avoid brownouts/resets
  156. ; Made auto bailout spoolup for main more smooth
  157. ; - Rev10.4 Ensured that main spoolup and governor activation will always be smooth, regardless of throttle input
  158. ; Added capability to operate on 12kHz input signal too
  159. ; - Rev11.0 Fixed bug of programming default values for governor in MULTI mode
  160. ; Disabled interrupts explicitly some places, to avoid possibilities for unintentional fet switching
  161. ; Changed interrupt disable strategy, to always allow pwm interrupts, to avoid noise when running at low rpms
  162. ; Added governor middle range for MAIN mode
  163. ; Added bidirectional mode for TAIL and MULTI mode with PPM input
  164. ; Changed and improved demag compensation
  165. ; Miscellaneous other changes
  166. ; - Rev11.1 Fixed bug of slow acceleration response for MAIN mode running without governor
  167. ; Fixed bug with PWM input, where throttle remains high even when zeroing throttle (seen on V922 tail)
  168. ; Fixed bug in bidirectional operation, where direction change could cause reset
  169. ; Improved autorotation bailout for MAIN
  170. ; Reduced min speed back to 1220 erpm
  171. ; Misc code cleanups
  172. ; - Rev11.2 Fixed throttle calibration bug
  173. ; Added high side driver precharge for all-nfet ESCs
  174. ; Optimized timing in general and for demag compensation in particular
  175. ; Auto bailout functionality modified
  176. ; Governor is deactivated for throttle inputs below 10%
  177. ; Increased beacon delay times
  178. ; - Rev12.0 Added programmable main spoolup time
  179. ; Added programmable temperature protection enable
  180. ; Bidirectional mode stop/start improved. Motor is now stopped before starting
  181. ; Power is limited for very low rpms (when BEMF is low), in order to avoid sync loss
  182. ; Damped light mode is made more smooth and quiet, particularly at low and high rpms
  183. ; Comparator signal qualification scheme is changed
  184. ; Demag compensation scheme is significantly changed
  185. ; Increased jitter tolerance for PPM frequency measurement
  186. ; Fully damped mode removed, and damped light only supported on damped capable ESCs
  187. ; Default tail mode changed to damped light
  188. ; Miscellaneous other changes
  189. ; - Rev12.1 Fixed bug in tail code
  190. ; Improved startup for Atmel
  191. ; Added support for multiple high BEC voltages
  192. ; Added support for RPM output
  193. ; - Rev12.2 Improved running smoothness, particularly for damped light
  194. ; Avoiding lockup at full throttle when input signal is noisy
  195. ; Avoiding detection of 1-wire programming signal as valid throttle signal
  196. ; - Rev13.0 Removed stepped start
  197. ; Removed throttle change rate and damping force parameters
  198. ; Added support for OneShot125
  199. ; Improved commutation timing accuracy
  200. ; - Rev13.1
  201. ;
  202. ;**** **** **** **** ****
  203. ; Up to 8K Bytes of In-System Self-Programmable Flash
  204. ; 768 Bytes Internal SRAM
  205. ;
  206. ;**** **** **** **** ****
  207. ; Master clock is internal 24MHz oscillator
  208. ; Timer 0 (167/500ns counts) always counts up and is used for
  209. ; - PWM generation
  210. ; Timer 1 (167/500ns counts) always counts up and is used for
  211. ; - Time from pwm on/off event
  212. ; Timer 2 (500ns counts) always counts up and is used for
  213. ; - RC pulse timeout/skip counts and commutation times
  214. ; Timer 3 (500ns counts) always counts up and is used for
  215. ; - Commutation timeouts
  216. ; PCA0 (500ns counts) always counts up and is used for
  217. ; - RC pulse measurement
  218. ;
  219. ;**** **** **** **** ****
  220. ; Interrupt handling
  221. ; The F330/2 does not disable interrupts when entering an interrupt routine.
  222. ; Also some interrupt flags need to be cleared by software
  223. ; The code disables interrupts in interrupt routines, in order to avoid too nested interrupts
  224. ; - Interrupts are disabled during beeps, to avoid audible interference from interrupts
  225. ; - RC pulse interrupts are periodically disabled in order to reduce interference with pwm interrupts.
  226. ;
  227. ;**** **** **** **** ****
  228. ; Motor control:
  229. ; - Brushless motor control with 6 states for each electrical 360 degrees
  230. ; - An advance timing of 0deg has zero cross 30deg after one commutation and 30deg before the next
  231. ; - Timing advance in this implementation is set to 15deg nominally
  232. ; - "Damped" commutation schemes are available, where more than one pfet is on when pwm is off. This will absorb energy from bemf and make step settling more damped.
  233. ; Motor sequence starting from zero crossing:
  234. ; - Timer wait: Wt_Comm 15deg ; Time to wait from zero cross to actual commutation
  235. ; - Timer wait: Wt_Advance 15deg ; Time to wait for timing advance. Nominal commutation point is after this
  236. ; - Timer wait: Wt_Zc_Scan 7.5deg ; Time to wait before looking for zero cross
  237. ; - Scan for zero cross 22.5deg , Nominal, with some motor variations
  238. ;
  239. ; Motor startup:
  240. ; Startup is the only phase, before normal bemf commutation run begins.
  241. ;
  242. ;**** **** **** **** ****
  243. ; List of enumerated supported ESCs and modes (main, tail or multi)
  244. XP_3A_Main EQU 1
  245. XP_3A_Tail EQU 2
  246. XP_3A_Multi EQU 3
  247. XP_7A_Main EQU 4
  248. XP_7A_Tail EQU 5
  249. XP_7A_Multi EQU 6
  250. XP_7A_Fast_Main EQU 7
  251. XP_7A_Fast_Tail EQU 8
  252. XP_7A_Fast_Multi EQU 9
  253. XP_12A_Main EQU 10
  254. XP_12A_Tail EQU 11
  255. XP_12A_Multi EQU 12
  256. XP_18A_Main EQU 13
  257. XP_18A_Tail EQU 14
  258. XP_18A_Multi EQU 15
  259. XP_25A_Main EQU 16
  260. XP_25A_Tail EQU 17
  261. XP_25A_Multi EQU 18
  262. XP_35A_SW_Main EQU 19
  263. XP_35A_SW_Tail EQU 20
  264. XP_35A_SW_Multi EQU 21
  265. DP_3A_Main EQU 22
  266. DP_3A_Tail EQU 23
  267. DP_3A_Multi EQU 24
  268. Supermicro_3p5A_Main EQU 25
  269. Supermicro_3p5A_Tail EQU 26
  270. Supermicro_3p5A_Multi EQU 27
  271. Turnigy_Plush_6A_Main EQU 28
  272. Turnigy_Plush_6A_Tail EQU 29
  273. Turnigy_Plush_6A_Multi EQU 30
  274. Turnigy_Plush_10A_Main EQU 31
  275. Turnigy_Plush_10A_Tail EQU 32
  276. Turnigy_Plush_10A_Multi EQU 33
  277. Turnigy_Plush_12A_Main EQU 34
  278. Turnigy_Plush_12A_Tail EQU 35
  279. Turnigy_Plush_12A_Multi EQU 36
  280. Turnigy_Plush_18A_Main EQU 37
  281. Turnigy_Plush_18A_Tail EQU 38
  282. Turnigy_Plush_18A_Multi EQU 39
  283. Turnigy_Plush_25A_Main EQU 40
  284. Turnigy_Plush_25A_Tail EQU 41
  285. Turnigy_Plush_25A_Multi EQU 42
  286. Turnigy_Plush_30A_Main EQU 43
  287. Turnigy_Plush_30A_Tail EQU 44
  288. Turnigy_Plush_30A_Multi EQU 45
  289. Turnigy_Plush_40A_Main EQU 46
  290. Turnigy_Plush_40A_Tail EQU 47
  291. Turnigy_Plush_40A_Multi EQU 48
  292. Turnigy_Plush_60A_Main EQU 49
  293. Turnigy_Plush_60A_Tail EQU 50
  294. Turnigy_Plush_60A_Multi EQU 51
  295. Turnigy_Plush_80A_Main EQU 52
  296. Turnigy_Plush_80A_Tail EQU 53
  297. Turnigy_Plush_80A_Multi EQU 54
  298. Turnigy_Plush_Nfet_18A_Main EQU 55
  299. Turnigy_Plush_Nfet_18A_Tail EQU 56
  300. Turnigy_Plush_Nfet_18A_Multi EQU 57
  301. Turnigy_Plush_Nfet_25A_Main EQU 58
  302. Turnigy_Plush_Nfet_25A_Tail EQU 59
  303. Turnigy_Plush_Nfet_25A_Multi EQU 60
  304. Turnigy_Plush_Nfet_30A_Main EQU 61
  305. Turnigy_Plush_Nfet_30A_Tail EQU 62
  306. Turnigy_Plush_Nfet_30A_Multi EQU 63
  307. Turnigy_AE_20A_Main EQU 64
  308. Turnigy_AE_20A_Tail EQU 65
  309. Turnigy_AE_20A_Multi EQU 66
  310. Turnigy_AE_25A_Main EQU 67
  311. Turnigy_AE_25A_Tail EQU 68
  312. Turnigy_AE_25A_Multi EQU 69
  313. Turnigy_AE_30A_Main EQU 70
  314. Turnigy_AE_30A_Tail EQU 71
  315. Turnigy_AE_30A_Multi EQU 72
  316. Turnigy_AE_45A_Main EQU 73
  317. Turnigy_AE_45A_Tail EQU 74
  318. Turnigy_AE_45A_Multi EQU 75
  319. Turnigy_KForce_40A_Main EQU 76
  320. Turnigy_KForce_40A_Tail EQU 77
  321. Turnigy_KForce_40A_Multi EQU 78
  322. Turnigy_KForce_70A_HV_Main EQU 79
  323. Turnigy_KForce_70A_HV_Tail EQU 80
  324. Turnigy_KForce_70A_HV_Multi EQU 81
  325. Turnigy_KForce_120A_HV_Main EQU 82
  326. Turnigy_KForce_120A_HV_Tail EQU 83
  327. Turnigy_KForce_120A_HV_Multi EQU 84
  328. Turnigy_KForce_120A_HV_v2_Main EQU 85
  329. Turnigy_KForce_120A_HV_v2_Tail EQU 86
  330. Turnigy_KForce_120A_HV_v2_Multi EQU 87
  331. Skywalker_20A_Main EQU 88
  332. Skywalker_20A_Tail EQU 89
  333. Skywalker_20A_Multi EQU 90
  334. Skywalker_40A_Main EQU 91
  335. Skywalker_40A_Tail EQU 92
  336. Skywalker_40A_Multi EQU 93
  337. HiModel_Cool_22A_Main EQU 94
  338. HiModel_Cool_22A_Tail EQU 95
  339. HiModel_Cool_22A_Multi EQU 96
  340. HiModel_Cool_33A_Main EQU 97
  341. HiModel_Cool_33A_Tail EQU 98
  342. HiModel_Cool_33A_Multi EQU 99
  343. HiModel_Cool_41A_Main EQU 100
  344. HiModel_Cool_41A_Tail EQU 101
  345. HiModel_Cool_41A_Multi EQU 102
  346. RCTimer_6A_Main EQU 103
  347. RCTimer_6A_Tail EQU 104
  348. RCTimer_6A_Multi EQU 105
  349. Align_RCE_BL15X_Main EQU 106
  350. Align_RCE_BL15X_Tail EQU 107
  351. Align_RCE_BL15X_Multi EQU 108
  352. Align_RCE_BL15P_Main EQU 109
  353. Align_RCE_BL15P_Tail EQU 110
  354. Align_RCE_BL15P_Multi EQU 111
  355. Align_RCE_BL35X_Main EQU 112
  356. Align_RCE_BL35X_Tail EQU 113
  357. Align_RCE_BL35X_Multi EQU 114
  358. Align_RCE_BL35P_Main EQU 115
  359. Align_RCE_BL35P_Tail EQU 116
  360. Align_RCE_BL35P_Multi EQU 117
  361. Gaui_GE_183_18A_Main EQU 118
  362. Gaui_GE_183_18A_Tail EQU 119
  363. Gaui_GE_183_18A_Multi EQU 120
  364. H_King_10A_Main EQU 121
  365. H_King_10A_Tail EQU 122
  366. H_King_10A_Multi EQU 123
  367. H_King_20A_Main EQU 124
  368. H_King_20A_Tail EQU 125
  369. H_King_20A_Multi EQU 126
  370. H_King_35A_Main EQU 127
  371. H_King_35A_Tail EQU 128
  372. H_King_35A_Multi EQU 129
  373. H_King_50A_Main EQU 130
  374. H_King_50A_Tail EQU 131
  375. H_King_50A_Multi EQU 132
  376. Polaris_Thunder_12A_Main EQU 133
  377. Polaris_Thunder_12A_Tail EQU 134
  378. Polaris_Thunder_12A_Multi EQU 135
  379. Polaris_Thunder_20A_Main EQU 136
  380. Polaris_Thunder_20A_Tail EQU 137
  381. Polaris_Thunder_20A_Multi EQU 138
  382. Polaris_Thunder_30A_Main EQU 139
  383. Polaris_Thunder_30A_Tail EQU 140
  384. Polaris_Thunder_30A_Multi EQU 141
  385. Polaris_Thunder_40A_Main EQU 142
  386. Polaris_Thunder_40A_Tail EQU 143
  387. Polaris_Thunder_40A_Multi EQU 144
  388. Polaris_Thunder_60A_Main EQU 145
  389. Polaris_Thunder_60A_Tail EQU 146
  390. Polaris_Thunder_60A_Multi EQU 147
  391. Polaris_Thunder_80A_Main EQU 148
  392. Polaris_Thunder_80A_Tail EQU 149
  393. Polaris_Thunder_80A_Multi EQU 150
  394. Polaris_Thunder_100A_Main EQU 151
  395. Polaris_Thunder_100A_Tail EQU 152
  396. Polaris_Thunder_100A_Multi EQU 153
  397. Platinum_Pro_30A_Main EQU 154
  398. Platinum_Pro_30A_Tail EQU 155
  399. Platinum_Pro_30A_Multi EQU 156
  400. Platinum_Pro_150A_Main EQU 157
  401. Platinum_Pro_150A_Tail EQU 158
  402. Platinum_Pro_150A_Multi EQU 159
  403. Platinum_50Av3_Main EQU 160
  404. Platinum_50Av3_Tail EQU 161
  405. Platinum_50Av3_Multi EQU 162
  406. EAZY_3Av2_Main EQU 163
  407. EAZY_3Av2_Tail EQU 164
  408. EAZY_3Av2_Multi EQU 165
  409. Tarot_30A_Main EQU 166
  410. Tarot_30A_Tail EQU 167
  411. Tarot_30A_Multi EQU 168
  412. SkyIII_30A_Main EQU 169
  413. SkyIII_30A_Tail EQU 170
  414. SkyIII_30A_Multi EQU 171
  415. EMAX_20A_Main EQU 172
  416. EMAX_20A_Tail EQU 173
  417. EMAX_20A_Multi EQU 174
  418. EMAX_40A_Main EQU 175
  419. EMAX_40A_Tail EQU 176
  420. EMAX_40A_Multi EQU 177
  421. ;**** **** **** **** ****
  422. ; Select the ESC and mode to use (or unselect all for use with external batch compile file)
  423. ;BESCNO EQU XP_3A_Main
  424. ;BESCNO EQU XP_3A_Tail
  425. ;BESCNO EQU XP_3A_Multi
  426. ;BESCNO EQU XP_7A_Main
  427. ;BESCNO EQU XP_7A_Tail
  428. ;BESCNO EQU XP_7A_Multi
  429. ;BESCNO EQU XP_7A_Fast_Main
  430. ;BESCNO EQU XP_7A_Fast_Tail
  431. ;BESCNO EQU XP_7A_Fast_Multi
  432. ;BESCNO EQU XP_12A_Main
  433. ;BESCNO EQU XP_12A_Tail
  434. ;BESCNO EQU XP_12A_Multi
  435. ;BESCNO EQU XP_18A_Main
  436. ;BESCNO EQU XP_18A_Tail
  437. ;BESCNO EQU XP_18A_Multi
  438. ;BESCNO EQU XP_25A_Main
  439. ;BESCNO EQU XP_25A_Tail
  440. ;BESCNO EQU XP_25A_Multi
  441. ;BESCNO EQU XP_35A_SW_Main
  442. ;BESCNO EQU XP_35A_SW_Tail
  443. ;BESCNO EQU XP_35A_SW_Multi
  444. ;BESCNO EQU DP_3A_Main
  445. ;BESCNO EQU DP_3A_Tail
  446. ;BESCNO EQU DP_3A_Multi
  447. ;BESCNO EQU Supermicro_3p5A_Main
  448. ;BESCNO EQU Supermicro_3p5A_Tail
  449. ;BESCNO EQU Supermicro_3p5A_Multi
  450. ;BESCNO EQU Turnigy_Plush_6A_Main
  451. ;BESCNO EQU Turnigy_Plush_6A_Tail
  452. ;BESCNO EQU Turnigy_Plush_6A_Multi
  453. ;BESCNO EQU Turnigy_Plush_10A_Main
  454. ;BESCNO EQU Turnigy_Plush_10A_Tail
  455. ;BESCNO EQU Turnigy_Plush_10A_Multi
  456. ;BESCNO EQU Turnigy_Plush_12A_Main
  457. ;BESCNO EQU Turnigy_Plush_12A_Tail
  458. ;BESCNO EQU Turnigy_Plush_12A_Multi
  459. ;BESCNO EQU Turnigy_Plush_18A_Main
  460. ;BESCNO EQU Turnigy_Plush_18A_Tail
  461. ;BESCNO EQU Turnigy_Plush_18A_Multi
  462. ;BESCNO EQU Turnigy_Plush_25A_Main
  463. ;BESCNO EQU Turnigy_Plush_25A_Tail
  464. ;BESCNO EQU Turnigy_Plush_25A_Multi
  465. ;BESCNO EQU Turnigy_Plush_30A_Main
  466. ;BESCNO EQU Turnigy_Plush_30A_Tail
  467. ;BESCNO EQU Turnigy_Plush_30A_Multi
  468. ;BESCNO EQU Turnigy_Plush_40A_Main
  469. ;BESCNO EQU Turnigy_Plush_40A_Tail
  470. ;BESCNO EQU Turnigy_Plush_40A_Multi
  471. ;BESCNO EQU Turnigy_Plush_60A_Main
  472. ;BESCNO EQU Turnigy_Plush_60A_Tail
  473. ;BESCNO EQU Turnigy_Plush_60A_Multi
  474. ;BESCNO EQU Turnigy_Plush_80A_Main
  475. ;BESCNO EQU Turnigy_Plush_80A_Tail
  476. ;BESCNO EQU Turnigy_Plush_80A_Multi
  477. ;BESCNO EQU Turnigy_Plush_Nfet_18A_Main
  478. ;BESCNO EQU Turnigy_Plush_Nfet_18A_Tail
  479. ;BESCNO EQU Turnigy_Plush_Nfet_18A_Multi
  480. ;BESCNO EQU Turnigy_Plush_Nfet_25A_Main
  481. ;BESCNO EQU Turnigy_Plush_Nfet_25A_Tail
  482. ;BESCNO EQU Turnigy_Plush_Nfet_25A_Multi
  483. ;BESCNO EQU Turnigy_Plush_Nfet_30A_Main
  484. ;BESCNO EQU Turnigy_Plush_Nfet_30A_Tail
  485. ;BESCNO EQU Turnigy_Plush_Nfet_30A_Multi
  486. ;BESCNO EQU Turnigy_AE_20A_Main
  487. ;BESCNO EQU Turnigy_AE_20A_Tail
  488. ;BESCNO EQU Turnigy_AE_20A_Multi
  489. ;BESCNO EQU Turnigy_AE_25A_Main
  490. ;BESCNO EQU Turnigy_AE_25A_Tail
  491. ;BESCNO EQU Turnigy_AE_25A_Multi
  492. ;BESCNO EQU Turnigy_AE_30A_Main
  493. ;BESCNO EQU Turnigy_AE_30A_Tail
  494. ;BESCNO EQU Turnigy_AE_30A_Multi
  495. ;BESCNO EQU Turnigy_AE_45A_Main
  496. ;BESCNO EQU Turnigy_AE_45A_Tail
  497. ;BESCNO EQU Turnigy_AE_45A_Multi
  498. ;BESCNO EQU Turnigy_KForce_40A_Main
  499. ;BESCNO EQU Turnigy_KForce_40A_Tail
  500. ;BESCNO EQU Turnigy_KForce_40A_Multi
  501. ;BESCNO EQU Turnigy_KForce_70A_HV_Main
  502. ;BESCNO EQU Turnigy_KForce_70A_HV_Tail
  503. ;BESCNO EQU Turnigy_KForce_70A_HV_Multi
  504. ;BESCNO EQU Turnigy_KForce_120A_HV_Main
  505. ;BESCNO EQU Turnigy_KForce_120A_HV_Tail
  506. ;BESCNO EQU Turnigy_KForce_120A_HV_Multi
  507. ;BESCNO EQU Turnigy_KForce_120A_HV_v2_Main
  508. ;BESCNO EQU Turnigy_KForce_120A_HV_v2_Tail
  509. ;BESCNO EQU Turnigy_KForce_120A_HV_v2_Multi
  510. ;BESCNO EQU Skywalker_20A_Main
  511. ;BESCNO EQU Skywalker_20A_Tail
  512. ;BESCNO EQU Skywalker_20A_Multi
  513. ;BESCNO EQU Skywalker_40A_Main
  514. ;BESCNO EQU Skywalker_40A_Tail
  515. ;BESCNO EQU Skywalker_40A_Multi
  516. ;BESCNO EQU HiModel_Cool_22A_Main
  517. ;BESCNO EQU HiModel_Cool_22A_Tail
  518. ;BESCNO EQU HiModel_Cool_22A_Multi
  519. ;BESCNO EQU HiModel_Cool_33A_Main
  520. ;BESCNO EQU HiModel_Cool_33A_Tail
  521. ;BESCNO EQU HiModel_Cool_33A_Multi
  522. ;BESCNO EQU HiModel_Cool_41A_Main
  523. ;BESCNO EQU HiModel_Cool_41A_Tail
  524. ;BESCNO EQU HiModel_Cool_41A_Multi
  525. ;BESCNO EQU RCTimer_6A_Main
  526. ;BESCNO EQU RCTimer_6A_Tail
  527. ;BESCNO EQU RCTimer_6A_Multi
  528. ;BESCNO EQU Align_RCE_BL15X_Main
  529. ;BESCNO EQU Align_RCE_BL15X_Tail
  530. ;BESCNO EQU Align_RCE_BL15X_Multi
  531. ;BESCNO EQU Align_RCE_BL15P_Main
  532. ;BESCNO EQU Align_RCE_BL15P_Tail
  533. ;BESCNO EQU Align_RCE_BL15P_Multi
  534. ;BESCNO EQU Align_RCE_BL35X_Main
  535. ;BESCNO EQU Align_RCE_BL35X_Tail
  536. ;BESCNO EQU Align_RCE_BL35X_Multi
  537. ;BESCNO EQU Align_RCE_BL35P_Main
  538. ;BESCNO EQU Align_RCE_BL35P_Tail
  539. ;BESCNO EQU Align_RCE_BL35P_Multi
  540. ;BESCNO EQU Gaui_GE_183_18A_Main
  541. ;BESCNO EQU Gaui_GE_183_18A_Tail
  542. ;BESCNO EQU Gaui_GE_183_18A_Multi
  543. ;BESCNO EQU H_King_10A_Main
  544. ;BESCNO EQU H_King_10A_Tail
  545. ;BESCNO EQU H_King_10A_Multi
  546. ;BESCNO EQU H_King_20A_Main
  547. ;BESCNO EQU H_King_20A_Tail
  548. ;BESCNO EQU H_King_20A_Multi
  549. ;BESCNO EQU H_King_35A_Main
  550. ;BESCNO EQU H_King_35A_Tail
  551. ;BESCNO EQU H_King_35A_Multi
  552. ;BESCNO EQU H_King_50A_Main
  553. ;BESCNO EQU H_King_50A_Tail
  554. ;BESCNO EQU H_King_50A_Multi
  555. ;BESCNO EQU Polaris_Thunder_12A_Main
  556. ;BESCNO EQU Polaris_Thunder_12A_Tail
  557. ;BESCNO EQU Polaris_Thunder_12A_Multi
  558. ;BESCNO EQU Polaris_Thunder_20A_Main
  559. ;BESCNO EQU Polaris_Thunder_20A_Tail
  560. ;BESCNO EQU Polaris_Thunder_20A_Multi
  561. ;BESCNO EQU Polaris_Thunder_30A_Main
  562. ;BESCNO EQU Polaris_Thunder_30A_Tail
  563. ;BESCNO EQU Polaris_Thunder_30A_Multi
  564. ;BESCNO EQU Polaris_Thunder_40A_Main
  565. ;BESCNO EQU Polaris_Thunder_40A_Tail
  566. ;BESCNO EQU Polaris_Thunder_40A_Multi
  567. ;BESCNO EQU Polaris_Thunder_60A_Main
  568. ;BESCNO EQU Polaris_Thunder_60A_Tail
  569. ;BESCNO EQU Polaris_Thunder_60A_Multi
  570. ;BESCNO EQU Polaris_Thunder_80A_Main
  571. ;BESCNO EQU Polaris_Thunder_80A_Tail
  572. ;BESCNO EQU Polaris_Thunder_80A_Multi
  573. ;BESCNO EQU Polaris_Thunder_100A_Main
  574. ;BESCNO EQU Polaris_Thunder_100A_Tail
  575. ;BESCNO EQU Polaris_Thunder_100A_Multi
  576. ;BESCNO EQU Platinum_Pro_30A_Main
  577. ;BESCNO EQU Platinum_Pro_30A_Tail
  578. ;BESCNO EQU Platinum_Pro_30A_Multi
  579. ;BESCNO EQU Platinum_Pro_150A_Main
  580. ;BESCNO EQU Platinum_Pro_150A_Tail
  581. ;BESCNO EQU Platinum_Pro_150A_Multi
  582. ;BESCNO EQU Platinum_50Av3_Main
  583. ;BESCNO EQU Platinum_50Av3_Tail
  584. ;BESCNO EQU Platinum_50Av3_Multi
  585. ;BESCNO EQU EAZY_3Av2_Main
  586. ;BESCNO EQU EAZY_3Av2_Tail
  587. ;BESCNO EQU EAZY_3Av2_Multi
  588. ;BESCNO EQU Tarot_30A_Main
  589. ;BESCNO EQU Tarot_30A_Tail
  590. ;BESCNO EQU Tarot_30A_Multi
  591. ;BESCNO EQU SkyIII_30A_Main
  592. ;BESCNO EQU SkyIII_30A_Tail
  593. ;BESCNO EQU SkyIII_30A_Multi
  594. ;BESCNO EQU EMAX_20A_Main
  595. ;BESCNO EQU EMAX_20A_Tail
  596. ;BESCNO EQU EMAX_20A_Multi
  597. ;BESCNO EQU EMAX_40A_Main
  598. ;BESCNO EQU EMAX_40A_Tail
  599. ;BESCNO EQU EMAX_40A_Multi
  600. ;**** **** **** **** ****
  601. ; ESC selection statements
  602. IF BESCNO == XP_3A_Main
  603. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  604. $include (XP_3A.inc) ; Select XP 3A pinout
  605. ENDIF
  606. IF BESCNO == XP_3A_Tail
  607. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  608. $include (XP_3A.inc) ; Select XP 3A pinout
  609. ENDIF
  610. IF BESCNO == XP_3A_Multi
  611. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  612. $include (XP_3A.inc) ; Select XP 3A pinout
  613. ENDIF
  614. IF BESCNO == XP_7A_Main
  615. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  616. $include (XP_7A.inc) ; Select XP 7A pinout
  617. ENDIF
  618. IF BESCNO == XP_7A_Tail
  619. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  620. $include (XP_7A.inc) ; Select XP 7A pinout
  621. ENDIF
  622. IF BESCNO == XP_7A_Multi
  623. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  624. $include (XP_7A.inc) ; Select XP 7A pinout
  625. ENDIF
  626. IF BESCNO == XP_7A_Fast_Main
  627. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  628. $include (XP_7A_Fast.inc) ; Select XP 7A Fast pinout
  629. ENDIF
  630. IF BESCNO == XP_7A_Fast_Tail
  631. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  632. $include (XP_7A_Fast.inc) ; Select XP 7A Fast pinout
  633. ENDIF
  634. IF BESCNO == XP_7A_Fast_Multi
  635. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  636. $include (XP_7A_Fast.inc) ; Select XP 7A Fast pinout
  637. ENDIF
  638. IF BESCNO == XP_12A_Main
  639. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  640. $include (XP_12A.inc) ; Select XP 12A pinout
  641. ENDIF
  642. IF BESCNO == XP_12A_Tail
  643. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  644. $include (XP_12A.inc) ; Select XP 12A pinout
  645. ENDIF
  646. IF BESCNO == XP_12A_Multi
  647. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  648. $include (XP_12A.inc) ; Select XP 12A pinout
  649. ENDIF
  650. IF BESCNO == XP_18A_Main
  651. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  652. $include (XP_18A.inc) ; Select XP 18A pinout
  653. ENDIF
  654. IF BESCNO == XP_18A_Tail
  655. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  656. $include (XP_18A.inc) ; Select XP 18A pinout
  657. ENDIF
  658. IF BESCNO == XP_18A_Multi
  659. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  660. $include (XP_18A.inc) ; Select XP 18A pinout
  661. ENDIF
  662. IF BESCNO == XP_25A_Main
  663. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  664. $include (XP_25A.inc) ; Select XP 25A pinout
  665. ENDIF
  666. IF BESCNO == XP_25A_Tail
  667. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  668. $include (XP_25A.inc) ; Select XP 25A pinout
  669. ENDIF
  670. IF BESCNO == XP_25A_Multi
  671. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  672. $include (XP_25A.inc) ; Select XP 25A pinout
  673. ENDIF
  674. IF BESCNO == XP_35A_SW_Main
  675. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  676. $include (XP_35A_SW.inc) ; Select XP 35A SW pinout
  677. ENDIF
  678. IF BESCNO == XP_35A_SW_Tail
  679. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  680. $include (XP_35A_SW.inc) ; Select XP 35A SW pinout
  681. ENDIF
  682. IF BESCNO == XP_35A_SW_Multi
  683. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  684. $include (XP_35A_SW.inc) ; Select XP 35A SW pinout
  685. ENDIF
  686. IF BESCNO == DP_3A_Main
  687. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  688. $include (DP_3A.inc) ; Select DP 3A pinout
  689. ENDIF
  690. IF BESCNO == DP_3A_Tail
  691. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  692. $include (DP_3A.inc) ; Select DP 3A pinout
  693. ENDIF
  694. IF BESCNO == DP_3A_Multi
  695. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  696. $include (DP_3A.inc) ; Select DP 3A pinout
  697. ENDIF
  698. IF BESCNO == Supermicro_3p5A_Main
  699. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  700. $include (Supermicro_3p5A.inc) ; Select Supermicro 3.5A pinout
  701. ENDIF
  702. IF BESCNO == Supermicro_3p5A_Tail
  703. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  704. $include (Supermicro_3p5A.inc) ; Select Supermicro 3.5A pinout
  705. ENDIF
  706. IF BESCNO == Supermicro_3p5A_Multi
  707. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  708. $include (Supermicro_3p5A.inc) ; Select Supermicro 3.5A pinout
  709. ENDIF
  710. IF BESCNO == Turnigy_Plush_6A_Main
  711. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  712. $include (Turnigy_Plush_6A.inc) ; Select Turnigy Plush 6A pinout
  713. ENDIF
  714. IF BESCNO == Turnigy_Plush_6A_Tail
  715. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  716. $include (Turnigy_Plush_6A.inc) ; Select Turnigy Plush 6A pinout
  717. ENDIF
  718. IF BESCNO == Turnigy_Plush_6A_Multi
  719. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  720. $include (Turnigy_Plush_6A.inc) ; Select Turnigy Plush 6A pinout
  721. ENDIF
  722. IF BESCNO == Turnigy_Plush_10A_Main
  723. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  724. $include (Turnigy_Plush_10A.inc) ; Select Turnigy Plush 10A pinout
  725. ENDIF
  726. IF BESCNO == Turnigy_Plush_10A_Tail
  727. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  728. $include (Turnigy_Plush_10A.inc) ; Select Turnigy Plush 10A pinout
  729. ENDIF
  730. IF BESCNO == Turnigy_Plush_10A_Multi
  731. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  732. $include (Turnigy_Plush_10A.inc) ; Select Turnigy Plush 10A pinout
  733. ENDIF
  734. IF BESCNO == Turnigy_Plush_12A_Main
  735. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  736. $include (Turnigy_Plush_12A.inc) ; Select Turnigy Plush 12A pinout
  737. ENDIF
  738. IF BESCNO == Turnigy_Plush_12A_Tail
  739. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  740. $include (Turnigy_Plush_12A.inc) ; Select Turnigy Plush 12A pinout
  741. ENDIF
  742. IF BESCNO == Turnigy_Plush_12A_Multi
  743. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  744. $include (Turnigy_Plush_12A.inc) ; Select Turnigy Plush 12A pinout
  745. ENDIF
  746. IF BESCNO == Turnigy_Plush_18A_Main
  747. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  748. $include (Turnigy_Plush_18A.inc) ; Select Turnigy Plush 18A pinout
  749. ENDIF
  750. IF BESCNO == Turnigy_Plush_18A_Tail
  751. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  752. $include (Turnigy_Plush_18A.inc) ; Select Turnigy Plush 18A pinout
  753. ENDIF
  754. IF BESCNO == Turnigy_Plush_18A_Multi
  755. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  756. $include (Turnigy_Plush_18A.inc) ; Select Turnigy Plush 18A pinout
  757. ENDIF
  758. IF BESCNO == Turnigy_Plush_25A_Main
  759. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  760. $include (Turnigy_Plush_25A.inc) ; Select Turnigy Plush 25A pinout
  761. ENDIF
  762. IF BESCNO == Turnigy_Plush_25A_Tail
  763. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  764. $include (Turnigy_Plush_25A.inc) ; Select Turnigy Plush 25A pinout
  765. ENDIF
  766. IF BESCNO == Turnigy_Plush_25A_Multi
  767. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  768. $include (Turnigy_Plush_25A.inc) ; Select Turnigy Plush 25A pinout
  769. ENDIF
  770. IF BESCNO == Turnigy_Plush_30A_Main
  771. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  772. $include (Turnigy_Plush_30A.inc) ; Select Turnigy Plush 30A pinout
  773. ENDIF
  774. IF BESCNO == Turnigy_Plush_30A_Tail
  775. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  776. $include (Turnigy_Plush_30A.inc) ; Select Turnigy Plush 30A pinout
  777. ENDIF
  778. IF BESCNO == Turnigy_Plush_30A_Multi
  779. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  780. $include (Turnigy_Plush_30A.inc) ; Select Turnigy Plush 30A pinout
  781. ENDIF
  782. IF BESCNO == Turnigy_Plush_40A_Main
  783. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  784. $include (Turnigy_Plush_40A.inc) ; Select Turnigy Plush 40A pinout
  785. ENDIF
  786. IF BESCNO == Turnigy_Plush_40A_Tail
  787. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  788. $include (Turnigy_Plush_40A.inc) ; Select Turnigy Plush 40A pinout
  789. ENDIF
  790. IF BESCNO == Turnigy_Plush_40A_Multi
  791. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  792. $include (Turnigy_Plush_40A.inc) ; Select Turnigy Plush 40A pinout
  793. ENDIF
  794. IF BESCNO == Turnigy_Plush_60A_Main
  795. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  796. $include (Turnigy_Plush_60A.inc) ; Select Turnigy Plush 60A pinout
  797. ENDIF
  798. IF BESCNO == Turnigy_Plush_60A_Tail
  799. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  800. $include (Turnigy_Plush_60A.inc) ; Select Turnigy Plush 60A pinout
  801. ENDIF
  802. IF BESCNO == Turnigy_Plush_60A_Multi
  803. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  804. $include (Turnigy_Plush_60A.inc) ; Select Turnigy Plush 60A pinout
  805. ENDIF
  806. IF BESCNO == Turnigy_Plush_80A_Main
  807. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  808. $include (Turnigy_Plush_80A.inc) ; Select Turnigy Plush 80A pinout
  809. ENDIF
  810. IF BESCNO == Turnigy_Plush_80A_Tail
  811. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  812. $include (Turnigy_Plush_80A.inc) ; Select Turnigy Plush 80A pinout
  813. ENDIF
  814. IF BESCNO == Turnigy_Plush_80A_Multi
  815. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  816. $include (Turnigy_Plush_80A.inc) ; Select Turnigy Plush 80A pinout
  817. ENDIF
  818. IF BESCNO == Turnigy_Plush_Nfet_18A_Main
  819. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  820. $include (Turnigy_Plush_Nfet_18A.inc) ; Select Turnigy Plush Nfet 18A pinout
  821. ENDIF
  822. IF BESCNO == Turnigy_Plush_Nfet_18A_Tail
  823. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  824. $include (Turnigy_Plush_Nfet_18A.inc) ; Select Turnigy Plush Nfet 18A pinout
  825. ENDIF
  826. IF BESCNO == Turnigy_Plush_Nfet_18A_Multi
  827. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  828. $include (Turnigy_Plush_Nfet_18A.inc) ; Select Turnigy Plush Nfet 18A pinout
  829. ENDIF
  830. IF BESCNO == Turnigy_Plush_Nfet_25A_Main
  831. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  832. $include (Turnigy_Plush_Nfet_25A.inc) ; Select Turnigy Plush Nfet 25A pinout
  833. ENDIF
  834. IF BESCNO == Turnigy_Plush_Nfet_25A_Tail
  835. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  836. $include (Turnigy_Plush_Nfet_25A.inc) ; Select Turnigy Plush Nfet 25A pinout
  837. ENDIF
  838. IF BESCNO == Turnigy_Plush_Nfet_25A_Multi
  839. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  840. $include (Turnigy_Plush_Nfet_25A.inc) ; Select Turnigy Plush Nfet 25A pinout
  841. ENDIF
  842. IF BESCNO == Turnigy_Plush_Nfet_30A_Main
  843. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  844. $include (Turnigy_Plush_Nfet_30A.inc) ; Select Turnigy Plush Nfet 30A pinout
  845. ENDIF
  846. IF BESCNO == Turnigy_Plush_Nfet_30A_Tail
  847. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  848. $include (Turnigy_Plush_Nfet_30A.inc) ; Select Turnigy Plush Nfet 30A pinout
  849. ENDIF
  850. IF BESCNO == Turnigy_Plush_Nfet_30A_Multi
  851. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  852. $include (Turnigy_Plush_Nfet_30A.inc) ; Select Turnigy Plush Nfet 30A pinout
  853. ENDIF
  854. IF BESCNO == Turnigy_AE_20A_Main
  855. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  856. $include (Turnigy_AE_20A.inc) ; Select Turnigy AE-20A pinout
  857. ENDIF
  858. IF BESCNO == Turnigy_AE_20A_Tail
  859. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  860. $include (Turnigy_AE_20A.inc) ; Select Turnigy AE-20A pinout
  861. ENDIF
  862. IF BESCNO == Turnigy_AE_20A_Multi
  863. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  864. $include (Turnigy_AE_20A.inc) ; Select Turnigy AE-20A pinout
  865. ENDIF
  866. IF BESCNO == Turnigy_AE_25A_Main
  867. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  868. $include (Turnigy_AE_25A.inc) ; Select Turnigy AE-25A pinout
  869. ENDIF
  870. IF BESCNO == Turnigy_AE_25A_Tail
  871. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  872. $include (Turnigy_AE_25A.inc) ; Select Turnigy AE-25A pinout
  873. ENDIF
  874. IF BESCNO == Turnigy_AE_25A_Multi
  875. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  876. $include (Turnigy_AE_25A.inc) ; Select Turnigy AE-25A pinout
  877. ENDIF
  878. IF BESCNO == Turnigy_AE_30A_Main
  879. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  880. $include (Turnigy_AE_30A.inc) ; Select Turnigy AE-30A pinout
  881. ENDIF
  882. IF BESCNO == Turnigy_AE_30A_Tail
  883. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  884. $include (Turnigy_AE_30A.inc) ; Select Turnigy AE-30A pinout
  885. ENDIF
  886. IF BESCNO == Turnigy_AE_30A_Multi
  887. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  888. $include (Turnigy_AE_30A.inc) ; Select Turnigy AE-30A pinout
  889. ENDIF
  890. IF BESCNO == Turnigy_AE_45A_Main
  891. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  892. $include (Turnigy_AE_45A.inc) ; Select Turnigy AE-45A pinout
  893. ENDIF
  894. IF BESCNO == Turnigy_AE_45A_Tail
  895. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  896. $include (Turnigy_AE_45A.inc) ; Select Turnigy AE-45A pinout
  897. ENDIF
  898. IF BESCNO == Turnigy_AE_45A_Multi
  899. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  900. $include (Turnigy_AE_45A.inc) ; Select Turnigy AE-45A pinout
  901. ENDIF
  902. IF BESCNO == Turnigy_KForce_40A_Main
  903. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  904. $include (Turnigy_KForce_40A.inc) ; Select Turnigy KForce 40A pinout
  905. ENDIF
  906. IF BESCNO == Turnigy_KForce_40A_Tail
  907. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  908. $include (Turnigy_KForce_40A.inc) ; Select Turnigy KForce 40A pinout
  909. ENDIF
  910. IF BESCNO == Turnigy_KForce_40A_Multi
  911. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  912. $include (Turnigy_KForce_40A.inc) ; Select Turnigy KForce 40A pinout
  913. ENDIF
  914. IF BESCNO == Turnigy_KForce_70A_HV_Main
  915. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  916. $include (Turnigy_KForce_70A_HV.inc) ; Select Turnigy KForce 70A HV pinout
  917. ENDIF
  918. IF BESCNO == Turnigy_KForce_70A_HV_Tail
  919. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  920. $include (Turnigy_KForce_70A_HV.inc) ; Select Turnigy KForce 70A HV pinout
  921. ENDIF
  922. IF BESCNO == Turnigy_KForce_70A_HV_Multi
  923. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  924. $include (Turnigy_KForce_70A_HV.inc) ; Select Turnigy KForce 70A HV pinout
  925. ENDIF
  926. IF BESCNO == Turnigy_KForce_120A_HV_Main
  927. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  928. $include (Turnigy_KForce_120A_HV.inc) ; Select Turnigy KForce 120A HV pinout
  929. ENDIF
  930. IF BESCNO == Turnigy_KForce_120A_HV_Tail
  931. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  932. $include (Turnigy_KForce_120A_HV.inc) ; Select Turnigy KForce 120A HV pinout
  933. ENDIF
  934. IF BESCNO == Turnigy_KForce_120A_HV_Multi
  935. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  936. $include (Turnigy_KForce_120A_HV.inc) ; Select Turnigy KForce 120A HV pinout
  937. ENDIF
  938. IF BESCNO == Turnigy_KForce_120A_HV_v2_Main
  939. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  940. $include (Turnigy_KForce_120A_HV_v2.inc); Select Turnigy KForce 120A HV v2 pinout
  941. ENDIF
  942. IF BESCNO == Turnigy_KForce_120A_HV_v2_Tail
  943. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  944. $include (Turnigy_KForce_120A_HV_v2.inc); Select Turnigy KForce 120A HV v2 pinout
  945. ENDIF
  946. IF BESCNO == Turnigy_KForce_120A_HV_v2_Multi
  947. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  948. $include (Turnigy_KForce_120A_HV_v2.inc); Select Turnigy KForce 120A HV v2 pinout
  949. ENDIF
  950. IF BESCNO == Skywalker_20A_Main
  951. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  952. $include (Skywalker_20A.inc) ; Select Skywalker 20A pinout
  953. ENDIF
  954. IF BESCNO == Skywalker_20A_Tail
  955. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  956. $include (Skywalker_20A.inc) ; Select Skywalker 20A pinout
  957. ENDIF
  958. IF BESCNO == Skywalker_20A_Multi
  959. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  960. $include (Skywalker_20A.inc) ; Select Skywalker 20A pinout
  961. ENDIF
  962. IF BESCNO == Skywalker_40A_Main
  963. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  964. $include (Skywalker_40A.inc) ; Select Skywalker 40A pinout
  965. ENDIF
  966. IF BESCNO == Skywalker_40A_Tail
  967. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  968. $include (Skywalker_40A.inc) ; Select Skywalker 40A pinout
  969. ENDIF
  970. IF BESCNO == Skywalker_40A_Multi
  971. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  972. $include (Skywalker_40A.inc) ; Select Skywalker 40A pinout
  973. ENDIF
  974. IF BESCNO == HiModel_Cool_22A_Main
  975. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  976. $include (HiModel_Cool_22A.inc) ; Select HiModel Cool 22A pinout
  977. ENDIF
  978. IF BESCNO == HiModel_Cool_22A_Tail
  979. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  980. $include (HiModel_Cool_22A.inc) ; Select HiModel Cool 22A pinout
  981. ENDIF
  982. IF BESCNO == HiModel_Cool_22A_Multi
  983. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  984. $include (HiModel_Cool_22A.inc) ; Select HiModel Cool 22A pinout
  985. ENDIF
  986. IF BESCNO == HiModel_Cool_33A_Main
  987. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  988. $include (HiModel_Cool_33A.inc) ; Select HiModel Cool 33A pinout
  989. ENDIF
  990. IF BESCNO == HiModel_Cool_33A_Tail
  991. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  992. $include (HiModel_Cool_33A.inc) ; Select HiModel Cool 33A pinout
  993. ENDIF
  994. IF BESCNO == HiModel_Cool_33A_Multi
  995. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  996. $include (HiModel_Cool_33A.inc) ; Select HiModel Cool 33A pinout
  997. ENDIF
  998. IF BESCNO == HiModel_Cool_41A_Main
  999. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1000. $include (HiModel_Cool_41A.inc) ; Select HiModel Cool 41A pinout
  1001. ENDIF
  1002. IF BESCNO == HiModel_Cool_41A_Tail
  1003. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1004. $include (HiModel_Cool_41A.inc) ; Select HiModel Cool 41A pinout
  1005. ENDIF
  1006. IF BESCNO == HiModel_Cool_41A_Multi
  1007. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1008. $include (HiModel_Cool_41A.inc) ; Select HiModel Cool 41A pinout
  1009. ENDIF
  1010. IF BESCNO == RCTimer_6A_Main
  1011. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1012. $include (RCTimer_6A.inc) ; Select RC Timer 6A pinout
  1013. ENDIF
  1014. IF BESCNO == RCTimer_6A_Tail
  1015. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1016. $include (RCTimer_6A.inc) ; Select RC Timer 6A pinout
  1017. ENDIF
  1018. IF BESCNO == RCTimer_6A_Multi
  1019. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1020. $include (RCTimer_6A.inc) ; Select RC Timer 6A pinout
  1021. ENDIF
  1022. IF BESCNO == Align_RCE_BL15X_Main
  1023. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1024. $include (Align_RCE_BL15X.inc) ; Select Align RCE-BL15X pinout
  1025. ENDIF
  1026. IF BESCNO == Align_RCE_BL15X_Tail
  1027. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1028. $include (Align_RCE_BL15X.inc) ; Select Align RCE-BL15X pinout
  1029. ENDIF
  1030. IF BESCNO == Align_RCE_BL15X_Multi
  1031. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1032. $include (Align_RCE_BL15X.inc) ; Select Align RCE-BL15X pinout
  1033. ENDIF
  1034. IF BESCNO == Align_RCE_BL15P_Main
  1035. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1036. $include (Align_RCE_BL15P.inc) ; Select Align RCE-BL15P pinout
  1037. ENDIF
  1038. IF BESCNO == Align_RCE_BL15P_Tail
  1039. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1040. $include (Align_RCE_BL15P.inc) ; Select Align RCE-BL15P pinout
  1041. ENDIF
  1042. IF BESCNO == Align_RCE_BL15P_Multi
  1043. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1044. $include (Align_RCE_BL15P.inc) ; Select Align RCE-BL15P pinout
  1045. ENDIF
  1046. IF BESCNO == Align_RCE_BL35X_Main
  1047. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1048. $include (Align_RCE_BL35X.inc) ; Select Align RCE-BL35X pinout
  1049. ENDIF
  1050. IF BESCNO == Align_RCE_BL35X_Tail
  1051. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1052. $include (Align_RCE_BL35X.inc) ; Select Align RCE-BL35X pinout
  1053. ENDIF
  1054. IF BESCNO == Align_RCE_BL35X_Multi
  1055. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1056. $include (Align_RCE_BL35X.inc) ; Select Align RCE-BL35X pinout
  1057. ENDIF
  1058. IF BESCNO == Align_RCE_BL35P_Main
  1059. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1060. $include (Align_RCE_BL35P.inc) ; Select Align RCE-BL35P pinout
  1061. ENDIF
  1062. IF BESCNO == Align_RCE_BL35P_Tail
  1063. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1064. $include (Align_RCE_BL35P.inc) ; Select Align RCE-BL35P pinout
  1065. ENDIF
  1066. IF BESCNO == Align_RCE_BL35P_Multi
  1067. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1068. $include (Align_RCE_BL35P.inc) ; Select Align RCE-BL35P pinout
  1069. ENDIF
  1070. IF BESCNO == Gaui_GE_183_18A_Main
  1071. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1072. $include (Gaui_GE_183_18A.inc) ; Select Gaui GE-183 18A pinout
  1073. ENDIF
  1074. IF BESCNO == Gaui_GE_183_18A_Tail
  1075. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1076. $include (Gaui_GE_183_18A.inc) ; Select Gaui GE-183 18A pinout
  1077. ENDIF
  1078. IF BESCNO == Gaui_GE_183_18A_Multi
  1079. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1080. $include (Gaui_GE_183_18A.inc) ; Select Gaui GE-183 18A pinout
  1081. ENDIF
  1082. IF BESCNO == H_King_10A_Main
  1083. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1084. $include (H_King_10A.inc) ; Select H-King 10A pinout
  1085. ENDIF
  1086. IF BESCNO == H_King_10A_Tail
  1087. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1088. $include (H_King_10A.inc) ; Select H-King 10A pinout
  1089. ENDIF
  1090. IF BESCNO == H_King_10A_Multi
  1091. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1092. $include (H_King_10A.inc) ; Select H-King 10A pinout
  1093. ENDIF
  1094. IF BESCNO == H_King_20A_Main
  1095. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1096. $include (H_King_20A.inc) ; Select H-King 20A pinout
  1097. ENDIF
  1098. IF BESCNO == H_King_20A_Tail
  1099. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1100. $include (H_King_20A.inc) ; Select H-King 20A pinout
  1101. ENDIF
  1102. IF BESCNO == H_King_20A_Multi
  1103. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1104. $include (H_King_20A.inc) ; Select H-King 20A pinout
  1105. ENDIF
  1106. IF BESCNO == H_King_35A_Main
  1107. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1108. $include (H_King_35A.inc) ; Select H-King 35A pinout
  1109. ENDIF
  1110. IF BESCNO == H_King_35A_Tail
  1111. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1112. $include (H_King_35A.inc) ; Select H-King 35A pinout
  1113. ENDIF
  1114. IF BESCNO == H_King_35A_Multi
  1115. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1116. $include (H_King_35A.inc) ; Select H-King 35A pinout
  1117. ENDIF
  1118. IF BESCNO == H_King_50A_Main
  1119. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1120. $include (H_King_50A.inc) ; Select H-King 50A pinout
  1121. ENDIF
  1122. IF BESCNO == H_King_50A_Tail
  1123. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1124. $include (H_King_50A.inc) ; Select H-King 50A pinout
  1125. ENDIF
  1126. IF BESCNO == H_King_50A_Multi
  1127. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1128. $include (H_King_50A.inc) ; Select H-King 50A pinout
  1129. ENDIF
  1130. IF BESCNO == Polaris_Thunder_12A_Main
  1131. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1132. $include (Polaris_Thunder_12A.inc) ; Select Polaris Thunder 12A pinout
  1133. ENDIF
  1134. IF BESCNO == Polaris_Thunder_12A_Tail
  1135. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1136. $include (Polaris_Thunder_12A.inc) ; Select Polaris Thunder 12A pinout
  1137. ENDIF
  1138. IF BESCNO == Polaris_Thunder_12A_Multi
  1139. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1140. $include (Polaris_Thunder_12A.inc) ; Select Polaris Thunder 12A pinout
  1141. ENDIF
  1142. IF BESCNO == Polaris_Thunder_20A_Main
  1143. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1144. $include (Polaris_Thunder_20A.inc) ; Select Polaris Thunder 20A pinout
  1145. ENDIF
  1146. IF BESCNO == Polaris_Thunder_20A_Tail
  1147. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1148. $include (Polaris_Thunder_20A.inc) ; Select Polaris Thunder 20A pinout
  1149. ENDIF
  1150. IF BESCNO == Polaris_Thunder_20A_Multi
  1151. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1152. $include (Polaris_Thunder_20A.inc) ; Select Polaris Thunder 20A pinout
  1153. ENDIF
  1154. IF BESCNO == Polaris_Thunder_30A_Main
  1155. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1156. $include (Polaris_Thunder_30A.inc) ; Select Polaris Thunder 30A pinout
  1157. ENDIF
  1158. IF BESCNO == Polaris_Thunder_30A_Tail
  1159. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1160. $include (Polaris_Thunder_30A.inc) ; Select Polaris Thunder 30A pinout
  1161. ENDIF
  1162. IF BESCNO == Polaris_Thunder_30A_Multi
  1163. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1164. $include (Polaris_Thunder_30A.inc) ; Select Polaris Thunder 30A pinout
  1165. ENDIF
  1166. IF BESCNO == Polaris_Thunder_40A_Main
  1167. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1168. $include (Polaris_Thunder_40A.inc) ; Select Polaris Thunder 40A pinout
  1169. ENDIF
  1170. IF BESCNO == Polaris_Thunder_40A_Tail
  1171. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1172. $include (Polaris_Thunder_40A.inc) ; Select Polaris Thunder 40A pinout
  1173. ENDIF
  1174. IF BESCNO == Polaris_Thunder_40A_Multi
  1175. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1176. $include (Polaris_Thunder_40A.inc) ; Select Polaris Thunder 40A pinout
  1177. ENDIF
  1178. IF BESCNO == Polaris_Thunder_60A_Main
  1179. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1180. $include (Polaris_Thunder_60A.inc) ; Select Polaris Thunder 60A pinout
  1181. ENDIF
  1182. IF BESCNO == Polaris_Thunder_60A_Tail
  1183. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1184. $include (Polaris_Thunder_60A.inc) ; Select Polaris Thunder 60A pinout
  1185. ENDIF
  1186. IF BESCNO == Polaris_Thunder_60A_Multi
  1187. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1188. $include (Polaris_Thunder_60A.inc) ; Select Polaris Thunder 60A pinout
  1189. ENDIF
  1190. IF BESCNO == Polaris_Thunder_80A_Main
  1191. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1192. $include (Polaris_Thunder_80A.inc) ; Select Polaris Thunder 80A pinout
  1193. ENDIF
  1194. IF BESCNO == Polaris_Thunder_80A_Tail
  1195. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1196. $include (Polaris_Thunder_80A.inc) ; Select Polaris Thunder 80A pinout
  1197. ENDIF
  1198. IF BESCNO == Polaris_Thunder_80A_Multi
  1199. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1200. $include (Polaris_Thunder_80A.inc) ; Select Polaris Thunder 80A pinout
  1201. ENDIF
  1202. IF BESCNO == Polaris_Thunder_100A_Main
  1203. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1204. $include (Polaris_Thunder_100A.inc); Select Polaris Thunder 100A pinout
  1205. ENDIF
  1206. IF BESCNO == Polaris_Thunder_100A_Tail
  1207. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1208. $include (Polaris_Thunder_100A.inc); Select Polaris Thunder 100A pinout
  1209. ENDIF
  1210. IF BESCNO == Polaris_Thunder_100A_Multi
  1211. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1212. $include (Polaris_Thunder_100A.inc); Select Polaris Thunder 100A pinout
  1213. ENDIF
  1214. IF BESCNO == Platinum_Pro_30A_Main
  1215. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1216. $include (Platinum_Pro_30A.inc) ; Select Platinum Pro 30A pinout
  1217. ENDIF
  1218. IF BESCNO == Platinum_Pro_30A_Tail
  1219. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1220. $include (Platinum_Pro_30A.inc) ; Select Platinum Pro 30A pinout
  1221. ENDIF
  1222. IF BESCNO == Platinum_Pro_30A_Multi
  1223. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1224. $include (Platinum_Pro_30A.inc) ; Select Platinum Pro 30A pinout
  1225. ENDIF
  1226. IF BESCNO == Platinum_Pro_150A_Main
  1227. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1228. $include (Platinum_Pro_150A.inc) ; Select Platinum Pro 150A pinout
  1229. ENDIF
  1230. IF BESCNO == Platinum_Pro_150A_Tail
  1231. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1232. $include (Platinum_Pro_150A.inc) ; Select Platinum Pro 150A pinout
  1233. ENDIF
  1234. IF BESCNO == Platinum_Pro_150A_Multi
  1235. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1236. $include (Platinum_Pro_150A.inc) ; Select Platinum Pro 150A pinout
  1237. ENDIF
  1238. IF BESCNO == Platinum_50Av3_Main
  1239. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1240. $include (Platinum_50Av3.inc) ; Select Platinum 50A v3 pinout
  1241. ENDIF
  1242. IF BESCNO == Platinum_50Av3_Tail
  1243. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1244. $include (Platinum_50Av3.inc) ; Select Platinum 50A v3 pinout
  1245. ENDIF
  1246. IF BESCNO == Platinum_50Av3_Multi
  1247. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1248. $include (Platinum_50Av3.inc) ; Select Platinum 50A v3 pinout
  1249. ENDIF
  1250. IF BESCNO == EAZY_3Av2_Main
  1251. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1252. $include (EAZY_3Av2.inc) ; Select Eazy 3A v2 pinout
  1253. ENDIF
  1254. IF BESCNO == EAZY_3Av2_Tail
  1255. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1256. $include (EAZY_3Av2.inc) ; Select Eazy 3A v2 pinout
  1257. ENDIF
  1258. IF BESCNO == EAZY_3Av2_Multi
  1259. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1260. $include (EAZY_3Av2.inc) ; Select Eazy 3A v2 pinout
  1261. ENDIF
  1262. IF BESCNO == Tarot_30A_Main
  1263. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1264. $include (Tarot_30A.inc) ; Select Tarot 30A pinout
  1265. ENDIF
  1266. IF BESCNO == Tarot_30A_Tail
  1267. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1268. $include (Tarot_30A.inc) ; Select Tarot 30A pinout
  1269. ENDIF
  1270. IF BESCNO == Tarot_30A_Multi
  1271. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1272. $include (Tarot_30A.inc) ; Select Tarot 30A pinout
  1273. ENDIF
  1274. IF BESCNO == SkyIII_30A_Main
  1275. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1276. $include (SkyIII_30A.inc) ; Select SkyIII 30A pinout
  1277. ENDIF
  1278. IF BESCNO == SkyIII_30A_Tail
  1279. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1280. $include (SkyIII_30A.inc) ; Select SkyIII 30A pinout
  1281. ENDIF
  1282. IF BESCNO == SkyIII_30A_Multi
  1283. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1284. $include (SkyIII_30A.inc) ; Select SkyIII 30A pinout
  1285. ENDIF
  1286. IF BESCNO == EMAX_20A_Main
  1287. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1288. $include (EMAX_20A.inc) ; Select EMAX 20A pinout
  1289. ENDIF
  1290. IF BESCNO == EMAX_20A_Tail
  1291. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1292. $include (EMAX_20A.inc) ; Select EMAX 20A pinout
  1293. ENDIF
  1294. IF BESCNO == EMAX_20A_Multi
  1295. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1296. $include (EMAX_20A_Low.inc) ; Select EMAX 20A pinout
  1297. ENDIF
  1298. IF BESCNO == EMAX_40A_Main
  1299. MODE EQU 0 ; Choose mode. Set to 0 for main motor
  1300. $include (EMAX_40A.inc) ; Select EMAX 40A pinout
  1301. ENDIF
  1302. IF BESCNO == EMAX_40A_Tail
  1303. MODE EQU 1 ; Choose mode. Set to 1 for tail motor
  1304. $include (EMAX_40A.inc) ; Select EMAX 40A pinout
  1305. ENDIF
  1306. IF BESCNO == EMAX_40A_Multi
  1307. MODE EQU 2 ; Choose mode. Set to 2 for multirotor
  1308. $include (EMAX_40A.inc) ; Select EMAX 40A pinout
  1309. ENDIF
  1310. ;**** **** **** **** ****
  1311. ; TX programming defaults
  1312. ;
  1313. ; Parameter dependencies:
  1314. ; - Governor P gain, I gain and Range is only used if one of the three governor modes is selected
  1315. ; - Governor setup target is only used if Setup governor mode is selected (or closed loop mode is on for multi)
  1316. ;
  1317. ; MAIN
  1318. DEFAULT_PGM_MAIN_P_GAIN EQU 7 ; 1=0.13 2=0.17 3=0.25 4=0.38 5=0.50 6=0.75 7=1.00 8=1.5 9=2.0 10=3.0 11=4.0 12=6.0 13=8.0
  1319. DEFAULT_PGM_MAIN_I_GAIN EQU 7 ; 1=0.13 2=0.17 3=0.25 4=0.38 5=0.50 6=0.75 7=1.00 8=1.5 9=2.0 10=3.0 11=4.0 12=6.0 13=8.0
  1320. DEFAULT_PGM_MAIN_GOVERNOR_MODE EQU 1 ; 1=Tx 2=Arm 3=Setup 4=Off
  1321. DEFAULT_PGM_MAIN_GOVERNOR_RANGE EQU 1 ; 1=High 2=Middle 3=Low
  1322. DEFAULT_PGM_MAIN_LOW_VOLTAGE_LIM EQU 4 ; 1=Off 2=3.0V/c 3=3.1V/c 4=3.2V/c 5=3.3V/c 6=3.4V/c
  1323. DEFAULT_PGM_MAIN_COMM_TIMING EQU 3 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High
  1324. IF DAMPED_MODE_ENABLE == 1
  1325. DEFAULT_PGM_MAIN_PWM_FREQ EQU 2 ; 1=High 2=Low 3=DampedLight
  1326. ELSE
  1327. DEFAULT_PGM_MAIN_PWM_FREQ EQU 2 ; 1=High 2=Low
  1328. ENDIF
  1329. DEFAULT_PGM_MAIN_DEMAG_COMP EQU 1 ; 1=Disabled 2=Low 3=High
  1330. DEFAULT_PGM_MAIN_DIRECTION EQU 1 ; 1=Normal 2=Reversed
  1331. DEFAULT_PGM_MAIN_RCP_PWM_POL EQU 1 ; 1=Positive 2=Negative
  1332. DEFAULT_PGM_MAIN_GOV_SETUP_TARGET EQU 180 ; Target for governor in setup mode. Corresponds to 70% throttle
  1333. DEFAULT_PGM_MAIN_REARM_START EQU 0 ; 1=Enabled 0=Disabled
  1334. DEFAULT_PGM_MAIN_BEEP_STRENGTH EQU 120 ; Beep strength
  1335. DEFAULT_PGM_MAIN_BEACON_STRENGTH EQU 200 ; Beacon strength
  1336. DEFAULT_PGM_MAIN_BEACON_DELAY EQU 4 ; 1=1m 2=2m 3=5m 4=10m 5=Infinite
  1337. ; TAIL
  1338. DEFAULT_PGM_TAIL_GAIN EQU 3 ; 1=0.75 2=0.88 3=1.00 4=1.12 5=1.25
  1339. DEFAULT_PGM_TAIL_IDLE_SPEED EQU 4 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High
  1340. DEFAULT_PGM_TAIL_COMM_TIMING EQU 3 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High
  1341. IF DAMPED_MODE_ENABLE == 1
  1342. DEFAULT_PGM_TAIL_PWM_FREQ EQU 3 ; 1=High 2=Low 3=DampedLight
  1343. ELSE
  1344. DEFAULT_PGM_TAIL_PWM_FREQ EQU 1 ; 1=High 2=Low
  1345. ENDIF
  1346. DEFAULT_PGM_TAIL_DEMAG_COMP EQU 1 ; 1=Disabled 2=Low 3=High
  1347. DEFAULT_PGM_TAIL_DIRECTION EQU 1 ; 1=Normal 2=Reversed 3=Bidirectional
  1348. DEFAULT_PGM_TAIL_RCP_PWM_POL EQU 1 ; 1=Positive 2=Negative
  1349. DEFAULT_PGM_TAIL_BEEP_STRENGTH EQU 250 ; Beep strength
  1350. DEFAULT_PGM_TAIL_BEACON_STRENGTH EQU 250 ; Beacon strength
  1351. DEFAULT_PGM_TAIL_BEACON_DELAY EQU 4 ; 1=1m 2=2m 3=5m 4=10m 5=Infinite
  1352. ; MULTI
  1353. DEFAULT_PGM_MULTI_FIRST_KEYWORD EQU 66h ;增加首个关键字 2015-02-06
  1354. DEFAULT_PGM_MULTI_P_GAIN EQU 9 ; 1=0.13 2=0.17 3=0.25 4=0.38 5=0.50 6=0.75 7=1.00 8=1.5 9=2.0 10=3.0 11=4.0 12=6.0 13=8.0
  1355. DEFAULT_PGM_MULTI_I_GAIN EQU 9 ; 1=0.13 2=0.17 3=0.25 4=0.38 5=0.50 6=0.75 7=1.00 8=1.5 9=2.0 10=3.0 11=4.0 12=6.0 13=8.0
  1356. ;DEFAULT_PGM_MULTI_GOVERNOR_MODE EQU 4 ; 1=HiRange 2=MidRange 3=LoRange 4=Off
  1357. DEFAULT_PGM_MULTI_GOVERNOR_MODE EQU 1 ; 1=Off 2=LoRange 3=MidRange 4=HiRange 2015-02-10
  1358. DEFAULT_PGM_MULTI_GAIN EQU 3 ; 1=0.75 2=0.88 3=1.00 4=1.12 5=1.25
  1359. ;DEFAULT_PGM_MULTI_LOW_VOLTAGE_LIM EQU 1 ; 1=Off 2=3.0V/c 3=3.1V/c 4=3.2V/c 5=3.3V/c 6=3.4V/c
  1360. DEFAULT_PGM_MULTI_LOW_VOLTAGE_LIM EQU 3 ; 1=Off 2=2.8V/c 3=3.0V/c 4=3.2V/c
  1361. DEFAULT_PGM_MULTI_LOW_VOLTAGE_CTL EQU 1 ;增加低压保护控制方式默认值 1=功率逐渐降到31% 2=关闭输出
  1362. DEFAULT_PGM_MULTI_COMM_TIMING EQU 3 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High
  1363. DEFAULT_PGM_MULTI_DAMPING_FORCE EQU 1 ; 1=VeryLow 2=Low 3=MediumLow 4=MediumHigh 5=High 6=Highest
  1364. IF DAMPED_MODE_ENABLE == 1
  1365. ;DEFAULT_PGM_MULTI_PWM_FREQ EQU 1 ; 1=High 2=Low 3=DampedLight
  1366. DEFAULT_PGM_MULTI_PWM_FREQ EQU 1 ; 1=Low 2=High 3=DampedLight
  1367. ELSE
  1368. DEFAULT_PGM_MULTI_PWM_FREQ EQU 1 ; 1=High 2=Low
  1369. ENDIF
  1370. DEFAULT_PGM_MULTI_DEMAG_COMP EQU 2 ; 1=Disabled 2=Low 3=High
  1371. DEFAULT_PGM_MULTI_DIRECTION EQU 1 ; 1=Normal 2=Reversed 3=Bidirectional
  1372. DEFAULT_PGM_MULTI_RCP_PWM_POL EQU 1 ; 1=Positive 2=Negative
  1373. DEFAULT_PGM_MULTI_BEEP_STRENGTH EQU 80 ; Beep strength
  1374. DEFAULT_PGM_MULTI_BEACON_STRENGTH EQU 80 ; Beacon strength
  1375. DEFAULT_PGM_MULTI_BEACON_DELAY EQU 4 ; 1=1m 2=2m 3=5m 4=10m 5=Infinite
  1376. ; COMMON
  1377. DEFAULT_PGM_ENABLE_TX_PROGRAM EQU 1 ; 1=Enabled 0=Disabled
  1378. DEFAULT_PGM_PPM_MIN_THROTTLE EQU 37 ; 4*37+1000=1148
  1379. DEFAULT_PGM_PPM_MAX_THROTTLE EQU 208 ; 4*208+1000=1832
  1380. DEFAULT_PGM_PPM_CENTER_THROTTLE EQU 122 ; 4*122+1000=1488 (used in bidirectional mode)
  1381. DEFAULT_PGM_BEC_VOLTAGE_HIGH EQU 0 ; 0=Low 1+= High or higher
  1382. DEFAULT_PGM_ENABLE_TEMP_PROT EQU 1 ; 1=Enabled 0=Disabled
  1383. ;**** **** **** **** ****
  1384. ; Constant definitions for main
  1385. IF MODE == 0
  1386. GOV_SPOOLRATE EQU 2 ; Number of steps for governor requested pwm per 32ms
  1387. RCP_TIMEOUT_PPM EQU 10 ; Number of timer2H overflows (about 32ms) before considering rc pulse lost
  1388. RCP_TIMEOUT EQU 64 ; Number of timer2L overflows (about 128us) before considering rc pulse lost
  1389. RCP_SKIP_RATE EQU 32 ; Number of timer2L overflows (about 128us) before reenabling rc pulse detection
  1390. RCP_MIN EQU 0 ; This is minimum RC pulse length
  1391. RCP_MAX EQU 255 ; This is maximum RC pulse length
  1392. RCP_VALIDATE EQU 2 ; Require minimum this pulse length to validate RC pulse
  1393. RCP_STOP EQU 1 ; Stop motor at or below this pulse length
  1394. RCP_STOP_LIMIT EQU 250 ; Stop motor if this many timer2H overflows (~32ms) are below stop limit
  1395. PWM_START EQU 50 ; PWM used as max power during start
  1396. COMM_TIME_RED EQU 1 ; Fixed reduction (in us) for commutation wait (to account for fixed delays)
  1397. COMM_TIME_MIN EQU 1 ; Minimum time (in us) for commutation wait
  1398. TEMP_CHECK_RATE EQU 8 ; Number of adc conversions for each check of temperature (the other conversions are used for voltage)
  1399. ENDIF
  1400. ; Constant definitions for tail
  1401. IF MODE == 1
  1402. GOV_SPOOLRATE EQU 1 ; Number of steps for governor requested pwm per 32ms
  1403. RCP_TIMEOUT_PPM EQU 10 ; Number of timer2H overflows (about 32ms) before considering rc pulse lost
  1404. RCP_TIMEOUT EQU 24 ; Number of timer2L overflows (about 128us) before considering rc pulse lost
  1405. RCP_SKIP_RATE EQU 6 ; Number of timer2L overflows (about 128us) before reenabling rc pulse detection
  1406. RCP_MIN EQU 0 ; This is minimum RC pulse length
  1407. RCP_MAX EQU 255 ; This is maximum RC pulse length
  1408. RCP_VALIDATE EQU 2 ; Require minimum this pulse length to validate RC pulse
  1409. RCP_STOP EQU 1 ; Stop motor at or below this pulse length
  1410. RCP_STOP_LIMIT EQU 130 ; Stop motor if this many timer2H overflows (~32ms) are below stop limit
  1411. PWM_START EQU 50 ; PWM used as max power during start
  1412. COMM_TIME_RED EQU 1 ; Fixed reduction (in us) for commutation wait (to account for fixed delays)
  1413. COMM_TIME_MIN EQU 1 ; Minimum time (in us) for commutation wait
  1414. TEMP_CHECK_RATE EQU 8 ; Number of adc conversions for each check of temperature (the other conversions are used for voltage)
  1415. ENDIF
  1416. ; Constant definitions for multi
  1417. IF MODE == 2
  1418. GOV_SPOOLRATE EQU 1 ; Number of steps for governor requested pwm per 32ms
  1419. RCP_TIMEOUT_PPM EQU 10 ; Number of timer2H overflows (about 32ms) before considering rc pulse lost
  1420. RCP_TIMEOUT EQU 24 ; Number of timer2L overflows (about 128us) before considering rc pulse lost
  1421. RCP_SKIP_RATE EQU 6 ; Number of timer2L overflows (about 128us) before reenabling rc pulse detection
  1422. RCP_MIN EQU 0 ; This is minimum RC pulse length
  1423. RCP_MAX EQU 255 ; This is maximum RC pulse length
  1424. RCP_VALIDATE EQU 2 ; Require minimum this pulse length to validate RC pulse
  1425. RCP_STOP EQU 1 ; Stop motor at or below this pulse length
  1426. RCP_STOP_LIMIT EQU 250 ; Stop motor if this many timer2H overflows (~32ms) are below stop limit
  1427. PWM_START EQU 50 ; PWM used as max power during start
  1428. COMM_TIME_RED EQU 1 ; Fixed reduction (in us) for commutation wait (to account for fixed delays)
  1429. COMM_TIME_MIN EQU 1 ; Minimum time (in us) for commutation wait
  1430. TEMP_CHECK_RATE EQU 8 ; Number of adc conversions for each check of temperature (the other conversions are used for voltage)
  1431. ENDIF
  1432. ;**** **** **** **** ****
  1433. ; Temporary register definitions
  1434. Temp1 EQU R0
  1435. Temp2 EQU R1
  1436. Temp3 EQU R2
  1437. Temp4 EQU R3
  1438. Temp5 EQU R4
  1439. Temp6 EQU R5
  1440. Temp7 EQU R6
  1441. Temp8 EQU R7
  1442. ;**** **** **** **** ****
  1443. ; Register definitions
  1444. DSEG AT 20h ; Variables segment
  1445. Bit_Access: DS 1 ; Variable at bit accessible address (for non interrupt routines)
  1446. Bit_Access_Int: DS 1 ; Variable at bit accessible address (for interrupts)
  1447. Requested_Pwm: DS 1 ; Requested pwm (from RC pulse value)
  1448. Governor_Req_Pwm: DS 1 ; Governor requested pwm (sets governor target)
  1449. Current_Pwm: DS 1 ; Current pwm
  1450. Current_Pwm_Limited: DS 1 ; Current pwm that is limited (applied to the motor output)
  1451. Rcp_Prev_Edge_L: DS 1 ; RC pulse previous edge timer3 timestamp (lo byte)
  1452. Rcp_Prev_Edge_H: DS 1 ; RC pulse previous edge timer3 timestamp (hi byte)
  1453. Rcp_Outside_Range_Cnt: DS 1 ; RC pulse outside range counter (incrementing)
  1454. Rcp_Timeout_Cnt: DS 1 ; RC pulse timeout counter (decrementing)
  1455. Rcp_Skip_Cnt: DS 1 ; RC pulse skip counter (decrementing)
  1456. Rcp_Edge_Cnt: DS 1 ; RC pulse edge counter
  1457. Flags0: DS 1 ; State flags. Reset upon init_start
  1458. T3_PENDING EQU 0 ; Timer3 pending flag
  1459. RCP_MEAS_PWM_FREQ EQU 1 ; Measure RC pulse pwm frequency
  1460. PWM_ON EQU 2 ; Set in on part of pwm cycle
  1461. DEMAG_ENABLED EQU 3 ; Set when demag compensation is enabled (above a min speed and throttle)
  1462. DEMAG_DETECTED EQU 4 ; Set when excessive demag time is detected
  1463. DEMAG_CUT_POWER EQU 5 ; Set when demag compensation cuts power
  1464. DIR_CHANGE_BRAKE EQU 6 ; Set when braking before direction change
  1465. ; EQU 7
  1466. Flags1: DS 1 ; State flags. Reset upon init_start
  1467. MOTOR_SPINNING EQU 0 ; Set when in motor is spinning
  1468. STARTUP_PHASE EQU 1 ; Set when in startup phase
  1469. INITIAL_RUN_PHASE EQU 2 ; Set when in initial run phase, before synchronized run is achieved
  1470. ; EQU 3
  1471. ; EQU 4
  1472. ERRO_DATA EQU 5 ;2015-02-06
  1473. LOW_LIMIT_STOP EQU 6 ;2015-02-06
  1474. PROGRAM_FUNC_FLAG EQU 7 ;2015-02-06
  1475. Flags2: DS 1 ; State flags. NOT reset upon init_start
  1476. RCP_UPDATED EQU 0 ; New RC pulse length value available
  1477. RCP_EDGE_NO EQU 1 ; RC pulse edge no. 0=rising, 1=falling
  1478. PGM_PWMOFF_DAMPED EQU 2 ; Programmed pwm off damped mode
  1479. PGM_PWM_HIGH_FREQ EQU 3 ; Progremmed pwm high frequency
  1480. RCP_PPM EQU 4 ; RC pulse ppm type input (set also when oneshot is set)
  1481. RCP_PPM_ONESHOT125 EQU 5 ; RC pulse ppm type input is OneShot125
  1482. ; EQU 6
  1483. ; EQU 7
  1484. Flags3: DS 1 ; State flags. NOT reset upon init_start
  1485. RCP_PWM_FREQ_1KHZ EQU 0 ; RC pulse pwm frequency is 1kHz
  1486. RCP_PWM_FREQ_2KHZ EQU 1 ; RC pulse pwm frequency is 2kHz
  1487. RCP_PWM_FREQ_4KHZ EQU 2 ; RC pulse pwm frequency is 4kHz
  1488. RCP_PWM_FREQ_8KHZ EQU 3 ; RC pulse pwm frequency is 8kHz
  1489. RCP_PWM_FREQ_12KHZ EQU 4 ; RC pulse pwm frequency is 12kHz
  1490. PGM_DIR_REV EQU 5 ; Programmed direction. 0=normal, 1=reversed
  1491. PGM_RCP_PWM_POL EQU 6 ; Programmed RC pulse pwm polarity. 0=positive, 1=negative
  1492. FULL_THROTTLE_RANGE EQU 7 ; When set full throttle range is used (1000-2000us) and stored calibration values are ignored
  1493. ;**** **** **** **** ****
  1494. ; RAM definitions
  1495. DSEG AT 30h ; Ram data segment, direct addressing
  1496. Initial_Arm: DS 1 ; Variable that is set during the first arm sequence after power on
  1497. Power_On_Wait_Cnt_L: DS 1 ; Power on wait counter (lo byte)
  1498. Power_On_Wait_Cnt_H: DS 1 ; Power on wait counter (hi byte)
  1499. Startup_Rot_Cnt: DS 1 ; Startup phase rotations counter
  1500. Startup_Ok_Cnt: DS 1 ; Startup phase ok comparator waits counter (incrementing)
  1501. Demag_Detected_Metric: DS 1 ; Metric used to gauge demag event frequency
  1502. Demag_Pwr_Off_Thresh: DS 1 ; Metric threshold above which power is cut
  1503. Low_Rpm_Pwr_Slope: DS 1 ; Sets the slope of power increase for low rpms
  1504. Prev_Comm_L: DS 1 ; Previous commutation timer3 timestamp (lo byte)
  1505. Prev_Comm_H: DS 1 ; Previous commutation timer3 timestamp (hi byte)
  1506. Comm_Period4x_L: DS 1 ; Timer3 counts between the last 4 commutations (lo byte)
  1507. Comm_Period4x_H: DS 1 ; Timer3 counts between the last 4 commutations (hi byte)
  1508. Comm_Phase: DS 1 ; Current commutation phase
  1509. Comparator_Read_Cnt: DS 1 ; Number of comparator reads done
  1510. Gov_Target_L: DS 1 ; Governor target (lo byte)
  1511. Gov_Target_H: DS 1 ; Governor target (hi byte)
  1512. Gov_Integral_L: DS 1 ; Governor integral error (lo byte)
  1513. Gov_Integral_H: DS 1 ; Governor integral error (hi byte)
  1514. Gov_Integral_X: DS 1 ; Governor integral error (ex byte)
  1515. Gov_Proportional_L: DS 1 ; Governor proportional error (lo byte)
  1516. Gov_Proportional_H: DS 1 ; Governor proportional error (hi byte)
  1517. Gov_Prop_Pwm: DS 1 ; Governor calculated new pwm based upon proportional error
  1518. Gov_Arm_Target: DS 1 ; Governor arm target value
  1519. Gov_Active: DS 1 ; Governor active (enabled when speed is above minimum)
  1520. Wt_Advance_L: DS 1 ; Timer3 counts for commutation advance timing (lo byte)
  1521. Wt_Advance_H: DS 1 ; Timer3 counts for commutation advance timing (hi byte)
  1522. Wt_Zc_Scan_L: DS 1 ; Timer3 counts from commutation to zero cross scan (lo byte)
  1523. Wt_Zc_Scan_H: DS 1 ; Timer3 counts from commutation to zero cross scan (hi byte)
  1524. Wt_Zc_Timeout_L: DS 1 ; Timer3 counts for zero cross scan timeout (lo byte)
  1525. Wt_Zc_Timeout_H: DS 1 ; Timer3 counts for zero cross scan timeout (hi byte)
  1526. Wt_Comm_L: DS 1 ; Timer3 counts from zero cross to commutation (lo byte)
  1527. Wt_Comm_H: DS 1 ; Timer3 counts from zero cross to commutation (hi byte)
  1528. Next_Wt_L: DS 1 ; Timer3 counts for next wait period (lo byte)
  1529. Next_Wt_H: DS 1 ; Timer3 counts for next wait period (hi byte)
  1530. Rcp_PrePrev_Edge_L: DS 1 ; RC pulse pre previous edge pca timestamp (lo byte)
  1531. Rcp_PrePrev_Edge_H: DS 1 ; RC pulse pre previous edge pca timestamp (hi byte)
  1532. Rcp_Edge_L: DS 1 ; RC pulse edge pca timestamp (lo byte)
  1533. Rcp_Edge_H: DS 1 ; RC pulse edge pca timestamp (hi byte)
  1534. Rcp_Prev_Period_L: DS 1 ; RC pulse previous period (lo byte)
  1535. Rcp_Prev_Period_H: DS 1 ; RC pulse previous period (hi byte)
  1536. Rcp_Period_Diff_Accepted: DS 1 ; RC pulse period difference acceptable
  1537. New_Rcp: DS 1 ; New RC pulse value in pca counts
  1538. Prev_Rcp_Pwm_Freq: DS 1 ; Previous RC pulse pwm frequency (used during pwm frequency measurement)
  1539. Curr_Rcp_Pwm_Freq: DS 1 ; Current RC pulse pwm frequency (used during pwm frequency measurement)
  1540. Rcp_Stop_Cnt: DS 1 ; Counter for RC pulses below stop value
  1541. Auto_Bailout_Armed: DS 1 ; Set when auto rotation bailout is armed
  1542. Pwm_Limit: DS 1 ; Maximum allowed pwm
  1543. Pwm_Limit_Spoolup: DS 1 ; Maximum allowed pwm during spoolup
  1544. Pwm_Limit_Low_Rpm: DS 1 ; Maximum allowed pwm for low rpms
  1545. Pwm_Spoolup_Beg: DS 1 ; Pwm to begin main spoolup with
  1546. Pwm_Motor_Idle: DS 1 ; Motor idle speed pwm
  1547. Pwm_On_Cnt: DS 1 ; Pwm on event counter (used to increase pwm off time for low pwm)
  1548. Spoolup_Limit_Cnt: DS 1 ; Interrupt count for spoolup limit
  1549. Spoolup_Limit_Skip: DS 1 ; Interrupt skips for spoolup limit increment (1=no skips, 2=skip one etc)
  1550. Main_Spoolup_Time_3x: DS 1 ; Main spoolup time x3
  1551. Main_Spoolup_Time_10x: DS 1 ; Main spoolup time x10
  1552. Main_Spoolup_Time_15x: DS 1 ; Main spoolup time x15
  1553. Lipo_Adc_Reference_L: DS 1 ; Voltage reference adc value (lo byte)
  1554. Lipo_Adc_Reference_H: DS 1 ; Voltage reference adc value (hi byte)
  1555. Lipo_Adc_Limit_L: DS 1 ; Low voltage limit adc value (lo byte)
  1556. Lipo_Adc_Limit_H: DS 1 ; Low voltage limit adc value (hi byte)
  1557. Adc_Conversion_Cnt: DS 1 ; Adc conversion counter
  1558. Limit_Count: DS 1 ;2015-02-6
  1559. Current_Average_Temp: DS 1 ; Current average temperature (lo byte ADC reading, assuming hi byte is 1)
  1560. Ppm_Throttle_Gain: DS 1 ; Gain to be applied to RCP value for PPM input
  1561. Beep_Strength: DS 1 ; Strength of beeps
  1562. Tx_Pgm_Func_No: DS 1 ; Function number when doing programming by tx
  1563. Tx_Pgm_Paraval_No: DS 1 ; Parameter value number when doing programming by tx
  1564. Tx_Pgm_Beep_No: DS 1 ; Beep number when doing programming by tx
  1565. Min_Throttle: DS 1 ;min throttle 2015-02-05
  1566. Lipo_Cell_Count: DS 1 ;Lipo counter 2015-02-05
  1567. Commu_Data_Buffer: DS 1 ;编程卡接收,发送数据暂存 2015-02-06
  1568. Commu_Sum: DS 1 ;增加编程卡校验和 2015-02-06
  1569. Pgm_Card_Sig_Count: DS 1 ;program card signal check counter 2015-02-09
  1570. ; Indirect addressing data segment. The variables below must be in this sequence
  1571. ISEG AT 080h
  1572. Pgm_Fir_Key: DS 1 ;增加首个关键字 2015-02-06
  1573. Pgm_Gov_P_Gain: DS 1 ; Programmed governor P gain
  1574. Pgm_Gov_I_Gain: DS 1 ; Programmed governor I gain
  1575. Pgm_Gov_Mode: DS 1 ; Programmed governor mode
  1576. Pgm_Low_Voltage_Lim: DS 1 ; Programmed low voltage limit
  1577. Pgm_Low_Voltage_Ctl: DS 1 ;Programmed low voltage control mode 2015-02-06
  1578. Pgm_Motor_Gain: DS 1 ; Programmed motor gain
  1579. Pgm_Motor_Idle: DS 1 ; Programmed motor idle speed
  1580. Pgm_Startup_Pwr: DS 1 ; Programmed startup power
  1581. Pgm_Pwm_Freq: DS 1 ; Programmed pwm frequency
  1582. Pgm_Direction: DS 1 ; Programmed rotation direction
  1583. Pgm_Input_Pol: DS 1 ; Programmed input pwm polarity
  1584. Initialized_L_Dummy: DS 1 ; Place holder
  1585. Initialized_H_Dummy: DS 1 ; Place holder
  1586. Pgm_Enable_TX_Program: DS 1 ; Programmed enable/disable value for TX programming
  1587. Pgm_Main_Rearm_Start: DS 1 ; Programmed enable/disable re-arming main every start
  1588. Pgm_Gov_Setup_Target: DS 1 ; Programmed main governor setup target
  1589. _Pgm_Startup_Rpm: DS 1 ; Programmed startup rpm (unused - place holder)
  1590. _Pgm_Startup_Accel: DS 1 ; Programmed startup acceleration (unused - place holder)
  1591. _Pgm_Volt_Comp: DS 1 ; Place holder
  1592. Pgm_Comm_Timing: DS 1 ; Programmed commutation timing
  1593. Pgm_Damping_Force: DS 1 ; Programmed damping force (unused - place holder)
  1594. Pgm_Gov_Range: DS 1 ; Programmed governor range
  1595. _Pgm_Startup_Method: DS 1 ; Programmed startup method (unused - place holder)
  1596. Pgm_Ppm_Min_Throttle: DS 1 ; Programmed throttle minimum
  1597. Pgm_Ppm_Max_Throttle: DS 1 ; Programmed throttle maximum
  1598. Pgm_Beep_Strength: DS 1 ; Programmed beep strength
  1599. Pgm_Beacon_Strength: DS 1 ; Programmed beacon strength
  1600. Pgm_Beacon_Delay: DS 1 ; Programmed beacon delay
  1601. _Pgm_Throttle_Rate: DS 1 ; Programmed throttle rate (unused - place holder)
  1602. Pgm_Demag_Comp: DS 1 ; Programmed demag compensation
  1603. Pgm_BEC_Voltage_High: DS 1 ; Programmed BEC voltage
  1604. Pgm_Ppm_Center_Throttle: DS 1 ; Programmed throttle center (in bidirectional mode)
  1605. Pgm_Main_Spoolup_Time: DS 1 ; Programmed main spoolup time
  1606. Pgm_Enable_Temp_Prot: DS 1 ; Programmed temperature protection enable
  1607. ; The sequence of the variables below is no longer of importance
  1608. Pgm_Gov_P_Gain_Decoded: DS 1 ; Programmed governor decoded P gain
  1609. Pgm_Gov_I_Gain_Decoded: DS 1 ; Programmed governor decoded I gain
  1610. Pgm_Startup_Pwr_Decoded: DS 1 ; Programmed startup power decoded
  1611. ; Indirect addressing data segment
  1612. ISEG AT 0D0h
  1613. Tag_Temporary_Storage: DS 48 ; Temporary storage for tags when updating "Eeprom"
  1614. ;**** **** **** **** ****
  1615. CSEG AT 1A00h ; "Eeprom" segment
  1616. EEPROM_FW_MAIN_REVISION EQU 13 ; Main revision of the firmware
  1617. EEPROM_FW_SUB_REVISION EQU 1 ; Sub revision of the firmware
  1618. EEPROM_LAYOUT_REVISION EQU 19 ; Revision of the EEPROM layout
  1619. Eep_FW_Main_Revision: DB EEPROM_FW_MAIN_REVISION ; EEPROM firmware main revision number
  1620. Eep_FW_Sub_Revision: DB EEPROM_FW_SUB_REVISION ; EEPROM firmware sub revision number
  1621. Eep_Layout_Revision: DB EEPROM_LAYOUT_REVISION ; EEPROM layout revision number
  1622. IF MODE == 0
  1623. Eep_Pgm_Gov_P_Gain: DB DEFAULT_PGM_MAIN_P_GAIN ; EEPROM copy of programmed governor P gain
  1624. Eep_Pgm_Gov_I_Gain: DB DEFAULT_PGM_MAIN_I_GAIN ; EEPROM copy of programmed governor I gain
  1625. Eep_Pgm_Gov_Mode: DB DEFAULT_PGM_MAIN_GOVERNOR_MODE ; EEPROM copy of programmed governor mode
  1626. Eep_Pgm_Low_Voltage_Lim: DB DEFAULT_PGM_MAIN_LOW_VOLTAGE_LIM ; EEPROM copy of programmed low voltage limit
  1627. _Eep_Pgm_Motor_Gain: DB 0FFh
  1628. _Eep_Pgm_Motor_Idle: DB 0FFh
  1629. Eep_Pgm_Startup_Pwr: DB DEFAULT_PGM_MAIN_STARTUP_PWR ; EEPROM copy of programmed startup power
  1630. Eep_Pgm_Pwm_Freq: DB DEFAULT_PGM_MAIN_PWM_FREQ ; EEPROM copy of programmed pwm frequency
  1631. Eep_Pgm_Direction: DB DEFAULT_PGM_MAIN_DIRECTION ; EEPROM copy of programmed rotation direction
  1632. Eep_Pgm_Input_Pol: DB DEFAULT_PGM_MAIN_RCP_PWM_POL ; EEPROM copy of programmed input polarity
  1633. Eep_Initialized_L: DB 0A5h ; EEPROM initialized signature low byte
  1634. Eep_Initialized_H: DB 05Ah ; EEPROM initialized signature high byte
  1635. Eep_Enable_TX_Program: DB DEFAULT_PGM_ENABLE_TX_PROGRAM ; EEPROM TX programming enable
  1636. Eep_Main_Rearm_Start: DB DEFAULT_PGM_MAIN_REARM_START ; EEPROM re-arming main enable
  1637. Eep_Pgm_Gov_Setup_Target: DB DEFAULT_PGM_MAIN_GOV_SETUP_TARGET ; EEPROM main governor setup target
  1638. _Eep_Pgm_Startup_Rpm: DB 0FFh
  1639. _Eep_Pgm_Startup_Accel: DB 0FFh
  1640. _Eep_Pgm_Volt_Comp: DB 0FFh
  1641. Eep_Pgm_Comm_Timing: DB DEFAULT_PGM_MAIN_COMM_TIMING ; EEPROM copy of programmed commutation timing
  1642. _Eep_Pgm_Damping_Force: DB 0FFh
  1643. Eep_Pgm_Gov_Range: DB DEFAULT_PGM_MAIN_GOVERNOR_RANGE ; EEPROM copy of programmed governor range
  1644. _Eep_Pgm_Startup_Method: DB 0FFh
  1645. Eep_Pgm_Ppm_Min_Throttle: DB DEFAULT_PGM_PPM_MIN_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1148)
  1646. Eep_Pgm_Ppm_Max_Throttle: DB DEFAULT_PGM_PPM_MAX_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1832)
  1647. Eep_Pgm_Beep_Strength: DB DEFAULT_PGM_MAIN_BEEP_STRENGTH ; EEPROM copy of programmed beep strength
  1648. Eep_Pgm_Beacon_Strength: DB DEFAULT_PGM_MAIN_BEACON_STRENGTH ; EEPROM copy of programmed beacon strength
  1649. Eep_Pgm_Beacon_Delay: DB DEFAULT_PGM_MAIN_BEACON_DELAY ; EEPROM copy of programmed beacon delay
  1650. _Eep_Pgm_Throttle_Rate: DB 0FFh
  1651. Eep_Pgm_Demag_Comp: DB DEFAULT_PGM_MAIN_DEMAG_COMP ; EEPROM copy of programmed demag compensation
  1652. Eep_Pgm_BEC_Voltage_High: DB DEFAULT_PGM_BEC_VOLTAGE_HIGH ; EEPROM copy of programmed BEC voltage
  1653. _Eep_Pgm_Ppm_Center_Throttle: DB 0FFh ; EEPROM copy of programmed center throttle (final value is 4x+1000=1488)
  1654. Eep_Pgm_Main_Spoolup_Time: DB DEFAULT_PGM_MAIN_SPOOLUP_TIME ; EEPROM copy of programmed main spoolup time
  1655. Eep_Pgm_Temp_Prot_Enable: DB DEFAULT_PGM_ENABLE_TEMP_PROT ; EEPROM copy of programmed temperature protection enable
  1656. ENDIF
  1657. IF MODE == 1
  1658. _Eep_Pgm_Gov_P_Gain: DB 0FFh
  1659. _Eep_Pgm_Gov_I_Gain: DB 0FFh
  1660. _Eep_Pgm_Gov_Mode: DB 0FFh
  1661. _Eep_Pgm_Low_Voltage_Lim: DB 0FFh
  1662. Eep_Pgm_Motor_Gain: DB DEFAULT_PGM_TAIL_GAIN ; EEPROM copy of programmed tail gain
  1663. Eep_Pgm_Motor_Idle: DB DEFAULT_PGM_TAIL_IDLE_SPEED ; EEPROM copy of programmed tail idle speed
  1664. Eep_Pgm_Startup_Pwr: DB DEFAULT_PGM_TAIL_STARTUP_PWR ; EEPROM copy of programmed startup power
  1665. Eep_Pgm_Pwm_Freq: DB DEFAULT_PGM_TAIL_PWM_FREQ ; EEPROM copy of programmed pwm frequency
  1666. Eep_Pgm_Direction: DB DEFAULT_PGM_TAIL_DIRECTION ; EEPROM copy of programmed rotation direction
  1667. Eep_Pgm_Input_Pol: DB DEFAULT_PGM_TAIL_RCP_PWM_POL ; EEPROM copy of programmed input polarity
  1668. Eep_Initialized_L: DB 05Ah ; EEPROM initialized signature low byte
  1669. Eep_Initialized_H: DB 0A5h ; EEPROM initialized signature high byte
  1670. Eep_Enable_TX_Program: DB DEFAULT_PGM_ENABLE_TX_PROGRAM ; EEPROM TX programming enable
  1671. _Eep_Main_Rearm_Start: DB 0FFh
  1672. _Eep_Pgm_Gov_Setup_Target: DB 0FFh
  1673. _Eep_Pgm_Startup_Rpm: DB 0FFh
  1674. _Eep_Pgm_Startup_Accel: DB 0FFh
  1675. _Eep_Pgm_Volt_Comp: DB 0FFh
  1676. Eep_Pgm_Comm_Timing: DB DEFAULT_PGM_TAIL_COMM_TIMING ; EEPROM copy of programmed commutation timing
  1677. _Eep_Pgm_Damping_Force: DB 0FFh
  1678. _Eep_Pgm_Gov_Range: DB 0FFh
  1679. _Eep_Pgm_Startup_Method: DB 0FFh
  1680. Eep_Pgm_Ppm_Min_Throttle: DB DEFAULT_PGM_PPM_MIN_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1148)
  1681. Eep_Pgm_Ppm_Max_Throttle: DB DEFAULT_PGM_PPM_MAX_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1832)
  1682. Eep_Pgm_Beep_Strength: DB DEFAULT_PGM_TAIL_BEEP_STRENGTH ; EEPROM copy of programmed beep strength
  1683. Eep_Pgm_Beacon_Strength: DB DEFAULT_PGM_TAIL_BEACON_STRENGTH ; EEPROM copy of programmed beacon strength
  1684. Eep_Pgm_Beacon_Delay: DB DEFAULT_PGM_TAIL_BEACON_DELAY ; EEPROM copy of programmed beacon delay
  1685. _Eep_Pgm_Throttle_Rate: DB 0FFh
  1686. Eep_Pgm_Demag_Comp: DB DEFAULT_PGM_TAIL_DEMAG_COMP ; EEPROM copy of programmed demag compensation
  1687. Eep_Pgm_BEC_Voltage_High: DB DEFAULT_PGM_BEC_VOLTAGE_HIGH ; EEPROM copy of programmed BEC voltage
  1688. Eep_Pgm_Ppm_Center_Throttle: DB DEFAULT_PGM_PPM_CENTER_THROTTLE ; EEPROM copy of programmed center throttle (final value is 4x+1000=1488)
  1689. _Eep_Pgm_Main_Spoolup_Time: DB 0FFh
  1690. Eep_Pgm_Temp_Prot_Enable: DB DEFAULT_PGM_ENABLE_TEMP_PROT ; EEPROM copy of programmed temperature protection enable
  1691. ENDIF
  1692. IF MODE == 2
  1693. Eep_Pgm_Fir_Key: DB DEFAULT_PGM_MULTI_FIRST_KEYWORD ;增加首个关键字 2015-02-06
  1694. Eep_Pgm_Gov_P_Gain: DB DEFAULT_PGM_MULTI_P_GAIN ; EEPROM copy of programmed closed loop P gain
  1695. Eep_Pgm_Gov_I_Gain: DB DEFAULT_PGM_MULTI_I_GAIN ; EEPROM copy of programmed closed loop I gain
  1696. Eep_Pgm_Gov_Mode: DB DEFAULT_PGM_MULTI_GOVERNOR_MODE ; EEPROM copy of programmed closed loop mode
  1697. Eep_Pgm_Low_Voltage_Lim: DB DEFAULT_PGM_MULTI_LOW_VOLTAGE_LIM ; EEPROM copy of programmed low voltage limit
  1698. Eep_Pgm_Low_Voltage_Ctl: DB DEFAULT_PGM_MULTI_LOW_VOLTAGE_CTL ; EEPROM copy of programmed low voltage control mode 2015-02-06
  1699. Eep_Pgm_Motor_Gain: DB DEFAULT_PGM_MULTI_GAIN ; EEPROM copy of programmed tail gain
  1700. _Eep_Pgm_Motor_Idle: DB 0FFh ; EEPROM copy of programmed tail idle speed
  1701. Eep_Pgm_Startup_Pwr: DB DEFAULT_PGM_MULTI_STARTUP_PWR ; EEPROM copy of programmed startup power
  1702. Eep_Pgm_Pwm_Freq: DB DEFAULT_PGM_MULTI_PWM_FREQ ; EEPROM copy of programmed pwm frequency
  1703. Eep_Pgm_Direction: DB DEFAULT_PGM_MULTI_DIRECTION ; EEPROM copy of programmed rotation direction
  1704. Eep_Pgm_Input_Pol: DB DEFAULT_PGM_MULTI_RCP_PWM_POL ; EEPROM copy of programmed input polarity
  1705. Eep_Initialized_L: DB 055h ; EEPROM initialized signature low byte
  1706. Eep_Initialized_H: DB 0AAh ; EEPROM initialized signature high byte
  1707. Eep_Enable_TX_Program: DB DEFAULT_PGM_ENABLE_TX_PROGRAM ; EEPROM TX programming enable
  1708. _Eep_Main_Rearm_Start: DB 0FFh
  1709. _Eep_Pgm_Gov_Setup_Target: DB 0FFh
  1710. _Eep_Pgm_Startup_Rpm: DB 0FFh
  1711. _Eep_Pgm_Startup_Accel: DB 0FFh
  1712. _Eep_Pgm_Volt_Comp: DB 0FFh
  1713. Eep_Pgm_Comm_Timing: DB DEFAULT_PGM_MULTI_COMM_TIMING ; EEPROM copy of programmed commutation timing
  1714. Eep_Pgm_Damping_Force: DB DEFAULT_PGM_MULTI_DAMPING_FORCE ; EEPROM copy of programmed damping force
  1715. _Eep_Pgm_Gov_Range: DB 0FFh
  1716. _Eep_Pgm_Startup_Method: DB 0FFh
  1717. Eep_Pgm_Ppm_Min_Throttle: DB DEFAULT_PGM_PPM_MIN_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1148)
  1718. Eep_Pgm_Ppm_Max_Throttle: DB DEFAULT_PGM_PPM_MAX_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1832)
  1719. Eep_Pgm_Beep_Strength: DB DEFAULT_PGM_MULTI_BEEP_STRENGTH ; EEPROM copy of programmed beep strength
  1720. Eep_Pgm_Beacon_Strength: DB DEFAULT_PGM_MULTI_BEACON_STRENGTH ; EEPROM copy of programmed beacon strength
  1721. Eep_Pgm_Beacon_Delay: DB DEFAULT_PGM_MULTI_BEACON_DELAY ; EEPROM copy of programmed beacon delay
  1722. _Eep_Pgm_Throttle_Rate: DB 0FFh
  1723. Eep_Pgm_Demag_Comp: DB DEFAULT_PGM_MULTI_DEMAG_COMP ; EEPROM copy of programmed demag compensation
  1724. Eep_Pgm_BEC_Voltage_High: DB DEFAULT_PGM_BEC_VOLTAGE_HIGH ; EEPROM copy of programmed BEC voltage
  1725. Eep_Pgm_Ppm_Center_Throttle: DB DEFAULT_PGM_PPM_CENTER_THROTTLE ; EEPROM copy of programmed center throttle (final value is 4x+1000=1488)
  1726. _Eep_Pgm_Main_Spoolup_Time: DB 0FFh
  1727. Eep_Pgm_Temp_Prot_Enable: DB DEFAULT_PGM_ENABLE_TEMP_PROT ; EEPROM copy of programmed temperature protection enable
  1728. ENDIF
  1729. Eep_Dummy: DB 0FFh ; EEPROM address for safety reason
  1730. CSEG AT 1A60h
  1731. Eep_Name: DB " " ; Name tag (16 Bytes)
  1732. ;**** **** **** **** ****
  1733. Interrupt_Table_Definition ; SiLabs interrupts
  1734. CSEG AT 80h ; Code segment after interrupt vectors
  1735. ;**** **** **** **** ****
  1736. ; Table definitions
  1737. GOV_GAIN_TABLE: DB 02h, 03h, 04h, 06h, 08h, 0Ch, 10h, 18h, 20h, 30h, 40h, 60h, 80h
  1738. STARTUP_POWER_TABLE: DB 04h, 06h, 08h, 0Ch, 10h, 18h, 20h, 30h, 40h, 60h, 80h, 0A0h, 0C0h
  1739. IF MODE == 0
  1740. IF DAMPED_MODE_ENABLE == 1
  1741. TX_PGM_PARAMS_MAIN: DB 13, 13, 4, 3, 6, 13, 5, 3, 3, 2, 2
  1742. ENDIF
  1743. IF DAMPED_MODE_ENABLE == 0
  1744. TX_PGM_PARAMS_MAIN: DB 13, 13, 4, 3, 6, 13, 5, 2, 3, 2, 2
  1745. ENDIF
  1746. ENDIF
  1747. IF MODE == 1
  1748. IF DAMPED_MODE_ENABLE == 1
  1749. TX_PGM_PARAMS_TAIL: DB 5, 5, 13, 5, 3, 3, 3, 2
  1750. ENDIF
  1751. IF DAMPED_MODE_ENABLE == 0
  1752. TX_PGM_PARAMS_TAIL: DB 5, 5, 13, 5, 2, 3, 3, 2
  1753. ENDIF
  1754. ENDIF
  1755. IF MODE == 2
  1756. IF DAMPED_MODE_ENABLE == 1
  1757. ; TX_PGM_PARAMS_MULTI: DB 13, 13, 4, 5, 6, 13, 5, 3, 3, 3, 2
  1758. TX_PGM_PARAMS_MULTI: DB 2, 5, 13, 4, 2, 4, 2, 3
  1759. ENDIF
  1760. IF DAMPED_MODE_ENABLE == 0
  1761. ; TX_PGM_PARAMS_MULTI: DB 13, 13, 4, 5, 6, 13, 5, 2, 3, 3, 2
  1762. TX_PGM_PARAMS_MULTI: DB 2, 5, 13, 4, 2, 4, 2, 3
  1763. ENDIF
  1764. ENDIF
  1765. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  1766. ;
  1767. ; Timer0 interrupt routine
  1768. ;
  1769. ; Assumptions: DPTR register must be set to desired pwm_nfet_on label
  1770. ; Requirements: Temp variables can NOT be used since PSW.3 is not set
  1771. ;
  1772. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  1773. t0_int: ; Used for pwm control
  1774. clr EA ; Disable all interrupts
  1775. push PSW ; Preserve registers through interrupt
  1776. push ACC
  1777. ; Check if pwm is on
  1778. jb Flags0.PWM_ON, t0_int_pwm_off ; Is pwm on?
  1779. ; Do not execute pwm when stopped
  1780. jb Flags1.MOTOR_SPINNING, ($+5)
  1781. ajmp t0_int_pwm_on_exit
  1782. ; Do not execute pwm on during demag recovery
  1783. jnb Flags0.DEMAG_CUT_POWER, ($+5)
  1784. ajmp t0_int_pwm_on_exit_pfets_off
  1785. ; Pwm on cycle.
  1786. IF MODE == 1 ; Tail
  1787. jnb Current_Pwm_Limited.7, t0_int_pwm_on_low_pwm ; Jump for low pwm (<50%)
  1788. ENDIF
  1789. t0_int_pwm_on_execute:
  1790. clr A
  1791. jmp @A+DPTR ; Jump to pwm on routines. DPTR should be set to one of the pwm_nfet_on labels
  1792. t0_int_pwm_on_low_pwm:
  1793. ; Skip pwm on cycles for very low pwm
  1794. inc Pwm_On_Cnt ; Increment event counter
  1795. clr C
  1796. mov A, #5 ; Only skip for very low pwm
  1797. subb A, Current_Pwm_Limited ; Check skipping shall be done (for low pwm only)
  1798. jc t0_int_pwm_on_execute
  1799. subb A, Pwm_On_Cnt ; Check if on cycle is to be skipped
  1800. jc t0_int_pwm_on_execute
  1801. mov TL0, #120 ; Write start point for timer
  1802. mov A, Current_Pwm_Limited
  1803. jnz ($+5)
  1804. mov TL0, #0 ; Write start point for timer (long time for zero pwm)
  1805. jmp t0_int_pwm_on_exit_no_timer_update
  1806. t0_int_pwm_off:
  1807. ; Pwm off cycle
  1808. mov TL0, Current_Pwm_Limited ; Load new timer setting
  1809. ; Clear pwm on flag
  1810. clr Flags0.PWM_ON
  1811. ; Set full PWM (on all the time) if current PWM near max. This will give full power, but at the cost of a small "jump" in power
  1812. mov A, Current_Pwm_Limited ; Load current pwm
  1813. cpl A ; Full pwm?
  1814. jnz ($+4) ; No - branch
  1815. ajmp t0_int_pwm_off_fullpower_exit ; Yes - exit
  1816. ; Do not execute pwm when stopped
  1817. jb Flags1.MOTOR_SPINNING, ($+5)
  1818. ajmp t0_int_pwm_off_exit_nfets_off
  1819. IF DAMPED_MODE_ENABLE == 1
  1820. ; If damped operation, set pFETs on in pwm_off
  1821. jb Flags2.PGM_PWMOFF_DAMPED, t0_int_pwm_off_damped ; Damped operation?
  1822. ENDIF
  1823. ; Separate exit commands here for minimum delay
  1824. mov TL1, #0 ; Reset timer1
  1825. pop ACC ; Restore preserved registers
  1826. pop PSW
  1827. All_nFETs_Off ; Switch off all nfets
  1828. setb EA ; Enable all interrupts
  1829. reti
  1830. t0_int_pwm_off_damped:
  1831. All_nFETs_Off ; Switch off all nfets
  1832. mov A, #PFETON_DELAY
  1833. djnz ACC, $
  1834. mov A, Comm_Phase ; Turn on pfets according to commutation phase
  1835. dec A
  1836. jb ACC.2, t0_int_pwm_off_comm_5_6
  1837. jb ACC.1, t0_int_pwm_off_comm_3_4
  1838. CpFET_On ; Comm phase 1 or 2 - turn on C
  1839. jmp t0_int_pwm_off_exit
  1840. t0_int_pwm_off_comm_3_4:
  1841. BpFET_On ; Comm phase 3 or 4 - turn on B
  1842. jmp t0_int_pwm_off_exit
  1843. t0_int_pwm_off_comm_5_6:
  1844. ApFET_On ; Comm phase 5 or 6 - turn on A
  1845. jmp t0_int_pwm_off_exit
  1846. t0_int_pwm_off_exit_nfets_off: ; Exit from pwm off cycle
  1847. mov TL1, #0 ; Reset timer1
  1848. pop ACC ; Restore preserved registers
  1849. pop PSW
  1850. All_nFETs_Off ; Switch off all nfets
  1851. setb EA ; Enable all interrupts
  1852. reti
  1853. t0_int_pwm_off_exit:
  1854. mov TL1, #0 ; Reset timer1
  1855. t0_int_pwm_off_fullpower_exit:
  1856. pop ACC ; Restore preserved registers
  1857. pop PSW
  1858. setb EA ; Enable all interrupts
  1859. reti
  1860. pwm_nofet_on: ; Dummy pwm on cycle
  1861. ajmp t0_int_pwm_on_exit
  1862. pwm_afet_on: ; Pwm on cycle afet on (bfet off)
  1863. AnFET_on
  1864. BnFET_off
  1865. ajmp t0_int_pwm_on_exit
  1866. pwm_bfet_on: ; Pwm on cycle bfet on (cfet off)
  1867. BnFET_on
  1868. CnFET_off
  1869. ajmp t0_int_pwm_on_exit
  1870. pwm_cfet_on: ; Pwm on cycle cfet on (afet off)
  1871. CnFET_on
  1872. AnFET_off
  1873. ajmp t0_int_pwm_on_exit
  1874. pwm_anfet_bpfet_on: ; Pwm on cycle anfet on (bnfet off) and bpfet on (used in damped state 6)
  1875. ; Delay from pFETs are turned off (only in damped mode) until nFET is turned on (pFETs are slow)
  1876. ApFET_off
  1877. CpFET_off
  1878. mov A, #NFETON_DELAY ; Set full delay
  1879. djnz ACC, $
  1880. AnFET_on ; Switch nFETs
  1881. BnFET_off
  1882. ajmp t0_int_pwm_on_exit
  1883. pwm_anfet_cpfet_on: ; Pwm on cycle anfet on (bnfet off) and cpfet on (used in damped state 5)
  1884. ; Delay from pFETs are turned off (only in damped mode) until nFET is turned on (pFETs are slow)
  1885. ApFET_off
  1886. BpFET_off
  1887. mov A, #NFETON_DELAY ; Set full delay
  1888. djnz ACC, $
  1889. AnFET_on ; Switch nFETs
  1890. BnFET_off
  1891. ajmp t0_int_pwm_on_exit
  1892. pwm_bnfet_cpfet_on: ; Pwm on cycle bnfet on (cnfet off) and cpfet on (used in damped state 4)
  1893. ; Delay from pFETs are turned off (only in damped mode) until nFET is turned on (pFETs are slow)
  1894. BpFET_off
  1895. ApFET_off
  1896. mov A, #NFETON_DELAY ; Set full delay
  1897. djnz ACC, $
  1898. BnFET_on ; Switch nFETs
  1899. CnFET_off
  1900. ajmp t0_int_pwm_on_exit
  1901. pwm_bnfet_apfet_on: ; Pwm on cycle bnfet on (cnfet off) and apfet on (used in damped state 3)
  1902. ; Delay from pFETs are turned off (only in damped mode) until nFET is turned on (pFETs are slow)
  1903. BpFET_off
  1904. CpFET_off
  1905. mov A, #NFETON_DELAY ; Set full delay
  1906. djnz ACC, $
  1907. BnFET_on ; Switch nFETs
  1908. CnFET_off
  1909. ajmp t0_int_pwm_on_exit
  1910. pwm_cnfet_apfet_on: ; Pwm on cycle cnfet on (anfet off) and apfet on (used in damped state 2)
  1911. ; Delay from pFETs are turned off (only in damped mode) until nFET is turned on (pFETs are slow)
  1912. CpFET_off
  1913. BpFET_off
  1914. mov A, #NFETON_DELAY ; Set full delay
  1915. djnz ACC, $
  1916. CnFET_on ; Switch nFETs
  1917. AnFET_off
  1918. ajmp t0_int_pwm_on_exit
  1919. pwm_cnfet_bpfet_on: ; Pwm on cycle cnfet on (anfet off) and bpfet on (used in damped state 1)
  1920. ; Delay from pFETs are turned off (only in damped mode) until nFET is turned on (pFETs are slow)
  1921. CpFET_off
  1922. ApFET_off
  1923. mov A, #NFETON_DELAY ; Set full delay
  1924. djnz ACC, $
  1925. CnFET_on ; Switch nFETs
  1926. AnFET_off
  1927. ajmp t0_int_pwm_on_exit
  1928. t0_int_pwm_on_exit_pfets_off:
  1929. jnb Flags2.PGM_PWMOFF_DAMPED, t0_int_pwm_on_exit ; If not damped operation - branch
  1930. mov A, Comm_Phase ; Turn off pfets according to commutation phase
  1931. jb ACC.2, t0_int_pfets_off_comm_4_5_6
  1932. jb ACC.1, t0_int_pfets_off_comm_2_3
  1933. t0_int_pfets_off_comm_1_6:
  1934. ApFET_Off ; Comm phase 1 and 6 - turn off A and C
  1935. CpFET_Off
  1936. jmp t0_int_pwm_on_exit
  1937. t0_int_pfets_off_comm_4_5_6:
  1938. jb ACC.1, t0_int_pfets_off_comm_1_6
  1939. ApFET_Off ; Comm phase 4 and 5 - turn off A and B
  1940. BpFET_Off
  1941. jmp t0_int_pwm_on_exit
  1942. t0_int_pfets_off_comm_2_3:
  1943. BpFET_Off ; Comm phase 2 and 3 - turn off B and C
  1944. CpFET_Off
  1945. t0_int_pwm_on_exit:
  1946. ; Set timer for coming on cycle length
  1947. mov A, Current_Pwm_Limited ; Load current pwm
  1948. cpl A ; cpl is 255-x
  1949. mov TL0, A ; Write start point for timer
  1950. ; Set other variables
  1951. mov TL1, #0 ; Reset timer1
  1952. IF MODE == 1 ; Tail
  1953. mov Pwm_On_Cnt, #0 ; Reset pwm on event counter
  1954. ENDIF
  1955. setb Flags0.PWM_ON ; Set pwm on flag
  1956. t0_int_pwm_on_exit_no_timer_update:
  1957. ; Exit interrupt
  1958. pop ACC ; Restore preserved registers
  1959. pop PSW
  1960. setb EA ; Enable all interrupts
  1961. reti
  1962. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  1963. ;
  1964. ; Timer2 interrupt routine
  1965. ;
  1966. ; No assumptions
  1967. ;
  1968. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  1969. t2_int: ; Happens every 128us for low byte and every 32ms for high byte
  1970. clr EA
  1971. clr ET2 ; Disable timer2 interrupts
  1972. anl EIE1, #0EFh ; Disable PCA0 interrupts
  1973. push PSW ; Preserve registers through interrupt
  1974. push ACC
  1975. setb PSW.3 ; Select register bank 1 for interrupt routines
  1976. setb EA
  1977. ; Clear low byte interrupt flag
  1978. clr TF2L ; Clear interrupt flag
  1979. ; Check RC pulse timeout counter
  1980. mov A, Rcp_Timeout_Cnt ; RC pulse timeout count zero?
  1981. jz t2_int_pulses_absent ; Yes - pulses are absent
  1982. ; Decrement timeout counter (if PWM)
  1983. jb Flags2.RCP_PPM, t2_int_skip_start ; If flag is set (PPM) - branch
  1984. dec Rcp_Timeout_Cnt ; No - decrement
  1985. ajmp t2_int_skip_start
  1986. t2_int_pulses_absent:
  1987. ; Timeout counter has reached zero, pulses are absent
  1988. mov Temp1, #RCP_MIN ; RCP_MIN as default
  1989. mov Temp2, #RCP_MIN
  1990. Read_Rcp_Int ; Look at value of Rcp_In
  1991. jnb ACC.Rcp_In, ($+5) ; Is it high?
  1992. mov Temp1, #RCP_MAX ; Yes - set RCP_MAX
  1993. Rcp_Int_First ; Set interrupt trig to first again
  1994. Rcp_Clear_Int_Flag ; Clear interrupt flag
  1995. clr Flags2.RCP_EDGE_NO ; Set first edge flag
  1996. Read_Rcp_Int ; Look once more at value of Rcp_In
  1997. jnb ACC.Rcp_In, ($+5) ; Is it high?
  1998. mov Temp2, #RCP_MAX ; Yes - set RCP_MAX
  1999. clr C
  2000. mov A, Temp1
  2001. subb A, Temp2 ; Compare the two readings of Rcp_In
  2002. jnz t2_int_pulses_absent ; Go back if they are not equal
  2003. jnb Flags0.RCP_MEAS_PWM_FREQ, ($+6) ; Is measure RCP pwm frequency flag set?
  2004. mov Rcp_Timeout_Cnt, #RCP_TIMEOUT ; Yes - set timeout count to start value
  2005. jb Flags2.RCP_PPM, t2_int_ppm_timeout_set ; If flag is set (PPM) - branch
  2006. mov Rcp_Timeout_Cnt, #RCP_TIMEOUT ; For PWM, set timeout count to start value
  2007. t2_int_ppm_timeout_set:
  2008. mov New_Rcp, Temp1 ; Store new pulse length
  2009. setb Flags2.RCP_UPDATED ; Set updated flag
  2010. t2_int_skip_start:
  2011. ; Check RC pulse skip counter
  2012. mov A, Rcp_Skip_Cnt
  2013. jz t2_int_skip_end ; If RC pulse skip count is zero - end skipping RC pulse detection
  2014. ; Decrement skip counter (only if edge counter is zero)
  2015. dec Rcp_Skip_Cnt ; Decrement
  2016. ajmp t2_int_rcp_update_start
  2017. t2_int_skip_end:
  2018. jb Flags2.RCP_PPM, t2_int_rcp_update_start ; If flag is set (PPM) - branch
  2019. ; Skip counter has reached zero, start looking for RC pulses again
  2020. Rcp_Int_Enable ; Enable RC pulse interrupt
  2021. Rcp_Clear_Int_Flag ; Clear interrupt flag
  2022. t2_int_rcp_update_start:
  2023. ; Process updated RC pulse
  2024. jb Flags2.RCP_UPDATED, ($+5) ; Is there an updated RC pulse available?
  2025. ajmp t2_int_current_pwm_done ; No - update pwm limits and exit
  2026. mov Temp1, New_Rcp ; Load new pulse value
  2027. clr Flags2.RCP_UPDATED ; Flag that pulse has been evaluated
  2028. ; Use a gain of 1.0625x for pwm input if not governor mode
  2029. jb Flags2.RCP_PPM, t2_int_pwm_min_run ; If flag is set (PPM) - branch
  2030. IF MODE == 0 ; Main - do not adjust gain
  2031. ajmp t2_int_pwm_min_run
  2032. ELSE
  2033. IF MODE == 2 ; Multi
  2034. mov Temp2, #Pgm_Gov_Mode ; Closed loop mode?
  2035. cjne @Temp2, #1, t2_int_pwm_min_run; Yes - branch
  2036. ENDIF
  2037. ; Limit the maximum value to avoid wrap when scaled to pwm range
  2038. clr C
  2039. mov A, Temp1
  2040. subb A, #240 ; 240 = (255/1.0625) Needs to be updated according to multiplication factor below
  2041. jc t2_int_rcp_update_mult
  2042. mov A, #240 ; Set requested pwm to max
  2043. mov Temp1, A
  2044. t2_int_rcp_update_mult:
  2045. ; Multiply by 1.0625 (optional adjustment gyro gain)
  2046. mov A, Temp1
  2047. swap A ; After this "0.0625"
  2048. anl A, #0Fh
  2049. add A, Temp1
  2050. mov Temp1, A
  2051. ; Adjust tail gain
  2052. mov Temp2, #Pgm_Motor_Gain
  2053. cjne @Temp2, #3, ($+5) ; Is gain 1?
  2054. ajmp t2_int_pwm_min_run ; Yes - skip adjustment
  2055. clr C
  2056. rrc A ; After this "0.5"
  2057. clr C
  2058. rrc A ; After this "0.25"
  2059. mov Bit_Access_Int, @Temp2 ; (Temp2 has #Pgm_Motor_Gain)
  2060. jb Bit_Access_Int.0, t2_int_rcp_gain_corr ; Branch if bit 0 in gain is set
  2061. clr C
  2062. rrc A ; After this "0.125"
  2063. t2_int_rcp_gain_corr:
  2064. jb Bit_Access_Int.2, t2_int_rcp_gain_pos ; Branch if bit 2 in gain is set
  2065. clr C
  2066. xch A, Temp1
  2067. subb A, Temp1 ; Apply negative correction
  2068. mov Temp1, A
  2069. ajmp t2_int_pwm_min_run
  2070. t2_int_rcp_gain_pos:
  2071. add A, Temp1 ; Apply positive correction
  2072. mov Temp1, A
  2073. jnc t2_int_pwm_min_run ; Above max?
  2074. mov A, #0FFh ; Yes - limit
  2075. mov Temp1, A
  2076. ENDIF
  2077. t2_int_pwm_min_run:
  2078. IF MODE == 1 ; Tail - limit minimum pwm
  2079. ; Limit minimum pwm
  2080. clr C
  2081. mov A, Temp1
  2082. subb A, Pwm_Motor_Idle ; Is requested pwm lower than minimum?
  2083. jnc t2_int_pwm_update ; No - branch
  2084. mov A, Pwm_Motor_Idle ; Yes - limit pwm to Pwm_Motor_Idle
  2085. mov Temp1, A
  2086. ENDIF
  2087. t2_int_pwm_update:
  2088. ; Update requested_pwm
  2089. mov Requested_Pwm, Temp1 ; Set requested pwm
  2090. ; Limit pwm during direct start
  2091. jnb Flags1.STARTUP_PHASE, t2_int_current_pwm_update
  2092. clr C
  2093. mov A, Requested_Pwm ; Limit pwm during direct start
  2094. subb A, Pwm_Limit
  2095. ; jc t2_int_current_pwm_update
  2096. jc ($+7)
  2097. mov Requested_Pwm, Pwm_Limit
  2098. ajmp t2_int_current_pwm_update
  2099. mov A, Requested_Pwm
  2100. add A, #11
  2101. mov Requested_Pwm, A
  2102. t2_int_current_pwm_update:
  2103. IF MODE == 0 ; Main
  2104. mov Temp1, #Pgm_Gov_Mode ; Governor mode?
  2105. cjne @Temp1, #4, t2_int_pwm_exit ; Yes - branch
  2106. ENDIF
  2107. IF MODE == 2 ; multi
  2108. mov Temp1, #Pgm_Gov_Mode ; Governor mode?
  2109. cjne @Temp1, #1, t2_int_pwm_exit ; Yes - branch
  2110. ENDIF
  2111. mov Current_Pwm, Requested_Pwm ; Set equal as default
  2112. t2_int_current_pwm_done:
  2113. IF MODE >= 1 ; Tail or multi
  2114. ; Set current_pwm_limited
  2115. mov Temp1, Current_Pwm ; Default not limited
  2116. clr C
  2117. mov A, Current_Pwm ; Check against limit
  2118. subb A, Pwm_Limit
  2119. jc ($+4) ; If current pwm below limit - branch
  2120. mov Temp1, Pwm_Limit ; Limit pwm
  2121. IF MODE == 2 ; Multi
  2122. ; Limit pwm for low rpms
  2123. clr C
  2124. mov A, Temp1 ; Check against limit
  2125. subb A, Pwm_Limit_Low_Rpm
  2126. jc ($+4) ; If current pwm below limit - branch
  2127. mov Temp1, Pwm_Limit_Low_Rpm ; Limit pwm
  2128. ENDIF
  2129. mov Current_Pwm_Limited, Temp1
  2130. ENDIF
  2131. t2_int_pwm_exit:
  2132. ; Set demag enabled if pwm is above limit
  2133. clr C
  2134. mov A, Current_Pwm_Limited
  2135. subb A, #40h ; Set if above 25%
  2136. jc ($+4)
  2137. setb Flags0.DEMAG_ENABLED
  2138. ; Check if high byte flag is set
  2139. jb TF2H, t2h_int
  2140. pop ACC ; Restore preserved registers
  2141. pop PSW
  2142. clr PSW.3 ; Select register bank 0 for main program routines
  2143. orl EIE1, #10h ; Enable PCA0 interrupts
  2144. setb ET2 ; Enable timer2 interrupts
  2145. reti
  2146. t2h_int:
  2147. ; High byte interrupt (happens every 32ms)
  2148. clr TF2H ; Clear interrupt flag
  2149. mov Temp1, #GOV_SPOOLRATE ; Load governor spool rate
  2150. ; Check RC pulse timeout counter (used here for PPM only)
  2151. mov A, Rcp_Timeout_Cnt ; RC pulse timeout count zero?
  2152. jz t2h_int_rcp_stop_check ; Yes - do not decrement
  2153. ; Decrement timeout counter (if PPM)
  2154. jnb Flags2.RCP_PPM, t2h_int_rcp_stop_check ; If flag is not set (PWM) - branch
  2155. dec Rcp_Timeout_Cnt ; No flag set (PPM) - decrement
  2156. t2h_int_rcp_stop_check:
  2157. ; Check RC pulse against stop value
  2158. clr C
  2159. mov A, New_Rcp ; Load new pulse value
  2160. subb A, #RCP_STOP ; Check if pulse is below stop value
  2161. jc t2h_int_rcp_stop
  2162. ; RC pulse higher than stop value, reset stop counter
  2163. mov Rcp_Stop_Cnt, #0 ; Reset rcp stop counter
  2164. ajmp t2h_int_rcp_gov_pwm
  2165. t2h_int_rcp_stop:
  2166. ; RC pulse less than stop value
  2167. mov Auto_Bailout_Armed, #0 ; Disarm bailout
  2168. mov Spoolup_Limit_Cnt, #0
  2169. mov A, Rcp_Stop_Cnt ; Increment stop counter
  2170. add A, #1
  2171. mov Rcp_Stop_Cnt, A
  2172. jnc t2h_int_rcp_gov_pwm ; Branch if counter has not wrapped
  2173. mov Rcp_Stop_Cnt, #0FFh ; Set stop counter to max
  2174. t2h_int_rcp_gov_pwm:
  2175. IF MODE == 0 ; Main
  2176. ; Update governor variables
  2177. mov Temp2, #Pgm_Gov_Mode ; Governor target by arm mode?
  2178. cjne @Temp2, #2, t2h_int_rcp_gov_by_setup ; No - branch
  2179. mov A, Gov_Active ; Is governor active?
  2180. jz t2h_int_rcp_gov_by_tx ; No - branch (this ensures soft spoolup by tx)
  2181. clr C
  2182. mov A, Requested_Pwm
  2183. subb A, #50 ; Is requested pwm below 20%?
  2184. jc t2h_int_rcp_gov_by_tx ; Yes - branch (this enables a soft spooldown)
  2185. mov Requested_Pwm, Gov_Arm_Target ; Yes - load arm target
  2186. t2h_int_rcp_gov_by_setup:
  2187. mov Temp2, #Pgm_Gov_Mode ; Governor target by setup mode?
  2188. cjne @Temp2, #3, t2h_int_rcp_gov_by_tx ; No - branch
  2189. mov A, Gov_Active ; Is governor active?
  2190. jz t2h_int_rcp_gov_by_tx ; No - branch (this ensures soft spoolup by tx)
  2191. clr C
  2192. mov A, Requested_Pwm
  2193. subb A, #50 ; Is requested pwm below 20%?
  2194. jc t2h_int_rcp_gov_by_tx ; Yes - branch (this enables a soft spooldown)
  2195. mov Temp2, #Pgm_Gov_Setup_Target ; Gov by setup - load setup target
  2196. mov Requested_Pwm, @Temp2
  2197. t2h_int_rcp_gov_by_tx:
  2198. clr C
  2199. mov A, Governor_Req_Pwm
  2200. subb A, Requested_Pwm ; Is governor requested pwm equal to requested pwm?
  2201. jz t2h_int_rcp_gov_pwm_done ; Yes - branch
  2202. jc t2h_int_rcp_gov_pwm_inc ; No - if lower, then increment
  2203. dec Governor_Req_Pwm ; No - if higher, then decrement
  2204. ajmp t2h_int_rcp_gov_pwm_done
  2205. t2h_int_rcp_gov_pwm_inc:
  2206. inc Governor_Req_Pwm ; Increment
  2207. t2h_int_rcp_gov_pwm_done:
  2208. djnz Temp1, t2h_int_rcp_gov_pwm ; If not number of steps processed - go back
  2209. inc Spoolup_Limit_Cnt ; Increment spoolup count
  2210. mov A, Spoolup_Limit_Cnt
  2211. jnz ($+4) ; Wrapped?
  2212. dec Spoolup_Limit_Cnt ; Yes - decrement
  2213. djnz Spoolup_Limit_Skip, t2h_int_rcp_exit ; Jump if skip count is not reached
  2214. mov Spoolup_Limit_Skip, #1 ; Reset skip count. Default is fast spoolup
  2215. mov Temp1, #5 ; Default fast increase
  2216. clr C
  2217. mov A, Spoolup_Limit_Cnt
  2218. subb A, Main_Spoolup_Time_3x ; No spoolup until 3*N*32ms
  2219. jc t2h_int_rcp_exit
  2220. clr C
  2221. mov A, Spoolup_Limit_Cnt
  2222. subb A, Main_Spoolup_Time_10x ; Slow spoolup until "100"*N*32ms
  2223. jnc t2h_int_rcp_limit_middle_ramp
  2224. mov Temp1, #1 ; Slow initial spoolup
  2225. mov Spoolup_Limit_Skip, #3
  2226. jmp t2h_int_rcp_set_limit
  2227. t2h_int_rcp_limit_middle_ramp:
  2228. clr C
  2229. mov A, Spoolup_Limit_Cnt
  2230. subb A, Main_Spoolup_Time_15x ; Faster spoolup until "150"*N*32ms
  2231. jnc t2h_int_rcp_set_limit
  2232. mov Temp1, #1 ; Faster middle spoolup
  2233. mov Spoolup_Limit_Skip, #1
  2234. t2h_int_rcp_set_limit:
  2235. ; Do not increment spoolup limit if higher pwm is not requested, unless governor is active
  2236. clr C
  2237. mov A, Pwm_Limit_Spoolup
  2238. subb A, Current_Pwm
  2239. jc t2h_int_rcp_inc_limit ; If Current_Pwm is larger than Pwm_Limit_Spoolup - branch
  2240. mov Temp2, #Pgm_Gov_Mode ; Governor mode?
  2241. cjne @Temp2, #4, ($+5)
  2242. ajmp t2h_int_rcp_bailout_arm ; No - branch
  2243. mov A, Gov_Active ; Is governor active?
  2244. jnz t2h_int_rcp_inc_limit ; Yes - branch
  2245. mov Pwm_Limit_Spoolup, Current_Pwm ; Set limit to what current pwm is
  2246. mov A, Spoolup_Limit_Cnt ; Check if spoolup limit count is 255. If it is, then this is a "bailout" ramp
  2247. inc A
  2248. jz ($+5)
  2249. mov Spoolup_Limit_Cnt, Main_Spoolup_Time_3x ; Stay in an early part of the spoolup sequence (unless "bailout" ramp)
  2250. mov Spoolup_Limit_Skip, #1 ; Set skip count
  2251. mov Governor_Req_Pwm, #60 ; Set governor requested speed to ensure that it requests higher speed
  2252. ; 20=Fail on jerk when governor activates
  2253. ; 30=Ok
  2254. ; 100=Fail on small governor settling overshoot on low headspeeds
  2255. ; 200=Fail on governor settling overshoot
  2256. jmp t2h_int_rcp_exit ; Exit
  2257. t2h_int_rcp_inc_limit:
  2258. mov A, Pwm_Limit_Spoolup ; Increment spoolup pwm
  2259. add A, Temp1
  2260. jnc t2h_int_rcp_no_limit ; If below 255 - branch
  2261. mov Pwm_Limit_Spoolup, #0FFh
  2262. ajmp t2h_int_rcp_bailout_arm
  2263. t2h_int_rcp_no_limit:
  2264. mov Pwm_Limit_Spoolup, A
  2265. t2h_int_rcp_bailout_arm:
  2266. mov A, Pwm_Limit_Spoolup
  2267. inc A
  2268. jnz t2h_int_rcp_exit
  2269. mov Auto_Bailout_Armed, #255 ; Arm bailout
  2270. mov Spoolup_Limit_Cnt, #255
  2271. t2h_int_rcp_exit:
  2272. ENDIF
  2273. pop ACC ; Restore preserved registers
  2274. pop PSW
  2275. clr PSW.3 ; Select register bank 0 for main program routines
  2276. orl EIE1, #10h ; Enable PCA0 interrupts
  2277. setb ET2 ; Enable timer2 interrupts
  2278. reti
  2279. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2280. ;
  2281. ; Timer3 interrupt routine
  2282. ;
  2283. ; No assumptions
  2284. ;
  2285. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2286. t3_int: ; Used for commutation timing
  2287. push PSW ; Preserve registers through interrupt
  2288. push ACC
  2289. clr EA ; Disable all interrupts
  2290. anl TMR3CN, #7Fh ; Clear timer3 interrupt flag
  2291. anl EIE1, #7Fh ; Disable timer3 interrupts
  2292. clr Flags0.T3_PENDING ; Flag that timer has wrapped
  2293. ; Set up next wait
  2294. mov TMR3CN, #00h ; Timer3 disabled
  2295. clr C
  2296. clr A
  2297. subb A, Next_Wt_L ; Set wait value
  2298. mov TMR3L, A
  2299. clr A
  2300. subb A, Next_Wt_H
  2301. mov TMR3H, A
  2302. mov TMR3CN, #04h ; Timer3 enabled
  2303. pop ACC ; Restore preserved registers
  2304. pop PSW
  2305. setb EA ; Enable all interrupts
  2306. reti
  2307. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2308. ;
  2309. ; PCA interrupt routine
  2310. ;
  2311. ; No assumptions
  2312. ;
  2313. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2314. pca_int: ; Used for RC pulse timing
  2315. clr EA
  2316. anl EIE1, #0EFh ; Disable PCA0 interrupts
  2317. clr ET2 ; Disable timer2 interrupts
  2318. push PSW ; Preserve registers through interrupt
  2319. push ACC
  2320. push B
  2321. setb PSW.3 ; Select register bank 1 for interrupt routines
  2322. setb EA
  2323. ; Get the PCA counter values
  2324. Get_Rcp_Capture_Values
  2325. ; Clear interrupt flag
  2326. Rcp_Clear_Int_Flag
  2327. ; Check which edge it is
  2328. jnb Flags2.RCP_EDGE_NO, ($+5) ; Is it a first edge trig?
  2329. ajmp pca_int_second_meas_pwm_freq ; No - branch to second
  2330. Rcp_Int_Second ; Yes - set second edge trig
  2331. setb Flags2.RCP_EDGE_NO ; Set second edge flag
  2332. ; Read RC signal level
  2333. Read_Rcp_Int
  2334. ; Test RC signal level
  2335. jb ACC.Rcp_In, ($+5) ; Is it high?
  2336. ajmp pca_int_fail_minimum ; No - jump to fail minimum
  2337. ; RC pulse was high, store RC pulse start timestamp
  2338. mov Rcp_Prev_Edge_L, Temp1
  2339. mov Rcp_Prev_Edge_H, Temp2
  2340. ajmp pca_int_exit ; Exit
  2341. pca_int_fail_minimum:
  2342. ; Prepare for next interrupt
  2343. Rcp_Int_First ; Set interrupt trig to first again
  2344. Rcp_Clear_Int_Flag ; Clear interrupt flag
  2345. clr Flags2.RCP_EDGE_NO ; Set first edge flag
  2346. jnb Flags2.RCP_PPM, ($+5) ; If flag is not set (PWM) - branch
  2347. ajmp pca_int_set_timeout ; If PPM - ignore trig as noise
  2348. mov Temp1, #RCP_MIN ; Set RC pulse value to minimum
  2349. Read_Rcp_Int ; Test RC signal level again
  2350. jnb ACC.Rcp_In, ($+5) ; Is it high?
  2351. ajmp pca_int_set_timeout ; Yes - set new timeout and exit
  2352. mov New_Rcp, Temp1 ; Store new pulse length
  2353. ajmp pca_int_limited ; Set new RC pulse, new timeout and exit
  2354. pca_int_second_meas_pwm_freq:
  2355. ; Prepare for next interrupt
  2356. Rcp_Int_First ; Set first edge trig
  2357. clr Flags2.RCP_EDGE_NO ; Set first edge flag
  2358. ; Check if pwm frequency shall be measured
  2359. jb Flags0.RCP_MEAS_PWM_FREQ, ($+5) ; Is measure RCP pwm frequency flag set?
  2360. ajmp pca_int_fall ; No - skip measurements
  2361. ; Set second edge trig only during pwm frequency measurement
  2362. Rcp_Int_Second ; Set second edge trig
  2363. Rcp_Clear_Int_Flag ; Clear interrupt flag
  2364. setb Flags2.RCP_EDGE_NO ; Set second edge flag
  2365. ; Store edge data to RAM
  2366. mov Rcp_Edge_L, Temp1
  2367. mov Rcp_Edge_H, Temp2
  2368. ; Calculate pwm frequency
  2369. clr C
  2370. mov A, Temp1
  2371. subb A, Rcp_PrePrev_Edge_L
  2372. mov Temp1, A
  2373. mov A, Temp2
  2374. subb A, Rcp_PrePrev_Edge_H
  2375. mov Temp2, A
  2376. clr A
  2377. mov Temp4, A
  2378. mov Temp7, #2 ; Set default period tolerance requirement (MSB)
  2379. mov Temp3, #0 ; (LSB)
  2380. ; Check if pulse is too short
  2381. clr C
  2382. mov A, Temp1
  2383. subb A, #low(140) ; If pulse below 70us, not accepted
  2384. mov A, Temp2
  2385. subb A, #high(140)
  2386. jnc rcp_int_check_12kHz
  2387. mov Rcp_Period_Diff_Accepted, #0 ; Set not accepted
  2388. ajmp pca_int_store_data
  2389. rcp_int_check_12kHz:
  2390. ; Check if pwm frequency is 12kHz
  2391. clr C
  2392. mov A, Temp1
  2393. subb A, #low(200) ; If below 100us, 12kHz pwm is assumed
  2394. mov A, Temp2
  2395. subb A, #high(200)
  2396. jnc pca_int_check_8kHz
  2397. clr A
  2398. setb ACC.RCP_PWM_FREQ_12KHZ
  2399. mov Temp4, A
  2400. mov Temp3, #10 ; Set period tolerance requirement (LSB)
  2401. ajmp pca_int_restore_edge_set_msb
  2402. pca_int_check_8kHz:
  2403. ; Check if pwm frequency is 8kHz
  2404. clr C
  2405. mov A, Temp1
  2406. subb A, #low(360) ; If below 180us, 8kHz pwm is assumed
  2407. mov A, Temp2
  2408. subb A, #high(360)
  2409. jnc pca_int_check_4kHz
  2410. clr A
  2411. setb ACC.RCP_PWM_FREQ_8KHZ
  2412. mov Temp4, A
  2413. mov Temp3, #15 ; Set period tolerance requirement (LSB)
  2414. ajmp pca_int_restore_edge_set_msb
  2415. pca_int_check_4kHz:
  2416. ; Check if pwm frequency is 4kHz
  2417. clr C
  2418. mov A, Temp1
  2419. subb A, #low(720) ; If below 360us, 4kHz pwm is assumed
  2420. mov A, Temp2
  2421. subb A, #high(720)
  2422. jnc pca_int_check_2kHz
  2423. clr A
  2424. setb ACC.RCP_PWM_FREQ_4KHZ
  2425. mov Temp4, A
  2426. mov Temp3, #30 ; Set period tolerance requirement (LSB)
  2427. ajmp pca_int_restore_edge_set_msb
  2428. pca_int_check_2kHz:
  2429. ; Check if pwm frequency is 2kHz
  2430. clr C
  2431. mov A, Temp1
  2432. subb A, #low(1440) ; If below 720us, 2kHz pwm is assumed
  2433. mov A, Temp2
  2434. subb A, #high(1440)
  2435. jnc pca_int_check_1kHz
  2436. clr A
  2437. setb ACC.RCP_PWM_FREQ_2KHZ
  2438. mov Temp4, A
  2439. mov Temp3, #60 ; Set period tolerance requirement (LSB)
  2440. ajmp pca_int_restore_edge_set_msb
  2441. pca_int_check_1kHz:
  2442. ; Check if pwm frequency is 1kHz
  2443. clr C
  2444. mov A, Temp1
  2445. subb A, #low(2200) ; If below 1100us, 1kHz pwm is assumed
  2446. mov A, Temp2
  2447. subb A, #high(2200)
  2448. jnc pca_int_restore_edge
  2449. clr A
  2450. setb ACC.RCP_PWM_FREQ_1KHZ
  2451. mov Temp4, A
  2452. mov Temp3, #120 ; Set period tolerance requirement (LSB)
  2453. pca_int_restore_edge_set_msb:
  2454. mov Temp7, #0 ; Set period tolerance requirement (MSB)
  2455. pca_int_restore_edge:
  2456. ; Calculate difference between this period and previous period
  2457. clr C
  2458. mov A, Temp1
  2459. subb A, Rcp_Prev_Period_L
  2460. mov Temp5, A
  2461. mov A, Temp2
  2462. subb A, Rcp_Prev_Period_H
  2463. mov Temp6, A
  2464. ; Make positive
  2465. jnb ACC.7, pca_int_check_diff
  2466. mov A, Temp5
  2467. cpl A
  2468. add A, #1
  2469. mov Temp5, A
  2470. mov A, Temp6
  2471. cpl A
  2472. mov Temp6, A
  2473. pca_int_check_diff:
  2474. ; Check difference
  2475. mov Rcp_Period_Diff_Accepted, #0 ; Set not accepted as default
  2476. inc Pgm_Card_Sig_Count
  2477. clr C
  2478. mov A, Temp5
  2479. subb A, Temp3 ; Check difference
  2480. mov A, Temp6
  2481. subb A, Temp7
  2482. jnc pca_int_store_data
  2483. mov Rcp_Period_Diff_Accepted, #1 ; Set accepted
  2484. pca_int_store_data:
  2485. ; Store previous period
  2486. mov Rcp_Prev_Period_L, Temp1
  2487. mov Rcp_Prev_Period_H, Temp2
  2488. ; Restore edge data from RAM
  2489. mov Temp1, Rcp_Edge_L
  2490. mov Temp2, Rcp_Edge_H
  2491. ; Store pre previous edge
  2492. mov Rcp_PrePrev_Edge_L, Temp1
  2493. mov Rcp_PrePrev_Edge_H, Temp2
  2494. pca_int_fall:
  2495. ; RC pulse edge was second, calculate new pulse length
  2496. clr C
  2497. mov A, Temp1
  2498. subb A, Rcp_Prev_Edge_L
  2499. mov Temp1, A
  2500. mov A, Temp2
  2501. subb A, Rcp_Prev_Edge_H
  2502. mov Temp2, A
  2503. jnb Flags3.RCP_PWM_FREQ_12KHZ, ($+5) ; Is RC input pwm frequency 12kHz?
  2504. ajmp pca_int_pwm_divide_done ; Yes - branch forward
  2505. jnb Flags3.RCP_PWM_FREQ_8KHZ, ($+5) ; Is RC input pwm frequency 8kHz?
  2506. ajmp pca_int_pwm_divide_done ; Yes - branch forward
  2507. jnb Flags3.RCP_PWM_FREQ_4KHZ, ($+5) ; Is RC input pwm frequency 4kHz?
  2508. ajmp pca_int_pwm_divide ; Yes - branch forward
  2509. jb Flags2.RCP_PPM_ONESHOT125, ($+5)
  2510. ajmp rcp_int_fall_not_oneshot
  2511. mov A, Temp2 ; Oneshot125 - move to I_Temp5/6
  2512. mov Temp6, A
  2513. mov A, Temp1
  2514. mov Temp5, A
  2515. ajmp rcp_int_fall_check_range
  2516. rcp_int_fall_not_oneshot:
  2517. mov A, Temp2 ; No - 2kHz. Divide by 2
  2518. clr C
  2519. rrc A
  2520. mov Temp2, A
  2521. mov A, Temp1
  2522. rrc A
  2523. mov Temp1, A
  2524. jnb Flags3.RCP_PWM_FREQ_2KHZ, ($+5) ; Is RC input pwm frequency 2kHz?
  2525. ajmp pca_int_pwm_divide ; Yes - branch forward
  2526. mov A, Temp2 ; No - 1kHz. Divide by 2 again
  2527. clr C
  2528. rrc A
  2529. mov Temp2, A
  2530. mov A, Temp1
  2531. rrc A
  2532. mov Temp1, A
  2533. jnb Flags3.RCP_PWM_FREQ_1KHZ, ($+5) ; Is RC input pwm frequency 1kHz?
  2534. ajmp pca_int_pwm_divide ; Yes - branch forward
  2535. mov A, Temp2 ; No - PPM. Divide by 2 (to bring range to 256) and move to Temp5/6
  2536. clr C
  2537. rrc A
  2538. mov Temp6, A
  2539. mov A, Temp1
  2540. rrc A
  2541. mov Temp5, A
  2542. rcp_int_fall_check_range:
  2543. ; Skip range limitation if pwm frequency measurement
  2544. jb Flags0.RCP_MEAS_PWM_FREQ, pca_int_ppm_check_full_range
  2545. ; Check if 2160us or above (in order to ignore false pulses)
  2546. clr C
  2547. mov A, Temp5 ; Is pulse 2160us or higher?
  2548. subb A, #28
  2549. mov A, Temp6
  2550. subb A, #2
  2551. jc ($+4) ; No - proceed
  2552. ajmp pca_int_ppm_outside_range ; Yes - ignore pulse
  2553. pca_int_ppm_below_full_range:
  2554. ; Check if below 800us (in order to ignore false pulses)
  2555. mov A, Temp6
  2556. jnz pca_int_ppm_check_full_range
  2557. clr C
  2558. mov A, Temp5 ; Is pulse below 800us?
  2559. subb A, #200
  2560. jnc pca_int_ppm_check_full_range ; No - proceed
  2561. pca_int_ppm_outside_range:
  2562. inc Rcp_Outside_Range_Cnt
  2563. clr C
  2564. mov A, Rcp_Outside_Range_Cnt
  2565. subb A, #10 ; Allow a given number of outside pulses
  2566. jnc ($+4)
  2567. ajmp pca_int_set_timeout ; If below limit - ignore pulse
  2568. mov New_Rcp, #0 ; Set pulse length to zero
  2569. setb Flags2.RCP_UPDATED ; Set updated flag
  2570. ajmp pca_int_set_timeout
  2571. pca_int_ppm_check_full_range:
  2572. mov A, Rcp_Outside_Range_Cnt
  2573. jz ($+4)
  2574. dec Rcp_Outside_Range_Cnt
  2575. ; Calculate "1000us" plus throttle minimum
  2576. mov A, #0 ; Set 1000us as default minimum
  2577. jb Flags3.FULL_THROTTLE_RANGE, pca_int_ppm_calculate ; Check if full range is chosen
  2578. IF MODE >= 1 ; Tail or multi
  2579. mov Temp1, #Pgm_Direction ; Check if bidirectional operation
  2580. mov A, @Temp1
  2581. ENDIF
  2582. mov Temp1, #Pgm_Ppm_Min_Throttle ; Min throttle value is in 4us units
  2583. IF MODE >= 1 ; Tail or multi
  2584. cjne A, #3, ($+5)
  2585. mov Temp1, #Pgm_Ppm_Center_Throttle ; Center throttle value is in 4us units
  2586. ENDIF
  2587. mov A, @Temp1
  2588. pca_int_ppm_calculate:
  2589. add A, #250 ; Add 1000us to minimum
  2590. mov Temp7, A
  2591. clr A
  2592. addc A, #0
  2593. mov Temp8, A
  2594. clr C
  2595. mov A, Temp5 ; Subtract minimum
  2596. subb A, Temp7
  2597. mov Temp5, A
  2598. mov A, Temp6
  2599. subb A, Temp8
  2600. mov Temp6, A
  2601. IF MODE >= 1 ; Tail or multi
  2602. mov Bit_Access_Int.0, C
  2603. mov Temp1, #Pgm_Direction ; Check if bidirectional operation
  2604. mov A, @Temp1
  2605. cjne A, #3, pca_int_ppm_bidir_dir_set ; No - branch
  2606. mov C, Bit_Access_Int.0
  2607. jnc pca_int_ppm_bidir_fwd ; If result is positive - branch
  2608. pca_int_ppm_bidir_rev:
  2609. jb Flags3.PGM_DIR_REV, pca_int_ppm_bidir_dir_set ; If same direction - branch
  2610. clr EA ; Direction change, turn off all fets
  2611. setb Flags3.PGM_DIR_REV
  2612. ajmp pca_int_ppm_bidir_dir_change
  2613. pca_int_ppm_bidir_fwd:
  2614. jnb Flags3.PGM_DIR_REV, pca_int_ppm_bidir_dir_set ; If same direction - branch
  2615. clr EA ; Direction change, turn off all fets
  2616. clr Flags3.PGM_DIR_REV
  2617. pca_int_ppm_bidir_dir_change:
  2618. All_nFETs_Off
  2619. All_pFETs_Off
  2620. jb Flags1.STARTUP_PHASE, ($+5) ; Do not brake when starting
  2621. setb Flags0.DIR_CHANGE_BRAKE ; Set brake flag
  2622. setb EA
  2623. pca_int_ppm_bidir_dir_set:
  2624. mov C, Bit_Access_Int.0
  2625. ENDIF
  2626. jnc pca_int_ppm_neg_checked ; If result is positive - branch
  2627. IF MODE >= 1 ; Tail or multi
  2628. mov A, @Temp1 ; Check if bidirectional operation (Temp1 has Pgm_Direction)
  2629. cjne A, #3, pca_int_ppm_unidir_neg ; No - branch
  2630. mov A, Temp5 ; Change sign
  2631. cpl A
  2632. add A, #1
  2633. mov Temp5, A
  2634. mov A, Temp6
  2635. cpl A
  2636. addc A, #0
  2637. mov Temp6, A
  2638. jmp pca_int_ppm_neg_checked
  2639. pca_int_ppm_unidir_neg:
  2640. ENDIF
  2641. mov Temp1, #RCP_MIN ; Yes - set to minimum
  2642. mov Temp2, #0
  2643. ajmp pca_int_pwm_divide_done
  2644. pca_int_ppm_neg_checked:
  2645. IF MODE >= 1 ; Tail or multi
  2646. mov Temp1, #Pgm_Direction ; Check if bidirectional operation
  2647. mov A, @Temp1
  2648. cjne A, #3, pca_int_ppm_bidir_done ; No - branch
  2649. mov A, Temp5 ; Multiply value by 2
  2650. rlc A
  2651. mov Temp5 A
  2652. mov A, Temp6
  2653. rlc A
  2654. mov Temp6 A
  2655. clr C ; Subtract deadband
  2656. mov A, Temp5
  2657. subb A, #5
  2658. mov Temp5, A
  2659. mov A, Temp6
  2660. subb A, #0
  2661. mov Temp6, A
  2662. jnc pca_int_ppm_bidir_done
  2663. mov Temp5, #RCP_MIN
  2664. mov Temp6, #0
  2665. pca_int_ppm_bidir_done:
  2666. ENDIF
  2667. clr C ; Check that RC pulse is within legal range (max 255)
  2668. mov A, Temp5
  2669. subb A, #RCP_MAX
  2670. mov A, Temp6
  2671. subb A, #0
  2672. jc pca_int_ppm_max_checked
  2673. mov Temp1, #RCP_MAX
  2674. mov Temp2, #0
  2675. ajmp pca_int_pwm_divide_done
  2676. pca_int_ppm_max_checked:
  2677. mov A, Temp5 ; Multiply throttle value by gain
  2678. mov B, Ppm_Throttle_Gain
  2679. mul AB
  2680. xch A, B
  2681. mov C, B.7 ; Multiply result by 2 (unity gain is 128)
  2682. rlc A
  2683. mov Temp1, A ; Transfer to Temp1/2
  2684. mov Temp2, #0
  2685. jc pca_int_ppm_limit_after_mult
  2686. jmp pca_int_limited
  2687. pca_int_ppm_limit_after_mult:
  2688. mov Temp1, #RCP_MAX
  2689. mov Temp2, #0
  2690. jmp pca_int_limited
  2691. pca_int_pwm_divide:
  2692. mov A, Temp2 ; Divide by 2
  2693. clr C
  2694. rrc A
  2695. mov Temp2, A
  2696. mov A, Temp1
  2697. rrc A
  2698. mov Temp1, A
  2699. pca_int_pwm_divide_done:
  2700. jnb Flags3.RCP_PWM_FREQ_12KHZ, pca_int_check_legal_range ; Is RC input pwm frequency 12kHz?
  2701. mov A, Temp2 ; Yes - check that value is not more than 255
  2702. jz ($+4)
  2703. mov Temp1, #RCP_MAX
  2704. clr C
  2705. mov A, Temp1 ; Multiply by 1.5
  2706. rrc A
  2707. addc A, Temp1
  2708. mov Temp1, A
  2709. clr A
  2710. addc A, #0
  2711. mov Temp2, A
  2712. pca_int_check_legal_range:
  2713. ; Check that RC pulse is within legal range
  2714. clr C
  2715. mov A, Temp1
  2716. subb A, #RCP_MAX
  2717. mov A, Temp2
  2718. subb A, #0
  2719. jc pca_int_limited
  2720. mov Temp1, #RCP_MAX
  2721. pca_int_limited:
  2722. ; RC pulse value accepted
  2723. mov New_Rcp, Temp1 ; Store new pulse length
  2724. setb Flags2.RCP_UPDATED ; Set updated flag
  2725. jb Flags0.RCP_MEAS_PWM_FREQ, ($+5) ; Is measure RCP pwm frequency flag set?
  2726. ajmp pca_int_set_timeout ; No - skip measurements
  2727. mov A, #((1 SHL RCP_PWM_FREQ_1KHZ)+(1 SHL RCP_PWM_FREQ_2KHZ)+(1 SHL RCP_PWM_FREQ_4KHZ)+(1 SHL RCP_PWM_FREQ_8KHZ)+(1 SHL RCP_PWM_FREQ_12KHZ))
  2728. cpl A
  2729. anl A, Flags3 ; Clear all pwm frequency flags
  2730. orl A, Temp4 ; Store pwm frequency value in flags
  2731. mov Flags3, A
  2732. clr Flags2.RCP_PPM ; Default, flag is not set (PWM)
  2733. clr A
  2734. add A, Temp4 ; Check if all flags are cleared
  2735. jnz pca_int_set_timeout
  2736. setb Flags2.RCP_PPM ; Set flag (PPM)
  2737. pca_int_set_timeout:
  2738. mov Rcp_Timeout_Cnt, #RCP_TIMEOUT ; Set timeout count to start value
  2739. jnb Flags2.RCP_PPM, pca_int_ppm_timeout_set ; If flag is not set (PWM) - branch
  2740. mov Rcp_Timeout_Cnt, #RCP_TIMEOUT_PPM ; No flag set means PPM. Set timeout count
  2741. pca_int_ppm_timeout_set:
  2742. jnb Flags0.RCP_MEAS_PWM_FREQ, ($+5) ; Is measure RCP pwm frequency flag set?
  2743. ajmp pca_int_exit ; Yes - exit
  2744. jb Flags2.RCP_PPM, pca_int_exit ; If flag is set (PPM) - branch
  2745. Rcp_Int_Disable ; Disable RC pulse interrupt
  2746. pca_int_exit: ; Exit interrupt routine
  2747. mov Rcp_Skip_Cnt, #RCP_SKIP_RATE ; Load number of skips
  2748. jnb Flags2.RCP_PPM, ($+6) ; If flag is not set (PWM) - branch
  2749. mov Rcp_Skip_Cnt, #10 ; Load number of skips
  2750. pop B ; Restore preserved registers
  2751. pop ACC
  2752. pop PSW
  2753. clr PSW.3 ; Select register bank 0 for main program routines
  2754. setb ET2 ; Enable timer2 interrupts
  2755. orl EIE1, #10h ; Enable PCA0 interrupts
  2756. reti
  2757. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2758. ;
  2759. ; Wait xms ~(x*4*250) (Different entry points)
  2760. ;
  2761. ; No assumptions
  2762. ;
  2763. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2764. wait1ms:
  2765. mov Temp2, #1
  2766. jmp waitxms_o
  2767. wait3ms:
  2768. mov Temp2, #3
  2769. jmp waitxms_o
  2770. wait10ms:
  2771. mov Temp2, #10
  2772. jmp waitxms_o
  2773. wait30ms:
  2774. mov Temp2, #30
  2775. jmp waitxms_o
  2776. wait100ms:
  2777. mov Temp2, #100
  2778. jmp waitxms_o
  2779. wait200ms:
  2780. mov Temp2, #200
  2781. jmp waitxms_o
  2782. waitxms_o: ; Outer loop
  2783. mov Temp1, #23
  2784. waitxms_m: ; Middle loop
  2785. clr A
  2786. djnz ACC, $ ; Inner loop (42.7us - 1024 cycles)
  2787. djnz Temp1, waitxms_m
  2788. djnz Temp2, waitxms_o
  2789. ret
  2790. ;**;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2791. ;
  2792. ; Wait 1 second routine
  2793. ;
  2794. ; No assumptions
  2795. ;
  2796. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2797. wait1s:
  2798. mov Temp5, #5
  2799. wait1s_loop:
  2800. call wait200ms
  2801. djnz Temp5, wait1s_loop
  2802. ret
  2803. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2804. ;
  2805. ; Beeper routines (4 different entry points)
  2806. ;
  2807. ; No assumptions
  2808. ;
  2809. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2810. beep_f1: ; Entry point 1, load beeper frequency 1 settings
  2811. mov Temp3, #18 ; Off wait loop length
  2812. mov Temp4, #100 ; Number of beep pulses
  2813. jmp beep
  2814. beep_f2: ; Entry point 2, load beeper frequency 2 settings
  2815. mov Temp3, #14
  2816. mov Temp4, #120
  2817. jmp beep
  2818. beep_f3: ; Entry point 3, load beeper frequency 3 settings
  2819. mov Temp3, #12
  2820. mov Temp4, #160
  2821. jmp beep
  2822. beep_f4: ; Entry point 4, load beeper frequency 4 settings
  2823. mov Temp3, #9
  2824. mov Temp4, #180
  2825. jmp beep
  2826. beep: ; Beep loop start
  2827. mov Temp5, Current_Pwm_Limited ; Store value
  2828. mov Current_Pwm_Limited, #1 ; Set to a nonzero value
  2829. mov Temp2, #2 ; Must be an even number (or direction will change)
  2830. beep_onoff:
  2831. cpl Flags3.PGM_DIR_REV ; Toggle between using A fet and C fet
  2832. clr A
  2833. BpFET_off ; BpFET off
  2834. djnz ACC, $ ; Allow some time after pfet is turned off
  2835. BnFET_on ; BnFET on (in order to charge the driver of the BpFET)
  2836. djnz ACC, $ ; Let the nfet be turned on a while
  2837. BnFET_off ; BnFET off again
  2838. djnz ACC, $ ; Allow some time after nfet is turned off
  2839. BpFET_on ; BpFET on
  2840. djnz ACC, $ ; Allow some time after pfet is turned on
  2841. ; Turn on nfet
  2842. AnFET_on ; AnFET on
  2843. mov A, Beep_Strength
  2844. djnz ACC, $
  2845. ; Turn off nfet
  2846. AnFET_off ; AnFET off
  2847. mov A, #100 ; 25祍 off
  2848. djnz ACC, $
  2849. djnz Temp2, beep_onoff
  2850. ; Copy variable
  2851. mov A, Temp3
  2852. mov Temp1, A
  2853. beep_off: ; Fets off loop
  2854. djnz ACC, $
  2855. djnz Temp1, beep_off
  2856. djnz Temp4, beep
  2857. BpFET_off ; BpFET off
  2858. mov Current_Pwm_Limited, Temp5 ; Restore value
  2859. ret
  2860. ;**;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2861. ;
  2862. ; Success beep routine
  2863. ;
  2864. ; No assumptions
  2865. ;
  2866. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2867. success_beep:
  2868. clr EA ; Disable all interrupts
  2869. call beep_f1
  2870. call beep_f2
  2871. call beep_f3
  2872. call wait10ms
  2873. call beep_f1
  2874. call beep_f2
  2875. call beep_f3
  2876. setb EA ; Enable all interrupts
  2877. ret
  2878. ;**;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2879. ;
  2880. ; Success beep inverted routine
  2881. ;
  2882. ; No assumptions
  2883. ;
  2884. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2885. success_beep_inverted:
  2886. clr EA ; Disable all interrupts
  2887. call beep_f3
  2888. call beep_f2
  2889. call beep_f1
  2890. call wait10ms
  2891. call beep_f3
  2892. call beep_f2
  2893. call beep_f1
  2894. call wait10ms
  2895. setb EA ; Enable all interrupts
  2896. ret
  2897. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2898. ;
  2899. ; Function and parameter value beep routine
  2900. ;
  2901. ; No assumptions
  2902. ;
  2903. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2904. function_paraval_beep:
  2905. mov Temp7, Tx_Pgm_Func_No ; Function no
  2906. mov Temp8, Tx_Pgm_Paraval_No ; Parameter value no
  2907. clr EA ; Disable all interrupts
  2908. jnb Flags1.PROGRAM_FUNC_FLAG,paraval_beep ;跳到参数选项叫
  2909. function_no_6:
  2910. clr C
  2911. mov A,Temp7
  2912. subb A,#5
  2913. jc function_below_beep
  2914. jz function_beep
  2915. mov Temp7, A
  2916. function_beep:
  2917. call beep_f3
  2918. call beep_f3
  2919. call beep_f3
  2920. call beep_f3
  2921. call beep_f3
  2922. call wait30ms
  2923. cjne Temp7,#5,($+6)
  2924. ljmp fun_par_end
  2925. function_below_beep:
  2926. call beep_f1
  2927. call beep_f1
  2928. call beep_f1
  2929. call wait100ms
  2930. djnz Temp7, function_below_beep
  2931. ljmp fun_par_end
  2932. paraval_beep:
  2933. clr A
  2934. mov Temp7,A
  2935. clr C
  2936. mov A,Temp8
  2937. subb A,#10 ;参数数 - 10
  2938. jc paraval_no_7 ;<10
  2939. inc Temp7 ;>=10
  2940. inc Temp7
  2941. jz paraval_below_beep
  2942. mov Temp8,A ;>=存差
  2943. ajmp paraval_below_beep
  2944. paraval_no_7:
  2945. clr C
  2946. mov A,Temp8
  2947. subb A,#5
  2948. jc paraval_no_below
  2949. inc Temp7
  2950. jz paraval_below_beep
  2951. mov Temp8,A ;>=存差
  2952. paraval_below_beep:
  2953. call beep_f2
  2954. call beep_f2
  2955. call beep_f2
  2956. call wait30ms
  2957. djnz Temp7,paraval_below_beep
  2958. cjne Temp8,#10,($+6)
  2959. ljmp fun_par_end
  2960. cjne Temp8,#5,($+6)
  2961. ljmp fun_par_end
  2962. paraval_no_below:
  2963. call wait100ms
  2964. call beep_f4
  2965. call wait100ms
  2966. djnz Temp8, paraval_no_below
  2967. fun_par_end:
  2968. setb EA ; Enable all interrupts
  2969. ret
  2970. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2971. ;
  2972. ; 接收一个数据(一个字节)
  2973. ;
  2974. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  2975. receive_a_byte:
  2976. mov Temp8,#0
  2977. mov Commu_Data_Buffer, #0
  2978. mov Temp6, #8
  2979. wait_high:
  2980. Read_Rcp_Int
  2981. ; Test RC signal level
  2982. jb ACC.Rcp_In,start_t2
  2983. jnb TF1, wait_high
  2984. start_t2:
  2985. mov TL0, #00h
  2986. mov TH0, #00h
  2987. setb TR0 ;启动T0
  2988. wait_low:
  2989. Read_Rcp_Int
  2990. ; Test RC signal level
  2991. jnb ACC.Rcp_In, measure_wide
  2992. jnb TF1, wait_low
  2993. measure_wide:
  2994. clr TR0 ;停止T0
  2995. mov New_Rcp, TL0
  2996. clr C
  2997. mov A, New_Rcp
  2998. subb A, #20 ;40us
  2999. jc receive_a_erro_byte ;<80us 错误字节
  3000. clr C
  3001. mov A, New_Rcp
  3002. subb A, #78 ;156us
  3003. jnc receive_bit_one ;>160us 进行位1判断
  3004. mov A, Temp8 ;取0
  3005. rl A
  3006. mov Temp8, A
  3007. ajmp receive_a_byte_exit
  3008. receive_bit_one:
  3009. clr C
  3010. mov A, New_Rcp
  3011. subb A, #102 ;240us
  3012. jc receive_a_erro_byte ;160< New_Rcp <240us 错误字节
  3013. clr C
  3014. mov A, New_Rcp
  3015. subb A, #204 ;400us
  3016. jnc receive_a_erro_byte ;>400us 错误字节
  3017. mov A, Temp8 ;取1
  3018. rl A
  3019. inc A
  3020. mov Temp8, A
  3021. ajmp receive_a_byte_exit
  3022. receive_a_erro_byte:
  3023. setb Flags1.ERRO_DATA
  3024. receive_a_byte_exit:
  3025. jnb TF1, ($+7)
  3026. setb Flags1.ERRO_DATA
  3027. ajmp ($+6)
  3028. djnz Temp6, wait_high
  3029. mov Commu_Data_Buffer, Temp8
  3030. receive_exit:
  3031. ret
  3032. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3033. ;
  3034. ; 接收一组数据
  3035. ;
  3036. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3037. receive_bytes:
  3038. mov Temp5, #48
  3039. mov Temp2, #Tag_Temporary_Storage ; Set RAM address
  3040. mov Commu_Data_Buffer, #0
  3041. wait_receive_bytes:
  3042. call receive_a_byte
  3043. mov @Temp2, Commu_Data_Buffer
  3044. inc Temp2
  3045. djnz Temp5, wait_receive_bytes
  3046. ret
  3047. store_tags:
  3048. mov Temp2, #Tag_Temporary_Storage ; Set RAM address
  3049. mov Temp5, #48
  3050. store_tag:
  3051. cjne Temp5, #45, store_two
  3052. mov Temp1, #Pgm_Gov_Mode
  3053. jmp store_eep
  3054. store_two:
  3055. cjne Temp5, #44, store_three
  3056. mov Temp1, #Pgm_Low_Voltage_Lim
  3057. jmp store_eep
  3058. store_three:
  3059. cjne Temp5, #43, store_four
  3060. mov Temp1, #Pgm_Low_Voltage_Ctl
  3061. jmp store_eep
  3062. store_four:
  3063. cjne Temp5, #40, store_five
  3064. mov Temp1, #Pgm_Startup_Pwr
  3065. jmp store_eep
  3066. store_five:
  3067. cjne Temp5, #39, store_six
  3068. mov Temp1, #Pgm_Pwm_Freq
  3069. jmp store_eep
  3070. store_six:
  3071. cjne Temp5, #38, store_seven
  3072. mov Temp1, #Pgm_Direction
  3073. jmp store_eep
  3074. store_seven:
  3075. cjne Temp5, #28, store_eight
  3076. mov Temp1, #Pgm_Comm_Timing
  3077. jmp store_eep
  3078. store_eight:
  3079. cjne Temp5, #27, store_next
  3080. mov Temp1, #Pgm_Damping_Force
  3081. store_eep:
  3082. mov Commu_Data_Buffer, @Temp2
  3083. mov @Temp1, Commu_Data_Buffer
  3084. call erase_and_store_all_in_eeprom
  3085. clr EA
  3086. store_next:
  3087. inc Temp2
  3088. djnz Temp5, store_tag
  3089. ret
  3090. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3091. ;
  3092. ; 发送一个数据(一个字节)
  3093. ;
  3094. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3095. send_a_byte:
  3096. clr EA
  3097. setb TR0 ;启动T0
  3098. mov Temp6, #8
  3099. mov A, Commu_Data_Buffer ;读取要发送的数据
  3100. wait_send_a_byte:
  3101. setb P0.7 ;发送高电平
  3102. jnb ACC.7, low_value ;($+8)
  3103. mov TL0, #067h ;发送1脉宽长
  3104. ajmp high_value ;($+5)
  3105. low_value:
  3106. mov TL0, #0CDh ;发送0脉宽长
  3107. high_value:
  3108. mov TH0, #0FFh
  3109. clr TF0 ;清T0溢出标志
  3110. jnb TF0, $ ;等待T0溢出 高电平发送完成
  3111. clr P0.7 ;发送低电平
  3112. mov TL0, #0CDh ;
  3113. mov TH0, #0FFh
  3114. clr TF0 ;清T0溢出标志
  3115. jnb TF0, $ ;等待T0溢出 低电平发送完成
  3116. rl A ;发送下一位
  3117. djnz Temp6, wait_send_a_byte
  3118. ret
  3119. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3120. ;
  3121. ; 发送一组数据
  3122. ;
  3123. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3124. send_bytes:
  3125. clr P0.7
  3126. mov P0MDOUT, #80h ;设置成推挽输出
  3127. mov Temp5, #48
  3128. mov DPTR, #Eep_Pgm_Fir_Key
  3129. mov Temp1, #Pgm_Fir_Key
  3130. wait_send_bytes:
  3131. mov Commu_Data_Buffer, @Temp1
  3132. inc DPTR
  3133. inc Temp1
  3134. call send_a_byte ;逐个字节发送
  3135. djnz Temp5, wait_send_bytes
  3136. mov P0MDOUT, #7Fh ;设置成漏极输出
  3137. setb P0.7
  3138. ret
  3139. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3140. ;
  3141. ; 编程卡校验和
  3142. ;
  3143. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3144. get_commu_buffers_sum:
  3145. mov Commu_Sum, #0
  3146. mov Temp5, #48
  3147. mov Temp2, #Tag_Temporary_Storage ; Set RAM address
  3148. wait_get_commu_buffers_sum:
  3149. mov A, @Temp2
  3150. clr C
  3151. addc A, Commu_Sum
  3152. mov Commu_Sum, A
  3153. inc Temp2
  3154. djnz Temp5, wait_get_commu_buffers_sum
  3155. ret
  3156. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3157. ;
  3158. ; 编程卡编程函数
  3159. ;
  3160. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3161. program_by_card:
  3162. mov Commu_Data_Buffer, #0
  3163. call wait10ms
  3164. clr Flags1.ERRO_DATA ;清错误字节标志
  3165. mov TL1, #00h
  3166. mov TH1, #00h
  3167. setb TR1 ; 启动T1
  3168. clr TF1 ;清T1溢出标志
  3169. call receive_a_byte ;读取一个字节(关键字)
  3170. jb Flags1.ERRO_DATA, program_by_card ;接收到错误字节,则重新接收
  3171. mov A, Commu_Data_Buffer ;读接收到的字节
  3172. cjne A, #05h, receive_next_keyword ;判断是否关键字 0x05
  3173. call wait3ms
  3174. call send_bytes
  3175. ajmp program_by_card
  3176. receive_next_keyword:
  3177. cjne A, #09h, program_by_card ;判断是否关键字 0x09
  3178. mov Commu_Data_Buffer, #0
  3179. mov TL1, #00h
  3180. mov TH1, #00h
  3181. setb TR1 ;启动T1
  3182. clr TF1 ;清T1溢出标志
  3183. call receive_bytes ;读取一组数据
  3184. call get_commu_buffers_sum
  3185. mov A, Commu_Sum ;读取校验和
  3186. jnz program_by_card ;判断校验和
  3187. mov Temp2, #Tag_Temporary_Storage ; Set RAM address
  3188. mov A, @Temp2 ;读取首字节
  3189. cjne A, #66h, program_by_card ;判断首字节
  3190. call wait1ms
  3191. clr P0.7
  3192. mov P0MDOUT, #080h
  3193. mov Commu_Data_Buffer, #088h
  3194. call send_a_byte ;逐个字节发送
  3195. mov P0MDOUT, #07Fh
  3196. setb P0.7
  3197. call store_tags ;存一组数据
  3198. ajmp program_by_card
  3199. ret
  3200. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3201. ;
  3202. ; Division 16bit unsigned by 16bit unsigned
  3203. ;
  3204. ; Dividend shall be in Temp2/Temp1, divisor in Temp4/Temp3
  3205. ; Result will be in Temp2/Temp1
  3206. ;
  3207. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3208. div_u16_by_u16:
  3209. clr C
  3210. mov Temp5, #0
  3211. mov Temp6, #0
  3212. mov B, #0
  3213. div_u16_by_u16_div1:
  3214. inc B ; Increment counter for each left shift
  3215. mov A, Temp3 ; Shift left the divisor
  3216. rlc A
  3217. mov Temp3, A
  3218. mov A, Temp4
  3219. rlc A
  3220. mov Temp4, A
  3221. jnc div_u16_by_u16_div1 ; Repeat until carry flag is set from high-byte
  3222. div_u16_by_u16_div2:
  3223. mov A, Temp4 ; Shift right the divisor
  3224. rrc A
  3225. mov Temp4, A
  3226. mov A, Temp3
  3227. rrc A
  3228. mov Temp3, A
  3229. clr C
  3230. mov A, Temp2 ; Make a safe copy of the dividend
  3231. mov Temp8, A
  3232. mov A, Temp1
  3233. mov Temp7, A
  3234. mov A, Temp1 ; Move low-byte of dividend into accumulator
  3235. subb A, Temp3 ; Dividend - shifted divisor = result bit (no factor, only 0 or 1)
  3236. mov Temp1, A ; Save updated dividend
  3237. mov A, Temp2 ; Move high-byte of dividend into accumulator
  3238. subb A, Temp4 ; Subtract high-byte of divisor (all together 16-bit substraction)
  3239. mov Temp2, A ; Save updated high-byte back in high-byte of divisor
  3240. jnc div_u16_by_u16_div3 ; If carry flag is NOT set, result is 1
  3241. mov A, Temp8 ; Otherwise result is 0, save copy of divisor to undo subtraction
  3242. mov Temp2, A
  3243. mov A, Temp7
  3244. mov Temp1, A
  3245. div_u16_by_u16_div3:
  3246. cpl C ; Invert carry, so it can be directly copied into result
  3247. mov A, Temp5
  3248. rlc A ; Shift carry flag into temporary result
  3249. mov Temp5, A
  3250. mov A, Temp6
  3251. rlc A
  3252. mov Temp6,A
  3253. djnz B, div_u16_by_u16_div2 ;Now count backwards and repeat until "B" is zero
  3254. mov A, Temp6 ; Move result to Temp2/Temp1
  3255. mov Temp2, A
  3256. mov A, Temp5
  3257. mov Temp1, A
  3258. ret
  3259. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3260. ;
  3261. ; Multiplication 16bit signed by 8bit unsigned
  3262. ;
  3263. ; Multiplicand shall be in Temp2/Temp1, multiplicator in Temp3
  3264. ; Result will be in Temp2/Temp1. Result will divided by 16
  3265. ;
  3266. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3267. mult_s16_by_u8_div_16:
  3268. mov A, Temp1 ; Read input to math registers
  3269. mov B, Temp2
  3270. mov Bit_Access, Temp3
  3271. setb PSW.4 ; Select register bank 2 for math routines
  3272. mov Temp1, A ; Store in math registers
  3273. mov Temp2, B
  3274. mov Temp4, #0 ; Set sign in Temp4 and test sign
  3275. jnb B.7, mult_s16_by_u8_positive
  3276. mov Temp4, #0FFh
  3277. cpl A
  3278. add A, #1
  3279. mov Temp1, A
  3280. mov A, Temp2
  3281. cpl A
  3282. addc A, #0
  3283. mov Temp2, A
  3284. mult_s16_by_u8_positive:
  3285. mov A, Temp1 ; Multiply LSB with multiplicator
  3286. mov B, Bit_Access
  3287. mul AB
  3288. mov Temp6, B ; Place MSB in Temp6
  3289. mov Temp1, A ; Place LSB in Temp1 (result)
  3290. mov A, Temp2 ; Multiply MSB with multiplicator
  3291. mov B, Bit_Access
  3292. mul AB
  3293. mov Temp8, B ; Place in Temp8/7
  3294. mov Temp7, A
  3295. mov A, Temp6 ; Add up
  3296. add A, Temp7
  3297. mov Temp2, A
  3298. mov A, #0
  3299. addc A, Temp8
  3300. mov Temp3, A
  3301. mov Temp5, #4 ; Set number of divisions
  3302. mult_s16_by_u8_div_loop:
  3303. clr C ; Rotate right
  3304. mov A, Temp3
  3305. rrc A
  3306. mov Temp3, A
  3307. mov A, Temp2
  3308. rrc A
  3309. mov Temp2, A
  3310. mov A, Temp1
  3311. rrc A
  3312. mov Temp1, A
  3313. djnz Temp5, mult_s16_by_u8_div_loop
  3314. mov B, Temp4 ; Test sign
  3315. jnb B.7, mult_s16_by_u8_exit
  3316. mov A, Temp1
  3317. cpl A
  3318. add A, #1
  3319. mov Temp1, A
  3320. mov A, Temp2
  3321. cpl A
  3322. addc A, #0
  3323. mov Temp2, A
  3324. mult_s16_by_u8_exit:
  3325. mov A, Temp1 ; Store output
  3326. mov B, Temp2
  3327. clr PSW.4 ; Select normal register bank
  3328. mov Temp1, A
  3329. mov Temp2, B
  3330. ret
  3331. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3332. ;
  3333. ; Calculate governor routines
  3334. ;
  3335. ; No assumptions
  3336. ;
  3337. ; Governs headspeed based upon the Comm_Period4x variable and pwm
  3338. ; The governor task is split into several routines in order to distribute processing time
  3339. ;
  3340. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3341. ; First governor routine - calculate governor target
  3342. IF MODE == 0 ; Main
  3343. calc_governor_target:
  3344. mov Temp1, #Pgm_Gov_Mode ; Governor mode?
  3345. cjne @Temp1, #4, governor_speed_check ; Yes
  3346. jmp calc_governor_target_exit ; No
  3347. governor_speed_check:
  3348. ; Stop governor for stop RC pulse
  3349. clr C
  3350. mov A, New_Rcp ; Check RC pulse against stop value
  3351. subb A, #(RCP_MAX/10) ; Is pulse below stop value?
  3352. jc governor_deactivate ; Yes - deactivate
  3353. mov A, Flags1
  3354. anl A, #((1 SHL STARTUP_PHASE)+(1 SHL INITIAL_RUN_PHASE))
  3355. jnz governor_deactivate ; Deactivate if any startup phase set
  3356. ; Skip speed check if governor is already active
  3357. mov A, Gov_Active
  3358. jnz governor_target_calc
  3359. ; Check speed (do not run governor for low speeds)
  3360. mov Temp1, #05h ; Default high range activation limit value (~62500 eRPM)
  3361. mov Temp2, #Pgm_Gov_Range
  3362. mov A, @Temp2 ; Check if high range (Temp2 has #Pgm_Gov_Range)
  3363. dec A
  3364. jz governor_act_lim_set ; If high range - branch
  3365. mov Temp1, #0Ah ; Middle range activation limit value (~31250 eRPM)
  3366. dec A
  3367. jz governor_act_lim_set ; If middle range - branch
  3368. mov Temp1, #12h ; Low range activation limit value (~17400 eRPM)
  3369. governor_act_lim_set:
  3370. clr C
  3371. mov A, Comm_Period4x_H
  3372. subb A, Temp1
  3373. jc governor_activate ; If speed above min limit - run governor
  3374. governor_deactivate:
  3375. mov A, Gov_Active
  3376. jz governor_first_deactivate_done; This code is executed continuously. Only execute the code below the first time
  3377. mov Pwm_Limit_Spoolup, Pwm_Spoolup_Beg
  3378. mov Spoolup_Limit_Cnt, #255
  3379. mov Spoolup_Limit_Skip, #1
  3380. governor_first_deactivate_done:
  3381. mov Current_Pwm, Requested_Pwm ; Set current pwm to requested
  3382. clr A
  3383. mov Gov_Target_L, A ; Set target to zero
  3384. mov Gov_Target_H, A
  3385. mov Gov_Integral_L, A ; Set integral to zero
  3386. mov Gov_Integral_H, A
  3387. mov Gov_Integral_X, A
  3388. mov Gov_Active, A
  3389. jmp calc_governor_target_exit
  3390. governor_activate:
  3391. mov Gov_Active, #1
  3392. governor_target_calc:
  3393. ; Governor calculations
  3394. mov Temp2, #Pgm_Gov_Range
  3395. mov A, @Temp2 ; Check high, middle or low range
  3396. dec A
  3397. jnz calc_governor_target_middle
  3398. mov A, Governor_Req_Pwm ; Load governor requested pwm
  3399. cpl A ; Calculate 255-pwm (invert pwm)
  3400. ; Calculate comm period target (1 + 2*((255-Requested_Pwm)/256) - 0.25)
  3401. rlc A ; Msb to carry
  3402. rlc A ; To bit0
  3403. mov Temp2, A ; Now 1 lsb is valid for H
  3404. rrc A
  3405. mov Temp1, A ; Now 7 msbs are valid for L
  3406. mov A, Temp2
  3407. anl A, #01h ; Calculate H byte
  3408. inc A ; Add 1
  3409. mov Temp2, A
  3410. mov A, Temp1
  3411. anl A, #0FEh ; Calculate L byte
  3412. jmp calc_governor_subtract_025
  3413. calc_governor_target_middle:
  3414. mov A, @Temp2 ; Check middle or low range (Temp2 has #Pgm_Gov_Range)
  3415. dec A
  3416. dec A
  3417. jnz calc_governor_target_low
  3418. mov A, Governor_Req_Pwm ; Load governor requested pwm
  3419. cpl A ; Calculate 255-pwm (invert pwm)
  3420. ; Calculate comm period target (1 + 4*((255-Requested_Pwm)/256))
  3421. rlc A ; Msb to carry
  3422. rlc A ; To bit0
  3423. rlc A ; To bit1
  3424. mov Temp2, A ; Now 2 lsbs are valid for H
  3425. rrc A
  3426. mov Temp1, A ; Now 6 msbs are valid for L
  3427. mov A, Temp2
  3428. anl A, #03h ; Calculate H byte
  3429. inc A ; Add 1
  3430. mov Temp2, A
  3431. mov A, Temp1
  3432. anl A, #0FCh ; Calculate L byte
  3433. jmp calc_governor_store_target
  3434. calc_governor_target_low:
  3435. mov A, Governor_Req_Pwm ; Load governor requested pwm
  3436. cpl A ; Calculate 255-pwm (invert pwm)
  3437. ; Calculate comm period target (2 + 8*((255-Requested_Pwm)/256) - 0.25)
  3438. rlc A ; Msb to carry
  3439. rlc A ; To bit0
  3440. rlc A ; To bit1
  3441. rlc A ; To bit2
  3442. mov Temp2, A ; Now 3 lsbs are valid for H
  3443. rrc A
  3444. mov Temp1, A ; Now 5 msbs are valid for L
  3445. mov A, Temp2
  3446. anl A, #07h ; Calculate H byte
  3447. inc A ; Add 1
  3448. inc A ; Add 1 more
  3449. mov Temp2, A
  3450. mov A, Temp1
  3451. anl A, #0F8h ; Calculate L byte
  3452. calc_governor_subtract_025:
  3453. clr C
  3454. subb A, #40h ; Subtract 0.25
  3455. mov Temp1, A
  3456. mov A, Temp2
  3457. subb A, #0
  3458. mov Temp2, A
  3459. calc_governor_store_target:
  3460. ; Store governor target
  3461. mov Gov_Target_L, Temp1
  3462. mov Gov_Target_H, Temp2
  3463. calc_governor_target_exit:
  3464. ret
  3465. ENDIF
  3466. IF MODE == 1 ; Tail
  3467. calc_governor_target:
  3468. ret
  3469. ENDIF
  3470. IF MODE == 2 ; Multi
  3471. calc_governor_target:
  3472. mov Temp1, #Pgm_Gov_Mode ; Closed loop mode?
  3473. cjne @Temp1, #1, governor_target_calc ; Yes - branch
  3474. jmp calc_governor_target_exit ; No
  3475. governor_target_calc:
  3476. ; Stop governor for stop RC pulse
  3477. clr C
  3478. mov A, New_Rcp ; Check RC pulse against stop value
  3479. subb A, #RCP_STOP ; Is pulse below stop value?
  3480. jc governor_deactivate ; Yes - deactivate
  3481. jmp governor_activate ; No - activate
  3482. governor_deactivate:
  3483. mov Current_Pwm, Requested_Pwm ; Set current pwm to requested
  3484. clr A
  3485. mov Gov_Target_L, A ; Set target to zero
  3486. mov Gov_Target_H, A
  3487. mov Gov_Integral_L, A ; Set integral to zero
  3488. mov Gov_Integral_H, A
  3489. mov Gov_Integral_X, A
  3490. mov Gov_Active, A
  3491. jmp calc_governor_target_exit
  3492. governor_activate:
  3493. mov Temp1, #Pgm_Gov_Mode ; Store gov mode
  3494. mov A, @Temp1
  3495. mov Temp5, A
  3496. mov Gov_Active, #1
  3497. mov A, Requested_Pwm ; Load requested pwm
  3498. mov Governor_Req_Pwm, A ; Set governor requested pwm
  3499. ; Calculate comm period target 2*(51000/Requested_Pwm)
  3500. mov Temp1, #38h ; Load 51000
  3501. mov Temp2, #0C7h
  3502. mov Temp3, Comm_Period4x_L ; Load comm period
  3503. mov Temp4, Comm_Period4x_H
  3504. ; Set speed range. Bare Comm_Period4x corresponds to 400k eRPM, because it is 500n units
  3505. ;Gov_Mode = HiRange
  3506. clr C
  3507. mov A, Temp4
  3508. rrc A
  3509. mov Temp4, A
  3510. mov A, Temp3
  3511. rrc A
  3512. mov Temp3, A ; 200k eRPM range here
  3513. ; Check range
  3514. mov A, Temp5
  3515. dec A
  3516. dec A
  3517. dec A
  3518. dec A
  3519. jz governor_activate_range_set ; 200k eRPM? - branch
  3520. ;Gov_Mode = MidRange
  3521. governor_activate_100k:
  3522. clr C
  3523. mov A, Temp4
  3524. rrc A
  3525. mov Temp4, A
  3526. mov A, Temp3
  3527. rrc A
  3528. mov Temp3, A ; 100k eRPM range here
  3529. mov A, Temp5 ; Check range again
  3530. dec A
  3531. dec A
  3532. dec A
  3533. jz governor_activate_range_set ; 100k eRPM? - branch
  3534. ;Gov_Mode = LoRange
  3535. governor_activate_50k:
  3536. clr C
  3537. mov A, Temp4
  3538. rrc A
  3539. mov Temp4, A
  3540. mov A, Temp3
  3541. rrc A
  3542. mov Temp3, A ; 50k eRPM range here
  3543. governor_activate_range_set:
  3544. call div_u16_by_u16
  3545. ; Store governor target
  3546. mov Gov_Target_L, Temp1
  3547. mov Gov_Target_H, Temp2
  3548. calc_governor_target_exit:
  3549. ret
  3550. ENDIF
  3551. ; Second governor routine - calculate governor proportional error
  3552. calc_governor_prop_error:
  3553. ; Exit if governor is inactive
  3554. mov A, Gov_Active
  3555. jz calc_governor_prop_error_exit
  3556. IF MODE <= 1 ; Main or tail
  3557. ; Load comm period and divide by 2
  3558. clr C
  3559. mov A, Comm_Period4x_H
  3560. rrc A
  3561. mov Temp2, A
  3562. mov A, Comm_Period4x_L
  3563. rrc A
  3564. mov Temp1, A
  3565. ; Calculate error
  3566. clr C
  3567. mov A, Gov_Target_L
  3568. subb A, Temp1
  3569. mov Temp1, A
  3570. mov A, Gov_Target_H
  3571. subb A, Temp2
  3572. mov Temp2, A
  3573. ENDIF
  3574. IF MODE == 2 ; Multi
  3575. ; Calculate error
  3576. clr C
  3577. mov A, Gov_Target_L
  3578. subb A, Governor_Req_Pwm
  3579. mov Temp1, A
  3580. mov A, Gov_Target_H
  3581. subb A, #0
  3582. mov Temp2, A
  3583. ENDIF
  3584. ; Check error and limit
  3585. jnc governor_check_prop_limit_pos ; Check carry
  3586. clr C
  3587. mov A, Temp1
  3588. subb A, #80h ; Is error too negative?
  3589. mov A, Temp2
  3590. subb A, #0FFh
  3591. jc governor_limit_prop_error_neg ; Yes - limit
  3592. jmp governor_store_prop_error
  3593. governor_check_prop_limit_pos:
  3594. clr C
  3595. mov A, Temp1
  3596. subb A, #7Fh ; Is error too positive?
  3597. mov A, Temp2
  3598. subb A, #00h
  3599. jnc governor_limit_prop_error_pos ; Yes - limit
  3600. jmp governor_store_prop_error
  3601. governor_limit_prop_error_pos:
  3602. mov Temp1, #7Fh ; Limit to max positive (2's complement)
  3603. mov Temp2, #00h
  3604. jmp governor_store_prop_error
  3605. governor_limit_prop_error_neg:
  3606. mov Temp1, #80h ; Limit to max negative (2's complement)
  3607. mov Temp2, #0FFh
  3608. governor_store_prop_error:
  3609. ; Store proportional
  3610. mov Gov_Proportional_L, Temp1
  3611. mov Gov_Proportional_H, Temp2
  3612. calc_governor_prop_error_exit:
  3613. ret
  3614. ; Third governor routine - calculate governor integral error
  3615. calc_governor_int_error:
  3616. ; Exit if governor is inactive
  3617. mov A, Gov_Active
  3618. jz calc_governor_int_error_exit
  3619. ; Add proportional to integral
  3620. mov A, Gov_Proportional_L
  3621. add A, Gov_Integral_L
  3622. mov Temp1, A
  3623. mov A, Gov_Proportional_H
  3624. addc A, Gov_Integral_H
  3625. mov Temp2, A
  3626. mov Bit_Access, Gov_Proportional_H ; Sign extend high byte
  3627. clr A
  3628. jnb Bit_Access.7, ($+4)
  3629. cpl A
  3630. addc A, Gov_Integral_X
  3631. mov Temp3, A
  3632. ; Check integral and limit
  3633. jnb ACC.7, governor_check_int_limit_pos ; Check sign bit
  3634. clr C
  3635. mov A, Temp3
  3636. subb A, #0F0h ; Is error too negative?
  3637. jc governor_limit_int_error_neg ; Yes - limit
  3638. jmp governor_check_pwm
  3639. governor_check_int_limit_pos:
  3640. clr C
  3641. mov A, Temp3
  3642. subb A, #0Fh ; Is error too positive?
  3643. jnc governor_limit_int_error_pos ; Yes - limit
  3644. jmp governor_check_pwm
  3645. governor_limit_int_error_pos:
  3646. mov Temp1, #0FFh ; Limit to max positive (2's complement)
  3647. mov Temp2, #0FFh
  3648. mov Temp3, #0Fh
  3649. jmp governor_check_pwm
  3650. governor_limit_int_error_neg:
  3651. mov Temp1, #00h ; Limit to max negative (2's complement)
  3652. mov Temp2, #00h
  3653. mov Temp3, #0F0h
  3654. governor_check_pwm:
  3655. ; Check current pwm
  3656. clr C
  3657. mov A, Current_Pwm
  3658. subb A, Pwm_Limit ; Is current pwm at or above pwm limit?
  3659. jnc governor_int_max_pwm ; Yes - branch
  3660. mov A, Current_Pwm ; Is current pwm at zero?
  3661. jz governor_int_min_pwm ; Yes - branch
  3662. ajmp governor_store_int_error ; No - store integral error
  3663. governor_int_max_pwm:
  3664. mov A, Gov_Proportional_H
  3665. jb ACC.7, calc_governor_int_error_exit ; Is proportional error negative - branch (high byte is always zero)
  3666. ajmp governor_store_int_error ; Positive - store integral error
  3667. governor_int_min_pwm:
  3668. mov A, Gov_Proportional_H
  3669. jnb ACC.7, calc_governor_int_error_exit ; Is proportional error positive - branch (high byte is always zero)
  3670. governor_store_int_error:
  3671. ; Store integral
  3672. mov Gov_Integral_L, Temp1
  3673. mov Gov_Integral_H, Temp2
  3674. mov Gov_Integral_X, Temp3
  3675. calc_governor_int_error_exit:
  3676. ret
  3677. ; Fourth governor routine - calculate governor proportional correction
  3678. calc_governor_prop_correction:
  3679. ; Exit if governor is inactive
  3680. mov A, Gov_Active
  3681. jnz calc_governor_prop_corr
  3682. jmp calc_governor_prop_corr_exit
  3683. calc_governor_prop_corr:
  3684. ; Load proportional gain
  3685. mov Temp1, #Pgm_Gov_P_Gain_Decoded; Load proportional gain
  3686. mov A, @Temp1
  3687. mov Temp3, A ; Store in Temp3
  3688. ; Load proportional
  3689. clr C
  3690. mov A, Gov_Proportional_L ; Nominal multiply by 2
  3691. rlc A
  3692. mov Temp1, A
  3693. mov A, Gov_Proportional_H
  3694. rlc A
  3695. mov Temp2, A
  3696. ; Apply gain
  3697. call mult_s16_by_u8_div_16
  3698. ; Check error and limit (to low byte)
  3699. mov A, Temp2
  3700. jnb ACC.7, governor_check_prop_corr_limit_pos ; Check sign bit
  3701. clr C
  3702. mov A, Temp1
  3703. subb A, #80h ; Is error too negative?
  3704. mov A, Temp2
  3705. subb A, #0FFh
  3706. jc governor_limit_prop_corr_neg ; Yes - limit
  3707. ajmp governor_apply_prop_corr
  3708. governor_check_prop_corr_limit_pos:
  3709. clr C
  3710. mov A, Temp1
  3711. subb A, #7Fh ; Is error too positive?
  3712. mov A, Temp2
  3713. subb A, #00h
  3714. jnc governor_limit_prop_corr_pos ; Yes - limit
  3715. ajmp governor_apply_prop_corr
  3716. governor_limit_prop_corr_pos:
  3717. mov Temp1, #7Fh ; Limit to max positive (2's complement)
  3718. mov Temp2, #00h
  3719. ajmp governor_apply_prop_corr
  3720. governor_limit_prop_corr_neg:
  3721. mov Temp1, #80h ; Limit to max negative (2's complement)
  3722. mov Temp2, #0FFh
  3723. governor_apply_prop_corr:
  3724. ; Test proportional sign
  3725. mov A, Temp1
  3726. jb ACC.7, governor_corr_neg_prop ; If proportional negative - go to correct negative
  3727. ; Subtract positive proportional
  3728. clr C
  3729. mov A, Governor_Req_Pwm
  3730. subb A, Temp1
  3731. mov Temp1, A
  3732. ; Check result
  3733. jc governor_corr_prop_min_pwm ; Is result negative?
  3734. clr C
  3735. mov A, Temp1 ; Is result below pwm min?
  3736. subb A, #1
  3737. jc governor_corr_prop_min_pwm ; Yes
  3738. jmp governor_store_prop_corr ; No - store proportional correction
  3739. governor_corr_prop_min_pwm:
  3740. mov Temp1, #1 ; Load minimum pwm
  3741. jmp governor_store_prop_corr
  3742. governor_corr_neg_prop:
  3743. ; Add negative proportional
  3744. mov A, Temp1
  3745. cpl A
  3746. add A, #1
  3747. add A, Governor_Req_Pwm
  3748. mov Temp1, A
  3749. ; Check result
  3750. jc governor_corr_prop_max_pwm ; Is result above max?
  3751. jmp governor_store_prop_corr ; No - store proportional correction
  3752. governor_corr_prop_max_pwm:
  3753. mov Temp1, #255 ; Load maximum pwm
  3754. governor_store_prop_corr:
  3755. ; Store proportional pwm
  3756. mov Gov_Prop_Pwm, Temp1
  3757. calc_governor_prop_corr_exit:
  3758. ret
  3759. ; Fifth governor routine - calculate governor integral correction
  3760. calc_governor_int_correction:
  3761. ; Exit if governor is inactive
  3762. mov A, Gov_Active
  3763. jnz calc_governor_int_corr
  3764. jmp calc_governor_int_corr_exit
  3765. calc_governor_int_corr:
  3766. ; Load integral gain
  3767. mov Temp1, #Pgm_Gov_I_Gain_Decoded; Load integral gain
  3768. mov A, @Temp1
  3769. mov Temp3, A ; Store in Temp3
  3770. ; Load integral
  3771. mov Temp1, Gov_Integral_H
  3772. mov Temp2, Gov_Integral_X
  3773. ; Apply gain
  3774. call mult_s16_by_u8_div_16
  3775. ; Check integral and limit
  3776. mov A, Temp2
  3777. jnb ACC.7, governor_check_int_corr_limit_pos ; Check sign bit
  3778. clr C
  3779. mov A, Temp1
  3780. subb A, #01h ; Is integral too negative?
  3781. mov A, Temp2
  3782. subb A, #0FFh
  3783. jc governor_limit_int_corr_neg ; Yes - limit
  3784. jmp governor_apply_int_corr
  3785. governor_check_int_corr_limit_pos:
  3786. clr C
  3787. mov A, Temp1
  3788. subb A, #0FFh ; Is integral too positive?
  3789. mov A, Temp2
  3790. subb A, #00h
  3791. jnc governor_limit_int_corr_pos ; Yes - limit
  3792. jmp governor_apply_int_corr
  3793. governor_limit_int_corr_pos:
  3794. mov Temp1, #0FFh ; Limit to max positive (2's complement)
  3795. mov Temp2, #00h
  3796. jmp governor_apply_int_corr
  3797. governor_limit_int_corr_neg:
  3798. mov Temp1, #01h ; Limit to max negative (2's complement)
  3799. mov Temp2, #0FFh
  3800. governor_apply_int_corr:
  3801. ; Test integral sign
  3802. mov A, Temp2
  3803. jb ACC.7, governor_corr_neg_int ; If integral negative - go to correct negative
  3804. ; Subtract positive integral
  3805. clr C
  3806. mov A, Gov_Prop_Pwm
  3807. subb A, Temp1
  3808. mov Temp1, A
  3809. ; Check result
  3810. jc governor_corr_int_min_pwm ; Is result negative?
  3811. clr C
  3812. mov A, Temp1 ; Is result below pwm min?
  3813. subb A, #1
  3814. jc governor_corr_int_min_pwm ; Yes
  3815. jmp governor_store_int_corr ; No - store correction
  3816. governor_corr_int_min_pwm:
  3817. mov Temp1, #0 ; Load minimum pwm
  3818. jmp governor_store_int_corr
  3819. governor_corr_neg_int:
  3820. ; Add negative integral
  3821. mov A, Temp1
  3822. cpl A
  3823. add A, #1
  3824. add A, Gov_Prop_Pwm
  3825. mov Temp1, A
  3826. ; Check result
  3827. jc governor_corr_int_max_pwm ; Is result above max?
  3828. jmp governor_store_int_corr ; No - store correction
  3829. governor_corr_int_max_pwm:
  3830. mov Temp1, #255 ; Load maximum pwm
  3831. governor_store_int_corr:
  3832. ; Store current pwm
  3833. mov Current_Pwm, Temp1
  3834. calc_governor_int_corr_exit:
  3835. ret
  3836. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3837. ;
  3838. ; Set pwm limit low rpm
  3839. ;
  3840. ; No assumptions
  3841. ;
  3842. ; Sets power limit for low rpms and disables demag for low rpms
  3843. ;
  3844. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3845. set_pwm_limit_low_rpm:
  3846. ; Set pwm limit and demag disable for low rpms
  3847. mov Temp1, #0FFh ; Default full power
  3848. clr Flags0.DEMAG_ENABLED ; Default disabled
  3849. mov A, Flags1
  3850. anl A, #((1 SHL STARTUP_PHASE)+(1 SHL INITIAL_RUN_PHASE))
  3851. jnz set_pwm_limit_low_rpm_exit ; Exit if any startup phase set
  3852. setb Flags0.DEMAG_ENABLED ; Enable demag
  3853. clr C
  3854. mov A, Comm_Period4x_H
  3855. subb A, #0Ah ; ~31250 eRPM
  3856. jc set_pwm_demag_done ; If speed above - branch
  3857. clr C
  3858. mov A, Current_Pwm_Limited
  3859. subb A, #40h ; Do not disable if pwm above 25%
  3860. jnc set_pwm_demag_done
  3861. clr Flags0.DEMAG_ENABLED ; Disable demag
  3862. set_pwm_demag_done:
  3863. mov A, Comm_Period4x_H
  3864. jz set_pwm_limit_low_rpm_exit ; Avoid divide by zero
  3865. mov A, #255 ; Divide 255 by Comm_Period4x_H
  3866. mov B, Comm_Period4x_H
  3867. div AB
  3868. mov B, Low_Rpm_Pwr_Slope ; Multiply by slope
  3869. mul AB
  3870. mov Temp1, A ; Set new limit
  3871. xch A, B
  3872. jz ($+4) ; Limit to max
  3873. mov Temp1, #0FFh
  3874. clr C
  3875. mov A, Temp1 ; Limit to min
  3876. subb A, Pwm_Spoolup_Beg
  3877. jnc set_pwm_limit_low_rpm_exit
  3878. mov Temp1, Pwm_Spoolup_Beg
  3879. set_pwm_limit_low_rpm_exit:
  3880. mov Pwm_Limit_Low_Rpm, Temp1
  3881. ret
  3882. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3883. ;
  3884. ; Measure lipo cells
  3885. ;
  3886. ; No assumptions
  3887. ;
  3888. ; Measure voltage and calculate lipo cells
  3889. ;
  3890. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  3891. measure_lipo_cells:
  3892. IF MODE == 1 ; Tail
  3893. ; If tail, then exit
  3894. jmp measure_lipo_exit
  3895. ENDIF
  3896. measure_lipo_start:
  3897. mov Lipo_Cell_Count, #0
  3898. ; Load programmed low voltage limit
  3899. mov Temp1, #Pgm_Low_Voltage_Lim ; Load limit
  3900. mov A, @Temp1
  3901. mov Bit_Access, A ; Store in Bit_Access
  3902. ; Set commutation to BpFET on
  3903. call comm5comm6
  3904. ; Start adc
  3905. Start_Adc
  3906. ; Wait for ADC reference to settle, and then start again
  3907. call wait1ms
  3908. Start_Adc
  3909. ; Wait for ADC conversion to complete
  3910. measure_lipo_wait_adc:
  3911. Get_Adc_Status
  3912. jb AD0BUSY, measure_lipo_wait_adc
  3913. ; Read ADC result
  3914. Read_Adc_Result
  3915. ; Stop ADC
  3916. Stop_Adc
  3917. ; Switch power off
  3918. call switch_power_off
  3919. ; Set limit step
  3920. mov Lipo_Adc_Limit_L, #ADC_LIMIT_L
  3921. mov Lipo_Adc_Limit_H, #ADC_LIMIT_H
  3922. clr C
  3923. mov A, #ADC_LIMIT_H ; Divide 2.8V value by 2
  3924. rrc A
  3925. mov Temp6, A
  3926. mov A, #ADC_LIMIT_L
  3927. rrc A
  3928. mov Temp5, A
  3929. mov A, #ADC_LIMIT_L ; Calculate 1.4*2.8V=4.2V value
  3930. add A, Temp5
  3931. add A, #3 ;4.3V 2015-02-10
  3932. mov Temp5, A
  3933. mov A, #ADC_LIMIT_H
  3934. addc A, Temp6
  3935. mov Temp6, A
  3936. mov A, Temp5 ; Copy step
  3937. mov Temp3, A
  3938. mov A, Temp6
  3939. mov Temp4, A
  3940. measure_lipo_cell_loop:
  3941. inc Lipo_Cell_Count ;2015-02-05
  3942. ; Check voltage against xS lower limit
  3943. clr C
  3944. mov A, Temp1
  3945. subb A, Temp3 ; Voltage above limit?
  3946. mov A, Temp2
  3947. subb A, Temp4
  3948. jc measure_lipo_adjust ; No - branch
  3949. ; Set xS voltage limit
  3950. mov A, Lipo_Adc_Limit_L
  3951. add A, #ADC_LIMIT_L
  3952. mov Lipo_Adc_Limit_L, A
  3953. mov A, Lipo_Adc_Limit_H
  3954. addc A, #ADC_LIMIT_H
  3955. mov Lipo_Adc_Limit_H, A
  3956. ; Set (x+1)S lower limit
  3957. mov A, Temp3
  3958. add A, Temp5 ; Add step
  3959. mov Temp3, A
  3960. mov A, Temp4
  3961. addc A, Temp6
  3962. mov Temp4, A
  3963. jmp measure_lipo_cell_loop ; Check for one more battery cell
  3964. measure_lipo_adjust:
  3965. mov Temp7, Lipo_Adc_Limit_L
  3966. mov Temp8, Lipo_Adc_Limit_H
  3967. ; Calculate 3.125%
  3968. clr C
  3969. mov A, Lipo_Adc_Limit_H
  3970. rrc A
  3971. mov Temp2, A
  3972. mov A, Lipo_Adc_Limit_L
  3973. rrc A
  3974. mov Temp1, A ; After this 50%
  3975. clr C
  3976. mov A, Temp2
  3977. rrc A
  3978. mov Temp2, A
  3979. mov A, Temp1
  3980. rrc A
  3981. mov Temp1, A ; After this 25%
  3982. mov A, Lipo_Adc_Limit_L ; Set adc reference for voltage compensation
  3983. add A, Temp1
  3984. mov Lipo_Adc_Reference_L, A
  3985. mov A, Lipo_Adc_Limit_H
  3986. addc A, Temp2
  3987. mov Lipo_Adc_Reference_H, A
  3988. ; Divide three times to get to 3.125%
  3989. mov Temp3, #2 ;2015-02-10
  3990. measure_lipo_divide_loop:
  3991. clr C
  3992. mov A, Temp2
  3993. rrc A
  3994. mov Temp2, A
  3995. mov A, Temp1
  3996. rrc A
  3997. mov Temp1, A
  3998. djnz Temp3, measure_lipo_divide_loop
  3999. ; Add the programmed number of 0.1V (or 3.125% increments)
  4000. mov Temp3, Bit_Access ; Load programmed limit (Bit_Access has Pgm_Low_Voltage_Lim)
  4001. dec Temp3
  4002. mov A, Temp3 ;2015-02-10
  4003. jnz measure_lipo_limit_on ; Is low voltage limiting on?
  4004. mov Lipo_Adc_Limit_L, #0 ; No - set limit to zero
  4005. mov Lipo_Adc_Limit_H, #0
  4006. jmp measure_lipo_exit
  4007. measure_lipo_limit_on:
  4008. dec Temp3
  4009. mov A, Temp3
  4010. jz measure_lipo_update
  4011. measure_lipo_add_loop:
  4012. mov A, Temp7 ; Add 3.125%
  4013. add A, Temp1
  4014. mov Temp7, A
  4015. mov A, Temp8
  4016. addc A, Temp2
  4017. mov Temp8, A
  4018. djnz Temp3, measure_lipo_add_loop
  4019. measure_lipo_update:
  4020. ; Set ADC limit
  4021. mov Lipo_Adc_Limit_L, Temp7
  4022. mov Lipo_Adc_Limit_H, Temp8
  4023. measure_lipo_exit:
  4024. ret
  4025. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4026. ;
  4027. ; Start ADC conversion
  4028. ;
  4029. ; No assumptions
  4030. ;
  4031. ; Start conversion used for measuring power supply voltage
  4032. ;
  4033. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4034. start_adc_conversion:
  4035. ; Start adc
  4036. Start_Adc
  4037. ret
  4038. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4039. ;
  4040. ; Check temperature, power supply voltage and limit power
  4041. ;
  4042. ; No assumptions
  4043. ;
  4044. ; Used to limit main motor power in order to maintain the required voltage
  4045. ;
  4046. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4047. check_temp_voltage_and_limit_power:
  4048. ; Load programmed low voltage limit
  4049. mov Temp1, #Pgm_Low_Voltage_Lim
  4050. mov A, @Temp1
  4051. mov Temp8, A ; Store in Temp8
  4052. ; Wait for ADC conversion to complete
  4053. Get_Adc_Status
  4054. jb AD0BUSY, check_temp_voltage_and_limit_power
  4055. ; Read ADC result
  4056. Read_Adc_Result
  4057. ; Stop ADC
  4058. Stop_Adc
  4059. inc Adc_Conversion_Cnt ; Increment conversion counter
  4060. clr C
  4061. mov A, Adc_Conversion_Cnt ; Is conversion count equal to temp rate?
  4062. subb A, #TEMP_CHECK_RATE
  4063. jc check_voltage_start ; No - check voltage
  4064. mov Adc_Conversion_Cnt, #0 ; Yes - temperature check. Reset counter
  4065. mov A, Temp2 ; Move ADC MSB to Temp3
  4066. mov Temp3, A
  4067. mov Temp2, #Pgm_Enable_Temp_Prot ; Is temp protection enabled?
  4068. mov A, @Temp2
  4069. jz temp_check_exit ; No - branch
  4070. mov A, Temp3 ; Is temperature reading below 256?
  4071. jnz temp_average_inc_dec ; No - proceed
  4072. mov A, Current_Average_Temp ; Yes - decrement average
  4073. jz temp_average_updated ; Already zero - no change
  4074. jmp temp_average_dec ; Decrement
  4075. temp_average_inc_dec:
  4076. clr C
  4077. mov A, Temp1 ; Check if current temperature is above or below average
  4078. subb A, Current_Average_Temp
  4079. jz temp_average_updated_load_acc ; Equal - no change
  4080. mov A, Current_Average_Temp ; Above - increment average
  4081. jnc temp_average_inc
  4082. jz temp_average_updated ; Below - decrement average if average is not already zero
  4083. temp_average_dec:
  4084. dec A ; Decrement average
  4085. jmp temp_average_updated
  4086. temp_average_inc:
  4087. inc A ; Increment average
  4088. jz temp_average_dec
  4089. jmp temp_average_updated
  4090. temp_average_updated_load_acc:
  4091. mov A, Current_Average_Temp
  4092. temp_average_updated:
  4093. mov Current_Average_Temp, A
  4094. clr C
  4095. subb A, #TEMP_LIMIT ; Is temperature below first limit?
  4096. jc temp_check_exit ; Yes - exit
  4097. mov Pwm_Limit, #192 ; No - limit pwm
  4098. clr C
  4099. subb A, #TEMP_LIMIT_STEP ; Is temperature below second limit
  4100. jc temp_check_exit ; Yes - exit
  4101. mov Pwm_Limit, #128 ; No - limit pwm
  4102. clr C
  4103. subb A, #TEMP_LIMIT_STEP ; Is temperature below third limit
  4104. jc temp_check_exit ; Yes - exit
  4105. mov Pwm_Limit, #64 ; No - limit pwm
  4106. clr C
  4107. subb A, #TEMP_LIMIT_STEP ; Is temperature below final limit
  4108. jc temp_check_exit ; Yes - exit
  4109. mov Pwm_Limit, #16 ; No - limit pwm 2015-02-06
  4110. temp_check_exit:
  4111. Set_Adc_Ip_Volt ; Select adc input for next conversion
  4112. ret
  4113. check_voltage_start:
  4114. IF MODE == 0 OR MODE == 2 ; Main or multi
  4115. ; Check if low voltage limiting is enabled
  4116. mov A, Temp8
  4117. clr C
  4118. subb A, #1 ; Is low voltage limit disabled?
  4119. jz check_voltage_good ; Yes - voltage declared good
  4120. ; Check if ADC is saturated
  4121. clr C
  4122. mov A, Temp1
  4123. subb A, #0FFh
  4124. mov A, Temp2
  4125. subb A, #03h
  4126. jnc check_voltage_good ; ADC saturated, can not make judgement
  4127. ; Check voltage against limit
  4128. clr C
  4129. mov A, Temp1
  4130. subb A, Lipo_Adc_Limit_L
  4131. mov A, Temp2
  4132. subb A, Lipo_Adc_Limit_H
  4133. jnc check_voltage_good ; If voltage above limit - branch
  4134. IF MODE == 0 ; Main
  4135. ; Decrease pwm limit
  4136. mov A, Pwm_Limit
  4137. jz check_voltage_lim ; If limit zero - branch
  4138. dec Pwm_Limit ; Decrement limit
  4139. jmp check_voltage_lim
  4140. ENDIF
  4141. IF MODE == 2 ; multi
  4142. mov Temp1, #Pgm_Low_Voltage_Ctl ;Read the control mode
  4143. mov A, @Temp1
  4144. clr C
  4145. subb A, #1 ;
  4146. jz check_voltage_next_way ;Is stop immediately? no - branch
  4147. mov A, Pwm_Limit
  4148. subb A, #20
  4149. jnc check_limit_count
  4150. setb Flags1.LOW_LIMIT_STOP ;set the flag of control mode 2
  4151. ljmp run_to_wait_for_power_on
  4152. check_voltage_next_way:
  4153. ; Decrease pwm limit
  4154. mov A, Pwm_Limit
  4155. subb A, #80 ;
  4156. jc check_voltage_lim ; If limit <80 - branch
  4157. check_limit_count:
  4158. mov A, Limit_Count
  4159. jz ($+6)
  4160. dec Limit_Count ; 2015-02-06
  4161. ajmp check_voltage_lim
  4162. mov Limit_Count, #5 ; 2015-02-06
  4163. dec Pwm_Limit ; Decrement limit
  4164. jmp check_voltage_lim
  4165. ENDIF
  4166. check_voltage_good:
  4167. ;restore the counter value
  4168. mov Limit_Count, #5 ; 2015-02-06
  4169. ; Increase pwm limit
  4170. mov A, Pwm_Limit
  4171. cpl A
  4172. jz check_voltage_lim ; If limit max - branch
  4173. inc Pwm_Limit ; Increment limit
  4174. IF MODE == 2 ; Multi
  4175. mov Temp1, #Pgm_Direction ; Check if bidirectional operation
  4176. mov A, @Temp1
  4177. cjne A, #3, check_voltage_lim
  4178. mov A, Pwm_Limit
  4179. add A, #4
  4180. jc check_voltage_lim ; If limit max - branch
  4181. mov Pwm_Limit, A ; Increment limit two steps more
  4182. ENDIF
  4183. check_voltage_lim:
  4184. mov Temp1, Pwm_Limit ; Set limit
  4185. clr C
  4186. mov A, Current_Pwm
  4187. subb A, Temp1
  4188. jnc check_voltage_spoolup_lim ; If current pwm above limit - branch and limit
  4189. mov Temp1, Current_Pwm ; Set current pwm (no limiting)
  4190. check_voltage_spoolup_lim:
  4191. ; Slow spoolup
  4192. clr C
  4193. mov A, Temp1
  4194. subb A, Pwm_Limit_Spoolup
  4195. jc check_voltage_exit ; If current pwm below limit - branch
  4196. mov Temp1, Pwm_Limit_Spoolup
  4197. mov A, Pwm_Limit_Spoolup ; Check if spoolup limit is max
  4198. cpl A
  4199. jz check_voltage_exit ; If max - branch
  4200. mov Pwm_Limit, Pwm_Limit_Spoolup ; Set pwm limit to spoolup limit during ramp (to avoid governor integral buildup)
  4201. check_voltage_exit:
  4202. IF MODE == 0 ; Main
  4203. mov Current_Pwm_Limited, Temp1
  4204. ENDIF
  4205. IF MODE == 2 ; Multi
  4206. ; Set current pwm limited if closed loop mode
  4207. mov Temp2, #Pgm_Gov_Mode ; Governor mode?
  4208. cjne @Temp2, #1, check_voltage_set_pwm ; Yes - branch
  4209. ajmp check_voltage_pwm_done
  4210. check_voltage_set_pwm:
  4211. ; Limit pwm for low rpms
  4212. clr C
  4213. mov A, Temp1 ; Check against limit
  4214. subb A, Pwm_Limit_Low_Rpm
  4215. jc ($+4) ; If current pwm below limit - branch
  4216. mov Temp1, Pwm_Limit_Low_Rpm ; Limit pwm
  4217. mov Current_Pwm_Limited, Temp1
  4218. check_voltage_pwm_done:
  4219. ENDIF
  4220. ENDIF
  4221. IF MODE == 1 ; Tail
  4222. ; Increase pwm limit
  4223. mov A, Pwm_Limit
  4224. cpl A
  4225. jz check_voltage_lim ; If limit max - branch
  4226. inc Pwm_Limit ; Increment limit
  4227. check_voltage_lim:
  4228. ENDIF
  4229. ; Set adc mux for next conversion
  4230. mov A, Adc_Conversion_Cnt ; Is next conversion for temperature?
  4231. cjne A, #(TEMP_CHECK_RATE-1), check_voltage_ret
  4232. Set_Adc_Ip_Temp ; Select temp sensor for next conversion
  4233. check_voltage_ret:
  4234. ret
  4235. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4236. ;
  4237. ; Set startup PWM routine
  4238. ;
  4239. ; Either the SETTLE_PHASE or the STEPPER_PHASE flag must be set
  4240. ;
  4241. ; Used for pwm control during startup
  4242. ;
  4243. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4244. set_startup_pwm:
  4245. ; Adjust startup power
  4246. mov A, #PWM_START ; Set power
  4247. mov Temp2, #Pgm_Startup_Pwr_Decoded
  4248. mov B, @Temp2
  4249. mul AB
  4250. xch A, B
  4251. mov C, B.7 ; Multiply result by 2 (unity gain is 128)
  4252. rlc A
  4253. mov Temp1, A ; Transfer to Temp1
  4254. clr C
  4255. mov A, Temp1 ; Check against limit
  4256. subb A, Pwm_Limit
  4257. jc startup_pwm_set_pwm ; If pwm below limit - branch
  4258. mov Temp1, Pwm_Limit ; Limit pwm
  4259. startup_pwm_set_pwm:
  4260. ; Set pwm variables
  4261. mov Requested_Pwm, Temp1 ; Update requested pwm
  4262. mov Current_Pwm, Temp1 ; Update current pwm
  4263. mov Current_Pwm_Limited, Temp1 ; Update limited version of current pwm
  4264. mov Pwm_Spoolup_Beg, Temp1 ; Yes - update spoolup beginning pwm (will use PWM_SETTLE or PWM_SETTLE/2)
  4265. ret
  4266. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4267. ;
  4268. ; Initialize all timings routine
  4269. ;
  4270. ; No assumptions
  4271. ;
  4272. ; Part of initialization before motor start
  4273. ;
  4274. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4275. initialize_all_timings:
  4276. mov Comm_Period4x_L, #00h ; Set commutation period registers
  4277. mov Comm_Period4x_H, #08h
  4278. ret
  4279. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4280. ;
  4281. ; Calculate next commutation timing routine
  4282. ;
  4283. ; No assumptions
  4284. ;
  4285. ; Called immediately after each commutation
  4286. ; Also sets up timer 3 to wait advance timing
  4287. ; Two entry points are used
  4288. ;
  4289. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4290. calc_next_comm_timing: ; Entry point for run phase
  4291. ; Read commutation time
  4292. mov TMR2CN, #20h ; Timer2 disabled
  4293. mov Temp1, TMR2L ; Load timer value
  4294. mov Temp2, TMR2H
  4295. mov TMR2CN, #24h ; Timer2 enabled
  4296. ; Calculate this commutation time
  4297. mov Temp3, Prev_Comm_L
  4298. mov Temp4, Prev_Comm_H
  4299. mov Prev_Comm_L, Temp1 ; Store timestamp as previous commutation
  4300. mov Prev_Comm_H, Temp2
  4301. clr C
  4302. mov A, Temp1
  4303. subb A, Temp3 ; Calculate the new commutation time
  4304. mov Temp1, A
  4305. mov A, Temp2
  4306. subb A, Temp4
  4307. mov Temp2, A
  4308. ; Calculate new commutation time
  4309. mov Temp3, Comm_Period4x_L ; Comm_Period4x(-l-h) holds the time of 4 commutations
  4310. mov Temp4, Comm_Period4x_H
  4311. mov Temp5, Comm_Period4x_L ; Copy variables
  4312. mov Temp6, Comm_Period4x_H
  4313. mov Temp7, #4 ; Divide Comm_Period4x 4 times as default
  4314. mov Temp8, #2 ; Divide new commutation time 2 times as default
  4315. clr C
  4316. mov A, Temp4
  4317. subb A, #04h
  4318. jc ($+4)
  4319. dec Temp7 ; Reduce averaging time constant for low speeds
  4320. dec Temp8
  4321. clr C
  4322. mov A, Temp4
  4323. subb A, #08h
  4324. jc ($+4)
  4325. dec Temp7 ; Reduce averaging time constant more for even lower speeds
  4326. dec Temp8
  4327. calc_next_comm_avg_period_div:
  4328. clr C
  4329. mov A, Temp6
  4330. rrc A ; Divide by 2
  4331. mov Temp6, A
  4332. mov A, Temp5
  4333. rrc A
  4334. mov Temp5, A
  4335. djnz Temp7, calc_next_comm_avg_period_div
  4336. clr C
  4337. mov A, Temp3
  4338. subb A, Temp5 ; Subtract a fraction
  4339. mov Temp3, A
  4340. mov A, Temp4
  4341. subb A, Temp6
  4342. mov Temp4, A
  4343. mov A, Temp8 ; Divide new time
  4344. jz calc_next_comm_new_period_div_done
  4345. calc_next_comm_new_period_div:
  4346. clr C
  4347. mov A, Temp2
  4348. rrc A ; Divide by 2
  4349. mov Temp2, A
  4350. mov A, Temp1
  4351. rrc A
  4352. mov Temp1, A
  4353. djnz Temp8, calc_next_comm_new_period_div
  4354. calc_next_comm_new_period_div_done:
  4355. mov A, Temp3
  4356. add A, Temp1 ; Add the divided new time
  4357. mov Temp3, A
  4358. mov A, Temp4
  4359. addc A, Temp2
  4360. mov Temp4, A
  4361. mov Comm_Period4x_L, Temp3 ; Store Comm_Period4x_X
  4362. mov Comm_Period4x_H, Temp4
  4363. jc calc_next_comm_slow ; If period larger than 0xffff - go to slow case
  4364. ret
  4365. calc_next_comm_slow:
  4366. mov Comm_Period4x_L, #0FFh ; Set commutation period registers to very slow timing (0xffff)
  4367. mov Comm_Period4x_H, #0FFh
  4368. ret
  4369. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4370. ;
  4371. ; Wait advance timing routine
  4372. ;
  4373. ; No assumptions
  4374. ;
  4375. ; Waits for the advance timing to elapse and sets up the next zero cross wait
  4376. ;
  4377. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4378. wait_advance_timing:
  4379. jnb Flags0.T3_PENDING, ($+5)
  4380. ajmp wait_advance_timing
  4381. ; Setup next wait time
  4382. mov Next_Wt_L, Wt_ZC_Timeout_L
  4383. mov Next_Wt_H, Wt_ZC_Timeout_H
  4384. setb Flags0.T3_PENDING
  4385. orl EIE1, #80h ; Enable timer3 interrupts
  4386. ret
  4387. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4388. ;
  4389. ; Calculate new wait times routine
  4390. ;
  4391. ; No assumptions
  4392. ;
  4393. ; Calculates new wait times
  4394. ;
  4395. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4396. calc_new_wait_times:
  4397. ; Load programmed commutation timing
  4398. mov Temp1, #Pgm_Comm_Timing ; Load timing setting
  4399. mov A, @Temp1
  4400. mov Temp8, A ; Store in Temp8
  4401. clr C
  4402. mov A, Demag_Detected_Metric ; Check demag metric
  4403. subb A, #130
  4404. jc ($+3)
  4405. inc Temp8 ; Increase timing
  4406. clr C
  4407. mov A, Demag_Detected_Metric
  4408. subb A, #160
  4409. jc ($+3)
  4410. inc Temp8 ; Increase timing again
  4411. clr C
  4412. mov A, Temp8 ; Limit timing to max
  4413. subb A, #6
  4414. jc ($+4)
  4415. mov Temp8, #5 ; Set timing to max
  4416. mov Temp7, #(COMM_TIME_RED SHL 1)
  4417. jnb Flags2.PGM_PWMOFF_DAMPED, ($+4) ; More reduction for damped
  4418. inc Temp7 ; Increase more
  4419. clr C
  4420. mov A, Comm_Period4x_H ; More reduction for higher rpms
  4421. subb A, #3 ; 104k eRPM
  4422. jnc calc_new_wait_per_low
  4423. inc Temp7 ; Increase
  4424. inc Temp7
  4425. jnb Flags2.PGM_PWMOFF_DAMPED, ($+5) ; More reduction for damped
  4426. inc Temp7 ; Increase more
  4427. inc Temp7
  4428. calc_new_wait_per_low:
  4429. clr C
  4430. mov A, Comm_Period4x_H ; More reduction for higher rpms
  4431. subb A, #2 ; 156k eRPM
  4432. jnc calc_new_wait_per_high
  4433. inc Temp7 ; Increase more
  4434. inc Temp7
  4435. jnb Flags2.PGM_PWMOFF_DAMPED, ($+5) ; More reduction for damped
  4436. inc Temp7 ; Increase more
  4437. inc Temp7
  4438. calc_new_wait_per_high:
  4439. ; Load current commutation timing
  4440. mov Temp2, Comm_Period4x_H ; Load Comm_Period4x
  4441. mov Temp1, Comm_Period4x_L
  4442. mov Temp3, #4 ; Divide 4 times
  4443. divide_wait_times:
  4444. clr C
  4445. mov A, Temp2
  4446. rrc A ; Divide by 2
  4447. mov Temp2, A
  4448. mov A, Temp1
  4449. rrc A
  4450. mov Temp1, A
  4451. djnz Temp3, divide_wait_times
  4452. clr C
  4453. mov A, Temp1
  4454. subb A, Temp7
  4455. mov Temp1, A
  4456. mov A, Temp2
  4457. subb A, #0
  4458. mov Temp2, A
  4459. jc load_min_time ; Check that result is still positive
  4460. clr C
  4461. mov A, Temp1
  4462. subb A, #(COMM_TIME_MIN SHL 1)
  4463. mov A, Temp2
  4464. subb A, #0
  4465. jnc adjust_timing ; Check that result is still above minumum
  4466. load_min_time:
  4467. mov Temp1, #(COMM_TIME_MIN SHL 1)
  4468. clr A
  4469. mov Temp2, A
  4470. adjust_timing:
  4471. mov A, Temp2 ; Copy values
  4472. mov Temp4, A
  4473. mov A, Temp1
  4474. mov Temp3, A
  4475. clr C
  4476. mov A, Temp2
  4477. rrc A ; Divide by 2
  4478. mov Temp6, A
  4479. mov A, Temp1
  4480. rrc A
  4481. mov Temp5, A
  4482. mov Wt_Zc_Timeout_L, Temp1 ; Set 15deg time for zero cross scan timeout
  4483. mov Wt_Zc_Timeout_H, Temp2
  4484. clr C
  4485. mov A, Temp8 ; (Temp8 has Pgm_Comm_Timing)
  4486. subb A, #3 ; Is timing normal?
  4487. jz store_times_decrease ; Yes - branch
  4488. mov A, Temp8
  4489. jb ACC.0, adjust_timing_two_steps ; If an odd number - branch
  4490. mov A, Temp1 ; Add 7.5deg and store in Temp1/2
  4491. add A, Temp5
  4492. mov Temp1, A
  4493. mov A, Temp2
  4494. addc A, Temp6
  4495. mov Temp2, A
  4496. mov A, Temp5 ; Store 7.5deg in Temp3/4
  4497. mov Temp3, A
  4498. mov A, Temp6
  4499. mov Temp4, A
  4500. jmp store_times_up_or_down
  4501. adjust_timing_two_steps:
  4502. mov A, Temp1 ; Add 15deg and store in Temp1/2
  4503. add A, Temp1
  4504. mov Temp1, A
  4505. mov A, Temp2
  4506. addc A, Temp2
  4507. mov Temp2, A
  4508. clr C
  4509. mov A, Temp1
  4510. subb A, #(COMM_TIME_MIN SHL 1)
  4511. mov Temp1, A
  4512. mov A, Temp2
  4513. subb A, #0
  4514. mov Temp2, A
  4515. mov Temp3, #(COMM_TIME_MIN SHL 1) ; Store minimum time in Temp3/4
  4516. clr A
  4517. mov Temp4, A
  4518. store_times_up_or_down:
  4519. clr C
  4520. mov A, Temp8
  4521. subb A, #3 ; Is timing higher than normal?
  4522. jc store_times_decrease ; No - branch
  4523. store_times_increase:
  4524. mov Wt_Comm_L, Temp3 ; Now commutation time (~60deg) divided by 4 (~15deg nominal)
  4525. mov Wt_Comm_H, Temp4
  4526. mov Wt_Advance_L, Temp1 ; New commutation advance time (~15deg nominal)
  4527. mov Wt_Advance_H, Temp2
  4528. mov Wt_Zc_Scan_L, Temp5 ; Use this value for zero cross scan delay (7.5deg)
  4529. mov Wt_Zc_Scan_H, Temp6
  4530. ret
  4531. store_times_decrease:
  4532. mov Wt_Comm_L, Temp1 ; Now commutation time (~60deg) divided by 4 (~15deg nominal)
  4533. mov Wt_Comm_H, Temp2
  4534. mov Wt_Advance_L, Temp3 ; New commutation advance time (~15deg nominal)
  4535. mov Wt_Advance_H, Temp4
  4536. mov Wt_Zc_Scan_L, Temp5 ; Use this value for zero cross scan delay (7.5deg)
  4537. mov Wt_Zc_Scan_H, Temp6
  4538. ret
  4539. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4540. ;
  4541. ; Wait before zero cross scan routine
  4542. ;
  4543. ; No assumptions
  4544. ;
  4545. ; Waits for the zero cross scan wait time to elapse
  4546. ; Also sets up timer 3 for the zero cross scan timeout time
  4547. ;
  4548. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4549. wait_before_zc_scan:
  4550. jnb Flags0.T3_PENDING, ($+5)
  4551. ajmp wait_before_zc_scan
  4552. setb Flags0.T3_PENDING
  4553. orl EIE1, #80h ; Enable timer3 interrupts
  4554. mov A, Flags1
  4555. anl A, #((1 SHL STARTUP_PHASE)+(1 SHL INITIAL_RUN_PHASE))
  4556. jz wait_before_zc_exit
  4557. mov Temp1, Comm_Period4x_L ; Set long timeout when starting
  4558. mov Temp2, Comm_Period4x_H
  4559. mov TMR3CN, #00h ; Timer3 disabled
  4560. clr C
  4561. clr A
  4562. subb A, Temp1 ; Set timeout
  4563. mov TMR3L, A
  4564. clr A
  4565. subb A, Temp2
  4566. mov TMR3H, A
  4567. mov TMR3CN, #04h ; Timer3 enabled
  4568. setb Flags0.T3_PENDING
  4569. anl TMR3CN, #07Fh ; Clear interrupt flag
  4570. orl EIE1, #80h ; Enable timer3 interrupts
  4571. wait_before_zc_exit:
  4572. ret
  4573. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4574. ;
  4575. ; Wait for comparator to go low/high routines
  4576. ;
  4577. ; No assumptions
  4578. ;
  4579. ; Waits for the zero cross scan wait time to elapse
  4580. ; Then scans for comparator going low/high
  4581. ;
  4582. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4583. wait_for_comp_out_low:
  4584. setb Flags0.DEMAG_DETECTED ; Set demag detected flag as default
  4585. mov Comparator_Read_Cnt, #0 ; Reset number of comparator reads
  4586. mov Bit_Access, #00h ; Desired comparator output
  4587. jmp wait_for_comp_out_start
  4588. wait_for_comp_out_high:
  4589. setb Flags0.DEMAG_DETECTED ; Set demag detected flag as default
  4590. mov Comparator_Read_Cnt, #0 ; Reset number of comparator reads
  4591. mov Bit_Access, #40h ; Desired comparator output
  4592. wait_for_comp_out_start:
  4593. mov A, Flags1 ; Clear demag detected flag if start phases
  4594. anl A, #((1 SHL STARTUP_PHASE)+(1 SHL INITIAL_RUN_PHASE))
  4595. jz ($+4)
  4596. clr Flags0.DEMAG_DETECTED
  4597. setb EA ; Enable interrupts
  4598. jb Flags0.T3_PENDING, wait_for_comp_out_not_timed_out; Has zero cross scan timeout elapsed?
  4599. mov A, Comparator_Read_Cnt ; Check that comparator has been read
  4600. jz wait_for_comp_out_not_timed_out ; If not read - branch
  4601. ret ; Return
  4602. wait_for_comp_out_not_timed_out:
  4603. ; Set default comparator response times
  4604. mov CPT0MD, #0 ; Set fast response (100ns) as default
  4605. IF COMP1_USED==1
  4606. mov CPT1MD, #0 ; Set fast response (100ns) as default
  4607. ENDIF
  4608. ; Set number of comparator readings
  4609. mov Temp1, #1
  4610. mov Temp3, #2
  4611. clr C ; Set number of readings higher for lower speeds
  4612. mov A, Comm_Period4x_H
  4613. subb A, #05h
  4614. jc ($+4)
  4615. mov Temp1, #2
  4616. clr C
  4617. mov A, Comm_Period4x_H
  4618. subb A, #0Ah
  4619. jc ($+4)
  4620. mov Temp1, #3
  4621. clr C ; Set number of consecutive readings higher for lower speeds
  4622. mov A, Comm_Period4x_H
  4623. subb A, #0Fh
  4624. jc ($+4)
  4625. mov Temp3, #3
  4626. jnb Flags1.STARTUP_PHASE, comp_wait_on_comp_able ; Set many samples during startup
  4627. mov Temp1, #15 ;2015.03.28
  4628. mov Temp3, #1 ;2015.03.28
  4629. mov CPT0MD, #3 ; Set slow response (1000ns)
  4630. IF COMP1_USED==1
  4631. mov CPT1MD, #3 ; Set slow response (1000ns)
  4632. ENDIF
  4633. comp_wait_on_comp_able:
  4634. jb Flags0.T3_PENDING, comp_wait_on_comp_able_not_timed_out ; Has zero cross scan timeout elapsed?
  4635. mov A, Comparator_Read_Cnt ; Check that comparator has been read
  4636. jz comp_wait_on_comp_able_not_timed_out ; If not read - branch
  4637. setb EA ; Enable interrupts
  4638. ret ; Yes - return
  4639. comp_wait_on_comp_able_not_timed_out:
  4640. setb EA ; Enable interrupts
  4641. nop ; Allocate only just enough time to capture interrupt
  4642. nop
  4643. clr EA ; Disable interrupts
  4644. clr C
  4645. mov A, Comm_Period4x_H ; Reduce required distance to pwm transition for higher speeds
  4646. mov Temp4, A
  4647. subb A, #0Fh
  4648. jc ($+4)
  4649. mov Temp4, #0Fh
  4650. mov A, Temp4
  4651. inc A
  4652. jnb Flags2.PGM_PWM_HIGH_FREQ, ($+4) ; More delay for high pwm frequency
  4653. rl A
  4654. jb Flags0.PWM_ON, ($+4) ; More delay for pwm off
  4655. rl A
  4656. mov Temp2, A
  4657. jnb Flags1.STARTUP_PHASE, ($+5) ; Set a long delay from pwm on/off events during startup
  4658. mov Temp2, #100 ;2015.03.28
  4659. clr C
  4660. mov A, TL1
  4661. subb A, Temp2
  4662. jc comp_wait_on_comp_able ; Re-evaluate pwm cycle
  4663. inc Comparator_Read_Cnt ; Increment comparator read count
  4664. mov A, Temp3
  4665. mov Temp4, A
  4666. read_comp_loop:
  4667. Read_Comp_Out ; Read comparator output
  4668. anl A, #40h
  4669. cjne A, Bit_Access, comp_read_wrong
  4670. djnz Temp4, read_comp_loop ; Decrement readings count
  4671. ajmp comp_read_ok
  4672. comp_read_wrong:
  4673. jb Flags0.DEMAG_DETECTED, ($+5)
  4674. ajmp wait_for_comp_out_start ; If comparator output is not correct, and timeout already extended - go back and restart
  4675. clr Flags0.DEMAG_DETECTED ; Clear demag detected flag
  4676. mov TMR3CN, #00h ; Timer3 disabled
  4677. clr C
  4678. clr A
  4679. subb A, Comm_Period4x_L ; Set timeout to zero comm period 4x value
  4680. mov TMR3L, A
  4681. clr A
  4682. subb A, Comm_Period4x_H
  4683. mov TMR3H, A
  4684. mov TMR3CN, #04h ; Timer3 enabled
  4685. setb Flags0.T3_PENDING
  4686. anl TMR3CN, #07Fh ; Clear interrupt flag in case there are pending interrupts
  4687. orl EIE1, #80h ; Enable timer3 interrupts
  4688. ajmp wait_for_comp_out_start ; If comparator output is not correct - go back and restart
  4689. comp_read_ok:
  4690. jnb Flags0.DEMAG_DETECTED, ($+5) ; Do not accept correct comparator output if it is demag
  4691. ajmp wait_for_comp_out_start
  4692. djnz Temp1, comp_wait_on_comp_able ; Decrement readings counter - repeat comparator reading if not zero
  4693. setb EA ; Enable interrupts
  4694. ret
  4695. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4696. ;
  4697. ; Evaluate comparator integrity
  4698. ;
  4699. ; No assumptions
  4700. ;
  4701. ; Checks comparator signal behaviour versus expected behaviour
  4702. ;
  4703. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4704. evaluate_comparator_integrity:
  4705. jnb Flags1.STARTUP_PHASE, eval_comp_check_timeout
  4706. inc Startup_Ok_Cnt ; Increment ok counter
  4707. jb Flags0.T3_PENDING, eval_comp_exit
  4708. mov Startup_Ok_Cnt, #0 ; Reset ok counter
  4709. jmp eval_comp_exit
  4710. eval_comp_check_timeout:
  4711. jb Flags0.T3_PENDING, eval_comp_exit ; Has timeout elapsed?
  4712. jb Flags0.DEMAG_DETECTED, eval_comp_exit ; Do not exit run mode if it is a demag situation
  4713. jb Flags0.DIR_CHANGE_BRAKE, eval_comp_exit ; Do not exit run mode if it is a direction change brake
  4714. dec SP ; Routine exit without "ret" command
  4715. dec SP
  4716. ljmp run_to_wait_for_power_on ; Yes - exit run mode
  4717. eval_comp_exit:
  4718. ret
  4719. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4720. ;
  4721. ; Setup commutation timing routine
  4722. ;
  4723. ; No assumptions
  4724. ;
  4725. ; Sets up and starts wait from commutation to zero cross
  4726. ;
  4727. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4728. setup_comm_wait:
  4729. mov TMR3CN, #00h ; Timer3 disabled
  4730. anl TMR3CN, #07Fh ; Clear interrupt flag
  4731. clr C
  4732. clr A
  4733. subb A, Wt_Comm_L ; Set wait commutation value
  4734. mov TMR3L, A
  4735. clr A
  4736. subb A, Wt_Comm_H
  4737. mov TMR3H, A
  4738. mov TMR3CN, #04h ; Timer3 enabled
  4739. ; Setup next wait time
  4740. mov Next_Wt_L, Wt_Advance_L
  4741. mov Next_Wt_H, Wt_Advance_H
  4742. setb Flags0.T3_PENDING
  4743. orl EIE1, #80h ; Enable timer3 interrupts
  4744. ret
  4745. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4746. ;
  4747. ; Wait for commutation routine
  4748. ;
  4749. ; No assumptions
  4750. ;
  4751. ; Waits from zero cross to commutation
  4752. ;
  4753. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4754. wait_for_comm:
  4755. ; Update demag metric
  4756. mov Temp1, #0
  4757. jnb Flags0.DEMAG_ENABLED, ($+8); If demag disabled - branch
  4758. jnb Flags0.DEMAG_DETECTED, ($+5)
  4759. mov Temp1, #1
  4760. mov A, Demag_Detected_Metric ; Sliding average of 8, 256 when demag and 0 when not. Limited to minimum 120
  4761. mov B, #7
  4762. mul AB ; Multiply by 7
  4763. mov Temp2, A
  4764. mov A, B ; Add new value for current demag status
  4765. add A, Temp1
  4766. mov B, A
  4767. mov A, Temp2
  4768. mov C, B.0 ; Divide by 8
  4769. rrc A
  4770. mov C, B.1
  4771. rrc A
  4772. mov C, B.2
  4773. rrc A
  4774. mov Demag_Detected_Metric, A
  4775. clr C
  4776. subb A, #120 ; Limit to minimum 120
  4777. jnc ($+5)
  4778. mov Demag_Detected_Metric, #120
  4779. clr C
  4780. mov A, Demag_Detected_Metric ; Check demag metric
  4781. subb A, Demag_Pwr_Off_Thresh
  4782. jc wait_for_comm_wait ; Cut power if many consecutive demags. This will help retain sync during hard accelerations
  4783. setb Flags0.DEMAG_CUT_POWER ; Set demag power cut flag
  4784. All_nFETs_off
  4785. wait_for_comm_wait:
  4786. jnb Flags0.T3_PENDING, ($+5)
  4787. ajmp wait_for_comm_wait
  4788. ; Setup next wait time
  4789. mov Next_Wt_L, Wt_Zc_Scan_L
  4790. mov Next_Wt_H, Wt_Zc_Scan_H
  4791. setb Flags0.T3_PENDING
  4792. orl EIE1, #80h ; Enable timer3 interrupts
  4793. ret
  4794. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4795. ;
  4796. ; Commutation routines
  4797. ;
  4798. ; No assumptions
  4799. ;
  4800. ; Performs commutation switching
  4801. ; Damped routines uses all pfets on when in pwm off to dampen the motor
  4802. ;
  4803. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4804. comm1comm2:
  4805. Set_RPM_Out
  4806. clr EA ; Disable all interrupts
  4807. All_pFETs_off ; All pfets off
  4808. jnb Flags2.PGM_PWMOFF_DAMPED, comm12_nondamp
  4809. mov DPTR, #pwm_cnfet_apfet_on
  4810. mov A, #NFETON_DELAY ; Delay
  4811. djnz ACC, $
  4812. jmp comm12_prech_done ; Do not do precharge when running damped
  4813. comm12_nondamp:
  4814. IF HIGH_DRIVER_PRECHG_TIME NE 0 ; Precharge high side gate driver
  4815. mov A, Comm_Period4x_H
  4816. anl A, #0F8h ; Check if comm period is less than 8
  4817. jz comm12_prech_done
  4818. AnFET_on
  4819. mov A, #HIGH_DRIVER_PRECHG_TIME
  4820. djnz ACC, $
  4821. AnFET_off
  4822. mov A, #PFETON_DELAY
  4823. djnz ACC, $
  4824. ENDIF
  4825. comm12_prech_done:
  4826. ApFET_on ; Ap on
  4827. Set_Comp_Phase_B ; Set comparator to phase B
  4828. mov Comm_Phase, #2
  4829. jmp comm_exit
  4830. comm2comm3:
  4831. Clear_RPM_Out
  4832. clr EA ; Disable all interrupts
  4833. CnFET_off ; Cn off
  4834. jnb Flags2.PGM_PWMOFF_DAMPED, comm23_nondamp
  4835. mov DPTR, #pwm_bnfet_apfet_on
  4836. BpFET_off
  4837. CpFET_off
  4838. mov A, #NFETON_DELAY ; Delay
  4839. djnz ACC, $
  4840. jmp comm23_nfet
  4841. comm23_nondamp:
  4842. mov DPTR, #pwm_bfet_on
  4843. comm23_nfet:
  4844. jnb Flags0.PWM_ON, comm23_cp ; Is pwm on?
  4845. BnFET_on ; Yes - Bn on
  4846. comm23_cp:
  4847. Set_Comp_Phase_C ; Set comparator to phase C
  4848. mov Comm_Phase, #3
  4849. jmp comm_exit
  4850. comm3comm4:
  4851. clr EA ; Disable all interrupts
  4852. All_pFETs_off ; All pfets off
  4853. jnb Flags2.PGM_PWMOFF_DAMPED, comm34_nondamp
  4854. mov DPTR, #pwm_bnfet_cpfet_on
  4855. mov A, #NFETON_DELAY ; Delay
  4856. djnz ACC, $
  4857. jmp comm34_prech_done ; Do not do precharge when running damped
  4858. comm34_nondamp:
  4859. IF HIGH_DRIVER_PRECHG_TIME NE 0 ; Precharge high side gate driver
  4860. mov A, Comm_Period4x_H
  4861. anl A, #0F8h ; Check if comm period is less than 8
  4862. jz comm34_prech_done
  4863. CnFET_on
  4864. mov A, #HIGH_DRIVER_PRECHG_TIME
  4865. djnz ACC, $
  4866. CnFET_off
  4867. mov A, #PFETON_DELAY
  4868. djnz ACC, $
  4869. ENDIF
  4870. comm34_prech_done:
  4871. CpFET_on ; Cp on
  4872. Set_Comp_Phase_A ; Set comparator to phase A
  4873. mov Comm_Phase, #4
  4874. jmp comm_exit
  4875. comm4comm5:
  4876. clr EA ; Disable all interrupts
  4877. BnFET_off ; Bn off
  4878. jnb Flags2.PGM_PWMOFF_DAMPED, comm45_nondamp
  4879. mov DPTR, #pwm_anfet_cpfet_on
  4880. ApFET_off
  4881. BpFET_off
  4882. mov A, #NFETON_DELAY ; Delay
  4883. djnz ACC, $
  4884. jmp comm45_nfet
  4885. comm45_nondamp:
  4886. mov DPTR, #pwm_afet_on
  4887. comm45_nfet:
  4888. jnb Flags0.PWM_ON, comm45_cp ; Is pwm on?
  4889. AnFET_on ; Yes - An on
  4890. comm45_cp:
  4891. Set_Comp_Phase_B ; Set comparator to phase B
  4892. mov Comm_Phase, #5
  4893. jmp comm_exit
  4894. comm5comm6:
  4895. clr EA ; Disable all interrupts
  4896. All_pFETs_off ; All pfets off
  4897. jnb Flags2.PGM_PWMOFF_DAMPED, comm56_nondamp
  4898. mov DPTR, #pwm_anfet_bpfet_on
  4899. mov A, #NFETON_DELAY ; Delay
  4900. djnz ACC, $
  4901. jmp comm56_prech_done ; Do not do precharge when running damped
  4902. comm56_nondamp:
  4903. IF HIGH_DRIVER_PRECHG_TIME NE 0 ; Precharge high side gate driver
  4904. mov A, Comm_Period4x_H
  4905. anl A, #0F8h ; Check if comm period is less than 8
  4906. jz comm56_prech_done
  4907. BnFET_on
  4908. mov A, #HIGH_DRIVER_PRECHG_TIME
  4909. djnz ACC, $
  4910. BnFET_off
  4911. mov A, #PFETON_DELAY
  4912. djnz ACC, $
  4913. ENDIF
  4914. comm56_prech_done:
  4915. BpFET_on ; Bp on
  4916. Set_Comp_Phase_C ; Set comparator to phase C
  4917. mov Comm_Phase, #6
  4918. jmp comm_exit
  4919. comm6comm1:
  4920. clr EA ; Disable all interrupts
  4921. AnFET_off ; An off
  4922. jnb Flags2.PGM_PWMOFF_DAMPED, comm61_nondamp
  4923. mov DPTR, #pwm_cnfet_bpfet_on
  4924. ApFET_off
  4925. CpFET_off
  4926. mov A, #NFETON_DELAY ; Delay
  4927. djnz ACC, $
  4928. jmp comm61_nfet
  4929. comm61_nondamp:
  4930. mov DPTR, #pwm_cfet_on
  4931. comm61_nfet:
  4932. jnb Flags0.PWM_ON, comm61_cp ; Is pwm on?
  4933. CnFET_on ; Yes - Cn on
  4934. comm61_cp:
  4935. Set_Comp_Phase_A ; Set comparator to phase A
  4936. mov Comm_Phase, #1
  4937. comm_exit:
  4938. IF MODE >= 1 ; Tail or multi
  4939. jnb Flags0.DIR_CHANGE_BRAKE, comm_dir_change_done ; Is it a direction change?
  4940. call switch_power_off ; Switch off power
  4941. mov A, #NFETON_DELAY ; Delay
  4942. djnz ACC, $
  4943. mov A, #PFETON_DELAY ; Delay
  4944. djnz ACC, $
  4945. All_pFETs_on ; All pfets on - Break
  4946. comm_dir_change_done:
  4947. ENDIF
  4948. clr Flags0.DEMAG_CUT_POWER ; Clear demag power cut flag
  4949. setb EA ; Enable all interrupts
  4950. ret
  4951. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4952. ;
  4953. ; Switch power off routine
  4954. ;
  4955. ; No assumptions
  4956. ;
  4957. ; Switches all fets off
  4958. ;
  4959. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4960. switch_power_off:
  4961. mov DPTR, #pwm_nofet_on ; Set DPTR register to pwm_nofet_on label
  4962. All_nFETs_Off ; Turn off all nfets
  4963. All_pFETs_Off ; Turn off all pfets
  4964. clr Flags0.PWM_ON ; Set pwm cycle to pwm off
  4965. ret
  4966. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4967. ;
  4968. ; Set default parameters
  4969. ;
  4970. ; No assumptions
  4971. ;
  4972. ; Sets default programming parameters
  4973. ;
  4974. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  4975. set_default_parameters:
  4976. IF MODE == 0 ; Main
  4977. mov Temp1, #Pgm_Gov_P_Gain
  4978. mov @Temp1, #DEFAULT_PGM_MAIN_P_GAIN
  4979. inc Temp1
  4980. mov @Temp1, #DEFAULT_PGM_MAIN_I_GAIN
  4981. inc Temp1
  4982. mov @Temp1, #DEFAULT_PGM_MAIN_GOVERNOR_MODE
  4983. inc Temp1
  4984. mov @Temp1, #DEFAULT_PGM_MAIN_LOW_VOLTAGE_LIM
  4985. inc Temp1
  4986. mov @Temp1, #0FFh ; Motor gain
  4987. inc Temp1
  4988. mov @Temp1, #0FFh ; Motor idle
  4989. inc Temp1
  4990. mov @Temp1, #DEFAULT_PGM_MAIN_STARTUP_PWR
  4991. inc Temp1
  4992. mov @Temp1, #DEFAULT_PGM_MAIN_PWM_FREQ
  4993. inc Temp1
  4994. mov @Temp1, #DEFAULT_PGM_MAIN_DIRECTION
  4995. inc Temp1
  4996. mov @Temp1, #DEFAULT_PGM_MAIN_RCP_PWM_POL
  4997. mov Temp1, #Pgm_Enable_TX_Program
  4998. mov @Temp1, #DEFAULT_PGM_ENABLE_TX_PROGRAM
  4999. inc Temp1
  5000. mov @Temp1, #DEFAULT_PGM_MAIN_REARM_START
  5001. inc Temp1
  5002. mov @Temp1, #DEFAULT_PGM_MAIN_GOV_SETUP_TARGET
  5003. inc Temp1
  5004. mov @Temp1, #0FFh ; Startup rpm
  5005. inc Temp1
  5006. mov @Temp1, #0FFh ; Startup accel
  5007. inc Temp1
  5008. mov @Temp1, #0FFh ; Voltage comp
  5009. inc Temp1
  5010. mov @Temp1, #DEFAULT_PGM_MAIN_COMM_TIMING
  5011. inc Temp1
  5012. mov @Temp1, #0FFh ; Damping force
  5013. inc Temp1
  5014. mov @Temp1, #DEFAULT_PGM_MAIN_GOVERNOR_RANGE
  5015. inc Temp1
  5016. mov @Temp1, #0FFh ; Startup method
  5017. inc Temp1
  5018. mov @Temp1, #DEFAULT_PGM_PPM_MIN_THROTTLE
  5019. inc Temp1
  5020. mov @Temp1, #DEFAULT_PGM_PPM_MAX_THROTTLE
  5021. inc Temp1
  5022. mov @Temp1, #DEFAULT_PGM_MAIN_BEEP_STRENGTH
  5023. inc Temp1
  5024. mov @Temp1, #DEFAULT_PGM_MAIN_BEACON_STRENGTH
  5025. inc Temp1
  5026. mov @Temp1, #DEFAULT_PGM_MAIN_BEACON_DELAY
  5027. inc Temp1
  5028. mov @Temp1, #0FFh ; Throttle rate
  5029. inc Temp1
  5030. mov @Temp1, #DEFAULT_PGM_MAIN_DEMAG_COMP
  5031. inc Temp1
  5032. mov @Temp1, #DEFAULT_PGM_BEC_VOLTAGE_HIGH
  5033. inc Temp1
  5034. mov @Temp1, #DEFAULT_PGM_PPM_CENTER_THROTTLE
  5035. inc Temp1
  5036. mov @Temp1, #DEFAULT_PGM_MAIN_SPOOLUP_TIME
  5037. inc Temp1
  5038. mov @Temp1, #DEFAULT_PGM_ENABLE_TEMP_PROT
  5039. ENDIF
  5040. IF MODE == 1 ; Tail
  5041. mov Temp1, #Pgm_Gov_P_Gain
  5042. mov @Temp1, #0FFh
  5043. inc Temp1
  5044. mov @Temp1, #0FFh ; Governor I gain
  5045. inc Temp1
  5046. mov @Temp1, #0FFh ; Governor mode
  5047. inc Temp1
  5048. mov @Temp1, #0FFh ; Low voltage limit
  5049. inc Temp1
  5050. mov @Temp1, #DEFAULT_PGM_TAIL_GAIN
  5051. inc Temp1
  5052. mov @Temp1, #DEFAULT_PGM_TAIL_IDLE_SPEED
  5053. inc Temp1
  5054. mov @Temp1, #DEFAULT_PGM_TAIL_STARTUP_PWR
  5055. inc Temp1
  5056. mov @Temp1, #DEFAULT_PGM_TAIL_PWM_FREQ
  5057. inc Temp1
  5058. mov @Temp1, #DEFAULT_PGM_TAIL_DIRECTION
  5059. inc Temp1
  5060. mov @Temp1, #DEFAULT_PGM_TAIL_RCP_PWM_POL
  5061. mov Temp1, #Pgm_Enable_TX_Program
  5062. mov @Temp1, #DEFAULT_PGM_ENABLE_TX_PROGRAM
  5063. inc Temp1
  5064. mov @Temp1, #0FFh ; Main rearm start
  5065. inc Temp1
  5066. mov @Temp1, #0FFh ; Governor setup target
  5067. inc Temp1
  5068. mov @Temp1, #0FFh ; Startup rpm
  5069. inc Temp1
  5070. mov @Temp1, #0FFh ; Startup accel
  5071. inc Temp1
  5072. mov @Temp1, #0FFh ; Voltage comp
  5073. inc Temp1
  5074. mov @Temp1, #DEFAULT_PGM_TAIL_COMM_TIMING
  5075. inc Temp1
  5076. mov @Temp1, #0FFh ; Damping force
  5077. inc Temp1
  5078. mov @Temp1, #0FFh ; Governor range
  5079. inc Temp1
  5080. mov @Temp1, #0FFh ; Startup method
  5081. inc Temp1
  5082. mov @Temp1, #DEFAULT_PGM_PPM_MIN_THROTTLE
  5083. inc Temp1
  5084. mov @Temp1, #DEFAULT_PGM_PPM_MAX_THROTTLE
  5085. inc Temp1
  5086. mov @Temp1, #DEFAULT_PGM_TAIL_BEEP_STRENGTH
  5087. inc Temp1
  5088. mov @Temp1, #DEFAULT_PGM_TAIL_BEACON_STRENGTH
  5089. inc Temp1
  5090. mov @Temp1, #DEFAULT_PGM_TAIL_BEACON_DELAY
  5091. inc Temp1
  5092. mov @Temp1, #0FFh ; Throttle rate
  5093. inc Temp1
  5094. mov @Temp1, #DEFAULT_PGM_TAIL_DEMAG_COMP
  5095. inc Temp1
  5096. mov @Temp1, #DEFAULT_PGM_BEC_VOLTAGE_HIGH
  5097. inc Temp1
  5098. mov @Temp1, #DEFAULT_PGM_PPM_CENTER_THROTTLE
  5099. inc Temp1
  5100. mov @Temp1, #0FFh
  5101. inc Temp1
  5102. mov @Temp1, #DEFAULT_PGM_ENABLE_TEMP_PROT
  5103. ENDIF
  5104. IF MODE == 2 ; Multi
  5105. mov Temp1, #Pgm_Fir_Key ;2015-02-06 增加关键字默认值
  5106. mov @Temp1, #DEFAULT_PGM_MULTI_FIRST_KEYWORD
  5107. mov Temp1, #Pgm_Gov_P_Gain
  5108. mov @Temp1, #DEFAULT_PGM_MULTI_P_GAIN
  5109. inc Temp1
  5110. mov @Temp1, #DEFAULT_PGM_MULTI_I_GAIN
  5111. inc Temp1
  5112. mov @Temp1, #DEFAULT_PGM_MULTI_GOVERNOR_MODE
  5113. inc Temp1
  5114. mov @Temp1, #DEFAULT_PGM_MULTI_LOW_VOLTAGE_LIM
  5115. inc Temp1
  5116. mov @Temp1, #DEFAULT_PGM_MULTI_LOW_VOLTAGE_CTL
  5117. inc Temp1
  5118. mov @Temp1, #DEFAULT_PGM_MULTI_GAIN
  5119. inc Temp1
  5120. mov @Temp1, #0FFh
  5121. inc Temp1
  5122. mov @Temp1, #DEFAULT_PGM_MULTI_STARTUP_PWR
  5123. inc Temp1
  5124. mov @Temp1, #DEFAULT_PGM_MULTI_PWM_FREQ
  5125. inc Temp1
  5126. mov @Temp1, #DEFAULT_PGM_MULTI_DIRECTION
  5127. inc Temp1
  5128. mov @Temp1, #DEFAULT_PGM_MULTI_RCP_PWM_POL
  5129. mov Temp1, #Pgm_Enable_TX_Program
  5130. mov @Temp1, #DEFAULT_PGM_ENABLE_TX_PROGRAM
  5131. inc Temp1
  5132. mov @Temp1, #0FFh ; Main rearm start
  5133. inc Temp1
  5134. mov @Temp1, #0FFh ; Governor setup target
  5135. inc Temp1
  5136. mov @Temp1, #0FFh ; Startup rpm
  5137. inc Temp1
  5138. mov @Temp1, #0FFh ; Startup accel
  5139. inc Temp1
  5140. mov @Temp1, #0FFh ; Voltage comp
  5141. inc Temp1
  5142. mov @Temp1, #DEFAULT_PGM_MULTI_COMM_TIMING
  5143. inc Temp1
  5144. mov @Temp1, #DEFAULT_PGM_MULTI_DAMPING_FORCE ; Damping force
  5145. inc Temp1
  5146. mov @Temp1, #0FFh ; Governor range
  5147. inc Temp1
  5148. mov @Temp1, #0FFh ; Startup method
  5149. inc Temp1
  5150. mov @Temp1, #DEFAULT_PGM_PPM_MIN_THROTTLE
  5151. inc Temp1
  5152. mov @Temp1, #DEFAULT_PGM_PPM_MAX_THROTTLE
  5153. inc Temp1
  5154. mov @Temp1, #DEFAULT_PGM_MULTI_BEEP_STRENGTH
  5155. inc Temp1
  5156. mov @Temp1, #DEFAULT_PGM_MULTI_BEACON_STRENGTH
  5157. inc Temp1
  5158. mov @Temp1, #DEFAULT_PGM_MULTI_BEACON_DELAY
  5159. inc Temp1
  5160. mov @Temp1, #0FFh ; Throttle rate
  5161. inc Temp1
  5162. mov @Temp1, #DEFAULT_PGM_MULTI_DEMAG_COMP
  5163. inc Temp1
  5164. mov @Temp1, #DEFAULT_PGM_BEC_VOLTAGE_HIGH
  5165. inc Temp1
  5166. mov @Temp1, #DEFAULT_PGM_PPM_CENTER_THROTTLE
  5167. inc Temp1
  5168. mov @Temp1, #0FFh
  5169. inc Temp1
  5170. mov @Temp1, #DEFAULT_PGM_ENABLE_TEMP_PROT
  5171. ENDIF
  5172. ret
  5173. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5174. ;
  5175. ; Decode parameters
  5176. ;
  5177. ; No assumptions
  5178. ;
  5179. ; Decodes programming parameters
  5180. ;
  5181. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5182. decode_parameters:
  5183. ; Load programmed Damping Force
  5184. mov Temp1, #Pgm_Damping_Force ; Load pwm freq
  5185. mov A, @Temp1
  5186. mov Temp8, A ; Store in Temp8
  5187. clr Flags2.PGM_PWMOFF_DAMPED
  5188. IF DAMPED_MODE_ENABLE == 1
  5189. cjne Temp8, #1, ($+5)
  5190. ajmp ($+4)
  5191. setb Flags2.PGM_PWMOFF_DAMPED
  5192. ENDIF
  5193. ; Load programmed pwm frequency
  5194. mov Temp1, #Pgm_Pwm_Freq ; Load pwm freq
  5195. mov A, @Temp1
  5196. mov Temp8, A ; Store in Temp8
  5197. ; Load programmed direction
  5198. mov Temp1, #Pgm_Direction
  5199. IF MODE >= 1 ; Tail or multi
  5200. mov A, @Temp1
  5201. clr C
  5202. subb A, #3
  5203. jz decode_params_dir_set
  5204. ENDIF
  5205. clr Flags3.PGM_DIR_REV
  5206. mov A, @Temp1
  5207. jnb ACC.1, ($+5)
  5208. setb Flags3.PGM_DIR_REV
  5209. decode_params_dir_set:
  5210. clr Flags3.PGM_RCP_PWM_POL
  5211. mov Temp1, #Pgm_Input_Pol
  5212. mov A, @Temp1
  5213. jnb ACC.1, ($+5)
  5214. setb Flags3.PGM_RCP_PWM_POL
  5215. clr C
  5216. mov A, Temp8
  5217. subb A, #1 ;2015-02-10
  5218. jz decode_pwm_freq_low
  5219. mov CKCON, #01h ; Timer0 set for clk/4 (22kHz pwm)
  5220. setb Flags2.PGM_PWM_HIGH_FREQ
  5221. jmp decode_pwm_freq_end
  5222. decode_pwm_freq_low:
  5223. mov CKCON, #00h ; Timer0 set for clk/12 (8kHz pwm)
  5224. clr Flags2.PGM_PWM_HIGH_FREQ
  5225. decode_pwm_freq_end:
  5226. ret
  5227. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5228. ;
  5229. ; Decode governor gain
  5230. ;
  5231. ; No assumptions
  5232. ;
  5233. ; Decodes governor gains
  5234. ;
  5235. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5236. decode_governor_gains:
  5237. ; Decode governor gains
  5238. mov Temp1, #Pgm_Gov_P_Gain ; Decode governor P gain
  5239. mov A, @Temp1
  5240. dec A
  5241. mov DPTR, #GOV_GAIN_TABLE
  5242. movc A, @A+DPTR
  5243. mov Temp1, #Pgm_Gov_P_Gain_Decoded
  5244. mov @Temp1, A
  5245. mov Temp1, #Pgm_Gov_I_Gain ; Decode governor I gain
  5246. mov A, @Temp1
  5247. dec A
  5248. mov DPTR, #GOV_GAIN_TABLE
  5249. movc A, @A+DPTR
  5250. mov Temp1, #Pgm_Gov_I_Gain_Decoded
  5251. mov @Temp1, A
  5252. call switch_power_off ; Reset DPTR
  5253. ret
  5254. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5255. ;
  5256. ; Decode startup power
  5257. ;
  5258. ; No assumptions
  5259. ;
  5260. ; Decodes startup power
  5261. ;
  5262. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5263. decode_startup_power:
  5264. ; Decode startup power
  5265. mov Temp1, #Pgm_Startup_Pwr
  5266. mov A, @Temp1
  5267. dec A
  5268. mov DPTR, #STARTUP_POWER_TABLE
  5269. movc A, @A+DPTR
  5270. mov Temp1, #Pgm_Startup_Pwr_Decoded
  5271. mov @Temp1, A
  5272. call switch_power_off ; Reset DPTR
  5273. ret
  5274. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5275. ;
  5276. ; Decode main spoolup time
  5277. ;
  5278. ; No assumptions
  5279. ;
  5280. ; Decodes main spoolup time
  5281. ;
  5282. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5283. decode_main_spoolup_time:
  5284. IF MODE == 0 ; Main
  5285. ; Decode spoolup time
  5286. mov Temp1, #Pgm_Main_Spoolup_Time
  5287. mov A, @Temp1
  5288. mov Temp1, A ; Store
  5289. jnz ($+3) ; If not zero - branch
  5290. inc Temp1
  5291. clr C
  5292. mov A, Temp1
  5293. subb A, #17 ; Limit to 17 max
  5294. jc ($+4)
  5295. mov Temp1, #17
  5296. mov A, Temp1
  5297. add A, Temp1
  5298. add A, Temp1 ; Now 3x
  5299. mov Main_Spoolup_Time_3x, A
  5300. add A, Main_Spoolup_Time_3x
  5301. add A, Main_Spoolup_Time_3x
  5302. add A, Temp1 ; Now 10x
  5303. mov Main_Spoolup_Time_10x, A
  5304. add A, Main_Spoolup_Time_3x
  5305. add A, Temp1
  5306. add A, Temp1 ; Now 15x
  5307. mov Main_Spoolup_Time_15x, A
  5308. ENDIF
  5309. ret
  5310. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5311. ;
  5312. ; Decode demag compensation
  5313. ;
  5314. ; No assumptions
  5315. ;
  5316. ; Decodes demag comp
  5317. ;
  5318. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5319. decode_demag_comp:
  5320. ; Decode demag compensation
  5321. mov Temp1, #Pgm_Demag_Comp
  5322. mov A, @Temp1
  5323. mov Demag_Pwr_Off_Thresh, #255 ; Set default
  5324. mov Low_Rpm_Pwr_Slope, #12 ; Set default
  5325. cjne A, #2, decode_demag_high
  5326. mov Demag_Pwr_Off_Thresh, #160 ; Settings for demag comp low
  5327. mov Low_Rpm_Pwr_Slope, #10
  5328. decode_demag_high:
  5329. cjne A, #3, decode_demag_done
  5330. mov Demag_Pwr_Off_Thresh, #130 ; Settings for demag comp high
  5331. mov Low_Rpm_Pwr_Slope, #5
  5332. decode_demag_done:
  5333. ret
  5334. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5335. ;
  5336. ; Set BEC voltage
  5337. ;
  5338. ; No assumptions
  5339. ;
  5340. ; Sets the BEC output voltage low or high
  5341. ;
  5342. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5343. set_bec_voltage:
  5344. ; Set bec voltage
  5345. IF HIGH_BEC_VOLTAGE == 1
  5346. Set_BEC_Lo ; Set default to low
  5347. mov Temp1, #Pgm_BEC_Voltage_High
  5348. mov A, @Temp1
  5349. jz set_bec_voltage_exit
  5350. Set_BEC_Hi ; Set to high
  5351. set_bec_voltage_exit:
  5352. ENDIF
  5353. IF HIGH_BEC_VOLTAGE == 2
  5354. Set_BEC_0 ; Set default to low
  5355. mov Temp1, #Pgm_BEC_Voltage_High
  5356. mov A, @Temp1
  5357. cjne A, #1, set_bec_voltage_2
  5358. Set_BEC_1 ; Set to level 1
  5359. set_bec_voltage_2:
  5360. cjne A, #2, set_bec_voltage_exit
  5361. Set_BEC_2 ; Set to level 2
  5362. set_bec_voltage_exit:
  5363. ENDIF
  5364. ret
  5365. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5366. ;
  5367. ; Find throttle gain
  5368. ;
  5369. ; The difference between max and min throttle must be more than 520us (a Pgm_Ppm_xxx_Throttle difference of 130)
  5370. ;
  5371. ; Finds throttle gain from throttle calibration values
  5372. ;
  5373. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5374. find_throttle_gain:
  5375. ; Load programmed minimum and maximum throttle
  5376. mov Temp1, #Pgm_Ppm_Min_Throttle
  5377. mov A, @Temp1
  5378. mov Temp3, A
  5379. mov Temp1, #Pgm_Ppm_Max_Throttle
  5380. mov A, @Temp1
  5381. mov Temp4, A
  5382. ; Check if full range is chosen
  5383. jnb Flags3.FULL_THROTTLE_RANGE, find_throttle_gain_calculate
  5384. mov Temp3, #0
  5385. mov Temp4, #255
  5386. find_throttle_gain_calculate:
  5387. ; Calculate difference
  5388. clr C
  5389. mov A, Temp4
  5390. subb A, Temp3
  5391. mov Temp5, A
  5392. ; Check that difference is minimum 130
  5393. clr C
  5394. subb A, #130
  5395. jnc ($+4)
  5396. mov Temp5, #130
  5397. ; Find gain
  5398. mov Ppm_Throttle_Gain, #0
  5399. test_throttle_gain:
  5400. inc Ppm_Throttle_Gain
  5401. mov A, Temp5
  5402. mov B, Ppm_Throttle_Gain ; A has difference, B has gain
  5403. mul AB
  5404. clr C
  5405. mov A, B
  5406. subb A, #128
  5407. jc test_throttle_gain
  5408. ret
  5409. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5410. ;
  5411. ; Average throttle
  5412. ;
  5413. ; Outputs result in Temp3
  5414. ;
  5415. ; Averages throttle calibration readings
  5416. ;
  5417. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5418. average_throttle:
  5419. setb Flags3.FULL_THROTTLE_RANGE ; Set range to 1000-2020us
  5420. call find_throttle_gain ; Set throttle gain
  5421. call wait30ms
  5422. mov Temp3, #0
  5423. mov Temp4, #0
  5424. mov Temp5, #16 ; Average 16 measurments
  5425. average_throttle_meas:
  5426. call wait3ms ; Wait for new RC pulse value
  5427. mov A, New_Rcp ; Get new RC pulse value
  5428. add A, Temp3
  5429. mov Temp3, A
  5430. mov A, #0
  5431. addc A, Temp4
  5432. mov Temp4, A
  5433. djnz Temp5, average_throttle_meas
  5434. mov Temp5, #4 ; Shift 4 times
  5435. average_throttle_div:
  5436. clr C
  5437. mov A, Temp4 ; Shift right
  5438. rrc A
  5439. mov Temp4, A
  5440. mov A, Temp3
  5441. rrc A
  5442. mov Temp3, A
  5443. djnz Temp5, average_throttle_div
  5444. mov Temp7, A ; Copy to Temp7
  5445. clr Flags3.FULL_THROTTLE_RANGE
  5446. call find_throttle_gain ; Set throttle gain
  5447. ret
  5448. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5449. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5450. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5451. ;
  5452. ; Main program start
  5453. ;
  5454. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5455. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5456. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5457. reset:
  5458. ; Check flash lock byte
  5459. mov A, RSTSRC
  5460. jb ACC.6, ($+6) ; Check if flash access error was reset source
  5461. mov Bit_Access, #0 ; No - then this is the first try
  5462. inc Bit_Access
  5463. mov DPTR, #LOCK_BYTE_ADDRESS_16K ; First try is for 16k flash size
  5464. mov A, Bit_Access
  5465. dec A
  5466. jz lock_byte_test
  5467. mov DPTR, #LOCK_BYTE_ADDRESS_8K ; Second try is for 8k flash size
  5468. dec A
  5469. jz lock_byte_test
  5470. lock_byte_test:
  5471. movc A, @A+DPTR ; Read lock byte
  5472. inc A
  5473. jz lock_byte_ok ; If lock byte is 0xFF, then start code execution
  5474. IF ONE_S_CAPABLE == 0
  5475. mov RSTSRC, #12h ; Generate hardware reset and set VDD monitor
  5476. ELSE
  5477. mov RSTSRC, #10h ; Generate hardware reset and disable VDD monitor
  5478. ENDIF
  5479. lock_byte_ok:
  5480. ; Select register bank 0 for main program routines
  5481. clr PSW.3 ; Select register bank 0 for main program routines
  5482. ; Disable the WDT.
  5483. anl PCA0MD, #NOT(40h) ; Clear watchdog enable bit
  5484. ; Initialize stack
  5485. mov SP, #0c0h ; Stack = 64 upper bytes of RAM
  5486. ; Initialize VDD monitor
  5487. orl VDM0CN, #080h ; Enable the VDD monitor
  5488. call wait1ms ; Wait at least 100us
  5489. IF ONE_S_CAPABLE == 0
  5490. mov RSTSRC, #02h ; Set VDD monitor as a reset source (PORSF) if not 1S capable
  5491. ELSE
  5492. mov RSTSRC, #00h ; Do not set VDD monitor as a reset source for 1S ESCSs, in order to avoid resets due to it
  5493. ENDIF
  5494. ; Set clock frequency
  5495. orl OSCICN, #03h ; Set clock divider to 1
  5496. mov A, OSCICL
  5497. add A, #04h ; 24.5MHz to 24MHz (~0.5% per step)
  5498. jc reset_cal_done ; Is carry set? - skip next instruction
  5499. mov OSCICL, A
  5500. reset_cal_done:
  5501. ; Switch power off
  5502. call switch_power_off
  5503. ; Ports initialization
  5504. mov P0, #P0_INIT
  5505. mov P0MDOUT, #P0_PUSHPULL
  5506. mov P0MDIN, #P0_DIGITAL
  5507. mov P0SKIP, #P0_SKIP
  5508. mov P1, #P1_INIT
  5509. mov P1MDOUT, #P1_PUSHPULL
  5510. mov P1MDIN, #P1_DIGITAL
  5511. mov P1SKIP, #P1_SKIP
  5512. IF PORT3_EXIST == 1
  5513. mov P2, #P2_INIT
  5514. ENDIF
  5515. mov P2MDOUT, #P2_PUSHPULL
  5516. IF PORT3_EXIST == 1
  5517. mov P2MDIN, #P2_DIGITAL
  5518. mov P2SKIP, #P2_SKIP
  5519. mov P3, #P3_INIT
  5520. mov P3MDOUT, #P3_PUSHPULL
  5521. mov P3MDIN, #P3_DIGITAL
  5522. ENDIF
  5523. ; Initialize the XBAR and related functionality
  5524. Initialize_Xbar
  5525. ; Clear RAM
  5526. clr A ; Clear accumulator
  5527. mov Temp1, A ; Clear Temp1
  5528. clear_ram:
  5529. mov @Temp1, A ; Clear RAM
  5530. djnz Temp1, clear_ram ; Is A not zero? - jump
  5531. ; Set default programmed parameters
  5532. call set_default_parameters
  5533. ; Read all programmed parameters
  5534. call read_all_eeprom_parameters
  5535. ; Decode parameters
  5536. call decode_parameters
  5537. ; Decode governor gains
  5538. call decode_governor_gains
  5539. ; Decode startup power
  5540. call decode_startup_power
  5541. ; Decode main spoolup time
  5542. call decode_main_spoolup_time
  5543. ; Decode demag compensation
  5544. call decode_demag_comp
  5545. ; Set BEC voltage
  5546. call set_bec_voltage
  5547. ; Find throttle gain from stored min and max settings
  5548. call find_throttle_gain
  5549. ; Set beep strength
  5550. mov Temp1, #Pgm_Beep_Strength
  5551. mov Beep_Strength, @Temp1
  5552. ; Switch power off
  5553. call switch_power_off
  5554. ; Timer control
  5555. mov TCON, #50h ; Timer0 and timer1 enabled
  5556. ; Timer mode
  5557. mov TMOD, #02h ; Timer0 as 8bit
  5558. ; Timer2: clk/12 for 128us and 32ms interrupts
  5559. mov TMR2CN, #24h ; Timer2 enabled, low counter interrups enabled
  5560. ; Timer3: clk/12 for commutation timing
  5561. mov TMR3CN, #04h ; Timer3 enabled
  5562. ; PCA
  5563. mov PCA0CN, #40h ; PCA enabled
  5564. ; Initializing beep
  5565. clr EA ; Disable interrupts explicitly
  5566. ;remove Initializing beep 2015-02-05
  5567. ; call wait200ms
  5568. ; call beep_f1
  5569. ; call wait30ms
  5570. ; call beep_f2
  5571. ; call wait30ms
  5572. ; call beep_f3
  5573. ; call wait30ms
  5574. ; Wait for receiver to initialize
  5575. IF MODE <= 1 ; Main or tail
  5576. call wait1s
  5577. call wait200ms
  5578. call wait200ms
  5579. call wait100ms
  5580. ENDIF
  5581. ; Enable interrupts
  5582. mov IE, #22h ; Enable timer0 and timer2 interrupts
  5583. mov IP, #02h ; High priority to timer0 interrupts
  5584. mov EIE1, #90h ; Enable timer3 and PCA0 interrupts
  5585. ; Initialize comparator
  5586. mov CPT0CN, #80h ; Comparator enabled, no hysteresis
  5587. mov CPT0MD, #00h ; Comparator response time 100ns
  5588. IF COMP1_USED == 1
  5589. mov CPT1CN, #80h ; Comparator enabled, no hysteresis
  5590. mov CPT1MD, #00h ; Comparator response time 100ns
  5591. ENDIF
  5592. ; Initialize ADC
  5593. Initialize_Adc ; Initialize ADC operation
  5594. call wait1ms
  5595. setb EA ; Enable all interrupts
  5596. ; Measure number of lipo cells
  5597. call Measure_Lipo_Cells ; Measure number of lipo cells
  5598. ; Initialize rc pulse
  5599. Rcp_Int_Enable ; Enable interrupt
  5600. Rcp_Clear_Int_Flag ; Clear interrupt flag
  5601. clr Flags2.RCP_EDGE_NO ; Set first edge flag
  5602. call wait200ms
  5603. ; Set initial arm variable
  5604. mov Initial_Arm, #1
  5605. ; Measure PWM frequency
  5606. measure_pwm_freq_init:
  5607. setb Flags0.RCP_MEAS_PWM_FREQ ; Set measure pwm frequency flag
  5608. measure_pwm_freq_start:
  5609. mov Temp3, #15 ; Number of pulses to measure
  5610. measure_pwm_freq_loop:
  5611. ; Check if period diff was accepted
  5612. mov A, Rcp_Period_Diff_Accepted
  5613. jnz measure_pwm_freq_next
  5614. mov A, Initial_Arm ; Yes - check if it is initial arm sequence
  5615. clr C
  5616. subb A, #1 ; Is it the initial arm sequence?
  5617. jc measure_pwm_freq_res_count ; Yes - proceed
  5618. clr C
  5619. mov A, Pgm_Card_Sig_Count
  5620. subb A, #100
  5621. jc measure_pwm_freq_start
  5622. clr EA
  5623. call beep_f2
  5624. call beep_f3
  5625. call beep_f1
  5626. mov TMOD, #11h ;T0/T1 工作方式1
  5627. mov CKCON, #02h ;T0/T1 48分频
  5628. mov P0MDIN, #80h
  5629. mov P0SKIP, #0FFh
  5630. call program_by_card ; Yes - enter programming mode
  5631. measure_pwm_freq_res_count:
  5632. mov Temp3, #15 ; Reset number of pulses to measure
  5633. measure_pwm_freq_next:
  5634. jnb Flags2.RCP_UPDATED, $ ; Is there an updated RC pulse available?
  5635. clr Flags2.RCP_UPDATED ; Flag that pulse has been evaluated
  5636. mov A, New_Rcp ; Load value
  5637. clr C
  5638. subb A, #RCP_VALIDATE ; Higher than validate level?
  5639. jc measure_pwm_freq_start ; No - start over
  5640. mov A, Flags3 ; Check pwm frequency flags
  5641. anl A, #((1 SHL RCP_PWM_FREQ_1KHZ)+(1 SHL RCP_PWM_FREQ_2KHZ)+(1 SHL RCP_PWM_FREQ_4KHZ)+(1 SHL RCP_PWM_FREQ_8KHZ)+(1 SHL RCP_PWM_FREQ_12KHZ))
  5642. mov Prev_Rcp_Pwm_Freq, Curr_Rcp_Pwm_Freq ; Store as previous flags for next pulse
  5643. mov Curr_Rcp_Pwm_Freq, A ; Store current flags for next pulse
  5644. cjne A, Prev_Rcp_Pwm_Freq, measure_pwm_freq_start ; Go back if new flags not same as previous
  5645. djnz Temp3, measure_pwm_freq_loop ; Go back if not required number of pulses seen
  5646. ; Clear measure pwm frequency flag
  5647. clr Flags0.RCP_MEAS_PWM_FREQ
  5648. ; Set up RC pulse interrupts after pwm frequency measurement
  5649. Rcp_Int_First ; Enable interrupt and set to first edge
  5650. Rcp_Clear_Int_Flag ; Clear interrupt flag
  5651. clr Flags2.RCP_EDGE_NO ; Set first edge flag
  5652. ; Test whether signal is OnShot125
  5653. clr Flags2.RCP_PPM_ONESHOT125 ; Clear OneShot125 flag
  5654. mov Rcp_Outside_Range_Cnt, #0 ; Reset out of range counter
  5655. call wait100ms ; Wait for new RC pulse
  5656. jnb Flags2.RCP_PPM, validate_rcp_start ; If flag is not set (PWM) - branch
  5657. clr C
  5658. mov A, Rcp_Outside_Range_Cnt ; Check how many pulses were outside normal PPM range (800-2160us)
  5659. subb A, #10
  5660. jc validate_rcp_start
  5661. setb Flags2.RCP_PPM_ONESHOT125 ; Set OneShot125 flag
  5662. ; Validate RC pulse
  5663. validate_rcp_start:
  5664. call wait3ms ; Wait for next pulse (NB: Uses Temp1/2!)
  5665. mov Temp1, #RCP_VALIDATE ; Set validate level as default
  5666. jnb Flags2.RCP_PPM, ($+5) ; If flag is not set (PWM) - branch
  5667. mov Temp1, #0 ; Set level to zero for PPM (any level will be accepted)
  5668. clr C
  5669. mov A, New_Rcp ; Load value
  5670. subb A, Temp1 ; Higher than validate level?
  5671. jc validate_rcp_start ; No - start over
  5672. ; Beep arm sequence start signal
  5673. ;remove Beep arm sequence start signal 2015-02-05
  5674. ; clr EA ; Disable all interrupts
  5675. ; call beep_f1 ; Signal that RC pulse is ready
  5676. ; call beep_f1
  5677. ; call beep_f1
  5678. ; setb EA ; Enable all interrupts
  5679. call wait200ms
  5680. ; Arming sequence start
  5681. mov Gov_Arm_Target, #0 ; Clear governor arm target
  5682. arming_start:
  5683. IF MODE >= 1 ; Tail or multi
  5684. mov Temp1, #Pgm_Direction ; Check if bidirectional operation
  5685. mov A, @Temp1
  5686. cjne A, #3, ($+5)
  5687. ajmp program_by_tx_checked ; Disable tx programming if bidirectional operation
  5688. ENDIF
  5689. call wait3ms
  5690. mov Temp1, #Pgm_Enable_TX_Program; Start programming mode entry if enabled
  5691. mov A, @Temp1
  5692. clr C
  5693. subb A, #1 ; Is TX programming enabled?
  5694. jnc arming_initial_arm_check ; Yes - proceed
  5695. jmp program_by_tx_checked ; No - branch
  5696. arming_initial_arm_check:
  5697. mov A, Initial_Arm ; Yes - check if it is initial arm sequence
  5698. clr C
  5699. subb A, #1 ; Is it the initial arm sequence?
  5700. jnc arming_ppm_check ; Yes - proceed
  5701. jmp wait_for_power_on ; No - branch
  5702. arming_ppm_check:
  5703. mov Temp1, #Pgm_Ppm_Min_Throttle ; Store
  5704. mov A, @Temp1
  5705. mov Min_Throttle, A
  5706. jb Flags2.RCP_PPM, throttle_high_cal_start ; If flag is set (PPM) - branch
  5707. ; PWM tx program entry
  5708. clr C
  5709. mov A, New_Rcp ; Load new RC pulse value
  5710. subb A, #RCP_MAX ; Is RC pulse max?
  5711. jnc program_by_tx_entry_pwm ; Yes - proceed
  5712. jmp program_by_tx_checked ; No - branch
  5713. program_by_tx_entry_pwm:
  5714. clr EA ; Disable all interrupts
  5715. call beep_f4
  5716. setb EA ; Enable all interrupts
  5717. call wait100ms
  5718. clr C
  5719. mov A, New_Rcp ; Load new RC pulse value
  5720. subb A, #RCP_STOP ; Below stop?
  5721. jnc program_by_tx_entry_pwm ; No - start over
  5722. program_by_tx_entry_wait_pwm:
  5723. clr EA ; Disable all interrupts
  5724. call beep_f1
  5725. call wait10ms
  5726. call beep_f1
  5727. setb EA ; Enable all interrupts
  5728. call wait100ms
  5729. clr C
  5730. mov A, New_Rcp ; Load new RC pulse value
  5731. subb A, #RCP_MAX ; At or above max?
  5732. jc program_by_tx_entry_wait_pwm ; No - start over
  5733. jmp program_by_tx ; Yes - enter programming mode
  5734. ; PPM throttle calibration and tx program entry
  5735. throttle_high_cal_start:
  5736. IF MODE <= 1 ; Main or tail
  5737. mov Temp8, #5 ; Set 3 seconds wait time
  5738. ELSE
  5739. mov Temp8, #20 ; Set 2 seconds wait time
  5740. ENDIF
  5741. throttle_high_cal:
  5742. setb Flags3.FULL_THROTTLE_RANGE ; Set range to 1000-2020us
  5743. call find_throttle_gain ; Set throttle gain
  5744. call wait100ms ; Wait for new throttle value
  5745. clr EA ; Disable interrupts (freeze New_Rcp value)
  5746. clr Flags3.FULL_THROTTLE_RANGE ; Set programmed range
  5747. call find_throttle_gain ; Set throttle gain
  5748. ; mov Temp7, New_Rcp ; Store new RC pulse value
  5749. clr C
  5750. mov A, New_Rcp ; Load new RC pulse value
  5751. subb A, #220 ; Is RC pulse above midstick? Is RC pulse above 1880us?
  5752. setb EA ; Enable interrupts
  5753. jc ($+6) ;yes-
  5754. ; jc arm_target_updated ; No - branch
  5755. djnz Temp8, throttle_high_cal
  5756. ajmp throttle_high_cal_save
  5757. ;check min throttle 2015-02-05
  5758. clr C
  5759. mov A, New_Rcp ; Load new RC pulse value
  5760. subb A, Min_Throttle ; Is RC pulse below min throttle?
  5761. jnc ($+4)
  5762. ajmp arm_target_updated ; No - branch
  5763. call wait1ms
  5764. clr EA ; Disable all interrupts
  5765. call beep_f1
  5766. call wait100ms
  5767. setb EA ; Enable all interrupts
  5768. ajmp throttle_high_cal_start
  5769. ;save max RC pulse
  5770. throttle_high_cal_save:
  5771. call average_throttle
  5772. clr C
  5773. mov A, New_Rcp ; Limit to max 250
  5774. subb A, #5 ; Subtract about 2% and ensure that it is 250 or lower
  5775. mov Temp1, #Pgm_Ppm_Max_Throttle ; Store
  5776. mov @Temp1, A
  5777. call wait200ms
  5778. call erase_and_store_all_in_eeprom
  5779. ;max throttle store beep
  5780. clr EA
  5781. call beep_f1
  5782. call beep_f1
  5783. call wait30ms
  5784. call beep_f4
  5785. call beep_f4
  5786. setb EA
  5787. wait_program_ppm:
  5788. mov Temp8,#20
  5789. wait_program_by_tx_entry_ppm:
  5790. call wait100ms
  5791. call find_throttle_gain ; Set throttle gain
  5792. clr C
  5793. mov A, New_Rcp ; Load new RC pulse value
  5794. subb A, #215 ;Is RC pulse above midstick?
  5795. jc ($+6)
  5796. djnz Temp8,wait_program_by_tx_entry_ppm
  5797. ajmp program_by_tx_entry_wait_ppm
  5798. clr C
  5799. mov A, New_Rcp ; Load new RC pulse value
  5800. subb A, #60 ;Is RC pulse above midstick?
  5801. jnc wait_program_ppm
  5802. ajmp throttle_low_cal_start
  5803. program_by_tx_entry_wait_ppm:
  5804. call program_by_tx ; Yes - enter programming mode
  5805. throttle_low_cal_start:
  5806. mov Temp8, #20 ; Set 3 seconds wait time
  5807. throttle_low_cal:
  5808. setb Flags3.FULL_THROTTLE_RANGE ; Set range to 1000-2020us
  5809. call find_throttle_gain ; Set throttle gain
  5810. call wait100ms
  5811. clr EA ; Disable interrupts (freeze New_Rcp value)
  5812. clr Flags3.FULL_THROTTLE_RANGE ; Set programmed range
  5813. call find_throttle_gain ; Set throttle gain
  5814. ; mov Temp7, New_Rcp ; Store new RC pulse value
  5815. clr C
  5816. mov A, New_Rcp ; Load new RC pulse value
  5817. subb A, #60 ; Below midstick?
  5818. setb EA ; Enable interrupts
  5819. jnc throttle_low_cal_start ; No - start over
  5820. djnz Temp8,throttle_low_cal ; Continue to wait
  5821. call average_throttle
  5822. mov A, New_Rcp
  5823. add A, #5 ; Add about 2%
  5824. mov Temp1, #Pgm_Ppm_Min_Throttle ; Store
  5825. mov @Temp1, A
  5826. call wait200ms
  5827. call erase_and_store_all_in_eeprom
  5828. call read_all_eeprom_parameters
  5829. ; Decode parameters
  5830. call decode_parameters
  5831. ; Decode governor gains
  5832. call decode_governor_gains
  5833. ; Decode startup power
  5834. call decode_startup_power
  5835. ; Decode main spoolup time
  5836. call decode_main_spoolup_time
  5837. ; Decode demag compensation
  5838. call decode_demag_comp
  5839. ; Set BEC voltage
  5840. call set_bec_voltage
  5841. ; Find throttle gain from stored min and max settings
  5842. call find_throttle_gain
  5843. program_by_tx_checked:
  5844. clr C
  5845. mov A, New_Rcp ; Load new RC pulse value
  5846. subb A, Gov_Arm_Target ; Is RC pulse larger than arm target?
  5847. jc arm_target_updated ; No - do not update
  5848. mov Gov_Arm_Target, New_Rcp ; Yes - update arm target
  5849. arm_target_updated:
  5850. call wait100ms ; Wait for new throttle value
  5851. mov Temp1, #RCP_STOP ; Default stop value
  5852. mov Temp2, #Pgm_Direction ; Check if bidirectional operation
  5853. mov A, @Temp2
  5854. cjne A, #3, ($+5) ; No - branch
  5855. mov Temp1, #(RCP_STOP+4) ; Higher stop value for bidirectional
  5856. clr C
  5857. mov A, New_Rcp ; Load new RC pulse value
  5858. subb A, Temp1 ; Below stop?
  5859. jc arm_end_beep ; Yes - proceed
  5860. jmp arm_target_updated ; No - start over
  5861. arm_end_beep:
  5862. ; Beep arm sequence end signal
  5863. clr EA ; Disable all interrupts
  5864. call beep_f1
  5865. call beep_f1
  5866. call beep_f1
  5867. call wait1s
  5868. ;*************************************************
  5869. ; beep count of Lipo cells
  5870. ;*************************************************
  5871. mov Temp8,Lipo_Cell_Count ;
  5872. lipo_cell_beep:
  5873. call beep_f1
  5874. call beep_f1
  5875. call wait30ms
  5876. djnz Temp8,lipo_cell_beep
  5877. call wait200ms
  5878. call wait200ms
  5879. ;reday beep
  5880. call beep_f1
  5881. call wait30ms
  5882. call wait30ms
  5883. call beep_f2
  5884. call wait30ms
  5885. call wait30ms
  5886. call beep_f3
  5887. call wait30ms
  5888. call wait30ms
  5889. setb EA ; Enable all interrupts
  5890. call wait100ms
  5891. ; Measure number of lipo cells
  5892. call Measure_Lipo_Cells ; Measure number of lipo cells
  5893. ; Clear initial arm variable
  5894. mov Initial_Arm, #0
  5895. mov Pgm_Card_Sig_Count, #0
  5896. ; Armed and waiting for power on
  5897. wait_for_power_on:
  5898. clr Flags1.LOW_LIMIT_STOP ;
  5899. ; clr A
  5900. ; mov Power_On_Wait_Cnt_L, A ; Clear wait counter
  5901. ; mov Power_On_Wait_Cnt_H, A
  5902. wait_for_power_on_loop:
  5903. ; inc Power_On_Wait_Cnt_L ; Increment low wait counter
  5904. ; mov A, Power_On_Wait_Cnt_L
  5905. ; cpl A
  5906. ; jnz wait_for_power_on_no_beep; Counter wrapping (about 1 sec)?
  5907. ; inc Power_On_Wait_Cnt_H ; Increment high wait counter
  5908. ; mov Temp1, #Pgm_Beacon_Delay
  5909. ; mov A, @Temp1
  5910. ; mov Temp1, #25 ; Approximately 1 min
  5911. ; dec A
  5912. ; jz beep_delay_set
  5913. ; mov Temp1, #50 ; Approximately 2 min
  5914. ; dec A
  5915. ; jz beep_delay_set
  5916. ; mov Temp1, #125 ; Approximately 5 min
  5917. ; dec A
  5918. ; jz beep_delay_set
  5919. ; mov Temp1, #250 ; Approximately 10 min
  5920. ; dec A
  5921. ; jz beep_delay_set
  5922. ; mov Power_On_Wait_Cnt_H, #0 ; Reset counter for infinite delay
  5923. ;beep_delay_set:
  5924. ; clr C
  5925. ; mov A, Power_On_Wait_Cnt_H
  5926. ; subb A, Temp1 ; Check against chosen delay
  5927. ; jc wait_for_power_on_no_beep; Has delay elapsed?
  5928. ; dec Power_On_Wait_Cnt_H ; Decrement high wait counter
  5929. ; mov Power_On_Wait_Cnt_L, #180; Set low wait counter
  5930. ; mov Temp1, #Pgm_Beacon_Strength
  5931. ; mov Beep_Strength, @Temp1
  5932. ; clr EA ; Disable all interrupts
  5933. ; call beep_f4 ; Signal that there is no signal
  5934. ; setb EA ; Enable all interrupts
  5935. ; mov Temp1, #Pgm_Beep_Strength
  5936. ; mov Beep_Strength, @Temp1
  5937. ; call wait100ms ; Wait for new RC pulse to be measured
  5938. wait_for_power_on_no_beep:
  5939. call wait10ms
  5940. mov A, Rcp_Timeout_Cnt ; Load RC pulse timeout counter value
  5941. jnz wait_for_power_on_ppm_not_missing ; If it is not zero - proceed
  5942. jnb Flags2.RCP_PPM, wait_for_power_on_ppm_not_missing ; If flag is not set (PWM) - branch
  5943. jmp measure_pwm_freq_init ; If ppm and pulses missing - go back to measure pwm frequency
  5944. wait_for_power_on_ppm_not_missing:
  5945. mov Temp1, #RCP_STOP
  5946. jb Flags2.RCP_PPM, ($+5) ; If flag is set (PPM) - branch
  5947. mov Temp1, #(RCP_STOP+5) ; Higher than stop (for pwm)
  5948. clr C
  5949. mov A, New_Rcp ; Load new RC pulse value
  5950. subb A, Temp1 ; Higher than stop (plus some hysteresis)?
  5951. jc wait_for_power_on ; No - start over
  5952. jb Flags1.LOW_LIMIT_STOP, wait_for_power_on_loop
  5953. IF MODE >= 1 ; Tail or multi
  5954. mov Temp1, #Pgm_Direction ; Check if bidirectional operation
  5955. mov A, @Temp1
  5956. clr C
  5957. subb A, #3
  5958. jz wait_for_power_on_check_timeout ; Do not wait if bidirectional operation
  5959. ENDIF
  5960. lcall wait100ms ; Wait to see if start pulse was only a glitch
  5961. wait_for_power_on_check_timeout:
  5962. mov A, Rcp_Timeout_Cnt ; Load RC pulse timeout counter value
  5963. jnz ($+4) ; If it is not zero - proceed
  5964. ajmp measure_pwm_freq_init ; If it is zero (pulses missing) - go back to measure pwm frequency
  5965. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5966. ;
  5967. ; Start entry point
  5968. ;
  5969. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  5970. init_start:
  5971. clr EA
  5972. call switch_power_off
  5973. clr A
  5974. mov Requested_Pwm, A ; Set requested pwm to zero
  5975. mov Governor_Req_Pwm, A ; Set governor requested pwm to zero
  5976. mov Current_Pwm, A ; Set current pwm to zero
  5977. mov Current_Pwm_Limited, A ; Set limited current pwm to zero
  5978. setb EA
  5979. mov Temp1, #Pgm_Motor_Idle
  5980. mov Pwm_Motor_Idle, @Temp1 ; Set idle pwm to programmed value
  5981. mov Gov_Target_L, A ; Set target to zero
  5982. mov Gov_Target_H, A
  5983. mov Gov_Integral_L, A ; Set integral to zero
  5984. mov Gov_Integral_H, A
  5985. mov Gov_Integral_X, A
  5986. mov Adc_Conversion_Cnt, A
  5987. mov Limit_Count, A
  5988. mov Gov_Active, A
  5989. mov Flags0, A ; Clear flags0
  5990. mov Flags1, A ; Clear flags1
  5991. mov Demag_Detected_Metric, A ; Clear demag metric
  5992. call initialize_all_timings ; Initialize timing
  5993. ;**** **** **** **** ****
  5994. ; Motor start beginning
  5995. ;**** **** **** **** ****
  5996. mov Adc_Conversion_Cnt, #TEMP_CHECK_RATE ; Make sure a temp reading is done
  5997. Set_Adc_Ip_Temp
  5998. call wait1ms
  5999. call start_adc_conversion
  6000. read_initial_temp:
  6001. Get_Adc_Status
  6002. jb AD0BUSY, read_initial_temp
  6003. Read_Adc_Result ; Read initial temperature
  6004. mov A, Temp2
  6005. jnz ($+3) ; Is reading below 256?
  6006. mov Temp1, A ; Yes - set average temperature value to zero
  6007. mov Current_Average_Temp, Temp1 ; Set initial average temperature
  6008. call check_temp_voltage_and_limit_power
  6009. mov Adc_Conversion_Cnt, #TEMP_CHECK_RATE ; Make sure a temp reading is done next time
  6010. Set_Adc_Ip_Temp
  6011. ; Set up start operating conditions
  6012. mov Temp1, #Pgm_Pwm_Freq
  6013. mov A, @Temp1
  6014. mov Temp7, A ; Store setting in Temp7
  6015. mov @Temp1, #1 ; Set nondamped low frequency pwm mode
  6016. call decode_parameters ; (Decode_parameters uses Temp1 and Temp8)
  6017. mov Temp1, #Pgm_Pwm_Freq
  6018. mov A, Temp7
  6019. mov @Temp1, A ; Restore settings
  6020. ; Set max allowed power
  6021. clr EA ; Disable interrupts to avoid that Requested_Pwm is overwritten
  6022. mov Pwm_Limit, #0FFh ; Set pwm limit to max
  6023. call set_startup_pwm
  6024. mov Pwm_Limit, Requested_Pwm
  6025. mov Pwm_Limit_Spoolup, Requested_Pwm
  6026. mov Pwm_Limit_Low_Rpm, Requested_Pwm
  6027. setb EA
  6028. mov Requested_Pwm, #1 ; Set low pwm again after calling set_startup_pwm
  6029. mov Current_Pwm, #1
  6030. mov Current_Pwm_Limited, #1
  6031. mov Spoolup_Limit_Cnt, Auto_Bailout_Armed
  6032. mov Spoolup_Limit_Skip, #1
  6033. ; Begin startup sequence
  6034. setb Flags1.MOTOR_SPINNING ; Set motor spinning flag
  6035. setb Flags1.STARTUP_PHASE ; Set startup phase flag
  6036. mov Startup_Ok_Cnt, #0 ; Reset ok counter
  6037. call comm5comm6 ; Initialize commutation
  6038. call comm6comm1
  6039. call calc_next_comm_timing ; Set virtual commutation point
  6040. call initialize_all_timings ; Initialize timing
  6041. call calc_new_wait_times ; Calculate new wait times
  6042. jmp run1
  6043. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  6044. ;
  6045. ; Run entry point
  6046. ;
  6047. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  6048. damped_transition:
  6049. ; Transition from nondamped to damped if applicable
  6050. call switch_power_off ; Switch off power while changing pwm mode
  6051. call decode_parameters ; Set programmed parameters
  6052. mov Adc_Conversion_Cnt, #0 ; Make sure a voltage reading is done next time
  6053. Set_Adc_Ip_Volt ; Set adc measurement to voltage
  6054. ; Run 1 = B(p-on) + C(n-pwm) - comparator A evaluated
  6055. ; Out_cA changes from low to high
  6056. run1:
  6057. call wait_for_comp_out_high ; Wait zero cross wait and wait for high
  6058. call evaluate_comparator_integrity ; Check whether comparator reading has been normal
  6059. call setup_comm_wait ; Setup wait time from zero cross to commutation
  6060. call calc_governor_target ; Calculate governor target
  6061. call wait_for_comm ; Wait from zero cross to commutation
  6062. call comm1comm2 ; Commutate
  6063. call calc_next_comm_timing ; Calculate next timing and start advance timing wait
  6064. call wait_advance_timing ; Wait advance timing and start zero cross wait
  6065. call calc_new_wait_times
  6066. call wait_before_zc_scan ; Wait zero cross wait and start zero cross timeout
  6067. ; Run 2 = A(p-on) + C(n-pwm) - comparator B evaluated
  6068. ; Out_cB changes from high to low
  6069. run2:
  6070. call wait_for_comp_out_low
  6071. call evaluate_comparator_integrity
  6072. call setup_comm_wait
  6073. call calc_governor_prop_error
  6074. call set_pwm_limit_low_rpm
  6075. call wait_for_comm
  6076. call comm2comm3
  6077. call calc_next_comm_timing
  6078. call wait_advance_timing
  6079. call calc_new_wait_times
  6080. call wait_before_zc_scan
  6081. ; Run 3 = A(p-on) + B(n-pwm) - comparator C evaluated
  6082. ; Out_cC changes from low to high
  6083. run3:
  6084. call wait_for_comp_out_high
  6085. call evaluate_comparator_integrity
  6086. call setup_comm_wait
  6087. call calc_governor_int_error
  6088. call wait_for_comm
  6089. call comm3comm4
  6090. call calc_next_comm_timing
  6091. call wait_advance_timing
  6092. call calc_new_wait_times
  6093. call wait_before_zc_scan
  6094. ; Run 4 = C(p-on) + B(n-pwm) - comparator A evaluated
  6095. ; Out_cA changes from high to low
  6096. run4:
  6097. call wait_for_comp_out_low
  6098. call evaluate_comparator_integrity
  6099. call setup_comm_wait
  6100. call calc_governor_prop_correction
  6101. call wait_for_comm
  6102. call comm4comm5
  6103. call calc_next_comm_timing
  6104. call wait_advance_timing
  6105. call calc_new_wait_times
  6106. call wait_before_zc_scan
  6107. ; Run 5 = C(p-on) + A(n-pwm) - comparator B evaluated
  6108. ; Out_cB changes from low to high
  6109. run5:
  6110. call wait_for_comp_out_high
  6111. call evaluate_comparator_integrity
  6112. call setup_comm_wait
  6113. call calc_governor_int_correction
  6114. call wait_for_comm
  6115. call comm5comm6
  6116. call calc_next_comm_timing
  6117. call wait_advance_timing
  6118. call calc_new_wait_times
  6119. call wait_before_zc_scan
  6120. ; Run 6 = B(p-on) + A(n-pwm) - comparator C evaluated
  6121. ; Out_cC changes from high to low
  6122. run6:
  6123. call wait_for_comp_out_low
  6124. call start_adc_conversion
  6125. call evaluate_comparator_integrity
  6126. call setup_comm_wait
  6127. call check_temp_voltage_and_limit_power
  6128. call wait_for_comm
  6129. call comm6comm1
  6130. call calc_next_comm_timing
  6131. call wait_advance_timing
  6132. call calc_new_wait_times
  6133. call wait_before_zc_scan
  6134. ; Check if it is direct startup
  6135. jnb Flags1.STARTUP_PHASE, normal_run_checks
  6136. jb Flags0.DIR_CHANGE_BRAKE, normal_run_checks ; If a direction change - branch
  6137. ; Set spoolup power variables
  6138. mov Pwm_Limit, Pwm_Spoolup_Beg ; Set initial max power
  6139. mov Pwm_Limit_Spoolup, Pwm_Spoolup_Beg ; Set initial slow spoolup power
  6140. mov Spoolup_Limit_Cnt, Auto_Bailout_Armed
  6141. mov Spoolup_Limit_Skip, #1
  6142. ; Check startup ok counter
  6143. mov Temp2, #100 ; Set nominal startup parameters
  6144. mov Temp3, #20
  6145. clr C
  6146. mov A, Startup_Ok_Cnt ; Load ok counter
  6147. subb A, Temp2 ; Is counter above requirement?
  6148. jc direct_start_check_rcp ; No - proceed
  6149. clr Flags1.STARTUP_PHASE ; Clear startup phase flag
  6150. setb Flags1.INITIAL_RUN_PHASE ; Set initial run phase flag
  6151. mov Startup_Rot_Cnt, Temp3 ; Set startup rotation count
  6152. IF MODE == 1 ; Tail
  6153. mov Pwm_Limit, #0FFh ; Allow full power
  6154. mov Pwm_Limit_Spoolup, #0FFh
  6155. ENDIF
  6156. IF MODE == 2 ; Multi
  6157. mov Pwm_Limit, Pwm_Spoolup_Beg
  6158. mov Pwm_Limit_Spoolup, #0FFh
  6159. mov Pwm_Limit_Low_Rpm, #20h
  6160. ENDIF
  6161. jmp normal_run_checks
  6162. direct_start_check_rcp:
  6163. clr C
  6164. mov A, New_Rcp ; Load new pulse value
  6165. subb A, #RCP_STOP ; Check if pulse is below stop value
  6166. jc ($+5)
  6167. ljmp run1 ; Continue to run
  6168. jmp run_to_wait_for_power_on
  6169. normal_run_checks:
  6170. ; Check if it is initial run phase
  6171. jnb Flags1.INITIAL_RUN_PHASE, initial_run_phase_done ; If not initial run phase - branch
  6172. jb Flags0.DIR_CHANGE_BRAKE, initial_run_phase_done ; If a direction change - branch
  6173. ; Decrement startup rotaton count
  6174. mov A, Startup_Rot_Cnt
  6175. dec A
  6176. ; Check number of nondamped rotations
  6177. jnz normal_run_check_startup_rot ; Branch if counter is not zero
  6178. clr Flags1.INITIAL_RUN_PHASE ; Clear initial run phase flag
  6179. IF MODE == 2 ; Multi
  6180. mov Pwm_Limit, #0FFh
  6181. ENDIF
  6182. jmp damped_transition ; Do damped transition if counter is zero
  6183. normal_run_check_startup_rot:
  6184. mov Startup_Rot_Cnt, A ; Not zero - store counter
  6185. clr C
  6186. mov A, New_Rcp ; Load new pulse value
  6187. subb A, #RCP_STOP ; Check if pulse is below stop value
  6188. jc ($+5)
  6189. ljmp run1 ; Continue to run
  6190. jmp run_to_wait_for_power_on
  6191. initial_run_phase_done:
  6192. IF MODE == 0 ; Main
  6193. ; Check if throttle is zeroed
  6194. clr C
  6195. mov A, Rcp_Stop_Cnt ; Load stop RC pulse counter value
  6196. subb A, #1 ; Is number of stop RC pulses above limit?
  6197. jc run6_check_rcp_stop_count ; If no - branch
  6198. mov Pwm_Limit_Spoolup, Pwm_Spoolup_Beg ; If yes - set initial max powers
  6199. mov Spoolup_Limit_Cnt, Auto_Bailout_Armed ; And set spoolup parameters
  6200. mov Spoolup_Limit_Skip, #1
  6201. run6_check_rcp_stop_count:
  6202. ENDIF
  6203. ; Exit run loop after a given time
  6204. clr C
  6205. mov A, Rcp_Stop_Cnt ; Load stop RC pulse counter low byte value
  6206. subb A, #RCP_STOP_LIMIT ; Is number of stop RC pulses above limit?
  6207. jnc run_to_wait_for_power_on ; Yes, go back to wait for poweron
  6208. run6_check_rcp_timeout:
  6209. jnb Flags2.RCP_PPM, run6_check_speed ; If flag is not set (PWM) - branch
  6210. mov A, Rcp_Timeout_Cnt ; Load RC pulse timeout counter value
  6211. jz run_to_wait_for_power_on ; If it is zero - go back to wait for poweron
  6212. run6_check_speed:
  6213. clr C
  6214. mov A, Comm_Period4x_H ; Is Comm_Period4x more than 32ms (~1220 eRPM)?
  6215. mov Temp1, #0F0h ; Default minimum speed
  6216. jnb Flags0.DIR_CHANGE_BRAKE, ($+5); Is it a direction change?
  6217. mov Temp1, #60h ; Bidirectional minimum speed
  6218. subb A, Temp1
  6219. jnc run_to_wait_for_power_on ; Yes - go back to motor start
  6220. jmp run1 ; Go back to run 1
  6221. run_to_wait_for_power_on:
  6222. clr EA
  6223. call switch_power_off
  6224. mov Temp1, #Pgm_Pwm_Freq
  6225. mov A, @Temp1
  6226. mov Temp7, A ; Store setting in Temp7
  6227. mov @Temp1, #1 ; Set low pwm mode (in order to turn off damping)
  6228. call decode_parameters ; (Decode_parameters uses Temp1 and Temp8)
  6229. mov Temp1, #Pgm_Pwm_Freq
  6230. mov A, Temp7
  6231. mov @Temp1, A ; Restore settings
  6232. clr A
  6233. mov Requested_Pwm, A ; Set requested pwm to zero
  6234. mov Governor_Req_Pwm, A ; Set governor requested pwm to zero
  6235. mov Current_Pwm, A ; Set current pwm to zero
  6236. mov Current_Pwm_Limited, A ; Set limited current pwm to zero
  6237. mov Pwm_Motor_Idle, A ; Set motor idle to zero
  6238. clr Flags1.MOTOR_SPINNING ; Clear motor spinning flag
  6239. setb EA
  6240. call wait1ms ; Wait for pwm to be stopped
  6241. call switch_power_off
  6242. IF MODE == 0 ; Main
  6243. jnb Flags2.RCP_PPM, run_to_next_state_main ; If flag is not set (PWM) - branch
  6244. mov A, Rcp_Timeout_Cnt ; Load RC pulse timeout counter value
  6245. jnz run_to_next_state_main ; If it is not zero - branch
  6246. jmp measure_pwm_freq_init ; If it is zero (pulses missing) - go back to measure pwm frequency
  6247. run_to_next_state_main:
  6248. mov Temp1, #Pgm_Main_Rearm_Start
  6249. mov A, @Temp1
  6250. clr C
  6251. subb A, #1 ; Is re-armed start enabled?
  6252. jc jmp_wait_for_power_on ; No - do like tail and start immediately
  6253. jmp validate_rcp_start ; Yes - go back to validate RC pulse
  6254. jmp_wait_for_power_on:
  6255. jmp wait_for_power_on ; Go back to wait for power on
  6256. ENDIF
  6257. IF MODE >= 1 ; Tail or multi
  6258. jnb Flags2.RCP_PPM, jmp_wait_for_power_on ; If flag is not set (PWM) - branch
  6259. mov A, Rcp_Timeout_Cnt ; Load RC pulse timeout counter value
  6260. jnz jmp_wait_for_power_on ; If it is not zero - go back to wait for poweron
  6261. jmp measure_pwm_freq_init ; If it is zero (pulses missing) - go back to measure pwm frequency
  6262. jmp_wait_for_power_on:
  6263. jmp wait_for_power_on_loop ; Go back to wait for power on
  6264. ENDIF
  6265. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  6266. $include (EMAX_BLHeliTxPgm.inc) ; Include source code for programming the ESC with the TX
  6267. ;**** **** **** **** **** **** **** **** **** **** **** **** ****
  6268. ; TEST code size
  6269. ;CSEG AT 1E00h ; Last code segment. Take care that there is enough space!
  6270. ;nop
  6271. END