1

Arduino CTC mode Programming with Examples

This is tutorial on Arduino CTC mode Programming with Examples. In CTC mode, the timer counts up until the counter value TCNTx(where x is 0, 1 or 2) reaches the TOP value and at this point match occurs and the output at the output compare pins OCxA or OCxB can be either cleared, set or toggled. The TOP value depends upon the which Timer is used. Arduino has three timers called timer 0, timer 1 and timer 2 and each timer can be used in CTC mode. The timer 0 has one CTC mode with TOP at OCR0A, timer 1 has two CTC mode with TOP at OCR1A and ICR1 and timer 2 has one CTC mode with TOP at OCR2A.

 

In CTC mode, the timer counts up until the counter value TCNTx(where x is 0, 1 or 2) reaches the TOP value and at this point match occurs and the output at the output compare pins OCxA or OCxB can be either cleared, set or toggled. The TOP value depends upon the which Timer is used. Arduino has three timers called timer 0, timer 1 and timer 2 and each timer can be used in CTC mode. The timer 0 has one CTC mode with TOP at OCR0A, timer 1 has two CTC mode with TOP at OCR1A and ICR1 and timer 2 has one CTC mode with TOP at OCR2A.

 

Arduino CTC mode Frequency & Count Value

The frequency((F_{w})) of the output waveform is given by the following equation.

[F_{w}=frac{F_{cpu}}{2N(C+1)}]

where N is pre-scalar(either 1, 8, 64, 256 or 1024) and C is the count value to be loaded into the OCR0xA register or ICR1 register. (F_{cpu}) is clock frequency of 16MHz for Arduino. 

 From the above equation we can calculate the count value C to be loaded into OCR0A as follows,

[C=frac{F_{cpu}}{2NF_{w}}-1]

 

1. Arduino Timer 0 in CTC mode

 The following are Programming examples of Arduino Timer 0 in CTC mode.


1.1 Square Wave using CTC mode 2 on OC0A pin

 

Example 1:  100KHz Square Wave using CTC Mode 2 on OC0A pin(pin 6 on Arduino)

The steps to generate square wave using Timer 0 CTC mode 2 is as follows.

1. Set Arduino Pin 6 (OC0A) as output

2. Load 79 into OCR0A to generate 100 kHz sq.wave

3. Configure TCCR0A & TCCR0B to configure Timer 0 to Toggle OC0A on compare match, Mode 2 (CTC),  No Prescalar, Start the timer

 

Program Code:


void setup () {
  //Pin 6 (OC0A) is output
  pinMode(6, OUTPUT);   
   // Load 79 to generate 100 kHz sq.wave
  OCR0A = 79; 
   // Toggle OC0A on compare match, WGM 2 (CTC)
  TCCR0A = (1 << COM0A0) | (1 << WGM01); 
  // Start the timer, no prescalar
  TCCR0B = (1 << CS00); 
}

void loop() {

}



The following shows square waveform graph of 100KHz signal and it's Fourier transform.





 1.2 Square Wave using CTC mode 2 on OC0B pin 

 

Example 1:  100KHz Square Wave using CTC Mode 2 on OC0B pin(pin 5 on Arduino)

The steps to generate square wave using Timer 0 CTC mode 2 on OC0B pin is as follows.

1. Set Arduino Pin 5 (OC0A) as output

2. Load 79 into OCR0A to generate 100 kHz sq.wave

3. Configure TCCR0A & TCCR0B to configure Timer 0 to Toggle OC0B on compare match, Mode 2 (CTC),  No Prescalar, Start the timer

 

Program Code:


void setup () {
  //Pin 5 (OC0B) is output
  pinMode(5, OUTPUT);  
   // Load 79 to generate 100 kHz sq.wave
  OCR0A = 79; 
   // Toggle OC0A on compare match, WGM 2 (CTC)
  TCCR0A = (1 << COM0B0) | (1 << WGM01); 
  // Start the timer, no prescalar
  TCCR0B = (1 << CS00); 
}

void loop() {
}


 

2. Arduino Timer 1 in CTC mode

 The following are Programming examples of Arduino Timer 1 in CTC mode.

 

2.1 Square Wave using CTC mode 4 on OC1A pin 

 

Example 1:  10KHz Square Wave using CTC Mode 4 on OC1A pin(pin 9 on Arduino)

 In this CTC mode example a 10KHz square wave is generated at OC1A. In this CTC modee 4 the TOP is OCR1A register. We can calculate the count value to be loaded into the OCR1A register to generate 100KHz square wave using the equation provided above or can be calculated directly using the . The calculated count value for 10KHz is 799.  


