我用C语言进行模运算有问题。 我已经定义了全局变量uint16_t Tmr_1ms,它是每1毫秒递增 。我想用这个变量 这是考虑功能用C语言进行模运算
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;
}
}
振荡器状态被存储在如下结构
// 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;
下面的实例实现以下 软件振荡器实现代码
// 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);
的问题是在下面的代码
if(((uint16_t)act_time - (uint16_t)(p->start_time)) >= ((uint16_t)(p->delta))){
NegLogicSignal(output);
p->start_time = act_time;
}
输出否定仅在Tmr_1ms溢出之前起作用。我不明白为什么。请有人给我任何指导?提前致谢。
提供一个[mcve]并按[ask [。 “funcional”是什么意思?没有否定运算符,并且'NegLogicSignal'不是标准函数。学习使用调试器。 – Olaf
您的代码中没有任何模运算符,因此不确定哪些模运算有问题? –
我根本不知道你的问题是什么。你的意思是说,一旦Tmr_1ms环绕着你,再也不会进入调用“NegLogicSignal(output)”的“if”语句了吗? – Basya