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.

7777 lines
234 KiB

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