只是为了方便阅读和理解,所以未做详细的正确性检查。
因为能力所限,程度中还是保留了很多跳转(goto)语句。
主循环已经完成,发上来供大家参考
int main(void)
{
if(0 == Prepare_Lock_Or_Fuse_Read() & 0x80)
{
while(1);
}
Disable_Watchdog();
Initialize stack();
switch_power_off();
Ports_initialization();
Clear_r0_r25();
Clear_ram();
Random = 1;
// Set default programmed parameters
set_default_parameters();
// Read all programmed parameters
read_all_eeprom_parameters();
// Initialize ADC
Initialize_Adc();
// Set beep strength
Beep_Strength = Pgm_Beep_Strength;
//Set initial arm variable
Initial_Arm = 1;
//关中断
cli();
//???蜂鸣123
wait200ms();
beep_f1();
wait30ms();
beep_f2();
wait30ms();
beep_f3();
wait30ms();
#if MODE <= 1 ; Main or tail
//Wait for receiver to initialize
wait1s();
wait200ms();
wait200ms();
wait100ms();
#endif
init_no_signal:
while(1)
{
cli();
temp1 = 250;
do
{
temp2 = 250;
do
{
if(0 == (Read_Rcp_Int() & (1 << Rcp_In)))
{
goto bootloader_done;
}
}while(--temp2 != 0);
}while(--temp1 != 0);
if(Be_Bootloader())
{
Booterloder();
while(1);
}
bootloader_done:
decode_parameters();
decode_settings();
set_bec_voltage();
find_throttle_gain();
Beep_Strength = Pgm_Beep_Strength;
switch_power_off();
//Timer0: clk/8 for regular interrupts
TCCR0 = (1 << CS01);
//Timer1: clk/8 for commutation control and RC pulse measurement
TCCR1B = (1 << CS11);
//Timer2: clk/8 for pwm
TCCR2 = (1 << CS21);
//Initialize interrupts and registers
TIFR = (1<
TIMSK = (1<
//Initialize comparator
Comp_Init();
wait1ms();
//Enable all interrupts
sei();
//Measure number of lipo cells
Measure_Lipo_Cells();
//Reset stall count
sts Stall_Cnt = 0;
//Initialize RC pulse
Rcp_Int_First(); //Enable interrupt and set to first edge
Rcp_Int_Enable();// Enable interrupt
Rcp_Clear_Int_Flag();// Clear interrupt flag
Flags2 &= ~(1<
wait200ms();
Flags0 |= (1<
Temp4 = 3;// ; Number of attempts before going back to detect input signal
measure_pwm_freq_start:
Temp3 = 12// ; Number of pulses to measure
measure_pwm_freq_loop:
//Check if period diff was accepted
if(Rcp_Period_Diff_Accepted == 0)
{
Temp3 = 12; // ; Reset number of pulses to measure
if(0 == --Temp4)
{
continue;
};
}
wait30ms(); // ; Wait 30ms for new pulse
if(0 == (Flags2 & (1 << RCP_UPDATED)))//; Is there an updated RC pulse available - proceed
{
continue;
}
Flags2 &= ~(1<
if(New_Rcp < RCP_VALIDATE) goto measure_pwm_freq_start; //New_Rcp太小
Temp1 = Curr_Rcp_Pwm_Freq;// ; Store as previous flags for next pulse
Prev_Rcp_Pwm_Freq = Temp1;//
// Store current flags for next pulse
Curr_Rcp_Pwm_Freq = (Flags3 & ((1<
if(Curr_Rcp_Pwm_Freq != Temp1) goto measure_pwm_freq_start;// ; Go back if new flags not same as previous
if(--Temp3 != 0) goto measure_pwm_freq_loop;// ; Go back if not required number of pulses seen
//Clear measure pwm frequency flag
Flags0 &= ~(1<
//Set up RC pulse interrupts after pwm frequency measurement
Rcp_Int_First();// ; Enable interrupt and set to first edge
Rcp_Clear_Int_Flag();// ; Clear interrupt flag
Flags2 &= ~(1<
if(Pgm_Enable_PWM_Input == 0)// ; Check if PWM input is enabled
{
Flags2 |= (1<
//__zlf__原程序此处是否正确???
Flags3 &= (!((1<
}
//Test whether signal is OnShot125
Flags2 &= ~(1 << RCP_PPM_ONESHOT125);// ; Clear OneShot125 flag
Rcp_Outside_Range_Cnt = 0;// ; Reset out of range counter
wait100ms();// ; Wait for new RC pulses
if((0 != (Flags2 & (1 << RCP_PPM))) &&// If flag is not set (PWM) - branch
(Rcp_Outside_Range_Cnt >= 10))// Check how many pulses were outside normal PPM range (800-2160us)
{
Flags2 |= (1<
}
//; Validate RC pulse
validate_rcp_start:
do
{
wait3ms();// ; Wait for next pulse (NB: Uses Temp1/2!)
Temp1 = RCP_VALIDATE;// ; Set validate level as default
if(0 != (Flags2 & ( 1<< RCP_PPM)))
{
Temp1 = 0;// ; Set level to zero for PPM (any level will be accepted)
}
}while (New_Rcp < Temp1);//New_Rcp太小
//; Beep arm sequence start signal
cli();// ; Disable all interrupts
beep_f1();// ; Signal that RC pulse is ready
beep_f1();// ; Signal that RC pulse is ready
beep_f1();// ; Signal that RC pulse is ready
sei();// ; Enable all interrupts
wait200ms();
// Arming sequence start
Gov_Arm_Target = 0;// ; Clear governor arm target
arming_start:
do
{
#if MODE >= 1// ; Tail or multi
if(Pgm_Direction != 3) // ; Check if bidirectional operation
{
#endif
wait3ms();
if((Pgm_Enable_TX_Program >= 1)|| //; Start programming mode entry if enabled
(Initial_Arm >= 1))// ; Yes - check if it is initial arm sequence
{
do
{
if(0 == (Flags2 & (1 << RCP_PPM)))
{
//PWM tx program entry
if(New_Rcp < RCP_MAX)// ; Is RC pulse max?
{
break;// ; No - branch
}
while(New_Rcp >= RCP_STOP)//New_Rcp >= RCP_STOP
{
cli();// ; Disable all interrupts
beep_f4();
sei();// ; Enable all interrupts
wait100ms();
}
while(New_Rcp < RCP_MAX);
{
cli();// ; Disable all interrupts
beep_f1();
wait10ms();
beep_f1();
sei();// ; Enable all interrupts
wait100ms();
}
program_by_tx();// ; Yes - enter programming mode
}
//; PPM throttle calibration and tx program entry
#if MODE <= 1 //; Main or tail
temp8 = 8;// ; Set 3 seconds wait time
#else
temp8 = 3;// ; Set 1 second wait time
#endif
for(;temp8 != 0; tmp8--)
{
Flags3 |= (1<
cli();
find_throttle_gain();// ; Set throttle gain
sei();
wait100ms();// ; Wait for new throttle value
cli();// ; Disable interrupts (freeze New_Rcp value)
Flags3 &= ~(1<
find_throttle_gain();// ; Set throttle gain
Temp7 = New_Rcp;// ; Store new RC pulse value
sei(); //; Enable interrupts
if(New_Rcp < (RCP_MAX/2))// Is RC pulse above midstick?
{
goto arm_target_updated;// ; No - branch
}
wait1ms();
cli();// ; Disable all interrupts
beep_f4();
sei(); ; Enable all interrupts
}
average_throttle();
#if MODE <= 1 //; Main or tail
Pgm_Ppm_Max_Throttle = Temp7 - 5;// ; Subtract about 2% and ensure that it is 250 or lower
#else
Pgm_Ppm_Max_Throttle = Temp7;
#endif
wait200ms();
cli();
success_beep();
sei();
for(Temp8 = 10; Temp8 != 0; Temp8--)// ; Set 3 seconds wait time
{
Flags3 |= (1<
cli();
find_throttle_gain();// ; Set throttle gain
sei();
wait100ms();
cli(); //; Disable interrupts (freeze New_Rcp value)
Flags3 &= ~(1<
find_throttle_gain();// ; Set throttle gain
Temp7 = New_Rcp;// ; Store new RC pulse value
sei(); ; Enable interrupts
if(New_Rcp >= (RCP_MAX/2))// ; Below midstick?
{
Temp8 = 10;// ; Set 3 seconds wait time
continue;// ; No - start over
}
wait1ms();
cli();// ; Disable all interrupts
beep_f1();
wait10ms();
beep_f1();
sei(); ; Enable all interrupts
}
average_throttle();
Temp1 = Pgm_Ppm_Min_Throttle = Temp7 + 5;// ; Add about 2% (subtract negative number), ; Min throttle in Temp1
Temp2 = Pgm_Ppm_Max_Throttle - 130;// ; Subtract 130 (520us) from max throttle
if((Pgm_Ppm_Max_Throttle < 130) ||
(Pgm_Ppm_Max_Throttle - 130 < Temp1)
{
Pgm_Ppm_Max_Throttle = Temp1 + 130;//Make max 520us higher than min
}
wait200ms();
cli();
store_all_in_eeprom();
success_beep_inverted();
sei();
wait100ms();
cli();
find_throttle_gain();// ; Set throttle gain
sei();
}while(New_Rcp < RCP_MAX);
program_by_tx();// ; Yes - enter programming mode
}
if(New_Rcp >= Gov_Arm_Target)// ; Is RC pulse larger than arm target?
{
Gov_Arm_Target = Temp1;// Yes - update arm target
}
arm_target_updated:
wait100ms();// ; Wait for new throttle value
Temp1 = RCP_STOP;// ; Default stop value
if(Pgm_Direction == 3)// ; Check if bidirectional operation
{
Temp1 = (RCP_STOP+4);// ; Higher stop value for bidirectional
}
#if MODE >= 1// ; Tail or multi
}
#endif
}while(New_Rcp >= Temp1);//No below stop
//; Beep arm sequence end signal
cli();// ; Disable all interrupts
beep_f4();// ; Signal that rcpulse is ready
beep_f4();// ; Signal that rcpulse is ready
beep_f4();// ; Signal that rcpulse is ready
sei();// ; Enable all interrupts
wait200ms();
//; Clear initial arm variable
Initial_Arm = 0;
//; Armed and waiting for power on
wait_for_power_on:
while(1)
{
Power_On_Wait_Cnt_L = 0;// Clear wait counter
Power_On_Wait_Cnt_H = 0;// Zero
do
{
Power_On_Wait_Cnt_L++;// ; Increment low wait counter
if(Power_On_Wait_Cnt_L == 0xFF)// Counter wrapping (about 3 sec)?
{
Power_On_Wait_Cnt_H++;// ; Increment high wait counter
Temp1 = 25;// ; Approximately 1 min
if(Pgm_Beacon_Delay = 1)
else if(Pgm_Beacon_Delay = 2)
{
Temp1 = 50;// ; Approximately 2 min
}
else if(Pgm_Beacon_Delay = 3)
{
Temp1 = 125;// ; Approximately 5 min
}
else if(Pgm_Beacon_Delay = 4)
{
Temp1 = 250;// ; Approximately 10 min
}
else
{
Power_On_Wait_Cnt_H = 0;//Reset counter for infinite delay
}
if(Power_On_Wait_Cnt_H >= Temp1)// ; Check against chosen delay
{
switch_power_off();// ; Switch power off in case braking is set
wait1ms();
Power_On_Wait_Cnt_H--;// Decrement high wait counter
Power_On_Wait_Cnt_L = 0; Set low wait counter
Beep_Strength = Pgm_Beacon_Strength;
cli();// ; Disable all interrupts
beep_f();// ; Signal that there is no signal
sei();// ; Enable all interrupts
Beep_Strength = Pgm_Beep_Strength;
wait100ms();// ; Wait for new RC pulse to be measured
}
}
wait10ms();
if(Rcp_Timeout_Cntd == 0)
{
if(0 != (Flags2 & (1 << RCP_PPM)))//; If ppm and pulses missing - go back to detect input signal
{
goto init_no_signal;
}
}
Temp1 = RCP_STOP;
if(0 == (Flags2 & (1 << RCP_PPM)))
{
Temp1 = (RCP_STOP+5);// ; Higher than stop (for pwm)
}
}while(New_Rcp < Temp1);
#if MODE >= 1 //; Tail or multi
if(Pgm_Direction != 3)
#endif
{
wait100ms();// ; Wait to see if start pulse was only a glitch
}
if(0 == Rcp_Timeout_Cntd)// ; If it is not zero - proceed
{
goto init_no_signal;// ; If it is zero (pulses missing) - go back to detect input signal
}
//Start entry point
cli();
switch_power_off();
Requested_Pwm = 0;//Set requested pwm to zero
Governor_Req_Pwm = 0;// Set governor requested pwm to zero
Current_Pwm = 0;//Set current pwm to zero
Current_Pwm_Limited = 0;//Set limited current pwm to zero
Current_Pwm_Lim_Dith = 0;
Pwm_Dither_Excess_Power = 0;
sei();
//; Set idle pwm to programmed value
Pwm_Motor_Idle = (Pgm_Motor_Idle << 1);
Gov_Target_L = 0;// ; Set target to zero
Gov_Target_H = 0;//
Gov_Integral_L = 0;// ; Set integral to zero
Gov_Integral_H = 0;//
Gov_Integral_X = 0;//
Adc_Conversion_Cnt = 0;//
Flags0 = 0;// ; Clear flags0
Flags1 = 0;// ; Clear flags1
Demag_Detected_Metric = 0;//Clear demag metric
//Motor start beginning
Adc_Conversion_Cnt = TEMP_CHECK_RATE; ; Make sure a temp reading is done
Set_Adc_Ip_Temp();
wait1ms();
Start_Adc();
//read_initial_temp
while(0 != (Get_Adc_Status() & (1 << ADSC))) {};
Temp1 = ADCL;
Temp2 = ADCH;
Stop_Adc();
if(0 != Temp2)//; Is reading below 256?
{
Temp1 = 0xFF; ; No - set average temperature value to 255
}
Current_Average_Temp_Adc = Temp1;// ; Set initial average temp ADC reading
check_temp_voltage_and_limit_power();
Adc_Conversion_Cnt = TEMP_CHECK_RATE;// ; Make sure a temp reading is done next time
Set_Adc_Ip_Temp();
//; Set up start operating conditions
decode_parameters();// ; (Decode_parameters uses Temp1 and Temp8)
//; Set max allowed power
cli();// ; Disable interrupts to avoid that Requested_Pwm is overwritten
Pwm_Limit = 0xFF;// ; Set pwm limit to max
set_startup_pwm();
Pwm_Limit = Requested_Pwm;
Pwm_Limit_Spoolup = Requested_Pwm;
Pwm_Limit_By_Rpm = Requested_Pwm;
sei();
//Set low pwm again after calling set_startup_pwm
Requested_Pwm = 1;
Current_Pwm = 1;
Current_Pwm_Limited = 1;
Current_Pwm_Lim_Dith = 1;
Spoolup_Limit_Skip = 1;
Spoolup_Limit_Cnt = Auto_Bailout_Armed;
//Begin startup sequence
if(Pgm_Direction == 3)// ; Check if bidirectional operation
{
Flags3 &= ~(1<
if(0 != (Flags2 & (1 << RCP_DIR_REV)))// ; Check force direction
{
Flags3 |= (1<
}
}
init_start_bidir_done:
Flags1 |= (1<
Flags1 |= (1<
Startup_Cnt = Zero;// ; Reset counter
comm5comm6();// ; Initialize commutation
comm6comm1();//
initialize_timing();// ; Initialize timing
calc_next_comm_timing();// ; Set virtual commutation point
initialize_timing();// ; Initialize timing
calc_next_comm_timing();//
initialize_timing();// ; Initialize timing
//; Run entry point
//; Run 1 = B(p-on) + C(n-pwm) - comparator A evaluated
//; Out_cA changes from low to high
run1:
wait_for_comp_out_high ; Wait zero cross wait and wait for high
//; setup_comm_wait();// ; Setup wait time from zero cross to commutation
//; evaluate_comparator_integrity();// ; Check whether comparator reading has been normal
calc_governor_target();// ; Calculate governor target
wait_for_comm();// ; Wait from zero cross to commutation
comm1comm2();// ; Commutate
calc_next_comm_timing();// ; Calculate next timing and start advance timing wait
//; wait_advance_timing();// ; Wait advance timing and start zero cross wait
//; calc_new_wait_times();//
//; set_comparator_phase();// ; Set comparator phase
//; wait_before_zc_scan();// ; Wait zero cross wait and start zero cross timeout
//; Run 2 = A(p-on) + C(n-pwm) - comparator B evaluated
//; Out_cB changes from high to low
run2:
wait_for_comp_out_low();
//; setup_comm_wait();
//; evaluate_comparator_integrity();
if(0 != (Flags0 & (1 << GOV_ACTIVE)))
{
calc_governor_prop_error();
}
if(0 == (Flags1 & (1 << HIGH_RPM)))// ; Skip if high rpm
{
set_pwm_limit_low_rpm();
}
if(0 != (Flags1 & (1 << HIGH_RPM)))// ; Do if high rpm
{
set_pwm_limit_high_rpm();
}
wait_for_comm();// ; Wait from zero cross to commutation
comm2comm3();// ; Commutate
calc_next_comm_timing();// ; Calculate next timing and start advance timing wait
//; wait_advance_timing();// ; Wait advance timing and start zero cross wait
//; calc_new_wait_times();//
//; set_comparator_phase();// ; Set comparator phase
//; wait_before_zc_scan();// ; Wait zero cross wait and start zero cross timeout
//; Run 3 = A(p-on) + B(n-pwm) - comparator C evaluated
//; Out_cC changes from low to high
run3:
wait_for_comp_out_high();
//; setup_comm_wait
//; evaluate_comparator_integrity
if(0 != (Flags0 & (1 << GOV_ACTIVE)))
{
calc_governor_prop_error();
}
wait_for_comm();// ; Wait from zero cross to commutation
comm3comm4();// ; Commutate
calc_next_comm_timing();// ; Calculate next timing and start advance timing wait
//; wait_advance_timing();// ; Wait advance timing and start zero cross wait
//; calc_new_wait_times();//
//; set_comparator_phase();// ; Set comparator phase
//; wait_before_zc_scan();// ; Wait zero cross wait and start zero cross timeout
//; Run 4 = C(p-on) + B(n-pwm) - comparator A evaluated
//; Out_cA changes from high to low
run4:
wait_for_comp_out_low();
//; setup_comm_wait();
//; evaluate_comparator_integrity();
if(0 != (Flags0 & (1 << GOV_ACTIVE)))
{
calc_governor_prop_error();
}
wait_for_comm();// ; Wait from zero cross to commutation
comm4comm5();// ; Commutate
calc_next_comm_timing();// ; Calculate next timing and start advance timing wait
//; wait_advance_timing();// ; Wait advance timing and start zero cross wait
//; calc_new_wait_times();//
//; set_comparator_phase();// ; Set comparator phase
//; wait_before_zc_scan();// ; Wait zero cross wait and start zero cross timeout
//; Run 5 = C(p-on) + A(n-pwm) - comparator B evaluated
//; Out_cB changes from low to high
run5:
wait_for_comp_out_high();
//; setup_comm_wait
//; evaluate_comparator_integrity
if(0 != (Flags0 & (1 << GOV_ACTIVE)))
{
calc_governor_prop_error();
}
wait_for_comm();// ; Wait from zero cross to commutation
comm5comm6();// ; Commutate
calc_next_comm_timing();// ; Calculate next timing and start advance timing wait
//; wait_advance_timing();// ; Wait advance timing and start zero cross wait
//; calc_new_wait_times();//
//; set_comparator_phase();// ; Set comparator phase
//; wait_before_zc_scan();// ; Wait zero cross wait and start zero cross timeout
//; Run 6 = B(p-on) + A(n-pwm) - comparator C evaluated
//; Out_cC changes from high to low
run6:
wait_for_comp_out_low();
//; setup_comm_wait();
//; evaluate_comparator_integrity();
Start_Adc();
wait_for_comm();// ; Wait from zero cross to commutation
comm6comm1();// ; Commutate
calc_next_comm_timing();// ; Calculate next timing and start advance timing wait
//; wait_advance_timing();// ; Wait advance timing and start zero cross wait
//; calc_new_wait_times();//
//; set_comparator_phase();// ; Set comparator phase
//; wait_before_zc_scan();// ; Wait zero cross wait and start zero cross timeout
check_temp_voltage_and_limit_power();
//; Check if it is startup
if(0 == (Flags1 & (1 << STARTUP_PHASE)))
{
goto normal_run_checks;
}
//Set spoolup power variables
Pwm_Limit = Pwm_Spoolup_Beg;// ; Set initial max power
Pwm_Limit_Spoolup = Pwm_Spoolup_Beg;// ; Set initial slow spoolup power
Spoolup_Limit_Cnt = Auto_Bailout_Armed;
Spoolup_Limit_Skip = 1;
//; Check startup counter
Temp2 = 24;// ; Set nominal startup parameters
Temp3 = 12;
if(Startup_Cnt < Temp2)// ; Is counter above requirement?
{
if(New_Rcp < RCP_STOP)// ; Check if pulse is below stop value
{
goto run_to_wait_for_power_on;
}
else
{
goto run1;// ; Continue to run
}
}
Flags1 &= ~(1<
Flags1 |= (1<
Initial_Run_Rot_Cntd = Temp3;// ; Set initial run rotation count
#if MODE == 1 //; Tail
Pwm_Limit = 0xff;// ; Allow full power
#elif MODE == 2 // ; Multi
Pwm_Limit = Pwm_Spoolup_Beg;
Pwm_Limit_By_Rpm = Pwm_Spoolup_Beg;
#endif
normal_run_checks:
//; Check if it is initial run phase
if(0 == (Flags1 & (1 << INITIAL_RUN_PHASE)))// ; If not initial run phase - branch
{
goto initial_run_phase_done;
}
if(1 == (Flags0 & (1 << DIR_CHANGE_BRAKE)))// ; If a direction change - branch
{
goto initial_run_phase_done;
}
//; Decrement startup rotation count
if(Initial_Run_Rot_Cntd - 1) == 0) //; Check number of nondamped rotations
{
Flags1 &= ~(1<
Flags1 |= (1<
goto run1
} ; Continue with normal run
normal_run_check_startup_rot:
Initial_Run_Rot_Cntd = Initial_Run_Rot_Cntd - 1;// ; Not zero - store counter
if(New_Rcp >= RCP_STOP)// ; Check if pulse is below stop value
{
goto run1();// ; Continue to run
}
if(Pgm_Direction != 3)// ; Check if bidirectional operation
{
goto run_to_wait_for_power_on;
}
initial_run_phase_done:
//; Reset stall count
Stall_Cnt = 0;
#if MODE == 0 //; Main
//; Check if throttle is zeroed
if(Rcp_Stop_Cnt >= 1)// ; Is number of stop RC pulses above limit?
{
Pwm_Limit_Spoolup = Pwm_Spoolup_Beg;// ; If yes - set initial max powers
Spoolup_Limit_Cnt = Auto_Bailout_Armed;// ; And set spoolup parameters
Spoolup_Limit_Skip = 1;
}
#endif
//; Exit run loop after a given time
Temp1 = RCP_STOP_LIMIT;
if(0 != Pgm_Brake_On_Stop)
{
Temp1 = 3;// ; About 100ms before stopping when brake is set
}
if(Rcp_Stop_Cnt > Temp1)// ; Is number of stop RC pulses above limit?
{
goto run_to_wait_for_power_on;// ; Yes, go back to wait for poweron
}
if(0 != (Flags2 & (1 << RCP_PPM)))
{
if(Rcp_Timeout_Cntd == 0)
{
goto run_to_wait_for_power_on;// ; If it is zero - go back to wait for poweron
}
}
run6_check_dir:
#if MODE >= 1 // ; Tail or multi
if((Pgm_Direction == 3) && //Check if bidirectional operation
((0 == (Flags3 & (1 << PGM_DIR_REV))) != //Check if actual rotation direction
(0 == (Flags2 & (1 << RCP_DIR_REV)))) && //Matches force direction
(0 == (Flags0 & (1 << DIR_CHANGE_BRAKE))))
{
Flags0 |= (1<
Pwm_Limit = Pwm_Spoolup_Beg;// ; Set max power while braking
goto run4;// ; Go back to run 4, thereby changing force direction
}
#endif
Temp1 = 0xF0;// ; Default minimum speed
if((0 != (Flags0 & (1 <
{
Pwm_Limit = Pwm_Spoolup_Beg;// ; Set max power while braking
Temp1 = 0x20;// ; Bidirectional braking termination speed
}
run6_brake_done:
if(Comm_Period4x_H < Temp1)// ; Is Comm_Period4x more than 32ms (~1220 eRPM)?
{
goto run1;// ; No - go back to run 1
}
//; Yes - stop or turn direction
#if MODE >= 1 //; Tail or multi
if(0 == (Flags0 & (1 << DIR_CHANGE_BRAKE)))// ; If it is not a direction change - stop
{
goto run_to_wait_for_power_on;
}
Flags0 &= ~(1<
Flags3 &= (1<
if(0 != (Flags2 & (1 << RCP_DIR_REV)))// ; Check force direction
{
Flags3 += (1<
}
Flags1 |= (1<
Initial_Run_Rot_Cntd = 18;
Pwm_Limit = Pwm_Spoolup_Beg;// ; Set initial max power
goto run1;// ; Go back to run 1
.ENDIF
run_to_wait_for_power_on_fail:
Stall_Cnt++;// ; Increment stall count
if(0 != New_Rcp)// ; Check if RCP is zero, then it is a normal stop
{
goto run_to_wait_for_power_on_stall_done;
}
run_to_wait_for_power_on:
Stall_Cnt = Zero;
run_to_wait_for_power_on_stall_done:
cli();
switch_power_off();
Temp7 = Pgm_Pwm_Freq;// ; Store setting in Temp7
Pgm_Pwm_Freq = 2;
decode_parameters();// ; (Decode_parameters uses Temp1 and Temp8)
Pgm_Pwm_Freq = Temp7;// ; Restore settings
Requested_Pwm = 0;// ; Set requested pwm to zero
Governor_Req_Pwm = 0;// ; Set governor requested pwm to zero
Current_Pwm = 0;// ; Set current pwm to zero
Current_Pwm_Limited = 0;// ; Set limited current pwm to zero
Current_Pwm_Lim_Dith = 0;//
Pwm_Motor_Idle = 0;// ; Set motor idle to zero
Flags0 = 0;// ; Clear flags0
Flags1 = 0;// ; Clear flags1
sei();
wait100ms();// ; Wait for pwm to be stopped
switch_power_off();
if(0 != Pgm_Brake_On_Stop)
{
Brake_FETs_On();
}
run_to_wait_for_power_on_brake_done:
Initialize_Adc();// ; Initialize ADC, to keep reference on for selected ESCs
#if MODE == 0 // ; Main
if((0 != (Flags2 & (1 << RCP_PPM)))
&& (0 == Rcp_Timeout_Cntd))// ; Load RC pulse timeout counter value
{
goto init_no_signal;// ; If it is zero (pulses missing) - go back to detect input signal
}
run_to_next_state_main:
if(Pgm_Main_Rearm_Start >= 1)// Is re-armed start enabled?
{
goto validate_rcp_start;// ; Yes - go back to validate RC pulse
}
//No - do like tail and start immediately
#elif MODE >= 1 //; Tail or multi
if((0 != (Flags2 & (1 << RCP_PPM)))
&& (Stall_Cnt >= 4))//
{
goto init_no_signal;// ; If it is zero (pulses missing) - go back to detect input signal
}
#endif
}
}
}