I have a problem with modulo arithmetics in C language. I have defined global variable uint16_t Tmr_1ms which is incremented every 1 ms. I want to use this variable in a software oscillator implementation which is realized in below given function
void OSC(uint32_t output, Osc_t *p){
float act_time;
if(p->start){
taskENTER_CRITICAL();
act_time = Tmr_1ms;
taskEXIT_CRITICAL();
if(p->init){
SetLogicSignal(output);
p->start_time = act_time;
p->delta = ((p->period)/(2*p->T));
p->init = FALSE;
}
if(((uint16_t)act_time - (uint16_t)(p->start_time)) >= ((uint16_t)(p->delta))){
NegLogicSignal(output); // my defined function for negation of a bit variable
p->start_time = act_time;
}
}else{
ClearLogicSignal(output);
p->init = TRUE;
}
}
The oscillator state is stored in an instance of below given structure
// oscillator state (oscillator with fixed duty cycle)
typedef struct{
float period; // period of the oscillations (ms)
float T; // function execution period (ms)
BOOL start; // oscillator start signal (start==TRUE, stop==FALSE)
BOOL init; // initiate the oscillator state
float delta; // time after which expiration the oscillator output is negated
float start_time; // captured Tmr_1ms value
}Osc_t;
Here is the code
// oscillator instance init
Test_Oscillator_state.T = 20;
Test_Oscillator_state.period = 1000;
Test_Oscillator_state.init = TRUE;
// calling the function
Test_Oscillator_state.start = TRUE;
OSC(LTestBlink, &Test_Oscillator_state);
The problem is in the following code
if(((uint16_t)act_time - (uint16_t)(p->start_time)) >= ((uint16_t)(p->delta))){
NegLogicSignal(output);
p->start_time = act_time;
}
The output negation is functional only before Tmr_1ms overflow. I don't understand why. Please can anybody give me any guidance? Thanks in advance.