[SOLVED] How to change pwmWrite freq?

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?

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?

melodic
Tue Oct 06, 2015 3:34 pm
I have the same problem. Then I use setOverflow then PWM stop to work.

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
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
Wed Oct 07, 2015 3:01 pm
Solved.
HardwareTimer pwmtimer(2);
void setup() {
// pinMode(11, INPUT_ANALOG);
// Serial.begin(115200);
pinMode(PA1, PWM);
pwmtimer.setPrescaleFactor(1);
pwmtimer.setPeriod(50);
}

mrburnette
Fri Oct 09, 2015 12:37 am
melodic wrote:Solved.

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


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

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


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

ismail.cal
Wed Mar 02, 2016 12:33 pm
How do we want to produce up to 100 kHz with maple . How do I need PWM frequency calculations

RogerClark
Wed Mar 02, 2016 9:23 pm
@ismail.cal

Don’t cross post.

Several people have replied to your other thread.

I will remove your posting to this thread as its unnecessary


DaveCalaway
Sun Jan 29, 2017 2:36 pm
melodic wrote:Solved.
HardwareTimer pwmtimer(2);
void setup() {
// pinMode(11, INPUT_ANALOG);
// Serial.begin(115200);
pinMode(PA1, PWM);
pwmtimer.setPrescaleFactor(1);
pwmtimer.setPeriod(50);
}

fari
Fri Sep 08, 2017 8:22 pm
Hi Dave!
You code does not work, because you pwmWrite is in the loop and it is permanently overwriting the signal.

Here is a code which work


#define PWM_OUT PA8 //PWM output
#define PWM_OUT_COMP PB13 //complementary output
void setup(){

// initialize digital pin PC13 as an output.
pinMode(PC13, OUTPUT);
HardwareTimer timer1 = HardwareTimer(1);

timer1.setPrescaleFactor(1);
timer1.setPeriod(2041); //490
//timer1.setPeriod(10); //100kHz

pinMode(PWM_OUT, PWM); //Ideally these would be done as bit wise as I dont know exactly whats being set here..
pinMode(PWM_OUT_COMP, PWM);

timer_dev *t = TIMER1; //refers t to Timer 8 memory location, how to read back?
timer_reg_map r = t->regs;

bitSet(r.adv->CCER,0); //this should enable complimentary outputs
bitSet(r.adv->CCER,2);
pwmWrite(PWM_OUT, 10); //

}

void loop(){

// I am alive
digitalWrite(PC13, HIGH); // turn the LED on (HIGH is the voltage level)
//digitalWrite(PA5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(PC13, LOW); // turn the LED off by making the voltage LOW
//digitalWrite(PA5, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}


Leave a Reply

Your email address will not be published. Required fields are marked *