@@ -71,29 +71,28 @@ void Servo_Handler(timer16_Sequence_t timer, Tc *pTc, uint8_t channel);
7171#endif
7272
7373void Servo_Handler (timer16_Sequence_t timer, Tc *tc, uint8_t channel) {
74+ const bool good_servo = SERVO_INDEX (timer, Channel[timer]) < ServoCount;
7475 if (Channel[timer] < 0 )
75- tc->TC_CHANNEL [channel].TC_CCR |= TC_CCR_SWTRG; // channel set to -1 indicated that refresh interval completed so reset the timer
76- else {
77- // else if (SERVO_INDEX(timer, Channel[timer]) < ServoCount && SERVO(timer, Channel[timer]).Pin.isActive)
78- extDigitalWrite (SERVO (timer, Channel[timer]).Pin .nbr , LOW); // pulse this channel low if activated
79- }
76+ tc->TC_CHANNEL [channel].TC_CCR |= TC_CCR_SWTRG; // channel set to -1 indicated that refresh interval completed so reset the timer
77+ else if (good_servo)
78+ extDigitalWrite (SERVO (timer, Channel[timer]).Pin .nbr , LOW); // pulse this channel low if activated
8079
81- // clear interrupt
82- tc->TC_CHANNEL [channel].TC_SR ;
80+ tc->TC_CHANNEL [channel].TC_SR ; // clear interrupt
8381
84- Channel[timer]++; // increment to the next channel
85- if (SERVO_INDEX (timer, Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) {
82+ Channel[timer]++; // increment to the next channel
83+ if (good_servo && Channel[timer] < SERVOS_PER_TIMER) {
8684 tc->TC_CHANNEL [channel].TC_RA = tc->TC_CHANNEL [channel].TC_CV + SERVO (timer,Channel[timer]).ticks ;
87- if (SERVO (timer,Channel[timer]).Pin .isActive ) // check if activated
88- extDigitalWrite (SERVO (timer, Channel[timer]).Pin .nbr , HIGH); // its an active channel so pulse it high
85+ if (SERVO (timer,Channel[timer]).Pin .isActive ) // check if activated
86+ extDigitalWrite (SERVO (timer, Channel[timer]).Pin .nbr , HIGH); // it's an active channel so pulse it high
8987 }
9088 else {
9189 // finished all channels so wait for the refresh period to expire before starting over
90+ const unsigned int iticks = (unsigned int )usToTicks (REFRESH_INTERVAL);
9291 tc->TC_CHANNEL [channel].TC_RA =
93- tc->TC_CHANNEL [channel].TC_CV < usToTicks (REFRESH_INTERVAL) - 4
94- ? ( unsigned int ) usToTicks (REFRESH_INTERVAL) // allow a few ticks to ensure the next OCR1A not missed
95- : tc->TC_CHANNEL [channel].TC_CV + 4 ; // at least REFRESH_INTERVAL has elapsed
96- Channel[timer] = -1 ; // this will get incremented at the end of the refresh period to start again at the first channel
92+ tc->TC_CHANNEL [channel].TC_CV + 4 < iticks
93+ ? iticks // allow a few ticks to ensure the next OCR1A not missed
94+ : tc->TC_CHANNEL [channel].TC_CV + 4 ; // at least REFRESH_INTERVAL has elapsed
95+ Channel[timer] = -1 ; // gets incremented at the end of the refresh period to start again at the first channel
9796 }
9897
9998}
0 commit comments