The steps to generate square wave using Timer 1 CTC mode 4 is as follows.

1. Set Arduino Pin 9 (OC1A) as output

2. Load 799 into OCR1A to generate 10 kHz sq.wave

3. Configure TCCR1A & TCCR1B to configure Timer 1 to Toggle OC0A on compare match, WGM 4 (CTC),  No Prescalar, Start the timer

 

Program Code:


void setup () {
  //Pin 9 (OC1A) is output
  pinMode(9, OUTPUT);  
   // Load 799 to generate 10 kHz sq.wave
  OCR1A = 799; 
   // Toggle OC0A on compare match, mode 4 (CTC),  No prescalar, Start the timer
  TCCR1A = (1 << COM1A0); 
  TCCR1B = (1<<WGM12) | (1 << CS10); 
}

void loop() {
}



 2.2 Square Wave using CTC mode 12 on OC1A pin 

 

Example 2: 10KHz Square Wave using CTC Mode 12 on OC1A pin(pin 9 on Arduino)

The following code generates 10KHz on OC1A pin(pin 9 on Arduino) in mode 12 CTC mode.


void setup () {
  //Pin 9 (OC1A) is output
  pinMode(9, OUTPUT);  
   // Load 799 to generate 10 kHz sq.wave
  ICR1 = 799; 
   // Toggle OC1A on compare match, mode 12 (CTC),  No prescalar, Start the timer
  TCCR1A = (1 << COM1A0); 
  TCCR1B = (1<<WGM13) | (1<<WGM12) | (1 << CS10); 
}

void loop() {
}


 

2.3 Square Wave using CTC mode 4 on OC1B pin 

 

Example 3: 10KHz Square Wave using CTC Mode 4 on OC1B pin(pin 10 on Arduino)

The following code generates 10KHz on OC1B pin(pin 10 on Arduino) in mode 4 CTC mode.


void setup () {
  //Pin 10 (OC1B) is output
  pinMode(10, OUTPUT);  
   // Load 799 to generate 10 kHz sq.wave
  OCR1A = 799; 
   // Toggle OC1B on compare match, WGM 4 (CTC),  No prescalar, Start the timer
  TCCR1A = (1 << COM1B0); 
  TCCR1B = (1<<WGM12) | (1 << CS10); 
}

void loop() {
}


 

3. Arduino Timer 2 in CTC mode

  The following are Programming examples of Arduino Timer 2 in CTC mode. 

 

 3.1 Square Wave using CTC mode 2 on OC2A pin 

 

Example 1: 500KHz Square Wave using CTC Mode 2 on OC2A pin(pin 11 on Arduino)

The  following are steps to generate 500KHz square wave using CTC mode 4 on OC2A pin(pin 11 on Arduino):

1. Set OC2A(pin 11 on Arduino) as output

2. Load 15 into OCR2A register for 500KHz

3. Configure TCCR2A and TCCR2B register to toggle OC2A pin, mode 2(CTC) with No Prescalar

 

Program Code:


void setup () {
  // Pin 11 (OC2A) is output
  pinMode(11, OUTPUT);  
   // Load 15 to generate 500 kHz sq.wave
  OCR2A = 15;
   // Toggle OC2A on compare match, WGM 2 (CTC),  No prescalar, Start the timer
  TCCR2A = (1 << COM2A0) | (1<<WGM21); 
  TCCR2B = (1 << CS20); 
}

void loop() {
}


The following shows Fourier transform of 500KHz square wave on OC2A pin.

 

 3.2 Square Wave using CTC mode 2 on OC2B pin 

 

Example 2: 500KHz Square Wave using CTC Mode 2 on OC2B pin(pin 3 on Arduino)

 The  following are steps to generate 500KHz square wave using CTC mode 4 on OC2B pin(pin 3 on Arduino):

1. Set OC2B(pin 3 on Arduino) as output

2. Load 15 into OCR2A register for 500KHz

3. Configure TCCR2A and TCCR2B register to toggle OC2B pin, mode 2(CTC) with No Prescalar

 

Program Code:


void setup () {
  // Pin 3 (OC2B) is output
  pinMode(3, OUTPUT);  
   // Load 15 to generate 500 kHz sq.wave
  OCR2A = 15;
   // Toggle OC2A on compare match, WGM 2 (CTC),  No prescalar, Start the timer
  TCCR2A = (1 << COM2B0) | (1<<WGM21); 
  TCCR2B = (1 << CS20); 
}

void loop() {
}


 The following are other Timer/Counter tutorials:

-

-

-

[fixed][/fixed]
[poll] {poll} [/poll]
4j