Skip to main content
Engineering LibreTexts

28.2: Normal Mode

  • Page ID
    35912
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Normal mode (WGM22:0 are clear) is the simplest mode of operation. The TC simply counts up until it overflows and wraps around back to zero. On overflow, the TOV2 flag (Timer OVerflow) is set in the TIFR2 flag register. More precisely, this bit will be set in the same clock cycle as when the timer/counter register TCNT2 becomes zero. There are no access restrictions on TCNT2. Your code may overwrite the contents of this register at any time in order to alter the counting behavior.

    A short code example follows. The program uses TC2 to create a time delay similar to the delay() function. This delay is then used to blink an LED connected to PORTB.0.

    /* Simple LED blinker using TC2 to create a time delay
       Active high LED connected to PORTB.0 */
    #define LEDMASK 0x01
    #define COUNTOFFSET 15
    
    void setup()
    {
        DDRB |= LEDMASK;
    
        TCCR2A=0;        // normal mode
        TCCR2B=0x07;     // 1024x prescale
    }
    
    void mydelay(int x)
    {
        while(x)
        {
            TCNT2=COUNTOFFSET;                //count up from here to 255 then overflow
            TIFR2=1<<TOV2;                    // clear flag, bit is set on overflow
            while( !(TIFR2&(1<<TOV2)) );      // wait for overflow bit
            x--;
        }
    }
    
    void loop()
    {
        PORTB |= LEDMASK;
        mydelay(120);
        PORTB &= ~LEDMASK;
        mydelay(30);
    }
    

    The setup() function sets the direction for the LED driver pin to output, places TC2 into normal mode and sets the clock prescaler to 1024. This means that each count will occur at a rate just slower than 16 kHz given the 16 MHz clock of the Uno. The mydelay() function initializes the counter’s main register at a predefined value (COUNTOFFSET). It will count from there to 255 after which point it will overflow and set the TOV2 flag. We ensure that this bit is clear before continuing using the seemingly backward command TIFR2=1<<TOV2; It first appears that this command should set the flag bit but it does indeed clear it (as verified in the Atmel documentation). At this point we busy-wait on this bit waiting for it to be set. In this example it will take 241 counts to overflow or about 15 milliseconds. This process is then iterated x times to achieve a longer and variable delay. Note that larger values of COUNTOFFSET will yield shorter blink times.


    This page titled 28.2: Normal Mode is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?