Electronic Basics #30: Microcontroller (Arduino) Timers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

In my opinion great scott goes a bit fast, as from an electronics background I can catch his talks but for people who are newbie often find it difficult to connect things , his creativity and electronics skill is mind blowing though no doubt

๐Ÿ‘๏ธŽ︎ 8 ๐Ÿ‘ค๏ธŽ︎ u/nukestar101 ๐Ÿ“…๏ธŽ︎ Jul 17 2017 ๐Ÿ—ซ︎ replies
Captions
If you're working on a more complex project based around the microcontroller like this alarm clock right here Then you might have noticed that precisely timed events are mandatory For example this second counter has to count up to exactly 60 seconds before increasing the minutes counter Or the to builder date of the clock should blink every half seconds or the Led Matrix Figure should move one step to the right every quarter seconds while the rows of the Led Matrix are simultaneously multiplexed at a frequency of 82 Hertz All of those tiny events and even generating a Pwm signal the variable frequency in order to create an alarm sound requires so-called timers, and it doesn't matter whether you use an MSP430 Microcontroller or A Pigman controller or the Admirer Predator 8 p microcontroller commonly found on the Arduino uno They all feature timers so in this video Let's have a closer look at the timer's of the Onaga up ready to appear learn the basics of how to create precisely timed events and ultimately create a small circuits that spits out a Pwm signal with variable duty cycle and frequency which goes up to 8 Megahertz Let's get started First off, let's take a look at a very simple Arduino sketch example Here we pull digital pin 3 hi wait for 1000 milliseconds or 1 seconds for the digital pin low and wait for another seconds After uploading the codes and connecting a led above resistor to the pin We can see that in fact it blinks every seconds which definitely counts as a time events, but there are two problems firstly if I would either push button as an inputs to the circuits and Edit the code so that the led would stay off if I press We can see that the input gets most of the time ignored by the microcontroller the reason for that is that the command execution is most of the time stuck inside the delay function and Thus ignores the input commands and the second reason is that why the da function promises a waiting time of 1 milliseconds? We can see that over longer periods of time this value drifts off by a couple of milliseconds so to solve those problems we can use one of the three timers of the admirer 3 - a p Which as we can see in the block diagram of the microcontroller are more like peripherals Which means the microcontrollers command execution sets them up only one time and then they do their job? Continuously without clogging up the main code loop as an example I will be using the 16-bit timer 1 which certainly offers a lot of useful features But let's start with the basics the normal mode To activate it. We have to set the WGM 13 12 11 and 10 bits to 0 Which can be found in the TCC R1A and TCC R1B register? so let's simply set all those bits to 0 But there's one the CS 10 bits Which is responsible for the pre scalar which I will talk about in a second which we have to set to 1 now if I upload the code like this the TC and t 1 register counts up one step each time the 16 megahertz clock completes the periods and Since the counter is a 16-bit register. We can store 65,536 Steps which means the Maximum value is 65535 after reaching this maximum value D counter overflows Starts back at 0 count up again and repeats this process over and over again But the most important fact is that when the counter overflows it sets a so-called overflow flag in the ti Fr1 register That means that by activating the overflow interrupts unable bits in the omsk one register We can create an overflow interrupt let's execute the function each time the counter overflows in our case V16 megahertz clock signal would equal 1 seconds Enthalpy Maximum register value of 65535 Evils around 4 milliseconds which was also confirmed by the output of the serial monitor Now to increase the time until the overflow occurs. We can use different pre scalars to Decrease the 16 Megahertz step duration For example if we set the Cs. 12 bit instead of the Cs. 10 bits. We use a prescaler of 256 Which means the new overflow time should be around one point zero four seconds or around? 1048 milliseconds as shown by the severe outputs now this time the interrupt is definitely usable when it comes to blinking an led and Certainly keeps the main loop responsive to other inputs, but we still didn't create a precise one second time event yet To do that we could simply increase the start value of the counter register So that it precisely needs one second to reach its maximum value the formula to calculate the start value looks like this and To no surprise it delivers us the required value to create a precise one-second interrupts But let's say we need two interrupts one at 1/4 seconds and one at half a seconds For that we could use the CTC modes aka clear timer on compare Match modes Which we can activate by setting b. WGM twelve bits to 1 this time the counter t CNT1 still counts upwards like usual But additionally we got to compare registers Oc O 1a and Oc R1b Whose value get constantly compared to the counter value and if they match a compare match flag is triggered? Which we can use in the codes to create two independent compare match interrupts But of course only if we said the compare match interrupt enable bits before hands It is also mentionable that the top value is not 65535 Anymore It is the Ocr one a value in stats So by using the simple formula we can calculate a value of 30 1249 for the Ocr one a register to create the hop second interrupts and by using the same formula We can determine a value of fifteen thousand six hundred twenty four for the ocr one b register for the quarter second interrupts At this point. I think reading suitable time events is not a problem anymore So let's move on to the eight bits fast Pwm modes by setting the bits WGM Twelve and Ten to one additional Year we have to set Com1 A1 and com1 B1 to binary one as well to activate the non-inverting output of the Oc one a and Oc one deep in which according to the pin mapping of the my controller equals pin nine and Ten Now the functional principle is pretty close to the CTC mode Except that the counter only goes up to an 8-bit value this time so maximum of 255 and the output pin Oc 1 a and Oc 1 be pulled output high ever compare match with the register values occurs and pooled output low when the counter overflows This way we can vary the register value would to create a variable duty cycle which we can do in a circuit by adding a potentiometer and utilizing the map function and to reach the highest possible frequency of 62 point Five Kilo Hertz of the Pwm signal We have to utilize the smallest prescaler of one Now this Pwm signal is already pretty useful for many applications But we can do even better by activating a fast Pwm mode Which uses the ICr one register as the top value? this register is usually utilized for input capturing but here we can sacrifice it to vary the top value between 0 and 255 By adding a second potentiometer vary the frequency up to eight Megahertz While still being able to adjust the ocr one a value in the higher frequency areas And with that being said you are now familiar with the basics of timers But feel free to read the 100 pages of the edd Mega 32 Ap datasheet dedicated to timers to learn even more as Always don't forget to like share and subscribe stay creative, and I will see you next time
Info
Channel: GreatScott!
Views: 354,505
Rating: 4.9221034 out of 5
Keywords: timer, timers, ยตC, microcontroller, arduino, atmega328p, atmega, ATmega328p, uno, pic, msp430, 16 bit, 8 bit, interrupt, setup, tutorial, how to, diy, explain, basic, basics, beginner, beginners, normal, mode, ctc, fast pwm, pwm, overflow, compare, match, electronics, electronic, greatscott, greatscott!, led, matrix, multiplex, clock, signal, frequency, calculate, formula
Id: IdL0_ZJ7V2s
Channel Id: undefined
Length: 9min 19sec (559 seconds)
Published: Sun Jul 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.