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.

6848 lines
209 KiB

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