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
https://github.com/PaulStoffregen/TimerOne/blob/master/examples/FanSpeed/FanSpeed.pde