我在mikroc写了一个代码,其中pwm信号应用于电机。 现在由于占用周期传递给PWM内置函数需要是短数据类型,而我的pid输出是浮点型,我将浮点数转换为short。但问题是,对于小于或等于100的值,正确完成了对,而对于超过100的值,给出诸如65535之类的值。 例如: 如果float值为255,则short等于65535 和 如果float值为100,则short等于100 我无法弄清楚这是什么错误?
下面是我的代码main和UART函数
主要功能
void main() {
UART1_Init(9600);
pro=5,i=0,der=0;
setPoint=100,sel=1;
actualOut=80;
pOut=0,iOut=0,dOut=0;
out=16;
error=0,lastError=0;
OPTION_REG=0;
T1CON=0;
INTCON=0;
ADCON0=0;
CMCON = 0x07;
TRISC.RC0=1;
TRISC.RC2=0;
TRISC.RC6=0;
PORTC=0;
while(1){
out = pid(actualOut);
duty=(short)out; out is float type and duty is short
Uart1_Intout_ReturnInt(duty);
UART1_Write(13); // newline
pwm(duty);
actualOut = feedback();
}
}
UART功能
Uart1_Intout_ReturnInt(unsigned i) {
char puf[6]; //for max 5 digits and the end-sign
WordToStr(i, puf); // in "Conversions" library
UART1_Write_Text(puf);
return i;
}
使用虚拟终端和uart在proteus中显示值 请帮帮我
答案 0 :(得分:0)
我发现解决方案类型转换需要按以下方式完成
unsigned short duty=(unsigned short)out;
- dcmotor