WAVE GENERATION AND INPUT CAPTURE

INTRODUCTION-

We have studied timers in AVR microcontroller. In timer section, we studied how to generate delay and count external events. In this section, we study how to generate square waves of specific duty cycle and frequency. Also, we study capturing of external events using AVR timers. At last, we study how to measure frequency and duty cycle of waves.

WAVE GENERATION USING 8-BIT TIMERS-

In Timer section, we studied for every Timer there is at least, an OCRn register ( like for TIMER0 there is OCR0 register ). The value of OCRn register is constantly compared with the value of counter register i.e.TCNTn register. If their value becomes equal ( compare match occurs ) then, the OCFn flag will be set high.

In AVR microcontroller, for each Timer there is one Waveform generator. The waveform generator, generate waves on specific pin i.e. OCn pin. The working of waveform generator depends on WGMn and COMn bits of the TCCR register. These waveform generators are informed whether the TCNTn register reaches TOP or BOTTOM or COMPARE MATCH OCCURS.  Then the waveform generator changes the state of OCn pin depending upon the mode of the Timer ( WGM00:01 bits of TCCR0 register ) and the COM01 ( Compare Output Match ) and COM00 bits.

WAVEFORM GENERATORS IN AVR

In ATmega16/32, the OC0 is the alternative function of PINB3. When both COM00 and COM01 bits are zero then, PINB3 acts as I/O port. Otherwise, the pin acts as a wave generator pin controlled by wave generator. We have DDR register ( Data Directional Register ), to set the function of the pin as I/O. So, we should set OC0 pin as an output pin when we want to generate waves.

Now we will study timer0 wave generation and then wave generation using timer 1. We know that timer0 and timer2 are similar but there are two major differences which we discussed in the previous section so we only study timer0 and timer1 wave generation.

 TIMER0 WAVE GENERATION-

Registers Used and their description-

TCCR0 ( TIMER/COUNTER CONTROL REGISTER ):

WAVE GENERATION NORMAL AND CTC MODE-

When timer0 is in NORMAL( WGM01-WGM00:00) or CTC mode( WGM01-WGM00:10) after a compare match occurs, OC0 pin can perform one of these following actions, depending on the value of COM01-COM00:

  1. Remain unaffected.
  2. Toggle OC0 pin.
  3. Clear (Drive low) the OC0 pin.
  4. Set (drive High) the OC0 pin.

Notice in CTC mode, when compare match occurs the value of timer register becomes zero. But in NORMAL mode if compare match occurs the counter still counts until the counter reaches its top value.

Generating Square Waves in NORMAL MODE-To generate Square wave we can set Timer0 to NORMAL mode and set COM bits to toggle mode (COM01:00-01). The OC0 pin will be toggled on each compare match and the square wave will be generated. Since we know that in NORMAL mode there is no compare match occurs between OCR0 register and TCNT0 register as we cannot access OCR0 register in NORMAL mode. So compare match here simply means that when TCNT0 reaches to TOP value (0xFF) than OC0 pin will be toggled.

GENERATION OF SQUARE WAVE USING TIMER0 IN NORMAL MODE PROGRAM CODE-

Generating Square Waves in CTC MODE-

The CTC mode is better than the NORMAL mode for generating square waves since in CTC mode we easily adjust the frequency of wave by using the OCR0 register. In CTC mode, when the value of OCR0 is low then compare match occurs earlier and the period of square wave generated is smaller (high frequency). If value loaded in OCR0 is high then, compare match occurs later and time period of generated square wave is large ( low frequency).

To select CTC mode set WGM00 bit in TCCR0 register first. Then, set COM00 bit to toggle OC0 for every compare match. The time period of generated square wave depends on the value loaded into OCR0 register. Suppose XTAL=1MHz, OCR0=100, no prescalar used (clock input to timer circuit is 1MHz). Now the value of TCNT0 is incremented by one in every 1µsec. If the value of TCNT0 become equal to OCR0 register (compare match occurs) than OC0 pin will toggle. Let OC0 pin initially is 0, so after 100µsec compare match occurs(as value of OCR0 is 100 and value of TCNT0 will be incremented by one every 1µsec ) and value of OC0 pin became 1. That means the square wave we generated on OC0 pin has a time period of 100µsec which directly depends on the value of OCR0 register.

