Page 1 of 1

PWM. Problem

Posted: Wed Feb 23, 2011 6:02 pm
by trisb
HI, All,

I need a bit of help.
I have a program that I have made to control A piece of equipment By PWM. I have to limit the PWM but need to have 10% steps, the program I have made will simulate fine.
However when I go to compile it gives me a error. (See below)

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1022): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1022:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1042): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1042:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1062): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1062:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1082): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1082:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1102): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1102:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1122): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1122:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1142): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1142:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1162): error: can't convert 'float' to 'unsigned char'

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1162:17): error: failed to generate expression

J:\Projects\PinCure\FlowCode Files\UV LED Controller V1-0.c(1450:17): warning: expression was optimized out



I have worked out that this is being generated due to me having a decimal point in my PWM values IE 30.4

IS there a way a can avoid this error but keep the values I have as a lot of the PWM levels I have created need to have the Decimal value.

Thanks

Tristan

Re: PWM. Problem

Posted: Thu Feb 24, 2011 9:22 am
by Benj
Hello Tristan,

Unfortunately you cannot pass floating point variables to the PWM function. What you can do is to use the PWM in 10-bit mode and this will allow for integer values between 0 and 1023. Therefore you can multiply your float variable to allow it to go in the range 0 - 1023 and then convert it to an integer before passing to the PWM duty cycle.

Re: PWM. Problem

Posted: Thu Feb 24, 2011 3:00 pm
by trisb
Hi, Ben,

Thanks for the reply, I am still trying to get my head round this.
Sorry to be a pain, but do you have a example you can give me.

Thanks

Tristan

Re: PWM. Problem

Posted: Fri Feb 25, 2011 9:30 am
by Benj
Hello,

If your range of your floating point variable is between 0 and 255 then you would first multiply by 4 to get a value between 0 and 1023. Once you have done this you can use the float to int function in the calculation icon to convert the variable type.

I've included a quick example.

Re: PWM. Problem

Posted: Fri Feb 25, 2011 2:23 pm
by trisb
Thanks Ben,

I will take a look and give this a Go.

Much appreciated.

Tristan