Skip to main content
Arduino

Return to Question

Commonmark migration
Source Link

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

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.

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.

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.

Corrected PB3 -> PD6
Source Link
Greenonline
  • 3.2k
  • 7
  • 36
  • 48

I have this code, which puts the Arduino into CTC mode with interrupts, and outputs a square wave on pin 3PD6 (OC0A):

void setup_timer(double p_ms, double duty){
 DDRBDDRD |= (1 << 36); // 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. pin 3PD6) 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 PORTBPORTD directly, with

bitRead(PORTBPORTD,36);

However, will that work for PWM output or will PWM also be stopped by reading PORTBPORTD?


###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.

I have this code, which puts the Arduino into CTC mode with interrupts, and outputs a square wave on pin 3 (OC0A):

void setup_timer(double p_ms, double duty){
 DDRB |= (1 << 3); // 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. pin 3) 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 PORTB directly, with

bitRead(PORTB,3);

However, will that work for PWM output or will PWM also be stopped by reading PORTB?


###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.

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.

Added addendum and AVR tag
Source Link
Greenonline
  • 3.2k
  • 7
  • 36
  • 48

I have this code, which puts the Arduino into CTC mode with interrupts, and outputs a square wave on pin 3 (OC0A):

void setup_timer(double p_ms, double duty){
 DDRB |= (1 << 3); // 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. pin 3) 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 PORTB directly, with

bitRead(PORTB,3);

However, will that work for PWM output or will PWM also be stopped by reading PORTB?


###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.

I have this code, which puts the Arduino into CTC mode with interrupts, and outputs a square wave on pin 3 (OC0A):

void setup_timer(double p_ms, double duty){
 DDRB |= (1 << 3); // 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. pin 3) 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 PORTB directly, with

bitRead(PORTB,3);

However, will that work for PWM output or will PWM also be stopped by reading PORTB?

I have this code, which puts the Arduino into CTC mode with interrupts, and outputs a square wave on pin 3 (OC0A):

void setup_timer(double p_ms, double duty){
 DDRB |= (1 << 3); // 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. pin 3) 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 PORTB directly, with

bitRead(PORTB,3);

However, will that work for PWM output or will PWM also be stopped by reading PORTB?


###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.

Source Link
Greenonline
  • 3.2k
  • 7
  • 36
  • 48
Loading

AltStyle によって変換されたページ (->オリジナル) /