Generating Pulses using  CTC MODE-

When a timer is in CTC mode and COM bits is in toggle mode, the of OCR0 represents how many clocks it count before it toggles the pin. This way we can generate different pulses by loading different values into the OCR0 register.

PROGRAM CODE TO GENERATE DIFFERENT PULSES USING TIMER0 IN CTC MODE-

 

FOC0 ( FORCE COMPARE OUTPUT ) FLAG- Sometimes we need to force the waveform generator act as if compare match has occurred.

WAVE GENERATION USING TIMER2 IN NORMAL AND CTC MODE-Timer2 is also 8-bit timer it moves from 0x00 to 0xFF. We can generate waves in timer2 in the same way as we did in timer0. The only difference is that we have no external clock input as present in timer0.

TIMER1 WAVE GENERATION-

We have previously studied timer1, In this section we first discuss different modes of timer1 and then study wave generation using timer1.

Different modes of timer1-The WGM13, WGM12,WGM11 and WGM10 bits define the different modes timer1. Timer1 has 16 different modes of these different mode , mode 13 is not implemented( reserved mode ). These modes can be categorized into five modes: Normal mode, CTC mode, Fast PWM, Phase Correct PWM and Phase and Frequency Correct PWM. Before discussing these modes first we need to know what is the meaning of TOP in timer1.

TOP IN TIMER1-

Top is the highest value that a TCNT1 register reaches while counting. The top value is different is different modes. Top value in Normal mode is fixed i.e.0xFFFF (65535). But for CTC mode top value is not fixed. The value in CTC mode is defined by OCRn register. The following are top values in different modes-

  • In NORMAL mode top value is 0xFFFF.
  • In some modes other than Normal mode top is fixed but it does not equal to maximum value i.e.0xFFFF. It can be 0xFF,0x1FF or 0x3FFF.
  • In CTC mode top value is not fixed, it can be equal to value content of some register like OCR1A, ICR1 etc.

 

BASIC REGISTERS USED AND THEIR DESCRIPTION-

TCCR1A: timer 1 control register.

From this register we set the mode of operation of timer1 it can be wave generation mode, normal mode, compare output mode.

TCCR1B:

MODE WGM13 WGM12 WGM11 WGM10 TIMER/COUNTER MODE OF OPERATION TOP UPDATE OF OCR1X TOV1 FLAG ON
0 0 0 0 0 NORMAL 0xFFFF Immediate MAX
1 0 0 0 1 PWM,PHASE CORRECT 8-BIT 0x00FF TOP BOTTOM
2 0 0 1 0 PWM,PHASE CORRECT 9-BIT 0x01FF TOP BOTTOM
3 0 0 1 1 PWM,PHASE CORRECT 10-BIT 0x03FF TOP BOTTOM
4 0 1 0 0 CTC OCR1A Immediate MAX
5 0 1 0 1 FAST PWM, 8-BIT 0x00FF TOP TOP
6 0 1 1 0 FAST PWM, 9-BIT 0x01FF TOP TOP
7 0 1 1 1 FAST PWM, 10-BIT 0x03FF TOP TOP
8 1 0 0 0 PWM,PHASE AND FREQUENCY CORRECT ICR1 BOTTOM BOTTOM
9 1 0 0 1 PWM,PHASE AND FREQUENCY CORRECT OCR1A BOTTOM BOTTOM
10 1 0 1 0 PWM,PHASE CORRECT ICR1 TOP BOTTOM
11 1 0 1 1 PWM,PHASE CORRECT OCR1A TOP BOTTOM
12 1 1 0 0 CTC ICR1 Immediate MAX
13 1 1 0 1 RESERVED
14 1 1 1 0 FAST PWM ICR1 TOP TOP
15 1 1 1 1 FAST PWM OCR1A TOP TOP

CTC MODE-

The mode 4 and mode 12 are for CTC mode. Is discussed in the previous section the top value of counter register up to which counter counts is defined by a register. The only difference between mode 4 and mode 12 is that, in mode 4 the top value is defined by OCR1A register whereas in mode 12 top value is defined by the ICR1 register. So, when compare match occurs in mode 4, OCF1A flag will be set high but in mode 12, when compare match occurs ICF1 flag will be set high.

