STM32f407vet6 blackboard malfunctional PWM

lobivan
Posts: 7
Joined: Wed Oct 21, 2020 3:06 pm

STM32f407vet6 blackboard malfunctional PWM

Post by lobivan »

Hello, everyone. I've bought a STM32f407vet6 blackboard from China. It seems to work mostly well when speaking about digital inputs and outputs, but I have problems with the PWM, it doesn't start at all neither with

Code: Select all

analogWrite()
nor with

Code: Select all

pwmWrite()
. I have checked whether pins I use include PWM and I've also checked these pins with

Code: Select all

digitalWrite
and they work. I have initialized them both with

Code: Select all

pinMode(...,OUTPUT)
and

Code: Select all

pinMode(...,PWM)
but nothing helps with PWM at all. I don't know what to do with that issue, I've thought about timer settings, but i don't know how to work with registers and basic timer settings don't seem to have any effect at all. Did anyone have this problem?
by stevestrong » Thu Oct 22, 2020 9:15 am
It is not enough to set the pin mode.
You have to setup the timer corresponding to the used pin (PA8 in your example is mapped to timer 1 channel 1, see here).
A usual setup looks like this:

Code: Select all

Timer1.init(); // initializes and stops the timer
pinMode(PA8, PWM); // sets the pin mode
Timer1.setPeriod(<micro_seconds>); // replace with your needed value
Timer1.resume(); // start the timer
Only after this you should use pwmWrite().
Go to full post
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32f407vet6 blackboard malfunctional PWM

Post by stevestrong »

viewtopic.php?f=2&t=301
Which core do you use?
Which IDE? Arduino?
Please post a simple sketch which hows the effect you are describing.
lobivan
Posts: 7
Joined: Wed Oct 21, 2020 3:06 pm

Re: STM32f407vet6 blackboard malfunctional PWM

Post by lobivan »

Yes, I use Arduino IDE. Approximate code:

Code: Select all

#define PWMPin        PA8

int PWMval;

void setup() {

  Serial1.begin(9600);
  pinMode(PWMPin,PWM);


}

void loop() {

  PWMval = millis()%(65535);
  pwmWrite(PWMPin, PWMval);

}
Does not work also if PWMwrite is replaced with analogWrite (PWMis also replaced with OUTPUT ) or if 65535 is replaced with 255 or 1024. I mean, I've read that analogWrite works with numbers 0:255, so I've tried different combinatins. STM32F40xxx manual says the timer is 16-bit as well as the PWM, so 65535 should work, but it doesn't. I suppose the problem is in the timer. I guess it needs to be initialized in some way, but I do not know how. I also tried with pins PB15, PC8 and maybe other pins, don't remember exactly, they belong to different timers, so there is a little chance they are broken simultaneously
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32f407vet6 blackboard malfunctional PWM

Post by stevestrong »

Which core do you use?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32f407vet6 blackboard malfunctional PWM

Post by fpiSTM »

I guess it is libmaple as STM32 core does not have pwmWrite API nor PWM as argument of pin mode.
lobivan
Posts: 7
Joined: Wed Oct 21, 2020 3:06 pm

Re: STM32f407vet6 blackboard malfunctional PWM

Post by lobivan »

I use STM32 master pack as usually recommended everywhere. Exactly this one: https://github.com/rogerclarkmelbourne/Arduino_STM32 . I am much confused because in some examples the pwm is enabled in one line, in others you have to turn on timers via registers, but no one gives manual to f407, there are only f103 examples.
lobivan
Posts: 7
Joined: Wed Oct 21, 2020 3:06 pm

Re: STM32f407vet6 blackboard malfunctional PWM

Post by lobivan »

fpiSTM wrote: Wed Oct 21, 2020 9:21 pm I guess it is libmaple as STM32 core does not have pwmWrite API nor PWM as argument of pin mode.
I tried both pwmWrite and analogWrite, neither works
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32f407vet6 blackboard malfunctional PWM

Post by stevestrong »

It is not enough to set the pin mode.
You have to setup the timer corresponding to the used pin (PA8 in your example is mapped to timer 1 channel 1, see here).
A usual setup looks like this:

Code: Select all

Timer1.init(); // initializes and stops the timer
pinMode(PA8, PWM); // sets the pin mode
Timer1.setPeriod(<micro_seconds>); // replace with your needed value
Timer1.resume(); // start the timer
Only after this you should use pwmWrite().
lobivan
Posts: 7
Joined: Wed Oct 21, 2020 3:06 pm

Re: STM32f407vet6 blackboard malfunctional PWM

Post by lobivan »

stevestrong wrote: Thu Oct 22, 2020 9:15 am It is not enough to set the pin mode.
You have to setup the timer corresponding to the used pin (PA8 in your example is mapped to timer 1 channel 1, see here).
A usual setup looks like this:

Code: Select all

Timer1.init(); // initializes and stops the timer
pinMode(PA8, PWM); // sets the pin mode
Timer1.setPeriod(<micro_seconds>); // replace with your needed value
Timer1.resume(); // start the timer
Only after this you should use pwmWrite().
Thank you, it helped, but TimerX.setPeriod is not nessesary, and I want to point out that the PWM works invertedly (0 = HIGH, 65535 = LOW)
For test I've switched to PA6 and LED_BUILTIN as it is more simple to operate this LED, and it uses TIM3. Also thanks for the table link. My code that works, the line with timerPeriod is commented

Code: Select all

int PWMval = 0;

void setup()
{
  
  Timer3.init();
  pinMode(PA6, PWM);
  //Timer3.setPeriod(1000);
  Timer3.refresh();
  Timer3.resume();
}

void loop() 
{

  PWMval = micros()%65535;
  pwmWrite(PA6,PWMval);
  
}
UPD: Seems like the only functional timer I have is Tim3 as no other seems to be working
Last edited by lobivan on Thu Oct 22, 2020 12:13 pm, edited 1 time in total.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32f407vet6 blackboard malfunctional PWM

Post by stevestrong »

Timerx.setPeriod() is necessary if you want to output PWM signal with a specific period, not identical with your case (maximum resolution 65535).
Post Reply

Return to “STM32F4 based boards”