dsPIC33 Sample ADC synchronous with PWM

For general Flowcode discussion that does not belong in the other sections.
Post Reply
pmgreene01
Posts: 43
http://meble-kuchenne.info.pl
Joined: Sat Jul 08, 2023 7:39 pm
Has thanked: 3 times
Been thanked: 4 times

dsPIC33 Sample ADC synchronous with PWM

Post by pmgreene01 »

I read that the dsPIC33EV that I am working with supports sampling an ADC channel synchronous with the PWM output. I want to do this to get accurate current sensing in a motor drive. I wanted to find out if Flowcode supports this configuration before trying to code it with a C-code block.

BenR
Matrix Staff
Posts: 2132
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 568 times
Been thanked: 752 times

Re: dsPIC33 Sample ADC synchronous with PWM

Post by BenR »

Hello,

No I'm afraid we don't support that out the box in Flowcode. It should be possible to setup the PWM and ADC and then add in a C code block to add the extra register configuration you require.

Shout if you have any issues and we will do our best to help.

pmgreene01
Posts: 43
Joined: Sat Jul 08, 2023 7:39 pm
Has thanked: 3 times
Been thanked: 4 times

Re: dsPIC33 Sample ADC synchronous with PWM

Post by pmgreene01 »

Hi, I am following up on this. I got things to work in my first application because I was using a single PWM channel. In my next iteration, I need to use 3 PWM channels. However, the dialog window to configure the PWM outputs on my 2D diagram has a limited list of pins and the pins linked to the PWM channels are not listed.

I am using a 44 pin dsPIC33EV256GM004 and it shows PWM1H on pin 14 (RB14), PWM2H on pin 10 (RB12) and PWM3H on pin 8 (RB10). Of these, only $PORTB.10 shows in the pull down menu. I don't see $PORTB.12 and $PORTB.14 I attached a screen shot of the configuration window as I see it on my computer. From the manual, it also says that the high speed PWM is not remappable.

Is it possible to access the pins I need or do I need to use a C-Code block?
pwm_window.png
pwm_window.png (65.51 KiB) Viewed 154 times

chipfryer27
Valued Contributor
Posts: 1929
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 421 times
Been thanked: 644 times

Re: dsPIC33 Sample ADC synchronous with PWM

Post by chipfryer27 »

Hi

Page 9 of the data sheet documents which pins can be used for PWM, not all are usable.

Regards

BenR
Matrix Staff
Posts: 2132
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 568 times
Been thanked: 752 times

Re: dsPIC33 Sample ADC synchronous with PWM

Post by BenR »

Hello,

Currently the definition for that family is only targetting the Output Compare / CCP type PWM. I'll investigate how easy it would be to also add support for the further 3 high speed PWM channels.

BenR
Matrix Staff
Posts: 2132
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 568 times
Been thanked: 752 times

Re: dsPIC33 Sample ADC synchronous with PWM

Post by BenR »

On further investigation it looks like there might be one or two technical issues with allowing the 16-bit PIC high speed PWM.

For the 8-bit PICs the advanced PWM modules continue numbering from the CCP PWM but on the 16-bit PIC the numbers start again from 1 so the channel selection gets complicated but maybe isn't impossible.

There's also is a fair bit more configuration setup requiring unlocking resources. Again not impossible but gets trickier to manage in all cases e.g. if interrupts are firing etc.

For now here's some code we use to drive a high speed PWM module on a dspIC33EP256MU806, it's likely to be fairly similar to what you need.

Here's the initialisation C code.

Code: Select all

asm volatile("mov #0xabcd,w10 ; Load first unlock key to w10 register");
asm volatile("mov #0x4321,w11 ; Load second unlock key to w11 register");
asm volatile("mov #0x0003,w0 ; Load desired value of FCLCON3 register in w0");
asm volatile("mov w10, PWMKEY ; Write first unlock key to PWMKEY register");
asm volatile("mov w11, PWMKEY ; Write second unlock key to PWMKEY register");
asm volatile("mov w0,FCLCON3 ; Write desired value to FCLCON3 register");

PTCON2 = 0x0000;      // Master prescaler = 1:1 (128975000Hz)
PTPER = 0xFFFF;       // Master period = 0xffff (calc at max resolution)
PDC3 = 0x0000;        // Duty = 0

asm volatile("mov #0xabcd,w10 ; Load first unlock key to w10 register");
asm volatile("mov #0x4321,w11 ; Load second unlock key to w11 register");
asm volatile("mov #0x0000,w0 ; Load desired value of IOCON1 register in w0");
asm volatile("mov w10, PWMKEY ; Write first unlock key to PWMKEY register");
asm volatile("mov w11, PWMKEY ; Write second unlock key to PWMKEY register");
asm volatile("mov w0,IOCON1 ; Write desired value to IOCON1 register");

asm volatile("mov #0xabcd,w10 ; Load first unlock key to w10 register");
asm volatile("mov #0x4321,w11 ; Load second unlock key to w11 register");
asm volatile("mov #0x0000,w0 ; Load desired value of IOCON1 register in w0");
asm volatile("mov w10, PWMKEY ; Write first unlock key to PWMKEY register");
asm volatile("mov w11, PWMKEY ; Write second unlock key to PWMKEY register");
asm volatile("mov w0,IOCON2 ; Write desired value to IOCON2 register");

asm volatile("mov #0xabcd,w10 ; Load first unlock key to w10 register");
asm volatile("mov #0x4321,w11 ; Load second unlock key to w11 register");
asm volatile("mov #0x5000,w0 ; Load desired value of IOCON1 register in w0");
asm volatile("mov w10, PWMKEY ; Write first unlock key to PWMKEY register");
asm volatile("mov w11, PWMKEY ; Write second unlock key to PWMKEY register");
asm volatile("mov w0,IOCON3 ; Write desired value to IOCON3 register");

PTCONbits.PTEN = 1;   // Enable....

And here is where we update the PWM output duty using a local variable named duty. For a global variable change FCL_ to FCV_.

Code: Select all

PDC3 = FCL_DUTY;
Hope this helps, let us know how you're getting on and I will add this to the list for more thorough investigation.

pmgreene01
Posts: 43
Joined: Sat Jul 08, 2023 7:39 pm
Has thanked: 3 times
Been thanked: 4 times

Re: dsPIC33 Sample ADC synchronous with PWM

Post by pmgreene01 »

Thank you for the detailed reply. This is all a bit over my head which is why I chose Flowcode in the first place.

In my first project (which is working), I used the PWM component and configured the PWM to $PORTB.10 and it is working fine.

My current PWM is configured at 40khz and I have a duty cycle range of 1500 counts (my master clock is 60MHZ).

Can I ignore the "high speed PWM" thing and just configure the pins with the PWM component? Will it work if I just choose pins that are available in the dialog box?

BenR
Matrix Staff
Posts: 2132
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 568 times
Been thanked: 752 times

Re: dsPIC33 Sample ADC synchronous with PWM

Post by BenR »

Hello,

Yes the pwm channels and pins that are available now should all work fine without resorting to using any C code. It's just the high speed PWM peripheral that is currently unsupported which are the pins marked PWMxH, PWMxL in the device datasheet.

Post Reply