I have this code, which puts the Arduino into CTC mode with interrupts, and outputs a square wave on PD6
(OC0A
):
void setup_timer(double p_ms, double duty){
DDRD |= (1 << 6); // set pin 3 as output
TCCR0A = _BV(COM0A0); // toggle OC0A on Compare Match
TCCR0B = _BV(WGM01); // set CTC mode WGM0[2,1,0] = 0b010
TIMSK0 |= _BV(OCIE0A); // Enable CTC interrupt
OCR0A = 128; // set on and off time
TCCR0B |= ( (0 << CS02) | (0 << CS01) | ( 1 << CS00)); // CS02:0 - No prescaling
sei();
}
In the ISR
(which is not shown) I want to be able to see whether OC0A
(i.e. PD6
) is HIGH
or LOW
.
I assumed that I could do a digitalRead()
, but this post, Re: How to read the state of an output pin ? says:
Looking at the code the digitalRead() function just does a read of the appropriate bit in the appropriate PINx register without modifying anything, so it should work.
One catch, if it's a PWM pin the PWM will be stopped.
So, will the toggling of OCR0A
be stopped?
This answer to How can I digitalRead a pin that is in pinMode OUTPUT? suggests reading PORTD directly, with
bitRead(PORTD,6);
However, will that work for PWM output or will PWM also be stopped by reading PORTD
?
Addendum
I plan to use this code either as (primarily) a stand alone AVR µController (48/88/168/328 IC), but maybe in a Nano/Uno (if it will work) for testing purposes. I understand that using Timer0
will affect the operation of millis()
, delay()
etc., but the rest of the code will not rely on any of those functions... so I presume (maybe incorrectly) that the code will work as expected. Is that correct?
In addition, I don't intend on using a setup()
& loop()
program structure but rather main()
. Just out of interest, would loop()
work (i.e. is it reliant on Timer0
)? I don't believe that it is.