Page **1** of **1**

### analog in PWM out

Posted: **Tue Jan 06, 2009 11:57 pm**

by **fonty**

Hallo

I’m a student of the fontys university. Per accident I fount your dsPIC toolbox. I was very impressed because we ware looking to make this library on ore own because we have some dsPIC boards at the UNI. The UNI what to change the les method. In the new method it is important that students can program a microcontroller (dsPIC) very fast whit a model made in simulink. So your toolbox is perfect. There is only on one thing I need to show sum profs at the UNI that is works. So I like to make a motor controller whit a analog input an a pwm output. But when I put in the PWM motor output I cant open the master block. Is there someone that can make a PWM output which change when the analog input change. I need this fast because I have a presentation at 8 January. Ant I like to show this. I think that when I can show a working model whit a analog in ant a pwm out the UNI will be very interested in this toolbox. I don’t think I can make it myself that fast. So is there someone who can help me?? It all neat to be run on a dsPIC30f6012

### Re: analog in PWM out

Posted: **Wed Jan 07, 2009 1:00 am**

by **LubinKerhuel**

Hi Fonty,

fonty wrote:Per accident

Nice accident

dsPIC30f6012 has no

PWM motor peripheral.

To generate a PWM signal with a 30f6012, use the

Output Compare (OC) block.

Therefore, place an ADC block, a gain and a OC block. It should works very easily! (Have a look on

examples...)

Post your model if you get into trouble.

Lubin

### Re: analog in PWM out

Posted: **Wed Jan 07, 2009 5:21 pm**

by **fonty**

Hallo Lubin

I just found some time to make a simulink model and it built whiteout errors so it could be working. The problem is that I’m at home at this moment and I cant test it. So I hope you maybe can tell me I understand it right.

I have a: ADC Input connected whit a gain (1) the gain is connected whit a data type conversion that is connected whit UC5_up from the Output Compare. Than I have a constant (5) connected whit the OC5_periode.

Is it right when I say that the output compare compares the ADC input whit the constant en generate a PWM out this? So when the input is 5 the PWM is100% when the input is 2.5 the PWM is 50% and when the input is 0 the PWM is 0??

Than my last question. Is it possible to make a simulation model of this model. Can I scoop the output of the OC block sum way?? So I can simulate without my microcontroller board??

### Re: analog in PWM out

Posted: **Wed Jan 07, 2009 6:42 pm**

by **LubinKerhuel**

Hi Fonty,

fonty wrote:So I can simulate without my microcontroller board??

dsPIC peripheral can't be simulated. It is not possible to simulate dsPIC peripheral because it would necessitate a simulation time step equal to the dsPIC Mips and this would not be efficient !

It is however not very usefull to do such a peripheral simulation. Thus, simulation of the embedded calculation and feedback controller is very usefull and this is of course possible with simulink...

It would be easier to help if you could upload your model on the forum.

The blockset will define the variable OC5max and ANmax in the workspace (check your workspace)

The gain between ADC and OC must be : OC5max / ANmax

The constant connected to the OC5_periode should have the value OC5max.

It should work as is. However, If OC5max/ANmax is not an integer

**and **if you want very precise calculation, you should considere add 2 converter before and after the gain block. The one before would convert value into double to do floating point multiplication, and the one afte would convert the result to uint16 value.

That's it

### Re: analog in PWM out

Posted: **Wed Jan 07, 2009 7:32 pm**

by **fonty**

thanks for helping me so fast. I have changest the gain in OC5max/ANmax and the constant in OC5max. and it built successful so I hope it will work tomorrow when Iload it in the controller..

I like to upload my model at this forum but I don’t know how to do that.

### Re: analog in PWM out

Posted: **Wed Jan 07, 2009 8:00 pm**

by **LubinKerhuel**

upload the model tomorrow if you get in trouble.

uplaod is just by the option tab at the bottom of the **reply page**, below the save-Preview-submit button

### Re: analog in PWM out

Posted: **Wed Jan 07, 2009 9:56 pm**

