Bit Registers

Can anyone tell me what is going on here and explain what any of this means? What does TCCR2A,OCR2A,TIMSK2, 0x02 or sei() represent? Thanks.

Code: void interruptSetup(){ 
// Initializes Timer2 to throw an interrupt every 2mS.
TCCR2A = 0x02; // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER 
OCR2A = 0X7C; // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
TIMSK2 = 0x02; // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED 
}

Read the datasheet of the ATmega328p (the full version, not the summary).

The TCCR2A,OCR2A,TIMSK2 are names of registers. In this case registers of an internal timer, timer 2.

0x02 is a hexadecimal notation of an 8-bit number. http://en.wikipedia.org/wiki/Hexadecimal

sei() is a function to enable the interrupts.

What does TCCR2A,OCR2A,TIMSK2

They are all registers for timer 2 the data sheet for the processor has all the information you would ever want about these.

0x02

That is a hexadecimal number it is the binary equivalent of 0000 0010 However if you don't know that you are not up to playing with those timing registers, learn about the arduino a bit more first.

sei()

enable the interrupts see:- http://playground.arduino.cc/Main/AVR But comment above applies, you probably don't know what an interrupt is or why you want to enable them.

Thanks folks! You have given me a good starting block! Cheers.

Looking for a bit more help on this. Im racking my head still dont fully understand this.

TCCR2A = 0x02; //This is beginning the counting sequence by setting WGM21 bits TCCR2B = 0x06; //This is beginning the counting sequence by setting WGM22 bits OCR2A = 0X7C; //Its counting to 124 and when it does the interrupt service routine begins TIMSK2 = 0x02; //This is a common register so when the times match for the timers TCCR2A and TCCR2B and the count in OCR2A

Do my comments above even make sense? What is the counting sequence doing and how long is it counting for? And what exactly is the WGM bits? I have read the datasheet but I'm stuggling to understand all its logic and purpose?

Thanks you.

First of all: we don't use it like that.

There are bit definition in the avr gcc compiler (the compiler used by Arduino). To write 0x02 to TCCR2A, we use:

TCCR2A = _BV(WGM21);

To set both bit0 and bit1, it would be like this:

TCCR2A = _BV(WGM21) | _BV(WGM20);

To write a count value to the counting register, you can use decimal:

OCR2A = 124;

If it counts from 0 to 124, it is doing a 125 count.

The WGM bits determine what mode the counter is in. Table 17-8 on page 160 of the full datasheet of the ATmega328P shows it.

In you code, WGM20 = 0, WGM21 = 1, WGM22 = 0 That is CTC mode. The CTC mode is often used to generate a software interrupt, while the timer output is not connected to any pin.

Setting 0x06 to TCCR2B sets CS22 and CS21, not the WGM22 bit. A timer starts after setting the CS2x bits, that are the prescaler bits (a divider of the cpu clock that is applied to the timer). Using both CS22 and CS21 is a prescaler of 1024. See table 17-9

And what exactly is the WGM bits?

Waveform Generation Mode bits. They set what waveform is generated.

how long is it counting for?

Table 17-8. in the data sheet outlines the 8 modes the counter can count in, note two are unused. Most count forever, that is repeatedly until you change them.

TCCR2A = 0x02; //This is beginning the counting sequence by setting WGM21 bits

That not only sets WGM21 and clears WGM20 but it also clears all the other bits in the register. Normally you only want to set or clear specific bits so you use the AND as well as the OR logic operations to do this. by itself it sets only these two bits you must also define WGM22 bit found in the TCCR2B Register to set a counting mode.

This is beginning the counting sequence mode by setting WGM21 bit~~s~~ and clearing WGM21

Thanks folks. This direction helps alot.

http://www.fourwalledcubicle.com/AVRArticles.php really good tutorial on timers