avatarJ3

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2425

Abstract

r prescale to 256:</p><div id="31ca"><pre><span class="hljs-attribute">foc</span> = <span class="hljs-number">16000000</span>/<span class="hljs-number">2</span><span class="hljs-number">256</span>[<span class="hljs-number">1</span>+<span class="hljs-number">65535</span>] <span class="hljs-attribute">foc</span> = <span class="hljs-number">0</span>,<span class="hljs-number">476837158203125</span></pre></div><p id="1f20">2¹⁶ = 65536–1= <b>65535 </b>(zero base too)</p><p id="5181"><b>note</b>: This will works quite nicely for us; It will overflow pretty much twice a second:)</p><p id="bd8b">Step#04→ Now coding! First thing to do is <b>comment the sleep mode</b>; so leave it so that we can have a sequence in our code.</p><p id="c7aa">The counting sequence is determined by the setting of the Waveform Generation mode bits (WGM13:0) located in the Timer/Counter Control Registers A and B (TCCR1A and TCCR1B).</p><p id="fa9f">There is nothing for us on TCCR1A (we want normal mode operation) so we’re going to <b>TCCR1B </b>register straight away (page 136):</p><div id="19f4"><pre>TCCR1B <span class="hljs-string">|= (1 << CS12);</span></pre></div><p id="7902">note: this set the Clock Select Bit Description is equal to 256 prescaler (0b100 )</p><p id="9b18">Step#<b>05</b>→ Go to <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf">this part of data sheet </a>(page 136 ):</p><p id="c6a0">13.11.8 <b>TIMSK1 </b>— Timer/Counter1 Interrupt Mask Register</p><p id="892c">And set this code:</p><div id="9a00"><pre>TIMSK1 <span class="hljs-string">|= (1 << TOIE1);</span></pre></div><p id="6583"><b>note</b>: Bit 0 — <b>TOIE1</b>: Timer/Counter1, Overflow Interrupt Enable:)</p><p id="f4c2">Step#<b>06</b>→ Go to the top of our code (already there!):</p><div id="939b"><pre><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><avr/interrupt.h></span></span></pre></div><p id="5a0b">Step#<b>07</b>→ Enable interrupts (already there!):</p><div id="333f"><pre>sei(); <span class="hljs-keyword">while</span>() <span class="hljs-meta">{...}</span></pre></div><p id="3e01">Step#<b>08</b>→ Implement our Interrupt Service Routine (ISR):</p><p id="80a3">Head over 12.1 <b>Interrupt Vector</b>s (page 137) in ATmega48A and ATmega48PA of <a href="http://ww1.microc

Options

hip.com/downloads/en/DeviceDoc/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf">ATMega238p data sheet</a> and find the vector of interest out.</p><p id="a771">Write it down:</p><div id="1388"><pre><span class="hljs-title function_">ISR</span>(<span class="hljs-params">TIMER1_OVF_vect</span>) { <span class="hljs-title class_">LedToggle</span>; }</pre></div><p id="ec17"><b>note</b>: Now the LED is toggle automatically at <b>roughtly </b>each 5 secs :) and there you have it!</p><p id="45b2">In the next post we’re gonna control the timer that toggle the LED a little bit precisely using the<b> output compare mode</b>.</p><p id="4215">Stay tuned! Bye!</p><p id="0650">Here is the code till here:</p><p id="c01b"><a href="https://github.com/giljr/avr/tree/master/atmel">https://github.com/giljr/avr/tree/master/atmel</a></p><p id="0885">Look for Episode #06 ;)</p><h2 id="1134">References & Credits</h2><p id="6997"><a href="https://github.com/giljr/avr/tree/master/atmel">Atmel Code base Github</a></p><p id="09ea">Follow along with the entire ‘Getting Started with AVR’ series: <a href="https://www.youtube.com/redirect?event=video_description&amp;v=gLrMgKGZtHg&amp;q=http%3A%2F%2Fbit.ly%2FGettingStartedwithAVR&amp;redir_token=7jbgaQLTZaAq1GzO5JFmMEIPaBt8MTUyNzk1Nzg0NUAxNTI3ODcxNDQ1">http://bit.ly/GettingStartedwithAVR</a></p><h2 id="6590">→Goto Episode#07</h2><p id="2413">Complete Serie:</p><p id="8f40"><a href="https://readmedium.com/atmel-studio-7-uno-ep-00-969b9cc3cf7b">#<b>00</b> HowTo-Load-Into-Arduino-AS7</a></p><p id="7cce"><a href="https://readmedium.com/atmel-studio-7-uno-serie-9508c1e89ace">#01_Hello-World</a></p><p id="12ad"><a href="https://readmedium.com/atmel-studio-7-uno-episode-02-42a006338c3c">#02_Presents-AVR-Freaks</a></p><p id="8af6"><a href="https://readmedium.com/atmel-studio-7-uno-serie-ep-03-9882f3c246ed">#03_Pressing-Button</a></p><p id="d400"><a href="https://readmedium.com/atmel-studio-7-uno-episode-04-92967d0f4d79">#04_Pin-Change-Interrup</a></p><p id="341e"><a href="https://readmedium.com/atmel-studio-7-uno-serie-episode-05-9ae1543f7b86">#05_Battery-Driven-App</a></p><p id="92aa">#06_Timer Counter</p><p id="babe"><a href="https://readmedium.com/compare-mode-w-atmel-studio-7-uno-episode-07-129ee964689b">#07_Compare Mode</a></p><p id="4222">#08</p><p id="d027">#09</p><p id="b0df">#10</p></article></body>

