Hi @mspider65 - the timer triggers in main that call motor and ebike every 4/50mS i think have an issue. I found the problem since because of the timer arrangement changes I copied your method across - but when I put my serial trace on I saw packets were being sent before the last packet was finished causing serial errors which weren't there before - so had a bit of a dig to see what was causing and noticed that your timer loops may fire early when the counters get close to wrapping causing a little jitter in how regularly the calls are made - and causing (in the osf firmware anyway) serial errors as packets don't get a chance to send before the next one starts every now and again... so causing a bad crc every few seconds or so...
it's these constructs that are problematic i think. Say if the test fires at tim4 counter = 254. Next firing the counter will be 3 but the two cycles before that when the test is say 0,1, or 2 minus 254 will be greater than 4 - so it fires a bit early.
Code:
ui16_1mS_counter = ui8_tim4_counter;
if((ui16_1mS_counter - ui16_motor_controller_counter) > 4) // every 4ms
I'm testing a change - to just detect change in the 1ms timer counter (this is ok as long as the loop runs faster than 1mS - and it does) - to give a 1mS pulse - and use that to increment separate trigger timers for the 4mS and 50mS calls... That seems to work and gets rid of the serial errors I was seeing.
I'm still testing - but this is the idea:
Code:
ui8_1mS_counter = ui8_tim4_counter;
if (ui8_1mS_counter != ui8_last_1mS_counter) // 1mS pulse
{
ui8_last_1mS_counter = ui8_1mS_counter;
if (++ui8_motor_controller_counter == 4) ui8_motor_controller_counter = 0; // every 4mS
if (++ui8_ebike_app_controller_counter == 50) ui8_ebike_app_controller_counter = 0; //every 50mS
ui8_motor_controller_trigger = ((ui8_motor_controller_counter | ui8_motor_controller_trigger) == 0);
ui8_ebike_app_controller_trigger = ((ui8_ebike_app_controller_counter | ui8_ebike_app_controller_trigger) == 0);
}
if(ui8_motor_controller_trigger) // every 4ms
{
ui8_motor_controller_trigger = 0;
motor_controller();
continue;
}
if(ui8_ebike_app_controller_trigger) // every 50ms
{
ui8_ebike_app_controller_trigger = 0;
ebike_app_controller();
continue;
}
}