0

I wrote a code in mikroc where pwm signal is applied to motor. Now since dutycycle passed to PWM inbuilt function needs to be short datatype and my pid output is of float type i casted float to short.But the problem is that for values less than or equal to hundred casting is done properly whereas for values beyond 100 it gives values such as 65535. For eg: if float value is 255 then short is equal to 65535 and if float value is 100 then short is equal to 100 i am not able to figure out what is the mistake?

Below is my code only main and UART function

main function

  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 function

 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;
  }

used virtual terminal and uart to display values in proteus Please help me

dcmotor
  • 58
  • 1
  • 12

1 Answers1

0

i found the solution type casting needs to be done in the following manner

unsigned short duty=(unsigned short)out;

– dcmotor

Armali
  • 18,255
  • 14
  • 57
  • 171