PWM in the MIAC V2

For MIAC users to discuss projects, applications, and any other issues related to the MIAC unit.

Moderators: Benj, Mods

go233
Posts: 96
Joined: Mon Mar 02, 2015 12:32 pm
Has thanked: 5 times
Been thanked: 11 times
Contact:

PWM in the MIAC V2

Postby go233 » Wed Jul 19, 2017 5:39 pm

Hello can I have a bit of help on how to set the PWM of my MIAC V2, please?
So I need to generate a square wave variable from 200Hz to 2Khz ish, and I need to vary the duty cycle from 50% to 90% ish.
what would you set in the PWM macro parameters?
ChangePeriod
SetDutyCycle

So when I want to generate the square waveform on the transistor at output A I will write the following instructions in flowcode 6:
OutputOn(1)
ChangePeriod (UINT, INT)
SetDutyCycle (BYTE)
Enable

and the MIAC should generate this waveform continuously, is it correct?
Thanks
Giovanni

User avatar
LeighM
Matrix Staff
Posts: 1429
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 369 times
Been thanked: 520 times
Contact:

Re: PWM in the MIAC V2

Postby LeighM » Wed Jul 19, 2017 7:15 pm

Hi,
Have a look at the examples in this thread
viewtopic.php?f=28&t=19006
If that does not help let me know and I will boot up Flowcode tomorrow.

go233
Posts: 96
Joined: Mon Mar 02, 2015 12:32 pm
Has thanked: 5 times
Been thanked: 11 times
Contact:

Re: PWM in the MIAC V2

Postby go233 » Thu Jul 20, 2017 1:38 pm

I cannot get the duty cycle and the frequency to change using ChangePeriod and SetDutyCycle!
I need to generate 200Hz to 2Khz ish, and I need to vary the duty cycle from 50% to 90% ish.
I have attached my program and a picture from the oscilloscope: I alwaus get the same picture regardless the values of INT Prescaler abd BYTE duty (I leave UINT Period = 255) I tried.
Giovanni
Attachments
pwm.fcfx
(4.42 KiB) Downloaded 69 times

User avatar
Steve
Matrix Staff
Posts: 3004
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 68 times
Been thanked: 352 times
Contact:

Re: PWM in the MIAC V2

Postby Steve » Fri Jul 21, 2017 2:44 pm

Your program does not appear to use the PWM component. You need to add this and use its functions to set the PWM duty.

Have you looked at the Flowcode programs in that thread Leigh suggested? Those are for the dsPIC MIAC v3.

There are also examples for the MIAC PIC (i.e. v2) here: http://www.matrixtsl.com/miac/resources/

In particular, the MOTOR_5 example uses PWM.

go233
Posts: 96
Joined: Mon Mar 02, 2015 12:32 pm
Has thanked: 5 times
Been thanked: 11 times
Contact:

Re: PWM in the MIAC V2

Postby go233 » Sat Jul 22, 2017 8:24 pm

Yes I can change the duty cycle no problem.
But
I have set ChangePeriod (255, INT)
I have noticed that not all the values are accepted by the prescaler:
for instance 1,4, 16 are accepted and the change the output frequency.
Could you tell me which number are accepted by the prescaler please?
Thanks
Giovanni

go233
Posts: 96
Joined: Mon Mar 02, 2015 12:32 pm
Has thanked: 5 times
Been thanked: 11 times
Contact:

Re: PWM in the MIAC V2

Postby go233 » Sun Jul 23, 2017 10:10 am

I looked at the datasheet of the 18F4455 and I have just realized that the prescaler can divide for 1 4 16.
So the minimum frequency can be freq of the clock / 16.
I guess I cannot change the clock frequency.....or I can?

User avatar
Steve
Matrix Staff
Posts: 3004
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 68 times
Been thanked: 352 times
Contact:

Re: PWM in the MIAC V2

Postby Steve » Mon Jul 24, 2017 8:32 am

The clock frequency within Flowcode should equal the clock frequency used by your hardware. So you can't just change that value and expect it to work.

But you can use a different clock speed in your hardware (e.g. a slower crystal or using the internal oscillator options).

go233
Posts: 96
Joined: Mon Mar 02, 2015 12:32 pm
Has thanked: 5 times
Been thanked: 11 times
Contact:

Re: PWM in the MIAC V2

Postby go233 » Mon Jul 24, 2017 11:58 am

how can I change the clock frequency of the micro inside my MIAC V2?
and
could you confirm the prescaler values that I can use, please?
Thanks
Giovanni

User avatar
LeighM
Matrix Staff
Posts: 1429
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 369 times
Been thanked: 520 times
Contact:

Re: PWM in the MIAC V2

Postby LeighM » Mon Jul 24, 2017 1:07 pm

Hi,
The clock could be changed but would cause issues elsewhere.
As the PWM component Prescaler values indicate, yes, only a prescaler of 1, 4 or 16 can be used.
So the slowest CCP clock is 750KHz.
If you use a 10 bit value for the Period and the Duty (using SetDutyCycle10Bit) you could get the frequency down to about 730Hz

Maybe you could use the PWM component down to say 700Hz, then use a bit-banged loop with timed delays for frequencies below that?

EDIT:
Ben tells me that the 10 bit support on this PIC device is not that simple, so the above is not quite true,
you are best to stick with the 8 bit period for hardware PWM, which will get you down to about 3KHz
Other than that you will need to use the timed loops, or interrupts, as above.

Note to self: read the device datasheet :lol:

go233
Posts: 96
Joined: Mon Mar 02, 2015 12:32 pm
Has thanked: 5 times
Been thanked: 11 times
Contact:

Re: PWM in the MIAC V2

Postby go233 » Tue Jul 25, 2017 10:54 am

I think I will use the 8 bit period for hardware PWM, which is gets me an output of about 3KHz.
Thanks
Giovanni