Thanks, I searched the web for timer tutorials and found this interesting blog post: http://letsmakerobots.com/node/28278
I loaded that sketch:
#define ledPin 13
void setup()
{
pinMode(ledPin, OUTPUT);// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
}ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
digitalWrite(ledPin, digitalRead(ledPin) ^ 1); // toggle LED pin
}void loop()
{
// your program here...
}
I played with OCR1A to change frequency, but I really don't get what this means (among other lines):
TCCR1B |= (1 << WGM12);
I know nothing about bitwise operators so I searched again and I found out it's like:
TCCR1B = TCCR1B | (1 << WGM12);
Now I'm wondering what "1 << WGM12" means... I think I understand this:
A = 0011 1100
B = A << 2
which makes B = 1111 0000, but in this example A is a series of 0s and 1s and the shifter is a decimal, but it isn't the case in the code line, which is confusing me. Can anyone help me understand that code?
Thanks!