by **fonty**

This is the model so far I will test it tomorrow.

- example1.mdl
*analog in PWM out* - (26.42 KiB) Downloaded 317 times

### Re: analog in PWM out

Posted: **Wed Jan 07, 2009 10:20 pm**

by **LubinKerhuel**

Will not work :

in your model, the output of the ADC is an integer.

OC5max/ANmax value will be defined as integer because of the inherited datatype option choosen. But in your case, real value of OC5max/ANmax is 0.0488 ; converted into integer, result is 0.

You multiply by 0.

solution :

turn your calculation into double (download the model attached)

or use a PWM with a duty cycle larger than 0,2ms (that's quite short becaue of the slow quartz (giving 1 MIPS) Check also that with such a quartz, the Quartz configuration should not be the XT instead of HS !)

Lubin

### Re: analog in PWM out

Posted: **Thu Jan 08, 2009 9:55 pm**

by **fonty**

Thanks for helping Lubin. I changest the duty cycle and the Quartz configuration. After bilding en programming the controller it works well. After showing it in my presentation everyone was very enthusiast about it.

### Re: analog in PWM out

Posted: **Tue Jan 27, 2009 2:56 pm**

by **fonty**

hallo lubin

Hallo I try to rebuild the model we make earlier. But it doesn’t work I check all the configurations but I cant find the problem. Can you tell me where I can find the problem??

This is the error I get

Error building Real-Time Workshop target for block diagram 'tutorial'. MATLAB error message:

Error using ==> genMakefileAndBuild at 1138

Error(s) encountered while building model "tutorial".

### Re: analog in PWM out

Posted: **Tue Jan 27, 2009 4:52 pm**

by **LubinKerhuel**

try the following :

add the block "Configure Model for dsPIC Target" in your model and dbl click on that block !

If it does not work, report the complete error message

Lubin

### Re: analog in PWM out

Posted: **Tue Jan 27, 2009 9:06 pm**

by **fonty**

I add the block configuration block and I dbl clickt on it but I still get the error.

I add also the model

this is the error that I get

Model error tutorial Simulink Error building Real-Time Workshop target for block diagram 'tutorial'. MATLAB error message:Error using ==> genMakefileAndBuild at 1138Error(s) encountered while building model "tutorial".

Error building Real-Time Workshop target for block diagram 'tutorial'. MATLAB error message:

Error using ==> genMakefileAndBuild at 1138

Error(s) encountered while building model "tutorial".

### Re: analog in PWM out

Posted: **Tue Jan 27, 2009 9:44 pm**

by **LubinKerhuel**

Hi Fonty,

I did not see the "**Configuration Model for dsPIC**" block on your model !

There is definitly a problem in your first problem, briefly :

the LDPICTYPE line in the simulink Configuration Parameters is :

C:\PROGRA~1\MICROC~1\MPLABC~2\lib\libpic30-coff.a C:\PROGRA~1\MICROC~1\MPLABC~2\lib\dsPIC30F\**libpPIC24Fxxx-coff.a** C:\PROGRA~1\MICROC~1\MPLABC~2\lib\libc-coff.a C:\PROGRA~1\MICROC~1\MPLABC~2\lib\libm-coff.a

The 24f has nothing to do there, This problem is corrected with the block I mention earlier.

If it does not work, make sure you are using a blockset up to date.

### Re: analog in PWM out

Posted: **Wed Jan 28, 2009 10:11 am**

by **fonty**

I’m sorry I add the wrong block

### Re: analog in PWM out

Posted: **Mon Apr 13, 2009 8:49 am**

by **crh**

Hello Lubin,

When I use the original model as above and I use a function generator instead of a potentiometer it works well. But when I insert a filter (in this model I use your filter form the explorer16) the system works during 30 to 60 seconds. I tried several kind of filters.

I connected the AN and OC pin with a scope to see what is happening. In the beginning I see the square wave from the OC, but then the voltage drops down and there is no signal any more.

Casparus