ex) I`d like to make a pulse 700HZ, hz/s ->> 1/700,
700hz ->1428571.428~ns -> 22,857clock (Uno 62.5ns / cal)
pulse up +down are one cycle(50%duty). UP & Down each 11428.5clock
Theoretically, it is considered to be 1 cycle every 4 times.(at 2857)
2 times to read the "for" statement, 2 times "for" the sub phrase.
but 700HZ was output only when a value corresponding to approx5.06(not4) was put (at 2258) in osciliscope.
Why does the code properly differ so much from the theory?
Hello young_k
Do I understand you right that you like to get a PWM (Puls Width Modulation) Signal with 700Hz and 50% Duyt Cycle? If so why not do this via the Interrupt timer? I made a code to get a 10ms and 100ms task raster for doing some calcalation. The adapted frame looks like this:
bool pintoggle = LOW; // toggle out pin
void setup()
{
noInterrupts(); // suspent interrupts
TCCR1A = 0; // Reset entire TCCR1A register
TCCR1B = 0; // Reset entire TCCR1B register
TCCR1B |= B00000001; // Set CS10 to 1 so we get Prescalar = 1
TIMSK1 |= B00000010; // Set OCIE1A to 1 will enable compare match A
OCR1A = 11428; // OCR1A = 11428 will give 700Hz Calculation: 1,428571428571ms / (62,5ns) /2 = 11428
TCNT1 = 0; // Set Counter1 value to 0
interrupts();
pinMode(2, OUTPUT); // Used for measurement with Oscilloscop
}
void loop()
{
}
ISR(TIMER1_COMPA_vect)
{
TCNT1 = 0; // First, set the timer back to 0 so it resets for next interrupt
if (pintoggle == LOW) {
pintoggle = HIGH;
} else {
pintoggle = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(2, pintoggle);
}
The TCCR1B OCR1A register settings have been adapted to your 700Hz.
Please let me know if I am on the right way for your problem? Observe digital Output 2 with Oszi.
Please check the video with good background on Interrupt usage: