how to change the frequency of the PWM signal created by the timer1?

I at the moment struggling to get a PWM output from my timer1.
The frequency of the PWM signal should be be changeable, such that after a desired amount of time will the frequency be increased to a new value and so on…

I am interfacing the timer1 with the help of the arduino library.

First of all is the problem that i can’t get an pwm out at all.

#include "test.h"

volatile int count = 1;

test::test()
{
  pinMode(step_pin, OUTPUT);
  Timer1.initialize(1000000);
  Timer1.attachInterrupt(callback);
}

bool value = false;
long period_used = 0;
static void test::callback()
{
  if(value == true)
  {
      period_used = 20000000;
      Timer1.setPeriod(period_used);
      value = !value;

  }
  else
  {
    period_used = 1000000;
    Timer1.setPeriod(period_used);
    value = !value;
  }
}


void test::test_pwm()
{

  
  Serial.print("period: ");
  Serial.println(period_used);
  Serial.print('\n');
  Serial.print("value: ");
  Serial.println(value);
  
  Timer1.pwm(step_pin, 512);

}

It seems like when i call Timer1.pwm(), are period = 0 and value = 0?

void TimerOne::pwm(char pin, int duty, long microseconds)  // expects duty cycle to be 10 bit (1024)
{
  if(microseconds > 0) setPeriod(microseconds);
  if(pin == 1 || pin == 9) {
    DDRB |= _BV(PORTB1);                                   // sets data direction register for pwm output pin
    TCCR1A |= _BV(COM1A1);                                 // activates the output pin
  }
  else if(pin == 2 || pin == 10) {
    DDRB |= _BV(PORTB2);
    TCCR1A |= _BV(COM1B1);
  }
  setPwmDuty(pin, duty);
  resume();			// Lex - make sure the clock is running.  We don't want to restart the count, in case we are starting the second WGM
					// and the first one is in the middle of a cycle
}

What could be wrong here ? the interrupt was suppose to change the frequency of the PWM, as well as changing the timespan between the interrupts?.. I am doing something wrong here?

this example doesn’t create a PWM on pin 4

I presume this is the same project as in your other Thread

If so please click Report to Moderator and ask for them to be merged so we can easily see all the project info in one place.

In the other Thread @Johnny010 went to the trouble of working out the register values for you.

...R

Based on the table the python code generates, it doesn't seem like that it is nessesary to change the prescaler but just just the OCR1AH/L register... is that correct understood?

Lets try this a different way…
I convinced myself that using the library doesn’t make me any better, so choose to set it up myself.
And i am still stuck…

The table should be based on formula

compare match register = [ 16,000,000Hz/ (prescaler * desired interrupt frequency) ] - 1

Which means if I want the interrupt to every 1Hz that would mean the value of OCR1A = 15624, and make use of he 1024 prescaler. and 16 bit timer.

Cool.

I change the state of the Pin in the ISR, but how come am I still getting an frequency of 490 hz?..

#include "test.h"

test::test()
{
  pinMode(10,OUTPUT);
  //Timer1 setup1 Interrup at 1hz
  cli(); // Stop interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  OCR1A = 1999; // Compare register value = cpu_fre/(interrupt_freq*prescaler)-1 (must be <65536)
  TCCR1B |= (1 << WGM12);
  TCCR1B |= (1 << CS11);
  TIMSK1 |= (1 << OCIE1A);

  sei(); //allow interrupts
}

bool toggle1 = 0;
ISR(TIMER1_COMPA_vect)
{
 digitalWrite(10,!digitalRead(10));
}

Your timer project must be very important to you. You posted here and here in addition to this thread. Plus here on Electronics Stack Exchange and here on Arduino Stack Exchange. and also here.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

This thread locked. It is a bit annoying to see you spraying your questions everywhere. Rather than get better answers you get fragmented ones, as people don't see the whole project. And then when they see what you are doing they get annoyed.