I need to generate a square wave signal in range 1 Hz - 200 Hz with 50% duty.
With library TimeOne it works fine from 1-64, doesnt from 65 to 124, and than works above 125 with more "holes"
as a workaround I'm able to fulfill the task using TimeOne for frequencies below 32 Hz and tone() for higher, but I'm interested in finding out what causes the holes.
if ( freq > 31 ) // do through tone()
else // do through t.pwm
because t.pwm(9, 16666) works
t.pwm(9, 13333) does not
and again
t.pwm(9, 8333) works
t.pwm(9, 9090) does not
etc
so as a workaround for freq < 32 I call t.pwm and for higher tone() and this works fine just I'm looking to figure out what prevents timerone to perform in the whole range
Why not use the Arduino micros() function? A simple example:
// type a frequency from 1 to 200 in top of serial monitor and hit [ENTER]
unsigned long cycleStart, period = 100000UL, onTime = period / 2;
const byte outPin = 13; // built in LED but could be any digital pin
void setup()
{
Serial.begin(9600);
pinMode(outPin, OUTPUT);
}
void loop()
{
digitalWrite(outPin, micros() - cycleStart < onTime);
if(micros() - cycleStart > period) // restart timer
cycleStart += period;
// if there's any serial available, read it:
if (Serial.available() > 0)
{
// look for the next valid integer in the incoming serial stream:
int freq = Serial.parseInt();
freq = constrain(freq, 1, 200);
period = 1000000 / freq; // 1 million / freq
onTime = period / 2; // duty cycle (100 / 2 = 50)
cycleStart = micros();
Serial.println(freq);
}
}
so the question is : did anyone experience issues using timerOne lib ? or i'm the lucky one 'cause i can accept current solution but i'm still curious ^^
principiante7:
so the question is : did anyone experience issues using timerOne lib ? or i'm the lucky one 'cause i can accept current solution but i'm still curious ^^
have you tried using timer interrupts; accessing the registers directly without the library?