Center Aligned PWM

Post here first, or if you can't find a relevant section!
Post Reply
Shayan Najam
Posts: 2
Joined: Fri May 15, 2020 12:19 pm

Center Aligned PWM

Post by Shayan Najam »

Hello,

I am a bit of a Newbie here. Last year I made a 3 phase bldc controller using the stm32f103, for that I implemented a centre aligned (symmetric) PWM with the help of this forum. Unfortunately I lost my code during a PC reset and worst yet all the links on this forum that helped me achieve it are now down. I desperately need to get it working again since I have made significant investements in hardware with the hope of writing the same code again.

I need help to get the PWMs in timer 1 and timer 4 to work in center aligned mode, as for now they work in edge aligned. Help would be much appreciated in how to manipuilate the timers to achieve this. As mentioned I got all the answer initially on this forum but those links are now dead.

Code: Select all

void setup(){
Timer1.pause();
Timer4.pause();
Timer1.setPrescaleFactor(1);
Timer1.setOverflow(1500);
Timer4.setPrescaleFactor(1);
Timer4.setOverflow(1500);
Timer1.refresh();
Timer4.refresh();
Timer1.resume();
Timer4.resume();

pinMode (PA8, PWM);
pinMode (PA9, PWM);
pinMode (PA10, PWM);
pinMode (PB6, PWM);
pinMode (PB7, PWM);
pinMode (PB8, PWM);
}
  void loop()
{
pwmWrite(PA8, 1000);
pwmWrite(PA9, 200);
pwmWrite(PA10, 500);
pwmWrite(PB6, 200);
pwmWrite(PB7, 300);
pwmWrite(PB8, 400);
}
outputs on timer1 (PA8 PA10 and PA9) look like so
asdas.JPG
asdas.JPG (59.38 KiB) Viewed 5988 times
I believe the answer was in one of these links but they are down;
viewtopic.php?t=1318
viewtopic.php?t=2561
viewtopic.php?t=3957

Looking forward to your suggestions, could really use some help here,

Thank you
by fpiSTM » Fri May 15, 2020 1:01 pm Go to full post
Shayan Najam
Posts: 2
Joined: Fri May 15, 2020 12:19 pm

Re: Center Aligned PWM

Post by Shayan Najam »

Thank you so much for the prompt reply, found my answer in the first link.

much appreciated
Rocco Tarocco
Posts: 2
Joined: Tue Jan 26, 2021 4:07 pm

Re: Center Aligned PWM

Post by Rocco Tarocco »

Hi All,

I am quite low level for programming micros.
Reading the previous posts I was not able to put in practise the Center Aligned way the others, yes.

