[SOLVED] How to change pwmWrite freq?

Post here first, or if you can't find a relevant section!
melodic
Posts: 8
Joined: Mon Oct 05, 2015 5:17 pm

[SOLVED] How to change pwmWrite freq?

Post by melodic » Mon Oct 05, 2015 5:22 pm

I need to use more than 500hz for generating sound but I can`t change PWM freq. How to do that?
Last edited by melodic on Fri Oct 09, 2015 4:48 pm, edited 1 time in total.

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: How to change pwmWrite freq?

Post by mrburnette » Mon Oct 05, 2015 7:05 pm

melodic wrote:I need to use more than 500hz for generating sound but I can`t change PWM freq. How to do that?
Fortunately, this has previously been documented:
http://www.stm32duino.com/viewtopic.php?t=587

This entire site has been indexed by Google. So, for future needs, try this first:

Code: Select all

PWM frequency site:stm32duino.com
Just put your search terms in Google and then add the site: suffix.


Ray

melodic
Posts: 8
Joined: Mon Oct 05, 2015 5:17 pm

Re: How to change pwmWrite freq?

Post by melodic » Tue Oct 06, 2015 3:34 pm

I have the same problem. Then I use setOverflow then PWM stop to work.

Code: Select all

const int analogInPin = 11;
const int pwmOutPin = PA1;

int sensorValue = 0;
int outputValue = 0;
HardwareTimer pwmtimer(2);
void setup() {
    pinMode(analogInPin, INPUT_ANALOG);
    Serial.begin(115200);
    pinMode(pwmOutPin, PWM);
pwmtimer.pause();
pwmtimer.setPrescaleFactor(1);
pwmtimer.setCount(0);
//pwmtimer.setOverflow(4000);
pwmtimer.setCompare(TIMER_CH2, 1000);
pwmtimer.refresh();
pwmtimer.resume();
  pinMode(pwmOutPin, PWM);
}

void loop() {
    sensorValue = analogRead(analogInPin);
    outputValue = map(sensorValue, 0, 4095, 0, 65535);
    pwmWrite(pwmOutPin, outputValue);


    Serial.print("sensor = " );
    Serial.print(sensorValue);
    Serial.print("\t output = ");
    Serial.println(outputValue);
}

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: How to change pwmWrite freq?

Post by madias » Tue Oct 06, 2015 4:57 pm

I think the slow frequency of PWM is caused by the higher bitrate (16bit vs 8bit):
http://leaflabs.com/docs/lang/api/analogwrite.html
So the maximum is about 1.1KHZ.
I didn't researched further (creating pwm from the core), because I switched to an octal DAC for my project.

melodic
Posts: 8
Joined: Mon Oct 05, 2015 5:17 pm

Re: How to change pwmWrite freq?

Post by melodic » Wed Oct 07, 2015 3:01 pm

Solved.

Code: Select all

HardwareTimer pwmtimer(2);
void setup() {
//    pinMode(11, INPUT_ANALOG);
//    Serial.begin(115200);
    pinMode(PA1, PWM);
    pwmtimer.setPrescaleFactor(1);
    pwmtimer.setPeriod(50);
}
But in this case maximum value of PWM is about 4000.

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: How to change pwmWrite freq?

Post by mrburnette » Fri Oct 09, 2015 12:37 am

melodic wrote:Solved.
Please edit title line in post #1 to include "Solved:" helps w/ searches.

Ray

monsonite
Posts: 49
Joined: Tue May 05, 2015 10:34 am

Re: Solved: How to change pwmWrite freq?

Post by monsonite » Mon Oct 26, 2015 3:45 pm

Hi Roger & All,

I am looking for complementary PWM output from a couple of pins, as part of my micro-solar inverter project

Has anyone got code to support this timer mode - that is compatible with PWMwrite function?


Thanks in advance


Ken

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Solved: How to change pwmWrite freq?

Post by mrburnette » Mon Oct 26, 2015 11:32 pm

monsonite wrote:Hi Roger & All,
I am looking for complementary PWM output from a couple of pins, as part of my micro-solar inverter project
Has anyone got code to support this timer mode - that is compatible with PWMwrite function?
Thanks in advance
Ken

Not trying to be a smartass, but are any of these relevant:
https://www.google.com/#q=stm32f103+complementary+pwm


Ray

monsonite
Posts: 49
Joined: Tue May 05, 2015 10:34 am

Re: Solved: How to change pwmWrite freq?

Post by monsonite » Tue Oct 27, 2015 8:08 am

Thanks Ray,

I really just wanted to know if the means to select the complimentary mode was included within the "Leaf Labs" library implementation.


Ken

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Solved: How to change pwmWrite freq?

Post by mrburnette » Tue Oct 27, 2015 11:55 am

monsonite wrote:Thanks Ray,
I really just wanted to know if the means to select the complimentary mode was included within the "Leaf Labs" library implementation.
Ken

Ah, sensible question.
That changes the query to: complementary pwm site:leaflabs.com which resolves to:
https://www.google.com/search?q=complem ... aflabs.com

And this looks like a likely discussion: http://forums.leaflabs.com/topic.php?id=2000
and this one, too: http://forums.leaflabs.com/topic.php?id=794

Ray

Post Reply