OK, I've got a waveform coming out of pin 11 but I don't think it's the right frequency (I'm looking for 1MHz).
I found some of this in some old code Mellis posted at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1141598539
and added a few As to the register names according to what's declared in header files in the hardware/cores/arduino directory.
I've copied over comments as well but they don't necessarily make sense any more....
Could someone with a scope handy tell me what frequency this is putting out on pin 11, or maybe what the values for OCR2A and OCR2B should be?
btw I'm using Arduino 0010 and testing this on a freeduino with a '168.
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
unsigned long clock, period;
void setup() {
Serial.begin(9600);
Serial.println("Alive");
clock = clockCyclesPerMicrosecond() * 1000000;
Serial.print("clock = ");
Serial.println(clock, DEC);
period = clock / (2 * 1000000L);
Serial.print("period = ");
Serial.print(period, DEC);
cli(); // turn interrupts off
// configure timer 2 for phase correct pwm
// this is better for motors as it ensures an even waveform
// note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
cbi(TCCR2A, WGM21);
sbi(TCCR2A, WGM20);
TIMSK2 = 0 ; // no interrupts
pinMode(11, OUTPUT);
// configure timer 2 for normal (non-inverting) pwm operation
// this attaches the timer to the pwm pin
sbi(TCCR2A, COM2A1);
cbi(TCCR2A, COM2A0);
OCR2A = period;
OCR2B = 0;
}
void loop() {
// tra la la....
}