moro
Member level 3
Hello,
i have a mcu application written in C language using IAR compiler, and i encounter some issues with a output variable type.
The PID outputs the result in signed integer, and a variable for controlling a duty cycle register is unsigned int.
Variable initialisation
After which the pid function outputs a value which ranges from -400 to 400. If pid is greater than 0, then everything is ok. But if pid goes bellow 0, even with -1 then the duty variable will show something like 65123. Why is this?
So for a pid value of -1 and a duty of 50, i would expect 50 + ( -1) to be 49, and not 65123. What i am missing here? I have also tryied duty = uint16_t( duty - pid) and it doesn't help
i have a mcu application written in C language using IAR compiler, and i encounter some issues with a output variable type.
The PID outputs the result in signed integer, and a variable for controlling a duty cycle register is unsigned int.
Variable initialisation
Code:
int pid =0;
uint16_t duty = 50;
After which the pid function outputs a value which ranges from -400 to 400. If pid is greater than 0, then everything is ok. But if pid goes bellow 0, even with -1 then the duty variable will show something like 65123. Why is this?
Code:
if(pid > 400)
duty = 400; // Runs OK
else if(pid < 0)
duty = duty + pid; // Here is the problem
else
duty = pid;
So for a pid value of -1 and a duty of 50, i would expect 50 + ( -1) to be 49, and not 65123. What i am missing here? I have also tryied duty = uint16_t( duty - pid) and it doesn't help