pwm whit timer 1 for h bridge motor driver

hi friends
I' used the following code to generate the pwm whit timer 1 on due board
I expect the output frequency on pin 9 &13 to be 50khz But the output frequency is only 650hz
Can anyone help me what is wrong in my cod ?

#include <timer.h>
int a=0;
int out1 = 9;
int out2 = 13;
Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution
bool toggle_out(void *) {
if (a==200){
a=0;
}
a=a+1;
return true; // repeat? true
}

void setup() {
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
// call the toggle_out function every 1 micros
timer.every(1, toggle_out);
}

void loop() {
timer.tick(); // tick the timer
if (a<=100)
{
digitalWrite(out1, HIGH);
}

if (a>100)
{
digitalWrite(out1, LOW);
}
if ((a>100)&&(a<199))
{
digitalWrite(out2, HIGH);
}

if (a>=199)
{
digitalWrite(out2, LOW);
}
}

pwm whit timer 1 for h bridge motor driver
Today at 05:32 am
hi friends
I' used the following code to generate the pwm whit timer 1 on due board
I expect the output frequency on pin 9 &13 to be 50khz But the output frequency is only 650hz
Can anyone help me what is wrong in my cod ?

#include <timer.h>
int a=0;
int out1 = 9;
int out2 = 13;
Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution
bool toggle_out(void *) {
if (a==200){
a=0;
}
a=a+1;
return true; // repeat? true
}

void setup() {
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
// call the toggle_out function every 1 micros
timer.every(1, toggle_out);
}

void loop() {
timer.tick(); // tick the timer
if (a<=100)
{
digitalWrite(out1, HIGH);
}

if (a>100)
{
digitalWrite(out1, LOW);
}
if ((a>100)&&(a<199))
{
digitalWrite(out2, HIGH);
}

if (a>=199)
{
digitalWrite(out2, LOW);
}
}

// call the toggle_out function every 1 micros Let me paraphrase that " // call the toggle_out function every 16 processor cycles".

Good luck with that.

Please remember to use code tags when posting code.

thanks for your response
How can I fix the problem?

You could search the forum for topics about how to change PWM frequency. There must be a ton of 'em.

I assumed you'd already done that.

can your hardware support a timer event every 1 usec (i.e. 1MHz)?

Duplicate topics merged

Do not start topics on the same subject in multiple sections of the forum