Is it possible to toggle a pin at 16MHz?

Hello,

I would like to toggle a pin using the system clock. This will be used as a clock for other hardware communicating with the arduino.

The code I am currently using generates a pulse with a frequency of about 125kHz and I don't know how to increase it...

void setup(){
  
DDRA = 0xFF;
cli();//stop interrupts

//set timer1 interrupt at 1Hz
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register 
  OCR1A = 0; //Compare value
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // no prescaler
  TCCR1B |= (1 << CS10);  
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);

sei();//allow interrupts

}//end setup

ISR(TIMER1_COMPA_vect){

  PORTA = PORTA ^ 0xFF;
}

void loop(){}

Thanks a lot.

Should be some fuse settings to set a pin to output the system clock.
http://www.ladyada.net/learn/avr/fuses.html

Is it possible to toggle a pin at 16MHz?

On a system where the clock speed is 16MHz? No. It takes more than one clock cycle to toggle a pin. Even if it did take only one, there would be no time for the Arduino to do anything else, like looping.

pYro_65: How do I go about accessing fuse settings?

PaulS: That makes sense, would it be possible at 8MHz?

I would like to just wire the clock directly to a pin but I cannot figure out how to do so.

"8.9 Clock Output Buffer
The device can output the system clock on the CLKO pin. To enable the output, the CKOUT Fuse has to be programmed. This mode is suitable when the chip clock is used to drive other circuits on the system. The clock also will be output during reset, and the normal operation of I/O pin will be overridden when the fuse is programmed. Any clock source, including the internal RC ?Oscillator, can be selected when the clock is output on CLKO. If the System Clock Prescaler is used, it is the divided system clock that is output."

The CLK0 pin is PORTB pin 0 which on the UNO is known as Pin 8.

You will need an ISP device to set the proper fuse bit. That's bit 6 of the Fuse Low Byte. Set it to 0 to activate the clock output buffer. In boards.txt you would change:

uno.bootloader.low_fuses=0xff

to

uno.bootloader.low_fuses=0xBF

Then re-burn the bootloader. (Part of the bootloader burning is setting the fuses.)

Firstly you can't generate more than 8MHz since the output of the timers is clocked at the system clock and can only change once per system clock.

To generate 8MHz you'd setup one of the counter-timers like this, using a fast PWM mode with TOP set from a register, here timer2 with OCR2A as the TOP value - this means the timer counts 0,1,0,1,0,1 etc, and setting the other OCR2B to 0 means the relevant pin will toggle at full rate.

void setup ()
{
  pinMode (3, OUTPUT) ;
  TCCR2A = 0x23 ;
  TCCR2B = 0x09 ;
  OCR2A = 0x01 ;
  OCR2B = 0x00 ;
  TCNT2 = 0x00 ;
}

However if you just want an overall 16MHz clock for several devices you can use a xtal-oscillator chip to generate the clock for the 328 as well as other chips - just feed the clock into pin XTAL1 instead of having a xtal there.

By the way I just noticed the original posting uses PORTA. PORTA??? there's only ports B, C and D in the 328

Ok, I will give that code a shot.

Sorry, I should have specified that I am using an Arduino MegaADK. I do have a PortA.

In that case I'm not sure which pin is attached to OCR2B, tends to vary between processors, but the pin should have "OC2B" as one of its names. In fact check the datasheet to see if it has a timer2(!)