Timer Counter w/ Atmel Studio 7 & UNO Serie (Episode#06)

Let’s start with a new peripheral: Timer Counter!

But now let’s toggle LED at every 5 milliseconds using Timer Counter Zero or One.

We are going to start off where we were in previous episode#05 (we have an interruption generated when we push the button attached on Arduino Pin 7:)

Step#01Timer Zero. On page 101 of the Atmega328p data sheet we encounter ( 12.7.2 Clear Timer on Compare Match (CTC) Mode).

The waveform frequency (f) is defined by the following equation:

foc = foc_CPU/2*N[1+OCRnx]

The N variable represents the prescale factor (1, 8, 64, 256, or 1024);

foc is the frequency output on compare match;

foc_CPU is the frequency of our Arduino(16 MHz) cpu;

OCRnx is the , the OC0A output that can be set to toggle its logical level on each compare match.

Good!

Step#02→ If we calculate what is the highest frequency we can get with 8-bits counter (Timer0) we have…

foc = 16000000/2*1024*[1+255]
foc = 30,517578125

2⁸= 256–1=255 (zero base count)

note: The counter will pop up 30 times per second; It is great for operations with communication, but very fast for interactions with humans:/

Step#03→ Let’s try out Timer One! (16-bit log:) on page 131 of the Atmega328p data sheet;

Let’s scale down a bit our prescale to 256:

foc = 16000000/2*256*[1+65535]
foc = 0,476837158203125

2¹⁶ = 65536–1= 65535 (zero base too)

note: This will works quite nicely for us; It will overflow pretty much twice a second:)

Step#04→ Now coding! First thing to do is comment the sleep mode; so leave it so that we can have a sequence in our code.

The counting sequence is determined by the setting of the Waveform Generation mode bits (WGM13:0) located in the Timer/Counter Control Registers A and B (TCCR1A and TCCR1B).

There is nothing for us on TCCR1A (we want normal mode operation) so we’re going to TCCR1B register straight away (page 136):

TCCR1B |= (1 << CS12);

note: this set the Clock Select Bit Description is equal to 256 prescaler (0b100 )

Step#05→ Go to this part of data sheet (page 136 ):

13.11.8 TIMSK1 — Timer/Counter1 Interrupt Mask Register

And set this code:

TIMSK1 |= (1 << TOIE1);

note: Bit 0 — TOIE1: Timer/Counter1, Overflow Interrupt Enable:)

Step#06→ Go to the top of our code (already there!):

#include <avr/interrupt.h>

Step#07→ Enable interrupts (already there!):

sei();
while() {...}

Step#08→ Implement our Interrupt Service Routine (ISR):

Head over 12.1 Interrupt Vectors (page 137) in ATmega48A and ATmega48PA of ATMega238p data sheet and find the vector of interest out.

Write it down:

ISR(TIMER1_OVF_vect)
  {
     LedToggle;
  }

note: Now the LED is toggle automatically at roughtly each 5 secs :) and there you have it!

In the next post we’re gonna control the timer that toggle the LED a little bit precisely using the output compare mode.

Stay tuned! Bye!

Here is the code till here:

https://github.com/giljr/avr/tree/master/atmel

Look for Episode #06 ;)

References & Credits

Atmel Code base Github

Follow along with the entire ‘Getting Started with AVR’ series: http://bit.ly/GettingStartedwithAVR

→Goto Episode#07

Complete Serie:

#00 HowTo-Load-Into-Arduino-AS7

#01_Hello-World

#02_Presents-AVR-Freaks

#03_Pressing-Button

#04_Pin-Change-Interrup

#05_Battery-Driven-App

#06_Timer Counter

#07_Compare Mode

#08

#09

#10

Recommended from ReadMedium