I opened the Timers libraries but I never seen (or because my poor level can't get infos I need) any reference useful to write the proper commands on Setup.

Is there anyone can help me? I need a minimum example to write to let it work.
The micro/board I can use is one 48 or 64 pins included on the STM32 library, ex: STM32F103RB or STM32F072RB similar smaller
Thanks
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Center Aligned PWM

Post by mrburnette »

ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Center Aligned PWM

Post by ag123 »

HardwareTimer from 'old' leaflabs, there are some examples there, mostly works for roger's (libmaple) core
http://docs.leaflabs.com/static.leaflab ... dwaretimer
you'd need to keep the reference manual handy as well e.g. for stm32f103
in particular the hardware timer sections
https://www.st.com/resource/en/referenc ... ronics.pdf
RM0008 page 338 General-purpose timers (TIM2 to TIM5)
PWM center-aligned mode
Center-aligned mode is active when the CMS bits in TIMx_CR1 register are different from
‘00 (all the remaining configurations having the same effect on the ocxref/OCx signals). The
compare flag is set when the counter counts up, when it counts down or both when it counts
up and down depending on the CMS bits configuration. The direction bit (DIR) in the
TIMx_CR1 register is updated by hardware and must not be changed by software. Refer to
Center-aligned mode (up/down counting).
Figure 131 shows some center-aligned PWM waveforms in an example where:
• TIMx_ARR=8,
• PWM mode is the PWM mode 1,
• The flag is set when the counter counts down corresponding to the center-aligned
mode 1 selected for CMS=01 in TIMx_CR1 register.
Rocco Tarocco
Posts: 2
Joined: Tue Jan 26, 2021 4:07 pm

Re: Center Aligned PWM

Post by Rocco Tarocco »

I will go deeper into stuffs sent me.
Thanks both of you for so prompt and plenty replies.
TheArvie
Posts: 1
Joined: Wed Mar 24, 2021 3:49 pm

Re: Center Aligned PWM

Post by TheArvie »

Hello, fellow bit-wise friend. If you, like me, arrived here trying to find a 6-wave pwm centered signal, being those 3 positive and 3 negative polarity (i.e. complementary pwm), I hope this helps:

Code: Select all

#define PWM_OUT_U      PA8   //PWM output for U phase
#define PWM_OUT_U_COMP PB13  //complementary output for U phase
#define PWM_OUT_V      PA9   //PWM output for V phase
#define PWM_OUT_V_COMP PB14  //complementary output for V phase
#define PWM_OUT_W      PA10  //PWM output for W phase
#define PWM_OUT_W_COMP PB15  //complementary output for W phase
Remember, it is NOT possible to choose the output pins (PA8, PB13, etc). Those are hardware-defined pins. (Maybe on 100 or 144 pin-chips, but you don't have one of those, so, don't try).

Code: Select all

  pinMode(PWM_OUT_U,PWM);
  pinMode(PWM_OUT_U_COMP,PWM);
  pinMode(PWM_OUT_V,PWM);
  pinMode(PWM_OUT_V_COMP,PWM);
  pinMode(PWM_OUT_W,PWM);
  pinMode(PWM_OUT_W_COMP,PWM);
Ports/pins must be declared as PWM output mode.

Let's set the Timer1, which is capable of handling 3 synchronized PWM signals and it's complementary friends.

Code: Select all

  HardwareTimer timer1(1);  
  timer1.pause();
  timer1.setPeriod(200); //72000/(200/40) = 14400 // 200 = period, 40 = constant, 14400 = max value on pwmWrite(channel, value) (test before using 40 as constant! not sure why 40!)
  TIMER1->regs.adv->CCER |= 0x555; //3ch compl enable. 0x55 = 2ch compl enable. 0x5 = 1ch compl enable. As seen on RM0008 pages 353 & 354, CCxNE and CCxE bits
  TIMER1->regs.bas->CR1 |= 0x20; //centered ye PWM waveforms (RM0008, page 338, CMS bits)
  timer1.refresh(); //go son
  timer1.resume(); //support gay and women rights & be happy
200 on setPeriod will give you a 400-microsecond period or 1/400 = 2.5kHz for all the three PWM channels. Why not 200 microseconds? I don't know. Do I care? No.
72000 is the main frequency for STM32, in KHz (72MHz).

The timer will count up to 72000/(200/40) = 14400, so that's the maximum value you can/need to put on pwmWrite. This 14400 number is automatically calculated and set by the setPeriod function.
Another way to see how far is the counter counting to, is reading the register:

Code: Select all

 int maxValue = TIMER1->regs.bas->ARR;
So, if you want a 0% to 100% PWM duty cycle, you can try this:

Code: Select all

int maxValue = TIMER1->regs.bas->ARR;
    pwmWrite(your_PWM_pin, your_duty*maxValue/100);
As you can see, you only need to set the positive polarity, the negative/complementary is automatic. Here are all the three channels:

Code: Select all

  pwmWrite(PWM_OUT_U, 1000); //This should now write to your Ports (PA8 -- PB13, PA9 -- PB14 and PA10 -- PB15 your PWM signal).
  pwmWrite(PWM_OUT_V, 2000); //I love you
  pwmWrite(PWM_OUT_W, 14400); //f! racism

Oh yes, one last thing, the setPeriod function takes care of both the prescaler factor and the overflow.

so that:

Code: Select all

    timer.setPrescaleFactor(5);
    timer.setOverflow(255);
is not necessary.

Apologies for my horrible english-writing abilities, and goodbye.
Post Reply

Return to “General discussion”