In both mode 4 and mode 12, the only difference is in a register which stores the top value otherwise they work in same way. In normal, CTC and Fast PWM mode timer count until it reaches the top, then it roll over to zero. But the top value is different in different modes. As a result, the flag which is set when compare match occurs is also different. When the top value is fixed, the TOV1 flag will be set on compare match; when OCR1A defines the top, OCF1A flag is set; when the top is defined by the ICR1 then, the ICF1 flag will be set high.

COUNTING-In NORMAL, CTC and FAST PWM mode timer counts until it reaches top value. Then the timer rolls over to zero and a flag is set:

  • If the top is fixed, the TOV1 flag is set.
  • If the value of top contained in OCR1A register then, OCF1A flag is set.
  • If the value of top contained in ICR1 then, ICF1 flag is set.

WAVE GENERATORS IN TIMER1-

In timer1 we see their there two waveform generators: Waveform Generator A and Waveform Generator B.

The compare match between TCNT1 and OCR1A affects waveform generator A, and wave generated by waveform generator A show up on pin OC1A pin.

The compare match between TCNT1 and OCR1B affects waveform generator B, and wave generated by waveform generator A show up on pin OC1B pin.

The COM1A1 and COM1A0 bits have control over Waveform generator A ; whereas COM1B1 and COM1B0 control Waveform generator B. All of these COM bits are in TCCR1A register.

The operation mode of timer1 (WGM13,WGM12,WGM11 and WGM10 bits of TCCR1A and TCCR1B register ) affects both registers.

REGISTER USED AND THEIR DESCRIPTION-

TCCR1A: timer 1 control register.

In ATmega16/32, OC1A and OC1B are a function of PIND5 and PIND4 respectively. In other words, PIND5 function as an I/O port if COM1A1 and COM1A0 are zero. Similarly, PIND4 function as an I/O port if COM1B1 and COM1B0 are zero. Otherwise, these pins act as wave generating pins. At PIND5 wave generated due to Waveform Generator A and at PIND4 wave generated due to Waveform Generator B.

We have to remember that, at these pins waveform is coming out of AVR microcontroller so their DDR bit should be set high. These pins act as output pins. The waveform generators in timer1 work almost the same way as does in timer0.

WAVE GENERATION IN NORMAL AND CTC MODE ( TIMER 1 )-

When timer1 is in CTC mode (WGM13:0=0100) or (WGM13:0=1100) or NORMAL mode (WGM13:0=0000) after a compare match occurs, the waveform generators can do one of the following actions, depending on the value of COM1A1:0 and COM1B1:0 bits-

  1. Remain unaffected.
  2. Toggle OC1x pin (OC1A or OC1B).
  3. Clear the OC1x pin.
  4. Set the OC1x pin.

The COM1A1 and COM1A0 bits select the operation of OC1A, while COM1B! and COM1B0 bits select the operation of OC1B pin.

GENERATING SQUARE WAVE-

To generate square wave we can set the timer to NORMAL or CTC mode and set the COM1x1 and COM1x0 bits of one of the waveform generators to the toggle mode (COM1A1:0=01 to generate waves with Waveform Generator A and COM1B!:0=01 for generating waves with Generator B).

The OC1x pin will be toggled on each compare match and the square wave will be generated.

We also know that CTC mode is better than NORMAL mode as the frequency of wave can be easily adjusted by changing the top value (the value of OCR1x in mode 4 and value of ICR1 in mode 12). In CTC mode, when OCR1x(or ICR1) has lower value then a period of generated wave is small. But if their value is large then, generated wave has a large time period.

FOC1A (FORCE OUTPUT COMPARE) AND FOC1B FLAGS-

Writing 1 to FOC1A bit of TCCR1A register forces Waveform Generator A to acts as if compare match occurred. Writing 1 to FOC1B bit, force Waveform Generator B to acts as if compare match occurred.

PROGRAM CODE FOR GENERATION OF SQUARE WAVE USING TIMER1 IN CTC MODE-

PROGRAM CODE FOR GENERATION OF SQUARE WAVE USING TIMER1 IN NORMAL MODE-

