;**** **** **** **** **** ; ; BLHeli program for controlling brushless motors in helicopters and multirotors ; ; Copyright 2011, 2012 Steffen Skaug ; This program is distributed under the terms of the GNU General Public License ; ; This file is part of BLHeli. ; ; BLHeli is free software: you can redistribute it and/or modify ; it under the terms of the GNU General Public License as published by ; the Free Software Foundation, either version 3 of the License, or ; (at your option) any later version. ; ; BLHeli is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; ; You should have received a copy of the GNU General Public License ; along with BLHeli. If not, see . ; ;**** **** **** **** **** ; ; The software was initially designed for use with Eflite mCP X, but is now adapted to copters/planes in general ; ; The software was inspired by and started from from Bernard Konze's BLMC: http://home.versanet.de/~bkonze/blc_6a/blc_6a.htm ; And also Simon Kirby's TGY: https://github.com/sim-/tgy ; ; This file is best viewed with tab width set to 5 ; ; The input signal can be positive 1kHz, 2kHz, 4kHz, 8kHz or 12kHz PWM (e.g. taken from the "resistor tap" on mCPx) ; And the input signal can be PPM (1-2ms) at rates up to several hundred Hz. ; The code adapts itself to the various input modes/frequencies ; The code ESC can also be programmed to accept inverted input signal. ; ; The first lines of the software must be modified according to the chosen environment: ; Uncomment the selected ESC and main/tail/multi mode ; BESC EQU "ESC"_"mode" ; ;**** **** **** **** **** ; Revision history: ; - Rev0.0: Initial revision ; - Rev1.0: Governor functionality added ; - Rev1.1: Increased tail gain to 1.0625. Implemented for tail only ; Decreased governor proportional and integral gain by 4 ; Fixed bug that caused tail power not always to be max ; - Rev1.2: Governor integral gain should be higher in order to achieve full PWM range ; Integral gain can be higher, and is increased by 2x. An integral of +-128 can now be added to requested PWM ; - Rev1.3: Governor integral extended to 24bit ; Governor proportional gain increased by 2x ; Added slow spoolup/down for governor ; Set pwm to 100% (do not turn off nFET) for high values of current pwm ; Added support for PPM input (1us to 2us pulse) ; Removed USE_COMP_STORED as it was never used ; - Rev2.0 Added measurement of pwm frequency and support for 1kHz, 2kHz, 4kHz and 8kHz ; Optimized pwm on and off routines ; Improved mosfet switching in beep routines, to reduce current draw ; Added support for ICP1 interrupt pin input ; Added ADC measurement of supply voltage, with limiting of main motor power for low voltage ; Miscellaneous other changes ; - Rev2.1 Rewritten INT0 routine to be similar to ICP ; Reduced validation threshold (RCP_VALIDATE) ; Removed requirement for RCP to go to zero again in tail arming sequence ; Removed PPM support ; - Rev2.2 Added support for HC 5A 1S ESC with Atmega48V MPU ; Increased governor proportional gain by 2x ; - Rev3.0 Added functionality for programming from TX ; Added low voltage limit scaling for 2S and 3S ; - Rev11.2 Copied over from the SiLabs version and adapted to Atmel ; Now requiring a 16MHz capable MCU for fullspec performance ; - Rev12.0 Added programmable main spoolup time ; Added programmable temperature protection enable ; Bidirectional mode stop/start improved. Motor is now stopped before starting ; Power is limited for very low rpms (when BEMF is low), in order to avoid sync loss ; Damped light mode is made more smooth and quiet, particularly at low and high rpms ; Comparator signal qualification scheme is changed ; Demag compensation scheme is significantly changed ; Increased jitter tolerance for PPM frequency measurement ; Fully damped mode removed, and damped light only supported on damped capable ESCs ; Default tail mode changed to damped light ; Miscellaneous other changes ; - Rev12.1 Fixed bug in tail code ; Improved startup for Atmel ; Added support for multiple high BEC voltages ; Added support for RPM output ; ; ;**** **** **** **** **** ; 8K Bytes of In-System Self-Programmable Flash ; 1K Bytes Internal SRAM ; 512 Bytes Internal EEPROM ; 16MHz clock ; ;**** **** **** **** **** ; Timer 0 (500ns counts) always counts up and is used for ; - RC pulse timeout and skip counts ; Timer 1 (500ns counts) always counts up and is used for ; - RC pulse measurement (via external interrupt 0 or input capture pin) ; - Commutation timing (via output compare register A interrupt) ; Timer 2 (500ns counts) always counts up and is used for ; - PWM generation ; ;**** **** **** **** **** ; Interrupt handling ; The Atmega8 disables all interrupts when entering an interrupt routine, ; The code reenables interrupts in some interrupt routines, in order to nest pwm interrupts ; - Interrupts are disabled during beeps, to avoid audible interference from interrupts ; - RC pulse interrupts are periodically disabled in order to reduce interference with pwm interrupts. ; ;**** **** **** **** **** ; Motor control: ; - Brushless motor control with 6 states for each electrical 360 degrees ; - An advance timing of 0deg has zero cross 30deg after one commutation and 30deg before the next ; - Timing advance in this implementation is set to 15deg nominally ; - "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. ; Motor sequence starting from zero crossing: ; - Timer wait: Wt_Comm 15deg ; Time to wait from zero cross to actual commutation ; - Timer wait: Wt_Advance 15deg ; Time to wait for timing advance. Nominal commutation point is after this ; - Timer wait: Wt_Zc_Scan 7.5deg ; Time to wait before looking for zero cross ; - Scan for zero cross 22.5deg , Nominal, with some motor variations ; ; Motor startup: ; Startup is the only phase, before normal bemf commutation run begins. ; ;**** **** **** **** **** ; Select the ESC and mode to use (or unselect all for use with external batch compile file); ;#define BLUESERIES_12A_MAIN ;#define BLUESERIES_12A_TAIL ;#define BLUESERIES_12A_MULTI ;#define BLUESERIES_20A_MAIN ;#define BLUESERIES_20A_TAIL ;#define BLUESERIES_20A_MULTI ;#define BLUESERIES_30A_MAIN ;#define BLUESERIES_30A_TAIL ;#define BLUESERIES_30A_MULTI ;#define BLUESERIES_40A_MAIN ;#define BLUESERIES_40A_TAIL ;#define BLUESERIES_40A_MULTI ;#define BLUESERIES_60A_MAIN ;#define BLUESERIES_60A_TAIL ;#define BLUESERIES_60A_MULTI ;#define BLUESERIES_70A_MAIN ;#define BLUESERIES_70A_TAIL ;#define BLUESERIES_70A_MULTI ;#define HK_UBEC_6A_MAIN ;#define HK_UBEC_6A_TAIL ;#define HK_UBEC_6A_MULTI ;#define HK_UBEC_10A_MAIN ;#define HK_UBEC_10A_TAIL ;#define HK_UBEC_10A_MULTI ;#define HK_UBEC_20A_MAIN ;#define HK_UBEC_20A_TAIL ;#define HK_UBEC_20A_MULTI ;#define HK_UBEC_30A_MAIN ;#define HK_UBEC_30A_TAIL ;#define HK_UBEC_30A_MULTI ;#define HK_UBEC_40A_MAIN ;#define HK_UBEC_40A_TAIL ;#define HK_UBEC_40A_MULTI ;#define SUPERSIMPLE_18A_MAIN ;#define SUPERSIMPLE_18A_TAIL ;#define SUPERSIMPLE_18A_MULTI ;#define SUPERSIMPLE_20A_MAIN ;#define SUPERSIMPLE_20A_TAIL ;#define SUPERSIMPLE_20A_MULTI ;#define SUPERSIMPLE_30A_MAIN ;#define SUPERSIMPLE_30A_TAIL ;#define SUPERSIMPLE_30A_MULTI ;#define SUPERSIMPLE_40A_MAIN ;#define SUPERSIMPLE_40A_TAIL ;#define SUPERSIMPLE_40A_MULTI ;#define MULTISTAR_15A_MAIN ; Inverted input ;#define MULTISTAR_15A_TAIL ;#define MULTISTAR_15A_MULTI ;#define MULTISTAR_20A_MAIN ; Inverted input ;#define MULTISTAR_20A_TAIL ;#define MULTISTAR_20A_MULTI ;#define MULTISTAR_20A_NFET_MAIN ; Inverted input ;#define MULTISTAR_20A_NFET_TAIL ;#define MULTISTAR_20A_NFET_MULTI ;#define MULTISTAR_30A_MAIN ; Inverted input ;#define MULTISTAR_30A_TAIL ;#define MULTISTAR_30A_MULTI ;#define MULTISTAR_45A_MAIN ; Inverted input ;#define MULTISTAR_45A_TAIL ;#define MULTISTAR_45A_MULTI ;#define MYSTERY_12A_MAIN ;#define MYSTERY_12A_TAIL ;#define MYSTERY_12A_MULTI ;#define MYSTERY_30A_MAIN ;#define MYSTERY_30A_TAIL ;#define MYSTERY_30A_MULTI ;#define SUNRISE_HIMULTI_20A_MAIN ; Inverted input ;#define SUNRISE_HIMULTI_20A_TAIL ;#define SUNRISE_HIMULTI_20A_MULTI ;#define SUNRISE_HIMULTI_30A_MAIN ; Inverted input ;#define SUNRISE_HIMULTI_30A_TAIL ;#define SUNRISE_HIMULTI_30A_MULTI ;#define SUNRISE_HIMULTI_40A_MAIN ; Inverted input ;#define SUNRISE_HIMULTI_40A_TAIL ;#define SUNRISE_HIMULTI_40A_MULTI ;#define RCTIMER_40A_MAIN ;#define RCTIMER_40A_TAIL ;#define RCTIMER_40A_MULTI ;#define RCTIMER_NFS_30A_MAIN ; ICP1 as input ;#define RCTIMER_NFS_30A_TAIL ;#define RCTIMER_NFS_30A_MULTI ;#define YEP_7A_MAIN ;#define YEP_7A_TAIL ;#define YEP_7A_MULTI ;#define AFRO_12A_MAIN ; ICP1 as input ;#define AFRO_12A_TAIL ;#define AFRO_12A_MULTI ;#define AFRO_20A_MAIN ; ICP1 as input ;#define AFRO_20A_TAIL ;#define AFRO_20A_MULTI ;#define AFRO_20A_HV_MAIN ; ICP1 as input ;#define AFRO_20A_HV_TAIL ;#define AFRO_20A_HV_MULTI ;#define AFRO_30A_MAIN ; ICP1 as input ;#define AFRO_30A_TAIL ;#define AFRO_30A_MULTI ;#define SUNRISE_BLHELI_SLIM_20A_MAIN ;#define SUNRISE_BLHELI_SLIM_20A_TAIL ;#define SUNRISE_BLHELI_SLIM_20A_MULTI ;#define SUNRISE_BLHELI_SLIM_30A_MAIN ;#define SUNRISE_BLHELI_SLIM_30A_TAIL ;#define SUNRISE_BLHELI_SLIM_30A_MULTI ;**** **** **** **** **** ; ESC selection statements #if defined(BLUESERIES_12A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "BlueSeries_12A.inc" ; Select BlueSeries 12A pinout #endif #if defined(BLUESERIES_12A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "BlueSeries_12A.inc" ; Select BlueSeries 12A pinout #endif #if defined(BLUESERIES_12A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "BlueSeries_12A.inc" ; Select BlueSeries 12A pinout #endif #if defined(BLUESERIES_20A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "BlueSeries_20A.inc" ; Select BlueSeries 20A pinout #endif #if defined(BLUESERIES_20A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "BlueSeries_20A.inc" ; Select BlueSeries 20A pinout #endif #if defined(BLUESERIES_20A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "BlueSeries_20A.inc" ; Select BlueSeries 20A pinout #endif #if defined(BLUESERIES_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "BlueSeries_30A.inc" ; Select BlueSeries 30A pinout #endif #if defined(BLUESERIES_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "BlueSeries_30A.inc" ; Select BlueSeries 30A pinout #endif #if defined(BLUESERIES_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "BlueSeries_30A.inc" ; Select BlueSeries 30A pinout #endif #if defined(BLUESERIES_40A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "BlueSeries_40A.inc" ; Select BlueSeries 40A pinout #endif #if defined(BLUESERIES_40A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "BlueSeries_40A.inc" ; Select BlueSeries 40A pinout #endif #if defined(BLUESERIES_40A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "BlueSeries_40A.inc" ; Select BlueSeries 40A pinout #endif #if defined(BLUESERIES_60A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "BlueSeries_60A.inc" ; Select BlueSeries 60A pinout #endif #if defined(BLUESERIES_60A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "BlueSeries_60A.inc" ; Select BlueSeries 60A pinout #endif #if defined(BLUESERIES_60A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "BlueSeries_60A.inc" ; Select BlueSeries 60A pinout #endif #if defined(BLUESERIES_70A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "BlueSeries_70A.inc" ; Select BlueSeries 70A pinout #endif #if defined(BLUESERIES_70A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "BlueSeries_70A.inc" ; Select BlueSeries 70A pinout #endif #if defined(BLUESERIES_70A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "BlueSeries_70A.inc" ; Select BlueSeries 70A pinout #endif #if defined(HK_UBEC_6A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "HK_UBEC_6A.inc" ; Select Hobbyking UBEC 6A pinout #endif #if defined(HK_UBEC_6A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "HK_UBEC_6A.inc" ; Select Hobbyking UBEC 6A pinout #endif #if defined(HK_UBEC_6A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "HK_UBEC_6A.inc" ; Select Hobbyking UBEC 6A pinout #endif #if defined(HK_UBEC_10A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "HK_UBEC_10A.inc" ; Select Hobbyking UBEC 10A pinout #endif #if defined(HK_UBEC_10A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "HK_UBEC_10A.inc" ; Select Hobbyking UBEC 10A pinout #endif #if defined(HK_UBEC_10A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "HK_UBEC_10A.inc" ; Select Hobbyking UBEC 10A pinout #endif #if defined(HK_UBEC_20A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "HK_UBEC_20A.inc" ; Select Hobbyking UBEC 20A pinout #endif #if defined(HK_UBEC_20A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "HK_UBEC_20A.inc" ; Select Hobbyking UBEC 20A pinout #endif #if defined(HK_UBEC_20A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "HK_UBEC_20A.inc" ; Select Hobbyking UBEC 20A pinout #endif #if defined(HK_UBEC_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "HK_UBEC_30A.inc" ; Select Hobbyking UBEC 30A pinout #endif #if defined(HK_UBEC_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "HK_UBEC_30A.inc" ; Select Hobbyking UBEC 30A pinout #endif #if defined(HK_UBEC_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "HK_UBEC_30A.inc" ; Select Hobbyking UBEC 30A pinout #endif #if defined(HK_UBEC_40A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "HK_UBEC_40A.inc" ; Select Hobbyking UBEC 40A pinout #endif #if defined(HK_UBEC_40A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "HK_UBEC_40A.inc" ; Select Hobbyking UBEC 40A pinout #endif #if defined(HK_UBEC_40A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "HK_UBEC_40A.inc" ; Select Hobbyking UBEC 40A pinout #endif #if defined(SUPERSIMPLE_18A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "SuperSimple_18A.inc" ; Select SuperSimple 18A pinout #endif #if defined(SUPERSIMPLE_18A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "SuperSimple_18A.inc" ; Select SuperSimple 18A pinout #endif #if defined(SUPERSIMPLE_18A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "SuperSimple_18A.inc" ; Select SuperSimple 18A pinout #endif #if defined(SUPERSIMPLE_20A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "SuperSimple_20A.inc" ; Select SuperSimple 20A pinout #endif #if defined(SUPERSIMPLE_20A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "SuperSimple_20A.inc" ; Select SuperSimple 20A pinout #endif #if defined(SUPERSIMPLE_20A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "SuperSimple_20A.inc" ; Select SuperSimple 20A pinout #endif #if defined(SUPERSIMPLE_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "SuperSimple_30A.inc" ; Select SuperSimple 30A pinout #endif #if defined(SUPERSIMPLE_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "SuperSimple_30A.inc" ; Select SuperSimple 30A pinout #endif #if defined(SUPERSIMPLE_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "SuperSimple_30A.inc" ; Select SuperSimple 30A pinout #endif #if defined(SUPERSIMPLE_40A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "SuperSimple_40A.inc" ; Select SuperSimple 40A pinout #endif #if defined(SUPERSIMPLE_40A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "SuperSimple_40A.inc" ; Select SuperSimple 40A pinout #endif #if defined(SUPERSIMPLE_40A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "SuperSimple_40A.inc" ; Select SuperSimple 40A pinout #endif #if defined(MULTISTAR_15A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Multistar_15A.inc" ; Select Multistar 15A pinout #endif #if defined(MULTISTAR_15A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Multistar_15A.inc" ; Select Multistar 15A pinout #endif #if defined(MULTISTAR_15A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Multistar_15A.inc" ; Select Multistar 15A pinout #endif #if defined(MULTISTAR_20A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Multistar_20A.inc" ; Select Multistar 20A pinout #endif #if defined(MULTISTAR_20A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Multistar_20A.inc" ; Select Multistar 20A pinout #endif #if defined(MULTISTAR_20A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Multistar_20A.inc" ; Select Multistar 20A pinout #endif #if defined(MULTISTAR_20A_NFET_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Multistar_20A_NFET.inc" ; Select Multistar 20A NFET pinout #endif #if defined(MULTISTAR_20A_NFET_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Multistar_20A_NFET.inc" ; Select Multistar 20A NFET pinout #endif #if defined(MULTISTAR_20A_NFET_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Multistar_20A_NFET.inc" ; Select Multistar 20A NFET pinout #endif #if defined(MULTISTAR_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Multistar_30A.inc" ; Select Multistar 30A pinout #endif #if defined(MULTISTAR_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Multistar_30A.inc" ; Select Multistar 30A pinout #endif #if defined(MULTISTAR_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Multistar_30A.inc" ; Select Multistar 30A pinout #endif #if defined(MULTISTAR_45A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Multistar_45A.inc" ; Select Multistar 45A pinout #endif #if defined(MULTISTAR_45A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Multistar_45A.inc" ; Select Multistar 45A pinout #endif #if defined(MULTISTAR_45A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Multistar_45A.inc" ; Select Multistar 45A pinout #endif #if defined(MYSTERY_12A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Mystery_12A.inc" ; Select Mystery 12A pinout #endif #if defined(MYSTERY_12A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Mystery_12A.inc" ; Select Mystery 12A pinout #endif #if defined(MYSTERY_12A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Mystery_12A.inc" ; Select Mystery 12A pinout #endif #if defined(MYSTERY_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Mystery_30A.inc" ; Select Mystery 30A pinout #endif #if defined(MYSTERY_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Mystery_30A.inc" ; Select Mystery 30A pinout #endif #if defined(MYSTERY_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Mystery_30A.inc" ; Select Mystery 30A pinout #endif #if defined(SUNRISE_HIMULTI_20A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Sunrise_HiMulti_20A.inc" ; Select Sunrise HiMulti 20A pinout #endif #if defined(SUNRISE_HIMULTI_20A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Sunrise_HiMulti_20A.inc" ; Select Sunrise HiMulti 20A pinout #endif #if defined(SUNRISE_HIMULTI_20A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Sunrise_HiMulti_20A.inc" ; Select Sunrise HiMulti 20A pinout #endif #if defined(SUNRISE_HIMULTI_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Sunrise_HiMulti_30A.inc" ; Select Sunrise HiMulti 30A pinout #endif #if defined(SUNRISE_HIMULTI_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Sunrise_HiMulti_30A.inc" ; Select Sunrise HiMulti 30A pinout #endif #if defined(SUNRISE_HIMULTI_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Sunrise_HiMulti_30A.inc" ; Select Sunrise HiMulti 30A pinout #endif #if defined(SUNRISE_HIMULTI_40A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Sunrise_HiMulti_40A.inc" ; Select Sunrise HiMulti 40A pinout #endif #if defined(SUNRISE_HIMULTI_40A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Sunrise_HiMulti_40A.inc" ; Select Sunrise HiMulti 40A pinout #endif #if defined(SUNRISE_HIMULTI_40A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Sunrise_HiMulti_40A.inc" ; Select Sunrise HiMulti 40A pinout #endif #if defined(RCTIMER_40A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "RCTimer_40A.inc" ; Select RCTimer 40A pinout #endif #if defined(RCTIMER_40A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "RCTimer_40A.inc" ; Select RCTimer 40A pinout #endif #if defined(RCTIMER_40A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "RCTimer_40A.inc" ; Select RCTimer 40A pinout #endif #if defined(RCTIMER_NFS_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "RCTimer_NFS_30A.inc" ; Select RCTimer NFS 30A pinout #endif #if defined(RCTIMER_NFS_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "RCTimer_NFS_30A.inc" ; Select RCTimer NFS 30A pinout #endif #if defined(RCTIMER_NFS_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "RCTimer_NFS_30A.inc" ; Select RCTimer NFS 30A pinout #endif #if defined(YEP_7A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "YEP_7A.inc" ; Select YEP 7A pinout #endif #if defined(YEP_7A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "YEP_7A.inc" ; Select YEP 7A pinout #endif #if defined(YEP_7A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "YEP_7A.inc" ; Select YEP 7A pinout #endif #if defined(AFRO_12A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "AFRO_12A.inc" ; Select AFRO 12A pinout #endif #if defined(AFRO_12A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "AFRO_12A.inc" ; Select AFRO 12A pinout #endif #if defined(AFRO_12A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "AFRO_12A.inc" ; Select AFRO 12A pinout #endif #if defined(AFRO_20A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "AFRO_20A.inc" ; Select AFRO 20A pinout #endif #if defined(AFRO_20A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "AFRO_20A.inc" ; Select AFRO 20A pinout #endif #if defined(AFRO_20A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "AFRO_20A.inc" ; Select AFRO 20A pinout #endif #if defined(AFRO_20A_HV_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "AFRO_20A_HV.inc" ; Select AFRO 20A HV pinout #endif #if defined(AFRO_20A_HV_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "AFRO_20A_HV.inc" ; Select AFRO 20A HV pinout #endif #if defined(AFRO_20A_HV_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "AFRO_20A_HV.inc" ; Select AFRO 20A HV pinout #endif #if defined(AFRO_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "AFRO_30A.inc" ; Select AFRO 30A pinout #endif #if defined(AFRO_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "AFRO_30A.inc" ; Select AFRO 30A pinout #endif #if defined(AFRO_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "AFRO_30A.inc" ; Select AFRO 30A pinout #endif #if defined(SUNRISE_BLHELI_SLIM_20A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Sunrise_BLHeli_Slim_20A.inc" ; Select Sunrise BLHeli slim 20A pinout #endif #if defined(SUNRISE_BLHELI_SLIM_20A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Sunrise_BLHeli_Slim_20A.inc" ; Select Sunrise BLHeli slim 20A pinout #endif #if defined(SUNRISE_BLHELI_SLIM_20A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Sunrise_BLHeli_Slim_20A.inc" ; Select Sunrise BLHeli slim 20A pinout #endif #if defined(SUNRISE_BLHELI_SLIM_30A_MAIN) .EQU MODE = 0 ; Choose mode. Set to 0 for main motor .INCLUDE "Sunrise_BLHeli_Slim_30A.inc" ; Select Sunrise BLHeli slim 30A pinout #endif #if defined(SUNRISE_BLHELI_SLIM_30A_TAIL) .EQU MODE = 1 ; Choose mode. Set to 1 for tail motor .INCLUDE "Sunrise_BLHeli_Slim_30A.inc" ; Select Sunrise BLHeli slim 30A pinout #endif #if defined(SUNRISE_BLHELI_SLIM_30A_MULTI) .EQU MODE = 2 ; Choose mode. Set to 2 for multirotor .INCLUDE "Sunrise_BLHeli_Slim_30A.inc" ; Select Sunrise BLHeli slim 30A pinout #endif ;**** **** **** **** **** ; TX programming defaults ; ; Parameter dependencies: ; - Governor P gain, I gain and Range is only used if one of the three governor modes is selected ; - Governor setup target is only used if Setup governor mode is selected (or closed loop mode is on for multi) ; - Startup rpm and startup accel is only used if stepped startup method is selected ; - Damping force is only used if DampedLight or Damped is selected ; ; Main .EQU DEFAULT_PGM_MAIN_P_GAIN = 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 .EQU DEFAULT_PGM_MAIN_I_GAIN = 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 .EQU DEFAULT_PGM_MAIN_GOVERNOR_MODE = 1 ; 1=Tx 2=Arm 3=Setup 4=Off .EQU DEFAULT_PGM_MAIN_GOVERNOR_RANGE = 1 ; 1=High 2=Middle 3=Low .EQU DEFAULT_PGM_MAIN_LOW_VOLTAGE_LIM = 4 ; 1=Off 2=3.0V/c 3=3.1V/c 4=3.2V/c 5=3.3V/c 6=3.4V/c .EQU DEFAULT_PGM_MAIN_COMM_TIMING = 3 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High .EQU DEFAULT_PGM_MAIN_THROTTLE_RATE = 13 ; 1=2 2=3 3=4 4=6 5=8 6=12 7=16 8=24 9=32 10=48 11=64 12=128 13=255 .EQU DEFAULT_PGM_MAIN_DAMPING_FORCE = 1 ; 1=VeryLow 2=Low 3=MediumLow 4=MediumHigh 5=High 6=Highest .IF DAMPED_MODE_ENABLE == 1 .EQU DEFAULT_PGM_MAIN_PWM_FREQ = 2 ; 1=High 2=Low 3=DampedLight .ELSE .EQU DEFAULT_PGM_MAIN_PWM_FREQ = 2 ; 1=High 2=Low .ENDIF .EQU DEFAULT_PGM_MAIN_DEMAG_COMP = 1 ; 1=Disabled 2=Low 3=High .EQU DEFAULT_PGM_MAIN_DIRECTION = 1 ; 1=Normal 2=Reversed .EQU DEFAULT_PGM_MAIN_RCP_PWM_POL = 1 ; 1=Positive 2=Negative .EQU DEFAULT_PGM_MAIN_GOV_SETUP_TARGET = 180; Target for governor in setup mode. Corresponds to 70% throttle .EQU DEFAULT_PGM_MAIN_REARM_START = 0 ; 1=Enabled 0=Disabled .EQU DEFAULT_PGM_MAIN_BEEP_STRENGTH = 120; Beep strength .EQU DEFAULT_PGM_MAIN_BEACON_STRENGTH = 200; Beacon strength .EQU DEFAULT_PGM_MAIN_BEACON_DELAY = 4 ; 1=1m 2=2m 3=5m 4=10m 5=Infinite ; Tail .EQU DEFAULT_PGM_TAIL_GAIN = 3 ; 1=0.75 2=0.88 3=1.00 4=1.12 5=1.25 .EQU DEFAULT_PGM_TAIL_IDLE_SPEED = 4 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High .EQU DEFAULT_PGM_TAIL_COMM_TIMING = 3 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High .EQU DEFAULT_PGM_TAIL_THROTTLE_RATE = 13 ; 1=2 2=3 3=4 4=6 5=8 6=12 7=16 8=24 9=32 10=48 11=64 12=128 13=255 .EQU DEFAULT_PGM_TAIL_DAMPING_FORCE = 6 ; 1=VeryLow 2=Low 3=MediumLow 4=MediumHigh 5=High 6=Highest .IF DAMPED_MODE_ENABLE == 1 .EQU DEFAULT_PGM_TAIL_PWM_FREQ = 3 ; 1=High 2=Low 3=DampedLight .ELSE .EQU DEFAULT_PGM_TAIL_PWM_FREQ = 1 ; 1=High 2=Low .ENDIF .EQU DEFAULT_PGM_TAIL_DEMAG_COMP = 1 ; 1=Disabled 2=Low 3=High .EQU DEFAULT_PGM_TAIL_DIRECTION = 1 ; 1=Normal 2=Reversed 3=Bidirectional .EQU DEFAULT_PGM_TAIL_RCP_PWM_POL = 1 ; 1=Positive 2=Negative .EQU DEFAULT_PGM_TAIL_BEEP_STRENGTH = 250; Beep strength .EQU DEFAULT_PGM_TAIL_BEACON_STRENGTH = 250; Beacon strength .EQU DEFAULT_PGM_TAIL_BEACON_DELAY = 4 ; 1=1m 2=2m 3=5m 4=10m 5=Infinite ; Multi .EQU DEFAULT_PGM_MULTI_P_GAIN = 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 .EQU DEFAULT_PGM_MULTI_I_GAIN = 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 .EQU DEFAULT_PGM_MULTI_GOVERNOR_MODE = 4 ; 1=HiRange 2=MidRange 3=LoRange 4=Off .EQU DEFAULT_PGM_MULTI_GAIN = 3 ; 1=0.75 2=0.88 3=1.00 4=1.12 5=1.25 .EQU DEFAULT_PGM_MULTI_LOW_VOLTAGE_LIM = 1 ; 1=Off 2=3.0V/c 3=3.1V/c 4=3.2V/c 5=3.3V/c 6=3.4V/c .EQU DEFAULT_PGM_MULTI_COMM_TIMING = 3 ; 1=Low 2=MediumLow 3=Medium 4=MediumHigh 5=High .EQU DEFAULT_PGM_MULTI_THROTTLE_RATE = 13 ; 1=2 2=3 3=4 4=6 5=8 6=12 7=16 8=24 9=32 10=48 11=64 12=128 13=255 .EQU DEFAULT_PGM_MULTI_DAMPING_FORCE = 6 ; 1=VeryLow 2=Low 3=MediumLow 4=MediumHigh 5=High 6=Highest .IF DAMPED_MODE_ENABLE == 1 .EQU DEFAULT_PGM_MULTI_PWM_FREQ = 1 ; 1=High 2=Low 3=DampedLight .ELSE .EQU DEFAULT_PGM_MULTI_PWM_FREQ = 1 ; 1=High 2=Low .ENDIF .EQU DEFAULT_PGM_MULTI_DEMAG_COMP = 2 ; 1=Disabled 2=Low 3=High .EQU DEFAULT_PGM_MULTI_DIRECTION = 1 ; 1=Normal 2=Reversed 3=Bidirectional .EQU DEFAULT_PGM_MULTI_RCP_PWM_POL = 1 ; 1=Positive 2=Negative .EQU DEFAULT_PGM_MULTI_BEEP_STRENGTH = 40 ; Beep strength .EQU DEFAULT_PGM_MULTI_BEACON_STRENGTH = 80 ; Beacon strength .EQU DEFAULT_PGM_MULTI_BEACON_DELAY = 4 ; 1=1m 2=2m 3=5m 4=10m 5=Infinite ; Common .EQU DEFAULT_PGM_ENABLE_TX_PROGRAM = 1 ; 1=Enabled 0=Disabled .EQU DEFAULT_PGM_PPM_MIN_THROTTLE = 37 ; 4*37+1000=1148 .EQU DEFAULT_PGM_PPM_MAX_THROTTLE = 208; 4*208+1000=1832 .EQU DEFAULT_PGM_PPM_CENTER_THROTTLE = 122; 4*122+1000=1488 (used in bidirectional mode) .EQU DEFAULT_PGM_BEC_VOLTAGE_HIGH = 0 ; 0=Low 1= High .EQU DEFAULT_PGM_ENABLE_TEMP_PROT = 1 ; 1=Enabled 0=Disabled ;**** **** **** **** **** ; Constant definitions for main .IF MODE == 0 .EQU GOV_SPOOLRATE = 2 ; Number of steps for governor requested pwm per 32ms .EQU RCP_TIMEOUT_PPM = 10 ; Number of timer2H overflows (about 32ms) before considering rc pulse lost .EQU RCP_TIMEOUT = 64 ; Number of timer2L overflows (about 128us) before considering rc pulse lost .EQU RCP_SKIP_RATE = 32 ; Number of timer2L overflows (about 128us) before reenabling rc pulse detection .EQU RCP_MIN = 0 ; This is minimum RC pulse length .EQU RCP_MAX = 255 ; This is maximum RC pulse length .EQU RCP_VALIDATE = 2 ; Require minimum this pulse length to validate RC pulse .EQU RCP_STOP = 1 ; Stop motor at or below this pulse length .EQU RCP_STOP_LIMIT = 250 ; Stop motor if this many timer2H overflows (~32ms) are below stop limit .EQU PWM_START = 50 ; PWM used as max power during start .EQU COMM_TIME_RED = 6 ; Fixed reduction (in us) for commutation wait (to account for fixed delays) .EQU COMM_TIME_MIN = 1 ; Minimum time (in us) for commutation wait .EQU TEMP_CHECK_RATE = 8 ; Number of adc conversions for each check of temperature (the other conversions are used for voltage) .ENDIF ; Constant definitions for tail .IF MODE == 1 .EQU GOV_SPOOLRATE = 1 ; Number of steps for governor requested pwm per 32ms .EQU RCP_TIMEOUT_PPM = 10 ; Number of timer2H overflows (about 32ms) before considering rc pulse lost .EQU RCP_TIMEOUT = 24 ; Number of timer2L overflows (about 128us) before considering rc pulse lost .EQU RCP_SKIP_RATE = 6 ; Number of timer2L overflows (about 128us) before reenabling rc pulse detection .EQU RCP_MIN = 0 ; This is minimum RC pulse length .EQU RCP_MAX = 255 ; This is maximum RC pulse length .EQU RCP_VALIDATE = 2 ; Require minimum this pulse length to validate RC pulse .EQU RCP_STOP = 1 ; Stop motor at or below this pulse length .EQU RCP_STOP_LIMIT = 130 ; Stop motor if this many timer2H overflows (~32ms) are below stop limit .EQU PWM_START = 50 ; PWM used as max power during start .EQU COMM_TIME_RED = 6 ; Fixed reduction (in us) for commutation wait (to account for fixed delays) .EQU COMM_TIME_MIN = 1 ; Minimum time (in us) for commutation wait .EQU TEMP_CHECK_RATE = 8 ; Number of adc conversions for each check of temperature (the other conversions are used for voltage) .ENDIF ; Constant definitions for multi .IF MODE == 2 .EQU GOV_SPOOLRATE = 1 ; Number of steps for governor requested pwm per 32ms .EQU RCP_TIMEOUT_PPM = 10 ; Number of timer2H overflows (about 32ms) before considering rc pulse lost .EQU RCP_TIMEOUT = 24 ; Number of timer2L overflows (about 128us) before considering rc pulse lost .EQU RCP_SKIP_RATE = 6 ; Number of timer2L overflows (about 128us) before reenabling rc pulse detection .EQU RCP_MIN = 0 ; This is minimum RC pulse length .EQU RCP_MAX = 255 ; This is maximum RC pulse length .EQU RCP_VALIDATE = 2 ; Require minimum this pulse length to validate RC pulse .EQU RCP_STOP = 1 ; Stop motor at or below this pulse length .EQU RCP_STOP_LIMIT = 250 ; Stop motor if this many timer2H overflows (~32ms) are below stop limit .EQU PWM_START = 50 ; PWM used as max power during start .EQU COMM_TIME_RED = 6 ; Fixed reduction (in us) for commutation wait (to account for fixed delays) .EQU COMM_TIME_MIN = 1 ; Minimum time (in us) for commutation wait .EQU TEMP_CHECK_RATE = 8 ; Number of adc conversions for each check of temperature (the other conversions are used for voltage) .ENDIF ;**** **** **** **** **** ; Register definitions .DEF Mul_Res_L = R0 ; Reserved for mul instruction .DEF Mul_Res_H = R1 ; Reserved for mul instruction .DEF Zero = R2 ; Register variable initialized to 0, always at 0 .DEF I_Sreg = R3 ; Status register saved in interrupts .DEF II_Sreg = R4 ; Status register saved in nested interrupts (pwm interrupts from timer2) .DEF Current_Pwm_Limited = R5 ; Current_Pwm_Limited is allocated to a register for fast access .DEF Temp1 = R16 ; Main temporary .DEF Temp2 = R17 ; Main temporary (Temp1/2 must be two consecutive registers) .DEF Temp3 = R18 ; Main temporary .DEF Temp4 = R19 ; Main temporary .DEF Temp5 = R6 ; Main temporary (limited operations) .DEF Temp6 = R7 ; Main temporary (limited operations) .DEF Temp7 = R8 ; Main temporary (limited operations) .DEF Temp8 = R9 ; Main temporary (limited operations) .DEF I_Temp1 = R20 ; Interrupt temporary .DEF I_Temp2 = R21 ; Interrupt temporary .DEF I_Temp3 = R10 ; Interrupt temporary (limited operations) .DEF I_Temp4 = R11 ; Interrupt temporary (limited operations) .DEF I_Temp5 = R12 ; Interrupt temporary (limited operations) .DEF I_Temp6 = R13 ; Interrupt temporary (limited operations) .DEF I_Temp7 = R14 ; Interrupt temporary (limited operations) .DEF I_Temp8 = R15 ; Interrupt temporary (limited operations) .DEF Flags0 = R22 ; State flags. Reset upon init_start .EQU OC1A_PENDING = 0 ; Timer1 output compare pending flag .EQU RCP_MEAS_PWM_FREQ = 1 ; Measure RC pulse pwm frequency .EQU PWM_ON = 2 ; Set in on part of pwm cycle .EQU DEMAG_ENABLED = 3 ; Set when demag compensation is enabled (above a min speed and throttle) .EQU DEMAG_DETECTED = 4 ; Set when excessive demag time is detected .EQU DEMAG_CUT_POWER = 5 ; Set when demag compensation cuts power .EQU GOV_ACTIVE = 6 ; Set when governor is active .EQU DIR_CHANGE_BRAKE = 7 ; Set when braking before direction change .DEF Flags1 = R23 ; State flags. Reset upon init_start .EQU MOTOR_SPINNING = 0 ; Set when in motor is spinning .EQU STARTUP_PHASE = 1 ; Set when in startup phase .EQU INITIAL_RUN_PHASE = 2 ; Set when in initial run phase, before synchronized run is achieved .EQU CURR_PWMOFF_DAMPED = 3 ; Currently running pwm off cycle is damped .EQU ADC_READ_TEMP = 4 ; Set when ADC input shall be set to read temperature ;.EQU = 5 ;.EQU = 6 ;.EQU = 7 .DEF Flags2 = R24 ; State flags. NOT reset upon init_start .EQU RCP_UPDATED = 0 ; New RC pulse length value available .EQU RCP_EDGE_NO = 1 ; RC pulse edge no. 0=rising, 1=falling .EQU PGM_PWMOFF_DAMPED = 2 ; Programmed pwm off damped mode .EQU PGM_PWM_HIGH_FREQ = 3 ; Progremmed pwm high frequency .EQU RCP_INT_NESTED_ENABLED = 4 ; Set when RC pulse interrupt is enabled around nested interrupts ;.EQU = 5 ;.EQU = 6 ;.EQU = 7 .DEF Flags3 = R25 ; State flags. NOT reset upon init_start .EQU RCP_PWM_FREQ_1KHZ = 0 ; RC pulse pwm frequency is 1kHz .EQU RCP_PWM_FREQ_2KHZ = 1 ; RC pulse pwm frequency is 2kHz .EQU RCP_PWM_FREQ_4KHZ = 2 ; RC pulse pwm frequency is 4kHz .EQU RCP_PWM_FREQ_8KHZ = 3 ; RC pulse pwm frequency is 8kHz .EQU RCP_PWM_FREQ_12KHZ = 4 ; RC pulse pwm frequency is 12kHz .EQU PGM_DIR_REV = 5 ; Programmed direction. 0=normal, 1=reversed .EQU PGM_RCP_PWM_POL = 6 ; Programmed RC pulse pwm polarity. 0=positive, 1=negative .EQU FULL_THROTTLE_RANGE = 7 ; When set full throttle range is used (1000-2000us) and stored calibration values are ignored ; Here the general temporary register XYZ are placed (R26-R31) ; XH: General temporary used by main routines ; XL: General temporary used by interrupt routines ; Y: General temporary used by timer2 pwm interrupt routine ; Z: Address of current PWM FET ON routine (eg: pwm_afet_on) ;**** **** **** **** **** ; RAM definitions .DSEG ; Data segment .ORG SRAM_START Timer0_Int_Cnt: .BYTE 1 ; Timer0 interrupt counter Requested_Pwm: .BYTE 1 ; Requested pwm (from RC pulse value) Governor_Req_Pwm: .BYTE 1 ; Governor requested pwm (sets governor target) Current_Pwm: .BYTE 1 ; Current pwm Rcp_Prev_Edge_L: .BYTE 1 ; RC pulse previous edge timer3 timestamp (lo byte) Rcp_Prev_Edge_H: .BYTE 1 ; RC pulse previous edge timer3 timestamp (hi byte) Rcp_Timeout_Cnt: .BYTE 1 ; RC pulse timeout counter (decrementing) Rcp_Skip_Cnt: .BYTE 1 ; RC pulse skip counter (decrementing) Rcp_Edge_Cnt: .BYTE 1 ; RC pulse edge counter Initial_Arm: .BYTE 1 ; Variable that is set during the first arm sequence after power on Power_On_Wait_Cnt_L: .BYTE 1 ; Power on wait counter (lo byte) Power_On_Wait_Cnt_H: .BYTE 1 ; Power on wait counter (hi byte) Startup_Rot_Cnt: .BYTE 1 ; Startup phase rotations counter Startup_Ok_Cnt: .BYTE 1 ; Startup phase ok comparator waits counter (incrementing) Demag_Detected_Metric: .BYTE 1 ; Metric used to gauge demag event frequency Demag_Pwr_Off_Thresh: .BYTE 1 ; Metric threshold above which power is cut Low_Rpm_Pwr_Slope: .BYTE 1 ; Sets the slope of power increase for low rpms Prev_Comm_L: .BYTE 1 ; Previous commutation timer3 timestamp (lo byte) Prev_Comm_H: .BYTE 1 ; Previous commutation timer3 timestamp (hi byte) Comm_Period4x_L: .BYTE 1 ; Timer3 counts between the last 4 commutations (lo byte) Comm_Period4x_H: .BYTE 1 ; Timer3 counts between the last 4 commutations (hi byte) Comm_Phase: .BYTE 1 ; Current commutation phase Comparator_Read_Cnt: .BYTE 1 ; Number of comparator reads done Gov_Target_L: .BYTE 1 ; Governor target (lo byte) Gov_Target_H: .BYTE 1 ; Governor target (hi byte) Gov_Integral_L: .BYTE 1 ; Governor integral error (lo byte) Gov_Integral_H: .BYTE 1 ; Governor integral error (hi byte) Gov_Integral_X: .BYTE 1 ; Governor integral error (ex byte) Gov_Proportional_L: .BYTE 1 ; Governor proportional error (lo byte) Gov_Proportional_H: .BYTE 1 ; Governor proportional error (hi byte) Gov_Prop_Pwm: .BYTE 1 ; Governor calculated new pwm based upon proportional error Gov_Arm_Target: .BYTE 1 ; Governor arm target value Wt_Advance_L: .BYTE 1 ; Timer3 counts for commutation advance timing (lo byte) Wt_Advance_H: .BYTE 1 ; Timer3 counts for commutation advance timing (hi byte) Wt_Zc_Scan_L: .BYTE 1 ; Timer3 counts from commutation to zero cross scan (lo byte) Wt_Zc_Scan_H: .BYTE 1 ; Timer3 counts from commutation to zero cross scan (hi byte) Wt_Comm_L: .BYTE 1 ; Timer3 counts from zero cross to commutation (lo byte) Wt_Comm_H: .BYTE 1 ; Timer3 counts from zero cross to commutation (hi byte) Rcp_PrePrev_Edge_L: .BYTE 1 ; RC pulse pre previous edge pca timestamp (lo byte) Rcp_PrePrev_Edge_H: .BYTE 1 ; RC pulse pre previous edge pca timestamp (hi byte) Rcp_Edge_L: .BYTE 1 ; RC pulse edge pca timestamp (lo byte) Rcp_Edge_H: .BYTE 1 ; RC pulse edge pca timestamp (hi byte) Rcp_Prev_Period_L: .BYTE 1 ; RC pulse previous period (lo byte) Rcp_Prev_Period_H: .BYTE 1 ; RC pulse previous period (hi byte) Rcp_Period_Diff_Accepted: .BYTE 1 ; RC pulse period difference acceptable New_Rcp: .BYTE 1 ; New RC pulse value in pca counts Prev_Rcp_Pwm_Freq: .BYTE 1 ; Previous RC pulse pwm frequency (used during pwm frequency measurement) Curr_Rcp_Pwm_Freq: .BYTE 1 ; Current RC pulse pwm frequency (used during pwm frequency measurement) Rcp_Stop_Cnt: .BYTE 1 ; Counter for RC pulses below stop value (lo byte) Auto_Bailout_Armed: .BYTE 1 ; Set when auto rotation bailout is armed Pwm_Limit: .BYTE 1 ; Maximum allowed pwm Pwm_Limit_Spoolup: .BYTE 1 ; Maximum allowed pwm during spoolup of main Pwm_Limit_Low_Rpm: .BYTE 1 ; Maximum allowed pwm for low rpms Pwm_Spoolup_Beg: .BYTE 1 ; Pwm to begin main spoolup with Pwm_Motor_Idle: .BYTE 1 ; Motor idle speed pwm Pwm_On_Cnt: .BYTE 1 ; Pwm on event counter (used to increase pwm off time for low pwm) Pwm_Off_Cnt: .BYTE 1 ; Pwm off event counter (used to run some pwm cycles without damping) Pwm_Prev_Edge: .BYTE 1 ; Timestamp from timer 2 when pwm toggles on or off Spoolup_Limit_Cnt: .BYTE 1 ; Interrupt count for spoolup limit Spoolup_Limit_Skip: .BYTE 1 ; Interrupt skips for spoolup limit increment (1=no skips, 2=skip one etc) Main_Spoolup_Time_3x: .BYTE 1 ; Main spoolup time x3 Main_Spoolup_Time_10x: .BYTE 1 ; Main spoolup time x10 Main_Spoolup_Time_15x: .BYTE 1 ; Main spoolup time x15 Damping_Period: .BYTE 1 ; Damping on/off period Damping_On: .BYTE 1 ; Damping on part of damping period Lipo_Adc_Reference_L: .BYTE 1 ; Voltage reference adc value (lo byte) Lipo_Adc_Reference_H: .BYTE 1 ; Voltage reference adc value (hi byte) Lipo_Adc_Limit_L: .BYTE 1 ; Low voltage limit adc value (lo byte) Lipo_Adc_Limit_H: .BYTE 1 ; Low voltage limit adc value (hi byte) Adc_Conversion_Cnt: .BYTE 1 ; Adc conversion counter Current_Average_Temp_Adc: .BYTE 1 ; Current average temp ADC reading (lo byte of ADC, assuming hi byte is 0) Ppm_Throttle_Gain: .BYTE 1 ; Gain to be applied to RCP value for PPM input Beep_Strength: .BYTE 1 ; Strength of beeps Tx_Pgm_Func_No: .BYTE 1 ; Function number when doing programming by tx Tx_Pgm_Paraval_No: .BYTE 1 ; Parameter value number when doing programming by tx Tx_Pgm_Beep_No: .BYTE 1 ; Beep number when doing programming by tx ; The variables below must be in this sequence Pgm_Gov_P_Gain: .BYTE 1 ; Programmed governor P gain Pgm_Gov_I_Gain: .BYTE 1 ; Programmed governor I gain Pgm_Gov_Mode: .BYTE 1 ; Programmed governor mode Pgm_Low_Voltage_Lim: .BYTE 1 ; Programmed low voltage limit Pgm_Motor_Gain: .BYTE 1 ; Programmed motor gain Pgm_Motor_Idle: .BYTE 1 ; Programmed motor idle speed Pgm_Startup_Pwr: .BYTE 1 ; Programmed startup power Pgm_Pwm_Freq: .BYTE 1 ; Programmed pwm frequency Pgm_Direction: .BYTE 1 ; Programmed rotation direction Pgm_Input_Pol: .BYTE 1 ; Programmed input pwm polarity Initialized_L_Dummy: .BYTE 1 ; Place holder Initialized_H_Dummy: .BYTE 1 ; Place holder Pgm_Enable_TX_Program: .BYTE 1 ; Programmed enable/disable value for TX programming Pgm_Main_Rearm_Start: .BYTE 1 ; Programmed enable/disable re-arming main every start Pgm_Gov_Setup_Target: .BYTE 1 ; Programmed main governor setup target Pgm_Startup_Rpm: .BYTE 1 ; Programmed startup rpm Pgm_Startup_Accel: .BYTE 1 ; Programmed startup acceleration Pgm_Volt_Comp_Dummy: .BYTE 1 ; Place holder Pgm_Comm_Timing: .BYTE 1 ; Programmed commutation timing Pgm_Damping_Force: .BYTE 1 ; Programmed damping force Pgm_Gov_Range: .BYTE 1 ; Programmed governor range Pgm_Startup_Method: .BYTE 1 ; Programmed startup method Pgm_Ppm_Min_Throttle: .BYTE 1 ; Programmed throttle minimum Pgm_Ppm_Max_Throttle: .BYTE 1 ; Programmed throttle maximum Pgm_Beep_Strength: .BYTE 1 ; Programmed beep strength Pgm_Beacon_Strength: .BYTE 1 ; Programmed beacon strength Pgm_Beacon_Delay: .BYTE 1 ; Programmed beacon delay Pgm_Throttle_Rate: .BYTE 1 ; Programmed throttle rate Pgm_Demag_Comp: .BYTE 1 ; Programmed demag compensation Pgm_BEC_Voltage_High: .BYTE 1 ; Programmed BEC voltage Pgm_Ppm_Center_Throttle: .BYTE 1 ; Programmed throttle center (in bidirectional mode) Pgm_Main_Spoolup_Time: .BYTE 1 ; Programmed main spoolup time Pgm_Enable_Temp_Prot: .BYTE 1 ; Programmed temperature protection enable ; The sequence of the variables below is no longer of importance Pgm_Gov_P_Gain_Decoded: .BYTE 1 ; Programmed governor decoded P gain Pgm_Gov_I_Gain_Decoded: .BYTE 1 ; Programmed governor decoded I gain Pgm_Throttle_Rate_Decoded: .BYTE 1 ; Programmed throttle rate decoded Pgm_Startup_Pwr_Decoded: .BYTE 1 ; Programmed startup power decoded .EQU SRAM_BYTES = 255 ; Bytes used in SRAM. Used for number of bytes to reset ;**** **** **** **** **** .ESEG ; Eeprom segment .ORG 0 .EQU EEPROM_FW_MAIN_REVISION = 12 ; Main revision of the firmware .EQU EEPROM_FW_SUB_REVISION = 1 ; Sub revision of the firmware .EQU EEPROM_LAYOUT_REVISION = 18 ; Revision of the EEPROM layout Eep_FW_Main_Revision: .DB EEPROM_FW_MAIN_REVISION ; EEPROM firmware main revision number Eep_FW_Sub_Revision: .DB EEPROM_FW_SUB_REVISION ; EEPROM firmware sub revision number Eep_Layout_Revision: .DB EEPROM_LAYOUT_REVISION ; EEPROM layout revision number .IF MODE == 0 Eep_Pgm_Gov_P_Gain: .DB DEFAULT_PGM_MAIN_P_GAIN ; EEPROM copy of programmed governor P gain Eep_Pgm_Gov_I_Gain: .DB DEFAULT_PGM_MAIN_I_GAIN ; EEPROM copy of programmed governor I gain Eep_Pgm_Gov_Mode: .DB DEFAULT_PGM_MAIN_GOVERNOR_MODE ; EEPROM copy of programmed governor mode Eep_Pgm_Low_Voltage_Lim: .DB DEFAULT_PGM_MAIN_LOW_VOLTAGE_LIM ; EEPROM copy of programmed low voltage limit _Eep_Pgm_Motor_Gain: .DB 0xFF _Eep_Pgm_Motor_Idle: .DB 0xFF Eep_Pgm_Startup_Pwr: .DB DEFAULT_PGM_MAIN_STARTUP_PWR ; EEPROM copy of programmed startup power Eep_Pgm_Pwm_Freq: .DB DEFAULT_PGM_MAIN_PWM_FREQ ; EEPROM copy of programmed pwm frequency Eep_Pgm_Direction: .DB DEFAULT_PGM_MAIN_DIRECTION ; EEPROM copy of programmed rotation direction Eep_Pgm_Input_Pol: .DB DEFAULT_PGM_MAIN_RCP_PWM_POL ; EEPROM copy of programmed input polarity Eep_Initialized_L: .DB 0xA5 ; EEPROM initialized signature low byte Eep_Initialized_H: .DB 0x5A ; EEPROM initialized signature high byte Eep_Enable_TX_Program: .DB DEFAULT_PGM_ENABLE_TX_PROGRAM ; EEPROM TX programming enable Eep_Main_Rearm_Start: .DB DEFAULT_PGM_MAIN_REARM_START ; EEPROM re-arming main enable Eep_Pgm_Gov_Setup_Target: .DB DEFAULT_PGM_MAIN_GOV_SETUP_TARGET ; EEPROM main governor setup target _Eep_Pgm_Startup_Rpm: .DB 0xFF _Eep_Pgm_Startup_Accel: .DB 0xFF _Eep_Pgm_Volt_Comp: .DB 0xFF Eep_Pgm_Comm_Timing: .DB DEFAULT_PGM_MAIN_COMM_TIMING ; EEPROM copy of programmed commutation timing Eep_Pgm_Damping_Force: .DB DEFAULT_PGM_MAIN_DAMPING_FORCE ; EEPROM copy of programmed damping force Eep_Pgm_Gov_Range: .DB DEFAULT_PGM_MAIN_GOVERNOR_RANGE ; EEPROM copy of programmed governor range _Eep_Pgm_Startup_Method: .DB 0xFF Eep_Pgm_Ppm_Min_Throttle: .DB DEFAULT_PGM_PPM_MIN_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1148) Eep_Pgm_Ppm_Max_Throttle: .DB DEFAULT_PGM_PPM_MAX_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1832) Eep_Pgm_Beep_Strength: .DB DEFAULT_PGM_MAIN_BEEP_STRENGTH ; EEPROM copy of programmed beep strength Eep_Pgm_Beacon_Strength: .DB DEFAULT_PGM_MAIN_BEACON_STRENGTH ; EEPROM copy of programmed beacon strength Eep_Pgm_Beacon_Delay: .DB DEFAULT_PGM_MAIN_BEACON_DELAY ; EEPROM copy of programmed beacon delay Eep_Pgm_Throttle_Rate: .DB DEFAULT_PGM_MAIN_THROTTLE_RATE ; EEPROM copy of programmed throttle rate Eep_Pgm_Demag_Comp: .DB DEFAULT_PGM_MAIN_DEMAG_COMP ; EEPROM copy of programmed demag compensation Eep_Pgm_BEC_Voltage_High: .DB DEFAULT_PGM_BEC_VOLTAGE_HIGH ; EEPROM copy of programmed BEC voltage _Eep_Pgm_Ppm_Center_Throttle: .DB 0xFF ; EEPROM copy of programmed center throttle (final value is 4x+1000=1488) Eep_Pgm_Main_Spoolup_Time: .DB DEFAULT_PGM_MAIN_SPOOLUP_TIME ; EEPROM copy of programmed main spoolup time Eep_Pgm_Temp_Prot_Enable: .DB DEFAULT_PGM_ENABLE_TEMP_PROT ; EEPROM copy of programmed temperature protection enable .ENDIF .IF MODE == 1 _Eep_Pgm_Gov_P_Gain: .DB 0xFF _Eep_Pgm_Gov_I_Gain: .DB 0xFF _Eep_Pgm_Gov_Mode: .DB 0xFF _Eep_Pgm_Low_Voltage_Lim: .DB 0xFF Eep_Pgm_Motor_Gain: .DB DEFAULT_PGM_TAIL_GAIN ; EEPROM copy of programmed tail gain Eep_Pgm_Motor_Idle: .DB DEFAULT_PGM_TAIL_IDLE_SPEED ; EEPROM copy of programmed tail idle speed Eep_Pgm_Startup_Pwr: .DB DEFAULT_PGM_TAIL_STARTUP_PWR ; EEPROM copy of programmed startup power Eep_Pgm_Pwm_Freq: .DB DEFAULT_PGM_TAIL_PWM_FREQ ; EEPROM copy of programmed pwm frequency Eep_Pgm_Direction: .DB DEFAULT_PGM_TAIL_DIRECTION ; EEPROM copy of programmed rotation direction Eep_Pgm_Input_Pol: .DB DEFAULT_PGM_TAIL_RCP_PWM_POL ; EEPROM copy of programmed input polarity Eep_Initialized_L: .DB 0x5A ; EEPROM initialized signature low byte Eep_Initialized_H: .DB 0xA5 ; EEPROM initialized signature high byte Eep_Enable_TX_Program: .DB DEFAULT_PGM_ENABLE_TX_PROGRAM ; EEPROM TX programming enable _Eep_Main_Rearm_Start: .DB 0xFF _Eep_Pgm_Gov_Setup_Target: .DB 0xFF _Eep_Pgm_Startup_Rpm: .DB 0xFF _Eep_Pgm_Startup_Accel: .DB 0xFF _Eep_Pgm_Volt_Comp: .DB 0xFF Eep_Pgm_Comm_Timing: .DB DEFAULT_PGM_TAIL_COMM_TIMING ; EEPROM copy of programmed commutation timing Eep_Pgm_Damping_Force: .DB DEFAULT_PGM_TAIL_DAMPING_FORCE ; EEPROM copy of programmed damping force _Eep_Pgm_Gov_Range: .DB 0xFF _Eep_Pgm_Startup_Method: .DB 0xFF Eep_Pgm_Ppm_Min_Throttle: .DB DEFAULT_PGM_PPM_MIN_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1148) Eep_Pgm_Ppm_Max_Throttle: .DB DEFAULT_PGM_PPM_MAX_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1832) Eep_Pgm_Beep_Strength: .DB DEFAULT_PGM_TAIL_BEEP_STRENGTH ; EEPROM copy of programmed beep strength Eep_Pgm_Beacon_Strength: .DB DEFAULT_PGM_TAIL_BEACON_STRENGTH ; EEPROM copy of programmed beacon strength Eep_Pgm_Beacon_Delay: .DB DEFAULT_PGM_TAIL_BEACON_DELAY ; EEPROM copy of programmed beacon delay Eep_Pgm_Throttle_Rate: .DB DEFAULT_PGM_TAIL_THROTTLE_RATE ; EEPROM copy of programmed throttle rate Eep_Pgm_Demag_Comp: .DB DEFAULT_PGM_TAIL_DEMAG_COMP ; EEPROM copy of programmed demag compensation Eep_Pgm_BEC_Voltage_High: .DB DEFAULT_PGM_BEC_VOLTAGE_HIGH ; EEPROM copy of programmed BEC voltage Eep_Pgm_Ppm_Center_Throttle: .DB DEFAULT_PGM_PPM_CENTER_THROTTLE ; EEPROM copy of programmed center throttle (final value is 4x+1000=1488) _Eep_Pgm_Main_Spoolup_Time: .DB 0xFF Eep_Pgm_Temp_Prot_Enable: .DB DEFAULT_PGM_ENABLE_TEMP_PROT ; EEPROM copy of programmed temperature protection enable .ENDIF .IF MODE == 2 Eep_Pgm_Gov_P_Gain: .DB DEFAULT_PGM_MULTI_P_GAIN ; EEPROM copy of programmed closed loop P gain Eep_Pgm_Gov_I_Gain: .DB DEFAULT_PGM_MULTI_I_GAIN ; EEPROM copy of programmed closed loop I gain Eep_Pgm_Gov_Mode: .DB DEFAULT_PGM_MULTI_GOVERNOR_MODE ; EEPROM copy of programmed closed loop mode Eep_Pgm_Low_Voltage_Lim: .DB DEFAULT_PGM_MULTI_LOW_VOLTAGE_LIM ; EEPROM copy of programmed low voltage limit Eep_Pgm_Motor_Gain: .DB DEFAULT_PGM_MULTI_GAIN ; EEPROM copy of programmed tail gain _Eep_Pgm_Motor_Idle: .DB 0xFF ; EEPROM copy of programmed tail idle speed Eep_Pgm_Startup_Pwr: .DB DEFAULT_PGM_MULTI_STARTUP_PWR ; EEPROM copy of programmed startup power Eep_Pgm_Pwm_Freq: .DB DEFAULT_PGM_MULTI_PWM_FREQ ; EEPROM copy of programmed pwm frequency Eep_Pgm_Direction: .DB DEFAULT_PGM_MULTI_DIRECTION ; EEPROM copy of programmed rotation direction Eep_Pgm_Input_Pol: .DB DEFAULT_PGM_MULTI_RCP_PWM_POL ; EEPROM copy of programmed input polarity Eep_Initialized_L: .DB 0x55 ; EEPROM initialized signature low byte Eep_Initialized_H: .DB 0xAA ; EEPROM initialized signature high byte Eep_Enable_TX_Program: .DB DEFAULT_PGM_ENABLE_TX_PROGRAM ; EEPROM TX programming enable _Eep_Main_Rearm_Start: .DB 0xFF _Eep_Pgm_Gov_Setup_Target: .DB 0xFF _Eep_Pgm_Startup_Rpm: .DB 0xFF _Eep_Pgm_Startup_Accel: .DB 0xFF _Eep_Pgm_Volt_Comp: .DB 0xFF Eep_Pgm_Comm_Timing: .DB DEFAULT_PGM_MULTI_COMM_TIMING ; EEPROM copy of programmed commutation timing Eep_Pgm_Damping_Force: .DB DEFAULT_PGM_MULTI_DAMPING_FORCE ; EEPROM copy of programmed damping force _Eep_Pgm_Gov_Range: .DB 0xFF _Eep_Pgm_Startup_Method: .DB 0xFF Eep_Pgm_Ppm_Min_Throttle: .DB DEFAULT_PGM_PPM_MIN_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1148) Eep_Pgm_Ppm_Max_Throttle: .DB DEFAULT_PGM_PPM_MAX_THROTTLE ; EEPROM copy of programmed minimum throttle (final value is 4x+1000=1832) Eep_Pgm_Beep_Strength: .DB DEFAULT_PGM_MULTI_BEEP_STRENGTH ; EEPROM copy of programmed beep strength Eep_Pgm_Beacon_Strength: .DB DEFAULT_PGM_MULTI_BEACON_STRENGTH ; EEPROM copy of programmed beacon strength Eep_Pgm_Beacon_Delay: .DB DEFAULT_PGM_MULTI_BEACON_DELAY ; EEPROM copy of programmed beacon delay Eep_Pgm_Throttle_Rate: .DB DEFAULT_PGM_MULTI_THROTTLE_RATE ; EEPROM copy of programmed throttle rate Eep_Pgm_Demag_Comp: .DB DEFAULT_PGM_MULTI_DEMAG_COMP ; EEPROM copy of programmed demag compensation Eep_Pgm_BEC_Voltage_High: .DB DEFAULT_PGM_BEC_VOLTAGE_HIGH ; EEPROM copy of programmed BEC voltage Eep_Pgm_Ppm_Center_Throttle: .DB DEFAULT_PGM_PPM_CENTER_THROTTLE ; EEPROM copy of programmed center throttle (final value is 4x+1000=1488) _Eep_Pgm_Main_Spoolup_Time: .DB 0xFF Eep_Pgm_Temp_Prot_Enable: .DB DEFAULT_PGM_ENABLE_TEMP_PROT ; EEPROM copy of programmed temperature protection enable .ENDIF Eep_Dummy: .DB 0xFF ; EEPROM address for safety reason .ORG 0x60 Eep_Name: .DB " " ; Name tag (16 Bytes) ;**** **** **** **** **** .CSEG ; Code segment .ORG 0 Interrupt_Table_Definition ; ATmega interrupts ;**** **** **** **** **** ; Table definitions GOV_GAIN_TABLE: .DB 0x02, 0x03, 0x04, 0x06, 0x08, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40, 0x60, 0x80, 0 ; Padded zero for an even number THROTTLE_RATE_TABLE: .DB 0x02, 0x03, 0x04, 0x06, 0x08, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40, 0x80, 0xFF, 0 ; Padded zero for an even number STARTUP_POWER_TABLE: .DB 0x04, 0x06, 0x08, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0 ; Padded zero for an even number .IF MODE == 0 .IF DAMPED_MODE_ENABLE == 1 TX_PGM_PARAMS_MAIN: .DB 13, 13, 4, 3, 6, 13, 5, 13, 6, 3, 3, 2, 2, 0 ; Padded zero for an even number .ENDIF .IF DAMPED_MODE_ENABLE == 0 TX_PGM_PARAMS_MAIN: .DB 13, 13, 4, 3, 6, 13, 5, 13, 6, 2, 3, 2, 2, 0 ; Padded zero for an even number .ENDIF .ENDIF .IF MODE == 1 .IF DAMPED_MODE_ENABLE == 1 TX_PGM_PARAMS_TAIL: .DB 5, 5, 13, 5, 13, 6, 3, 3, 3, 2 .ENDIF .IF DAMPED_MODE_ENABLE == 0 TX_PGM_PARAMS_TAIL: .DB 5, 5, 13, 5, 13, 6, 2, 3, 3, 2 .ENDIF .ENDIF .IF MODE == 2 .IF DAMPED_MODE_ENABLE == 1 TX_PGM_PARAMS_MULTI: .DB 13, 13, 4, 5, 6, 13, 5, 13, 6, 3, 3, 3, 2, 0 ; Padded zero for an even number .ENDIF .IF DAMPED_MODE_ENABLE == 0 TX_PGM_PARAMS_MULTI: .DB 13, 13, 4, 5, 6, 13, 5, 13, 6, 2, 3, 3, 2, 0 ; Padded zero for an even number .ENDIF .ENDIF ;**** **** **** **** **** **** **** **** **** **** **** **** **** ; ; Timer2 interrupt routine ; ; Assumptions: Z register must be set to desired pwm_nfet_on label ; Requirements: I_Temp variables can NOT be used ; ;**** **** **** **** **** **** **** **** **** **** **** **** **** t2_int: ; Used for pwm control in II_Sreg, SREG ; Check if pwm is on sbrc Flags0, PWM_ON ; Is pwm on? rjmp t2_int_pwm_off ; Do not execute pwm when stopped sbrs Flags1, MOTOR_SPINNING rjmp t2_int_pwm_on_exit ; Do not execute pwm on during demag recovery sbrc Flags0, DEMAG_CUT_POWER rjmp t2_int_pwm_on_exit_pfets_off ; Pwm on cycle. .IF MODE == 1 ; Tail sbrs Current_Pwm_Limited, 7 ; Jump for low pwm (<50%) rjmp t2_int_pwm_on_low_pwm .ENDIF t2_int_pwm_on_execute: ijmp ; Jump to pwm on routines. Z should be set to one of the pwm_nfet_on labels t2_int_pwm_on_low_pwm: ; Skip pwm on cycles for very low pwm lds YL, Pwm_On_Cnt ; Increment event counter inc YL sts Pwm_On_Cnt, YL ldi YL, 5 ; Only skip for very low pwm sub YL, Current_Pwm_Limited ; Check skipping shall be done (for low pwm only) brcs t2_int_pwm_on_execute lds YH, Pwm_On_Cnt ; Check if on cycle is to be skipped sub YL, YH brcs t2_int_pwm_on_execute ldi YL, 120 ; Write start point for timer sec sbrc Flags2, PGM_PWM_HIGH_FREQ ror YL .IF CLK_8M == 1 sec ror YL .ENDIF Set_TCNT2 YL mov YL, Current_Pwm_Limited tst YL brne t2_int_pwm_on_low_pwm_not_zero ldi YL, 0 ; Write start point for timer (long time for zero pwm) sbrc Flags2, PGM_PWM_HIGH_FREQ ldi YL, 0x80 .IF CLK_8M == 1 ldi YL, 0xC0 .ENDIF Set_TCNT2 YL t2_int_pwm_on_low_pwm_not_zero: rjmp t2_int_pwm_on_exit_no_timer_update t2_int_pwm_off: sbrs Flags1, STARTUP_PHASE rjmp t2_int_pwm_off_start_checked All_nFETs_Off YL ; Switch off all nfets early during start, for a smooth start t2_int_pwm_off_start_checked: ; Pwm off cycle mov YL, Current_Pwm_Limited sec sbrc Flags2, PGM_PWM_HIGH_FREQ ; Use half the time when pwm frequency is high ror YL .IF CLK_8M == 1 sec ror YL .ENDIF Set_TCNT2 YL ; Load new timer setting sts Pwm_Prev_Edge, YL ; Set timestamp ; Clear pwm on flag cbr Flags0, (1<= 1 ; Tail or multi ; Set current_pwm_limited lds I_Temp1, Current_Pwm ; Default not limited lds XL, Pwm_Limit ; Check against limit cp I_Temp1, XL brcs PC+2 ; If current pwm below limit - branch mov I_Temp1, XL ; Limit pwm .IF MODE == 2 ; Multi ; Limit pwm for low rpms lds XL, Pwm_Limit_Low_Rpm ; Check against limit cp I_Temp1, XL brcs PC+2 ; If current pwm below limit - branch mov I_Temp1, XL ; Limit pwm .ENDIF mov Current_Pwm_Limited, I_Temp1 .ENDIF t0_int_pwm_exit: ; Set demag enabled if pwm is above limit mov XL, Current_Pwm_Limited cpi XL, 0x40 ; Set if above 25% brcs PC+2 sbr Flags0, (1<= 1 ; Tail or multi lds XL, Pgm_Direction ; Check if bidirectional operation cpi XL, 3 brne PC+3 ; No - branch lds I_Temp7, Pgm_Ppm_Center_Throttle ; Center throttle value is in 4us units .ENDIF rcp_int_ppm_calculate: ldi XL, 250 ; Add 1000us to minimum add I_Temp7, XL mov I_Temp8, Zero adc I_Temp8, Zero sub I_Temp5, I_Temp7 ; Subtract minimum sbc I_Temp6, I_Temp8 in I_Temp1, SREG andi I_Temp1, (1<= 1 ; Tail or multi lds XL, Pgm_Direction ; Check if bidirectional operation cpi XL, 3 brne rcp_int_ppm_bidir_dir_set ; No - branch tst I_Temp1 breq rcp_int_ppm_bidir_fwd ; If result is positive - branch rcp_int_ppm_bidir_rev: sbrc Flags3, PGM_DIR_REV rjmp rcp_int_ppm_bidir_dir_set ; If same direction - branch cli ; Direction change, turn off all fets sbr Flags3, (1<= 1 ; Tail or multi lds XL, Pgm_Direction ; Check if bidirectional operation cpi XL, 3 brne rcp_int_ppm_unidir_neg ; No - branch ldi XL, 0xFF ; Change sign - invert and subtract minus one com I_Temp5 com I_Temp6 sub I_Temp5, XL sbc I_Temp6, XL rjmp rcp_int_ppm_neg_checked rcp_int_ppm_unidir_neg: .ENDIF ldi I_Temp1, RCP_MIN ; Yes - set to minimum ldi I_Temp2, 0 rjmp rcp_int_pwm_divide_done rcp_int_ppm_neg_checked: .IF MODE >= 1 ; Tail or multi lds XL, Pgm_Direction ; Check if bidirectional operation cpi XL, 3 brne rcp_int_ppm_bidir_done ; No - branch lsl I_Temp5 ; Multiply value by 2 rol I_Temp6 ldi XL, 5 ; Subtract deadband sub I_Temp5, XL sbc I_Temp6, Zero brcc rcp_int_ppm_bidir_done ldi XL, RCP_MIN mov I_Temp5, XL mov I_Temp6, Zero rcp_int_ppm_bidir_done: .ENDIF ldi XL, RCP_MAX ; Check that RC pulse is within legal range (max 255) cp I_Temp5, XL cpc I_Temp6, Zero brcs rcp_int_ppm_max_checked ldi I_Temp1, RCP_MAX ldi I_Temp2, 0 rjmp rcp_int_pwm_divide_done rcp_int_ppm_max_checked: lds I_Temp8, Ppm_Throttle_Gain ; Multiply throttle value by gain mul I_Temp5, I_Temp8 mov I_Temp1, Mul_Res_H ; Transfer result lsl Mul_Res_L ; Multiply result by 2 (unity gain is 128) rol I_Temp1 ldi I_Temp2, 0 brcs rcp_int_ppm_limit_after_mult rjmp rcp_int_limited rcp_int_ppm_limit_after_mult: ldi I_Temp1, RCP_MAX ldi I_Temp2, 0 rjmp rcp_int_limited rcp_int_pwm_divide: lsr I_Temp2 ; Divide by 2 ror I_Temp1 rcp_int_pwm_divide_done: sbrs Flags3, RCP_PWM_FREQ_12KHZ ; Is RC input pwm frequency 12kHz? rjmp rcp_int_check_legal_range tst I_Temp2 ; Yes - check that value is not more than 255 breq PC+2 ldi I_Temp1, RCP_MAX mov XL, I_Temp1 ; Multiply by 1.5 lsr XL add I_Temp1, XL adc I_Temp2, Zero rcp_int_check_legal_range: ; Check that RC pulse is within legal range cpi I_Temp1, RCP_MAX cpc I_Temp2, Zero brcs rcp_int_limited ldi I_Temp1, RCP_MAX rcp_int_limited: ; RC pulse value accepted sts New_Rcp, I_Temp1 ; Store new pulse length sbr Flags2, (1<= 1 ; Tail or multi sbrs Flags0, DIR_CHANGE_BRAKE ; Is it a direction change? rjmp comm_dir_change_done xcall switch_power_off ; Switch off power ldi XH, NFETON_DELAY ; Delay dec XH brne PC-1 ldi XH, PFETON_DELAY ; Delay dec XH brne PC-1 All_pFETs_on XH ; All pfets on - Break comm_dir_change_done: .ENDIF cbr Flags0, (1<= 1 ; Tail or multi cpi XH, 3 breq decode_params_dir_set .ENDIF cbr Flags3, (1<= 1 ; Tail or multi lds XH, Pgm_Direction ; Check if bidirectional operation cpi XH, 3 brne PC+2 rjmp program_by_tx_checked ; Disable tx programming if bidirectional operation .ENDIF xcall wait3ms lds XH, Pgm_Enable_TX_Program; Start programming mode entry if enabled cpi XH, 1 ; Is TX programming enabled? brcc arming_initial_arm_check ; Yes - proceed rjmp program_by_tx_checked ; No - branch arming_initial_arm_check: lds XH, Initial_Arm ; Yes - check if it is initial arm sequence cpi XH, 1 ; Is it the initial arm sequence? brcc arming_ppm_check ; Yes - proceed rjmp program_by_tx_checked ; No - branch arming_ppm_check: mov XH, Flags3 ; Check pwm frequency flags andi XH, ((1<= 1 ; Tail or multi lds XH, Pgm_Direction ; Check if bidirectional operation subi XH, 3 breq wait_for_power_on_check_timeout ; Do not wait if bidirectional operation .ENDIF xcall wait100ms ; Wait to see if start pulse was only a glitch wait_for_power_on_check_timeout: lds XH, Rcp_Timeout_Cnt ; Load RC pulse timeout counter value tst XH brne PC+2 ; If it is not zero - proceed rjmp measure_pwm_freq_init ; If it is zero (pulses missing) - go back to measure pwm frequency ;**** **** **** **** **** **** **** **** **** **** **** **** **** ; ; Start entry point ; ;**** **** **** **** **** **** **** **** **** **** **** **** **** init_start: cli xcall switch_power_off sts Requested_Pwm, Zero ; Set requested pwm to zero sts Governor_Req_Pwm, Zero ; Set governor requested pwm to zero sts Current_Pwm, Zero ; Set current pwm to zero mov Current_Pwm_Limited, Zero; Set limited current pwm to zero sei lds XH, Pgm_Motor_Idle sts Pwm_Motor_Idle, XH ; Set idle pwm to programmed value sts Gov_Target_L, Zero ; Set target to zero sts Gov_Target_H, Zero sts Gov_Integral_L, Zero ; Set integral to zero sts Gov_Integral_H, Zero sts Gov_Integral_X, Zero sts Adc_Conversion_Cnt, Zero ldi Flags0, 0 ; Clear flags0 ldi Flags1, 0 ; Clear flags1 sts Demag_Detected_Metric, Zero ; Clear demag metric xcall initialize_all_timings ; Initialize timing ;**** **** **** **** **** ; Motor start beginning ;**** **** **** **** **** ldi XH, TEMP_CHECK_RATE ; Make sure a temp reading is done sts Adc_Conversion_Cnt, XH Set_Adc_Ip_Temp xcall wait1ms Start_Adc XH read_initial_temp: Get_Adc_Status XH sbrc XH, ADSC rjmp read_initial_temp Read_Adc_Result Temp1, Temp2 ; Read initial temperature Stop_Adc XH tst Temp2 breq PC+2 ; Is reading below 256? ldi Temp1, 0xFF ; No - set average temperature value to 255 sts Current_Average_Temp_Adc, Temp1 ; Set initial average temp ADC reading xcall check_temp_voltage_and_limit_power ldi XH, TEMP_CHECK_RATE ; Make sure a temp reading is done next time sts Adc_Conversion_Cnt, XH Set_Adc_Ip_Temp ; Set up start operating conditions lds Temp7, Pgm_Pwm_Freq ; Store setting in Temp7 ldi XH, 2 ; Set nondamped low frequency pwm mode sts Pgm_Pwm_Freq, XH xcall decode_parameters ; (Decode_parameters uses Temp1 and Temp8) sts Pgm_Pwm_Freq, Temp7 ; Restore settings ; Set max allowed power cli ; Disable interrupts to avoid that Requested_Pwm is overwritten ldi XH, 0xFF ; Set pwm limit to max sts Pwm_Limit, XH xcall set_startup_pwm lds XH, Requested_Pwm sts Pwm_Limit, XH sts Pwm_Limit_Spoolup, XH sts Pwm_Limit_Low_Rpm, XH sei ldi XH, 1 ; Set low pwm again after calling set_startup_pwm sts Requested_Pwm, XH sts Current_Pwm, XH mov Current_Pwm_Limited, XH sts Spoolup_Limit_Skip, XH lds XH, Auto_Bailout_Armed sts Spoolup_Limit_Cnt, XH ; Begin startup sequence sbr Flags1, (1<= 1 ; Tail or multi mov XH, Flags3 ; Check pwm frequency flags andi XH, ((1<