[SOLVED] How to change pwmWrite freq?

Post here first, or if you can't find a relevant section!
ismail.cal
Posts: 2
Joined: Wed Mar 02, 2016 9:21 am

Re: Solved: How to change pwmWrite freq?

Post by 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
Last edited by ismail.cal on Sun Mar 27, 2016 4:41 pm, edited 1 time in total.

User avatar
RogerClark
Posts: 7482
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Solved: How to change pwmWrite freq?

Post by 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
Posts: 3
Joined: Tue Jan 17, 2017 8:26 pm

Re: How to change pwmWrite freq?

Post by DaveCalaway » Sun Jan 29, 2017 2:36 pm

melodic wrote: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);
}
Hi guys, i tried this code but nothing works.

Code: Select all

HardwareTimer timer(2);
void setup() {
   // set up pin 9 for PWM
       pinMode(9, PWM);
       
    // The frequency of the PWM signal (i.e., the frequency of a complete on/off cycle) on the Arduino is approximately 490 Hz.
    // On the Maple, the frequency is configurable, defaulting to about 1100 Hz, or 1.1 KHz.
      timer.setPeriod(2041); // 2041 microsecond = 0,0020408 second = 1/490Hz
}

void loop() {
    // void pwmWrite(uint8 pin, uint16 duty_cycle)
    pwmWrite(9,50000);  // Maple duty_cycle = 0-65,535
}
what's wrong?

fari
Posts: 23
Joined: Sun Feb 26, 2017 12:01 pm

Re: Solved: How to change pwmWrite freq?

Post by 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);
}

Post Reply