2

I am controlling an ADC with my Arduino Uno. I would like the clock of the ADC to be the same frequency of the Arduino. Is there any way that I can have a constant clock output from one of the Arduino pin ?

Thanks,

Liam

6

This outputs 8 MHz on pin 9:

#ifdef __AVR_ATmega2560__
  const byte CLOCKOUT = 11;  // Mega 2560
#else
  const byte CLOCKOUT = 9;   // Uno, Duemilanove, etc.
#endif

void setup ()
  {
  // set up 8 MHz timer on CLOCKOUT (OC1A)
  pinMode (CLOCKOUT, OUTPUT); 
  // set up Timer 1
  TCCR1A = bit (COM1A0);  // toggle OC1A on Compare Match
  TCCR1B = bit (WGM12) | bit (CS10);   // CTC, no prescaling
  OCR1A =  0;       // output every cycle
  }  // end of setup

void loop ()
  {
  // whatever 
  }  // end of loop

How can add a prescaler?

You change the prescaler bits. You can look at the datasheet or my cheat sheet here:

Timer 1 bits

You may not need a prescaler, depending on the frequency. Change OCR1A to some number between 0 and 65535 to slow it down.


you use TCR1A, COM1A0 and similar variables. Are these always present and implicitly defined when running code on the ATmega

Registers like TCCR1A and so on are defined in files which are automatically included by the Arduino IDE. If you use another toolchain they may be also automatically included. The start point is:

#define <avr/io.h>

Inside that file it checks your processor type (from a symbol passed to the compiler) and then includes an appropriate sub-file. Inside those files are defines which relate the register names to their address in the address-space of that particular chip. For example:

#define TCCR1A _SFR_MEM8(0x80)

The _SFR_MEM8 basically generates a pointer to a volatile address (because it might change without the compiler knowing it) and then dereferences that variable.

Notice that the number 0x80 in that define agrees with the number shown on my chart.

Underneath that define in the appropriate file are also the bit positions for the bits in that register, like this:

#define TCCR1A _SFR_MEM8(0x80)
#define WGM10 0
#define WGM11 1
#define COM1B0 4
#define COM1B1 5
#define COM1A0 6
#define COM1A1 7

Secondly, do I correctly understand that the output pin is defined in TCR1A, Output A and Output B, to be on Digital Pin 9 and 10 respectively?

Yes, in effect. The datasheet says that if you set the appropriate bits in TCCR1A (note the spelling) then OC1A (board pin 9 on the Uno) or OC1B (board pin 10 on the Uno) will be unchanged/toggled/cleared/set depending on the bits. You can find these names on the datasheet for the Atmega328P (and other devices) and then use the Arduino schematic to find which processor pins are connected to which board pins.

Atmega328P datasheet snippet:

Atmega328P datasheet

Uno datasheet snippet:

Uno datasheet


Why do you set it to "Toggle" rather than "Set"?

Because every time the counter matches I want to flip the pin. That is, on/off/on/off etc.


What does CTC stand for?

Clear Timer on Compare. What this means is that (unlike other modes) once the compare match is made, the timer is cleared, thus it starts counting up from zero again.

  • This is exactly what I needed . How can add a prescaler ? – Liam F-A Oct 10 '15 at 20:39
  • See amended reply. – Nick Gammon Oct 10 '15 at 22:10
  • Thank you for this helpful chart and code. I have trouble understanding some finer points. First, you just, well, use TCR1A, COM1A0 and similar variables. Are these always present and implicitly defined when running code on the ATmega (or just on an arduino) or do you actually declare them outside of your shown code snippet? Secondly, do I correctly understand that the output pin is defined in TCR1A, Output A and Output B, to be on Digital Pin 9 and 10 respectively? Why do you set it to "Toggle" rather than "Set"? And lastly, an easy one: What does CTC stand for? Many thanks to you. – Kjeld Schmidt Oct 10 '16 at 17:49
  • See amended reply. – Nick Gammon Oct 10 '16 at 20:18
  • This is an absolutely amazing answer. I wish I had more rep to give you bounty points. For now: Thank you! – Kjeld Schmidt Oct 11 '16 at 19:13
3

You can use one of the PWM pins on Arduino to output a PWM signal. If you want a constant clock, you need to set the duty cycle of the PWM to be 0.5, i.e. 50%.

Syntax: analogWrite(pin, value) where the parameter "value" is the duty cycle ranges from 0 (always off) to 255 (always on) since it is a 8-bit PWM generator inside Arduino.

If you need a PWM wave with duty cycle of 0.5, you need to set the "value" above to be 127, which is exactly in middle of 0 and 255:

analogWrite(clkpin, 127);
  • analogWrite only works on PWM pins, not on the analog pins. – Gerben Oct 10 '15 at 18:37
  • You are right, my fault. – Penthrite Oct 11 '15 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.