I need an output to test a clock circuit on a machine preferably 16MHz but since the Arduino clock is that speed I know that is not possible. However since it is for testing I will take the fastest I can get. I know that pwm works by taking the clock speed and dividing it by a multiplier 1,2,4,etc and counting 0-254 but that is too slow. I chose the simplest code I could think of...
void setup(){
DDRA = B00000001;
}
void loop(){
PORTA = B00000001;
PORTA = B00000000;}
thinking that since it is only 2 lines that I would have a clock speed of 8 MHz However my scope shows 1 MHz. Is this because I am using the Arduino Mega? Is there a way to make an output faster?
I don't know if it is any faster, or if the compiler makes it all the same in the end, but you can use the avr special write sequence to PINX to toggle a pin or pins.
Since that pin is not accessible, It would mean soldering it to a proto-type board on top of an arduino Uno.Not sure where I could access it on my Mega. I could try that but would a 3" lead to the inverter affect the frequency? If at all possible I would like to use the internal output by using better code that I could think of. But I will try it. And I have never heard of PINX could you give an example?
void setup() {
//sketch written for port/pin architecture of Arduino Uno atmega 328
//blink led
DDRB = B00100000; //set pin 13, led to output (PORTB, pin5)
}
void loop() {
PINB = B00100000;
delay(200);
}
void setup ()
{
// set up 8 MHz timer on pin 9
pinMode (9, 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 () { }
For the Mega2560 it is pin 11:
void setup ()
{
// set up 8 MHz timer on pin 11
pinMode (11, 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 () { }
thinking that since it is only 2 lines that I would have a clock speed of 8 MHz However my scope shows 1 MHz. Is this because I am using the Arduino Mega? Is there a way to make an output faster?
Nick--Why is this slower than the two line PORTA code?
In the set/clear scenario, the overhead of calling loop is spread over one cycle. In other words:
set pin / clear pin ... then 9 cycles overhead.
In the toggle scenario, the overhead of calling loop is doubled (one cycle is a set or clear). Thus you effectively have 18 cycles of overhead rather than 9.
With the PINA = 1 method loop is actually slightly shorter:
If you have an ISP programmer or second arduino (to run arduinoasisp) you could set the CLKO fuse (low fuse for '328(p) would be 0xBF if using external 16mhz crystal) and get the system clock output on arduino pin 8 (physical pin 14, PB0).
Nick your explanation is clear I see how my example was 1 MHz and after loading your code The scope is 8 MHz. I still may try the soldering method unless there is something faster. But as per post#1 this may be the fastest software I can expect.