Programming Arduino Mega in CTC mode

CTC(Clear Timer on Compare Match) mode is one of four wave generation modes in Arduino Mega 2560 and other Arduino boards. The other wave generation modes are normal mode fast PWM mode and phase correct PWM mode. CTC mode is useful for generating square wave signal and therefore useful in various application or projects. Here it is shown how to program how to program Arduino Mega in CTC mode. 

To program Arduino Mega in CTC mode we have to know about the Timer/counters of the Arduino Mega. Arduino Mega 2560 board is build using ATmega 2560 microcontroller. This microcontroller has six timers of which two are 8-bit timers and the other four are 16-bit timers. The Timer/Counter 0 and Timer/Counter 2 are 8 bits timers while timer/counter 1, timer/counter 3, timer/counter 4 and timer/counter 5 are 16 bit timers.

 Each of these six timers can be operated in either normal mode, fast or phase correct PWM(Pulse Width Modulation) or CTC mode. The programming of 8 bits timers, timer0 and timer2 are similar while the programing of 16 bits timers, timer1, timer3, timer4 and timer5 are similar. The programming of 8bits timers timer0 and timer2 is similar to programming the timer0 and timer 2 of Arduino Uno or Arduino Nano. Similarly programming Arduino Mega2560 timer1, timer3, timer4 and timer 5 are similar to programming Arduino Uno or Arduino Nano timer1. Arduino Uno and nano are based on ATmega328(p) micrcontroller which only has three timers-timer0(8 bit timer/counter), timer1(16 bit timer/counter) and timer2(8 bit timer/counter). For Arduino Uno and ATmega328p microcontroller see  Arduino CTC mode Programming with Examples and Programming ATmega328p in CTC mode

Programming Arduino Mega in CTC mode

While programming Arduino Mega 2560 and Arduino Uno in CTC mode(and other modes) are similar in that the same process is followed, there is difference in the location of the output compare pins. That is if we want to use CTC mode then only specific pins can be used. The OC pins on different ATmega microcontrollers can be different like between Arduino Uno and Arduino Mega.

Arduino Mega 2560Timer/Counter 0 in CTC mode

The operation and programing of CTC mode of Arduino Mega 2560 timer 0 in CTC mode is best understood with help of block diagram. The following shows the block diagram of Arduino Mega 2560 timer 0.

Timer 0 Arduino Mega 2560

In the above diagram, we can see that the output of the microcontroller in CTC mode appears at the pins OC0A which is pin 13 of Arduino Mega and OC0B pin which is pin 4 of the Arduino Mega. So when using Arduino Mega timer 0 in CTC mode, the output can only appear at pin 13 or pin 4. One can output signal at either one of the pins or both of them because the micrcontroller has two independent units of the output compare match hardware. This can be seen in the block diagram. The two registers OCR0A and OCR0B(n=0) can be independently loaded and waveforms on compare match can be generated independently. Also we can have different modes within CTC mode- toggle mode, clear output on compare match or set on compare match. which are configured using the compare output match mode bits.

The CTC mode operates in the following way. The OCR0A or OCR0B or both are loaded with a count value(say C) and then the timer is started. When the timer value in TCNT0 reaches the count value in the OCR0A or OCR0B register then output compare flag, OCF0A or OCF0B or both depending upon the setup is/are set(in next clock cycle). The TCNT0 register is also cleared. So in using CTC mode we can load different value of count and thereby we can control the frequency of the output wave.

This output compare flag can be monitored and cleared by the user through the program if interrupt is not used. The users can also enable the output compare match interrupt and in this case when output compare match occurs, the output compare flags are automatically cleared.

So the programming steps for Arduino Mega 2560 in CTC mode are as follows:

1. Configure Arduino Mega in CTC and also select mode of CTC

 The Arduino Mega is configured in CTC mode and in one of CTC mode using the WGM(Waveform Generation Mode) bits WGM02,WGM01, WGM00 and the COM0A1, COM0A0 for OC0A pin and COM0B1, COM0B0 for OC0B pin. These bits are located in the TCCR0A and TCCR0B registers as shown below.

TCCR0 Arduino Mega

For using OC0A pin we have to set the COM0A1 and COM0A0 for CTC mode according to the following table.

For using OC0B pin we have to set the COM0B1 and COM0B0 for CTC mode according to the following table.

Next we have to configure the WGM bits which is done according to the following figure. This is applicable for both OC0A and OC0B.

Note that when using the CTC mode, the OC0A or OC0B or both whichever are used must be configured as output pin using the data direction register. Also one can use the FOC0A or FOC0B bits in the TCCR0B register, which are Force Output Compare 0 A/B bits to force the output compare match according to requirement in user application. Also these FOC0A/B bits works only in non-PWM mode.

2. Load the count value into the OCR0A or OCR0B or both

After the microcontroller is configured in CTC mode, the next step is to load the count value in the OCR0A or OCR0B. The count value is determined according to user requirement of the frequency of the output wave at the OC0A(pin 13) or OC0B(pin 4) or both. The following equation is used to calculate the count value.

\[C(OCR0A or OCR0B) =\frac{F_{clk}}{2 N F_{w}}-1\]

 where, Fclk is the microcontroller CPU clock frequency, N is the pre-scalar value and Fw is the output wave frequency.

The pre-scalar value N is configured using the CS bits in the TCCR0B register according to the following table.

CTC mode Program Example

In this example, a 200KHz square wave is generated on OC0A(pin 13) of Timer 0 of Arduino Mega 2560. The frequency of oscillator or cpu of Arduino Mega is 16MHz.

For this the COM0A1=0 and COM0A0=1 bits are selected in non-PWM table above. Then the WGM bits are selected as 010 to select the CTC mode. Then using the above CTC mode formula the count value is calculated for 200KHz frequency of the square wave.

\[C =\frac{F_{clk}}{2 N F_{w}}-1 = \frac{16MHz}{2 \times N \times 200KHz } - 1\]

We have to select N such that C is non-negative and it is less than 256, let's choose N=1, that is no pre-scalar then,

\[C = \frac{16MHz}{2 \times 1 \times 200KHz } - 1 = 39\]

So for N=1, the CS bits in TCCR0B is 001. The above count value can also be calculated using the ATmega microcontroller online calculator.

So the program is as follows.

void setup() {
  //set OC0A as output
  pinMode(13, OUTPUT);

  // reset timer 0 control registers
  TCCR0B = 0;  
  TCCR0A = 0;
  
   // Load 39 to generate 200 kHz sq.wave
  OCR0A = 39; 
   // Toggle OC0A on compare match, WGM 2 (CTC)
  TCCR0A = (1 << COM0A0) | (1 << WGM01); 
  // Start the timer, no prescalar
  TCCR0B = (1 << CS00); 
}

void loop() {

}

 

Post a Comment

Previous Post Next Post