cattledog:
I was testing using two indicator leds with visually apparent pwm timing. One led was turned on at the exit from setup.The led controlled by the compare match at TOP was interesting. When TCNT4 was set to 0 at the start, the led turned immediately on.
What I later found with starting TCNT4 =1 , the timer would go through one complete cycle to the 16bit max value, and then start the periodic 50% output.
There is a "hack" which actualy has the pin low for the first half cycle, and that is to start with TCNT4 at the compare match value less than the 16 bit top.
For example in this case if TCNT4 is initialized to 65535-7815 = 57720 there is a half second dead band before the output goes high.
//// set up Timer 4
int myEraserDir = 7; // this is 111 in binary and is used as an eraser
TCCR4B &= ~myEraserDir; // this operation (AND plus NOT), set the three bits in TCCR4B to 0
TCCR4A = _BV(COM4A0)| _BV(WGM41) | _BV(WGM40); // fast PWM
TCCR4B = _BV(WGM43) | _BV(WGM42); // 16MHz/1024=15625Hz
OCR4A = 7815; // 15625Hz/1Hz=15625 (50% duty--> 7812.5)
TCNT4 = 57720;
TCCR4B |= _BV(CS42)| _BV(CS40); //start timer
Can I ask you why 7815? If I am getting it correctly is because 15625-->1 Hz so 15625/2 is 30 seconds. But then, why 7815 and not 7813?
In case it's 2Hz and I want to keep the pin low for the first quarter cycle should I replace 7813 with 3907?