INPUT CAPTURE PROGRAMMING-

The input capture function widely used for many applications. Among them are:

  1. Recording the arrival time of an event.
  2. Measurement of the pulse
  3. Measurement of the time

In ATMEGA32 timer1 can be used to detect and measure the events happening outside the AVR chip. Upon detection of an event, the TCNT1 value is loaded into an ICR1 register and the ICF1 flag is set.

There are two event sources-

  1. The ICP1 pin, which is PIND6.
  2. The output of the analog

We can use ACIC flag to set the event source. The ACIC bit of register ACSR as shown below-

ACSR REGISTER-

TCCR1B REGISTER-

STEPS TO PROGRAM THE INPUT CAPTURE FUNCTION-

  1. Initialize TCCR1A and TCCR1B with proper timer mode other than those modes in which ICR1 defines TOP value.
  2. Select the edge we want to measure the arrival time for.
  3. Initialize ASCR to desired event source.
  4. Enable or disable noise canceller.
  5. Notice ICF1 flag in TIFR register to see if edge has arrived. Upon the arrival of edge, the TCNT1 value is loaded to ICR1 register automatically by the AVR.

 

INPUT CAPTURE FUNCTION USE-

  1. Measure the period.
  2. Measure the pulse width.

STEPS TO MEASURE THE TIME PERIOD OF WAVE-

  1. Initialize the TCCR1A and TCCR1B register.
  2. Initialize the ASCR register with desired event source.
  3. Monitor ICF1 flag in TIFR to see if the edge has arrived. Upon the arrival of edge, the value of TCNT1 is automatically loaded into the ICR1 register by AVR.
  4. Load the value of an ICR1 register into another temporary register.
  5. Again Monitor ICF1 flag in TIFR to see if the second edge has arrived. Upon the arrival of edge, the value of TCNT1 is automatically loaded into the ICR1 register by AVR.
  6. Load the value of an ICR1 register into another temporary register.
  7. To get the time period of wave subtract the value of two temporary registers of steps 4 and 6.

 

  1. Initialize the TCCR1A and TCCR1B register and select capturing on rising edge.
  2. Initialize the ASCR register with desired event source.
  3. Monitor ICF1 flag in TIFR to see if the edge has arrived. Upon the arrival of edge, the value of TCNT1 is automatically loaded into the ICR1 register by AVR.
  4. Load the value of an ICR1 register into another temporary register and change the capturing edge to falling edge.
  5. Again Monitor ICF1 flag in TIFR to see if the second edge has arrived. Upon the arrival of edge, the value of TCNT1 is automatically loaded into the ICR1 register by AVR.
  6. Load the value of ICR1 register into another temporary register.
  7. To get the time period of wave subtract the value of two temporary registers of steps 4 and 6.

 

QUESTION-WRITE A PROGRAM MEASURE THE TIME PERIOD OF WAVE FED TO PIND6 AND PLACES THE BINARY RESULT ON PORTA AND PORTB.

 

ANSWER-

 ALGORITHM

1.MAKE PORTA AND PORTB AS OUTPUT PORT.

2.MAKE PIND6 INPUT AS WAVE IS COMING INSIDE THE AVR                MICROCONTROLLER.

3.LOAD THE VALUES INTO TCCR1A AND TCCR1B.

4.WAIT FOR RISING EDGE.

5.IF RISING EDGE OCCURS THEN, LOAD THE VALUE OF ICR1 INTO SOME VARIABLE.

6.CLEAR THE ICF1 FLAG BY WRITING ‘1’ TO IT.

7.AGAIN WAIT FOR RISING EDGE.

8.IF RISING EDGE OCCUR THEN, LOAD THE VALUE INTO ANOTHER VARIABLE.

9.CLEAR THE ICF1 FLAG BY WRITING ‘1’ TO IT.

10.AGAIN WAIT FOR RISING EDGE.

11.SUBTRACT THE VALUE OF VARIABLES USED IN STEP 5 AND 8 AND PLACE RESULT IN ANOTHER VARIABLE.

12.PLACE THIS VALUE TO PORTA (LOW BYTE).

13.RIGHT SHIFT THE VARIABLE AND THEN PLACE ONTO PORTB.

PROGRAM CODE-