#209 Arduino Timer Interrupts - Overflow & Comparator. Easy Peasy.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back yes we're back to flashing leds on my workbench what on earth is going on i'm sure you're thinking bacon what are you doing have we moved beyond flashing leds and the answer is there's a very very good reason why i'm using leds because it's a visual indication of what's happening behind the scenes and now we're not just flashing three leds for the sake of it we're talking about timer interrupts today and there's a sort of a long and convoluted story about why i'm showing you that basically it's all back down to do with my web radio i try to use the same technique for playing the music failed miserably and i'll tell you why a bit later on so i thought you know this is still a very useful feature of the arduino chip the 328p so why on earth haven't i told you about it before so here we go what do we mean by timer interrupts well let me get the whiteboard out just a little bit just for a minute or two just to show you what i mean by that and then we'll come back to a simpler version of what's running here right back soon let's have a shout out for pcb way now they're having their pcb way big sale for christmas 2020. as you can see here they're having a shopping festival and yes you can win lots of coupons you can see what pcb way does all this thousands of users and daily processes very nicely here and there's a lucky draw just press the start button and see what you can win and they have a big year and sale big discounts up to 15 percent click the quote now button to see what you get oh and look you can now get buried or blind viewers in multi-layer pcbs if you know what they are you know why you want them and you can see here many many projects that people have made for christmas go in and have a look and apart from all the festive christmas cheer you can still get your smt order for just 30 dollars which includes free shipping great why don't you try them out now right let's get on with that whiteboard now shall we so what do we mean by time at interest exactly well let's think about a standard uh arduino program where you have a setup and a loop the setup runs once doesn't it and then the loop runs forever just keeps going around doing the same old stuff except if we have a timer interrupt this is a hardware interrupt and if a condition is met this loop stops and the other bit of code that we've said this is my interrupt service routine will run next and when that finishes it just ends and stops the loop then continues it's a little bit like when the arduino goes to sleep it will just stop and then continue from where it left off well same with the interrupt service routine the loop stops here goes into this point here runs this comes out and carries on back here and of course you can have more than one interrupt service routine you can have several the thing to remember is this has priority when a timer interrupt is triggered it's a hardware timer interrupt this runs at the expense of the loop because the arduino has only got a single core it's only got a single thread and it'll either run this or it will run this not both together so we have to watch what we do with this and that basically is how simple timer interrupts work now you can there are three timers on the arduino timer zero timer one and timer two for this particular purpose we're using timer two because it tends to be the most available timer it's only eight bit though so unfortunately it gets triggered a lot but we'll talk about that in a minute okay so now we have a sort of a general idea what a timer interrupt is trying to do and i've got a simpler version to the one that i showed you a few minutes ago this is just flashing a single led to prove the point so let's have a look at the code behind it now the code is avr c plus plus it's not friendly arduino speak because there is no friendly arduino speak to do this uh to be quite honest i'm rather surprised that nobody's actually created a bit of arduino speak to enable beginners to get a handle on to timer interrupts and timers generally but here we go all right hang on to your onto your hats let's dive straight in to the code okay this is the code running now yes this is in my eclipse ide but i've also got it work in the arduino no trouble at all it is proper arduino ide compatible code so let's just whiz through this now as you saw from the the whiteboard the timer register determines exactly what's going on with that timer so what i've got some documentation here you can see all this uh how we divide the 16 megahertz clock down by setting various bits in fact these bits here look cs20 21 and 22 in ttc r2b that's timer counter control register 2 b right now there's a number of ways we can we can set that value that's probably the easiest to understand because we're looking at each individual bit um and it's well it's it's pretty straightforward what what's that cs22 yeah okay clock source 22 clock source 21 clock source 20. they're the three bits of a particular um register so let's have a look at um somebody who is not related to me regardless of what his name is it's good old nick gammon so here you see a page from nick gammon's website all about timer two and um well the bit we're interested look are these three bits here at the end and they're called cs22 cs21 and cs20 clock source 2021 so on and it's all part of tccc r2b timer counter control register 2b right and uh yeah i know these these names are somewhat esoteric but there is no friendly name as i say in the arduino language of this i'm guessing arduino themselves thought you know what if you get to this stage you're obviously beyond arduino speak so we'll just leave it as is either that or they just ran out of time money and impetus and doing it which is probably more likely isn't it but this this page tells you everything you need to know about timer two and yes there is timer zero and timer one as well but we're just going to quietly ignore those for a bit time is zero you definitely don't want to muck about with because timer zero controls things like millies right which is very useful for well timing things really right make creating state machines and so forth we should be talking about as part of this timer one is a 16 bit timer so you can count for far longer and timer 2 is a standard 8 bit timer just like timer 0 which is the one we're using today because as far as i know we're not using it for anything else but other things might like for example tone libraries might use timer 2 because after all it's the one that's available for you so people who write libraries out there tend to use timer too and if you think oh i'll just use timer 2 for this project i'm working on and you've already got a few chirps and whistles and bells and whatever added onto your project you might find that you're out of luck it's already been used anyway back to the code okay so what we said was we're going to set these three bits here in this register according to this divide by or prescaler value right now obviously i want this to be as slow as possible because we want to see things if we just go back to the the workbench that led there which is is synchronized with this because it's using the same pin is flashing at a rate you can see it if i hadn't slowed the whole thing down it just be it would look as though it's on all the time you might just see a almost imperceptible flicker because it is after all turning on and off about uh 16 000 times a second right so let's um let's uh carry on with the code so we're saying divide the 16 megahertz clock from your arduino board by 1024 it's this one here right to give us this 15.625 kilohertz signal which is to say it's too fast to see normally so i've done some jiggery poker on my board deliberately just to show you now there are various ways to set the register and we've covered this in other videos and i'll just give you the options here to see which way you might like best and there's no right way there's no wrong way this is the way that's probably the most common though where you're actually calling the bits themselves the name that have been given by arduino and which you saw in nick gammon's cheat sheet there you could of course just put a basic binary value in there to say this register i want ored with this value which means the first few bits this one here it will leave alone because we are oring it here but the last three we are setting so that's doing the same job as this line here or you can say rather than using this one shift shift left shift right whatever it's going the bit value i want set is the one here and the bit value here is the one i said it sticks the one half it does on the other whatever works for you works for you okay now with the timer controller you really need to set all the values first and then the data says now set your pin mode that you want to change stuff to make the interrupt do things with once you've previously set everything do not set this first don't know why it's one of those little glitches little gotchas that probably have some undefined behavior that probably works most of the time but occasionally doesn't and then you'll be left there scratching your head going i don't know why this doesn't work okay right so that's what all we're doing in this bit here is saying to the timer timer 2 8-bit timer divide the 16 million 16 megahertz clock signal that you're receiving by the value we've put in here which is 1024 right okay that's all it does the loop here is simply printing out this number you're seeing here just to prove that it can do some work but it has absolutely zero impact on the timer interrupt what's that yes john you at the back what's what's your question time it yet timer interrupt we're coming to that these are specially named functions that the compiler recognizes as being an interrupt function and well it looks like this look we call it isr interrupt service routine and it's for timer 2 overflow vector all right yeah i know technical names it's done to confuse people well not you now because you know all about it don't you so you can confuse other people fine okay so this interrupt routine is going to be called when the timer overflows so the timer 8-bit what's the maximum it can go up to you at the back yeah 255 correct so the clock signal is being read by the timer to 16 million pulses a second right but we're saying divide that by 1024 so 15 000 times a second now this is going to be called yeah i know it's a lot isn't it it's a lot to get your head around how quick things happen in hardware so what i've done also here is a little tiny bit of code to say slow things down a bit in fact all this does is slow it down by half i could have probably got away with not putting any slowdown in here but you'll see in the next sketch that we most definitely do need some slow outs so all we're doing here is saying toggle pin what we call in arduino world gpio13 yeah or d13 i think we also say but we have to say in arduino speak set the pin on the port that controls d13 on or in fact toggle it if it's on turn it off if it's off turn it on okay and that's what this does and why is it pin bb5 well we're looking at a port a port is a collection of eight pins scattered around the arduino normally clumped together port b and d but we're talking about port b and we're saying port b bit five all right so that's the detail we're saying yes d13 physical pin 19 not that we really care about physical pins unless you've got this on a breadboard and you need to know which one you need to attach things to and it just toggles it here so 13 or 15 000 times a second this is being called and i'm saying half that time but then i'm doing something different just for the purposes of this demo if we go back to the workbench we can see this is not being called 15 000 times a second or even half that 7 000 times a second it's it's probably about twice a second isn't it why well i've put the bootloader on here i've changed the flags the the fuses right on here to say do not use this crystal at all use the internal oscillator for one megahertz so it's running 16 times slower than it would do normally okay and of course you can do this with your 80 mega 328p on a breadboard you can make it run on the internal clock we've covered all that in previous videos there's that we covered it in that for example and it means you can run without crystal for battery operated stuff you know that turns on and off then gets into deep sleep fine so as it happens this particular board is currently not using that crystal down there at all it's using its own internal clock but i'm only doing that for the purposes of this demonstration if i didn't do it that way this would be flashing on and off so fast it would be a pointless exercise you would never bear to see this thing flashing right back to the code as you can see this all this does then is toggle that pin so you can see what's going on if we look at the loop though the loop is simply saying add one to a big long unsigned integer and uh print it out over here and that's what it's doing and it's not being affected by this bit of code here being called x times a second right it's just it's just totally independent so let's go back now why i'm showing this or what triggered my thought process showing this because i wanted to use this sort of feature a timer interrupt to run a bit of code every so often on my esp32 web radio i thought i know what i can do i can use the timer interrupt just the same way i'm doing it here and i can say every millisecond call the bit of code that pumps data out to my vs 1053 mp3 decoder great and it'll run that run a bit of music stop and then the timer will once again come in and say go and do your stuff except what i forgot and what i was rudely uh reminded of was that the esp32 is not a dumb piece of hardware like this one sorry no offense arduino i wasn't dissing you really in this bit of code that you're looking at on the screen if you were to put a long running process here it would just run that long process no matter how long it took yeah it would but while it's doing that of course it wouldn't be doing this it only does one thing at a time after all and whilst it looks like we're doing two things here what's happening is that this counter is running and 15 000 times a second if i hadn't put the delay in and all that it will come down here run this little bit of code come back out and then carry on with this so you can see that if this takes i know a millisecond to run which is quite a long time it would have no impact on the loop but if you put something in this timer interrupt that takes say five seconds then it would most certainly impact the loop definitely right your loop stops basically whilst the interrupt runs and then resumes once the interrupt is finished on the esp32 because it's not just a dumb piece of hardware that does what you tell it there's the operating system the rtos operating system watching what's going on so when my timer interrupt was running to say go and put some music into the vs 1053 please and play that music there's a watchdog watching me to see what's happening with this interrupt and the minute you go over something like 300 milliseconds it goes oy you've been running too long i'm rebooting so sometimes on the esp32 web radio project it would run and it would run and it'd run after about 20 seconds and then go no you run too long now kaboom and it reset which made me think about um i've got to do that a different way which is why i put that into a task right fine and it's been running fine ever since on the arduino though you'll get away and if you did think this interrupt that i'm running here every so often yeah regularly is more important than the stuff i'm doing in the loop then you can put whatever you like down here normally though you tend to put stuff down here that's either very quick or that sets a flag a volatile variable that the loop can then read and go oh i've got some work to do now because you've set a particular flag i'll go and do that as part of an if condition but i won't do it normally because that takes too much time to keep constantly checking a particular variable i don't need to check it unless it gets set by an interrupt routine like this here though we're just toggling a pin which is very very quick especially when we're doing it like this much quicker than doing a digital right to that pin and as i say i'm even slowing things down here basically it has to count this delay up until it hits one then it resets it and does the business but that's purely for demo purposes but it does also give you an indication that you can do a bit more in the interim routine okay that's all there is to that better code and there's lots of documentation up here a bit of initialization and things how to enable the timer and things i'll let you read that in your own time the most important thing is this bit here where you tell the timer how often you want it to run how often you need to divide by here so we're dividing by 1 2 4 just to get things to slow down but you could do it you know divide by 32 and then the thing would run at 500 000 times a second it's a lot but what you consider a lot the hardware does not a millisecond in the arduino times is an eternity okay that's one thing we can do then with timers is there anything else right so back to the workbench you now see three leds well just about see i think with this lifi shield you will see that the three leds are going on and off at different rates so the green one's flashing quicker then you get the yellow one and the red one's probably slowest of all i think in this case and yes it's all still in slow motion deliberately for this demo because i've got this board running at one megahertz not 16 megahertz right so that's 16 times slower than it would be just so that we can see things flashing otherwise it's all a bit too quick even the humble 80 mega 328p is a very fast chip yeah can do a lot of work in that time so what's different about this sketch then then the previous one we just saw let's have a quick look right this is the upgraded sketch right still using timer 2 still using the overflow feature right the interrupt on the overflow but we can do two other things we can have two comparisons with a known value so as well as the overflow now i'm saying by setting these two values as part of the atomic control to say i want you to compare the timer value as you're counting up to 255 these two values down here so 42 the meaning of life universe and everything and 99 probably my iq or something yeah know i'm not not that bright so when it hits those two values it will trigger two different interrupt service routines one for ocr2a so the comparator two a and one two 2b right and apart from that very little has changed so if we just scroll down a bit we can see that the divide by is exactly the same i haven't changed any of that all right so we're still dividing by 1024 yes all right forget about the fact we're not running at 16 megahertz let's pretend we do um now here previously we had pin mode 13 output but just in the interest of talking in avr c plus plus rather than arduino speak instead of setting all three pins now like that because we're using three different routines for three different leds we can do it like this and we covered all this in a previous video there is that one there all about arduino speak data direction register b for port b which is the eight pins that make up port b luckily for us 13 10 and 8 all part of port b we're not using some that are part of a different port and we can say well the first two bits do not control any pins because 13 is the highest digital pin 14 15 are controlled by a different register so they do nothing but we can say pin 13 yes i want that one on please 12 11 oh yes 10 i'd like that one as an output please uh nine no and eight i like that as an output so the ones with the digit one in our output now running that is probably going to be you know 10 20 times faster than doing that that's arduino speak very friendly you can see immediately what it's doing this is extremely quick but you really have to look into what that's doing and you count the bits backward don't you and think hang on was that 12 or 11 start again yeah quick but not so friendly to use mistakes can creep in okay here we have the loop same loop as i had time just running a big long well this over here as you can see every second it's just counting up here's our overflow handler that we had last time except this time i've got a slightly longer delay in it and then we have two more isrs one for comparator a and one for comparator b and you can see the names reflect what they're doing and all i've done is to say i want a different delay in each so i run these every so often but at different rates otherwise all three leds will of course all flash at exactly the same time not very interesting so i've sort of you know devise the method where whereby they flash at different rates simply by delaying how often they run but they set the pins exactly the same way as what we did in the first one so we can say of the pins that make up port b i want you to all the value of pin b0 in this case which is as it says over here d8 or pin what we call an arduino land gpio8 which is physical pin 14. and ditto for that's the now the pin 13 and i've changed this one up here to uh pin b2 which is d10 oh sure must have written it oh there we are d10 all right so we're just toggling three pins at slightly different rates even though the interrupt service routines themselves are all being pretty much triggered at the same time because it counts up to 255 doesn't it a thousand 24 times and it goes oh the value is the same now i'm going to trigger each one of these independently now we spoke just a minute ago like what happens to the loop well once again any one of these running will cause the loop to stop the loop freezes the interrupt service routines run at whatever rate so that all three are running now and then as each one of those interrupt routines finishes it goes back to the loop continue and if you look over here it still looks like we're running pretty much every second because we couldn't possibly detect the minute delay that any one of these service routines is causing but if you put a long running routine in there you most certainly will see stutters on the main loop and that could give you problems as i say with button presses or displays on an lcd screen or something like that to keep the code in your interrupt service routines small not least because that's best practice but when you move over as i say to the esp32 or esp8266 you put a long running service routine there and the whole thing will just reboot after a certain time because it's a naughty thing to do yes that that's the technical explanation anyway so there we have three different interrupt service routines all running from timer to an 8-bit timer and the only change we've made in this sketch is to say yes i want the comparator enabled for 2a and 2b and the values i want you to compare against are these two values here how you use that in your project well i'll leave you to figure that out but it's always good to know that you can do this and basically every so often the interrupt routine will run totally independently from the loop so you don't have to worry about calling stuff in the loop and all the rest of it it's a hardware interrupt cool now if there's time i'd like to show you something about the esp32 web radio just just two seconds worth and i have a very very important announcement to make so all those people who have stopped watching this video before now are going to be well they'll be left in the dark weren't they right big announcement well for me a big announcement it looks like i really am going to move house this time and it's going to happen probably very early january 2021 so if you're watching this video after january 2021 then things are probably back to normal i hope if you're watching this video pretty much as i make it sometime during december 2020 or very early 2021 then you'll wonder what's happened to me because i've not released any more videos and the reason being is because i'm moving house and obviously right now mid-december i'm having to start packing up my house and i'm hoping my workshop will stay functional for at least one more video before christmas but after that i've got to pack everything in here into boxes including my video cameras and microphones and everything else so things will stop for a while okay but it doesn't mean my my channel stopped it just means i'm pausing while i get a new workshop built at my new place and all this reset up yeah i know so that's just a heads up really there'll probably be one more video before christmas and that's it cool what else was i going to talk to you about oh yes the internet radio project has finally come to fruition now i'm going to have to move my camera that way so you can see it because i've got it on a different desk right over there on the other workbench this bit over here that was the original esp32 radio there's the screen still right or on the on the breadboard and that worked great occasionally i got a little glitch in things because these dupont cables aren't really designed to you know be a permanent connection but as you can see the radio is running over here and it's connected via this sort of um this idc cable from the back of this pc b that you see here to the one down here and it's all connected this is simply my serial port that i'm monitoring and there's the vs10 53 connected via little tiny 10-way cable to the main motherboard and there's the other end of the cable connected to the screen now this is all running 100 and i'm i'm delighted with it i've got to admit i'm delighted and the reason i designed it this way to have the screen separate from the main motherboard was so that this can be placed it remotely a little bit remotely anyway from the main motherboard itself right so the reason i'm not going to cover this today because i haven't got the box that this is going to go into and i haven't quite finished it it doesn't look i'm going to finish that now in time for christmas so next week we'll talk about something entirely different but i do hope to put all this into a case very nicely and it'll probably cost more than all the rest of this put together as it always does doesn't it enclosures and stuff and it's been running continuously for about must be this must be the third or fourth day not a single glitch absolutely rock solid to those people who've made comments about the esp32 web radio that go and top of the list is reliability this now is reliable because all these connections on here of course are well they're solid aren't they they're not done by dupont cables so full details of all this will be you know put up on my github in due course not just yet because i'm waiting for the enclosure as i say but at least you've had a little update and i've not forgotten about it okay before the entire workbench over there collapses i love your comments about the uses for the timer to interrupt i don't know what you could use those for as i say i tried to use it on esp32 it's all falling apart um and that was the wrong approach if you can think of a right approach where you might use time it interrupts go ahead and put you down in your comments and anything else you want to know about this this particular topic and i'll see you one more time one more time before christmas and that's it for several weeks thereafter great see you in the next video i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching you
Info
Channel: Ralph S Bacon
Views: 6,396
Rating: 4.9810429 out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: fFrL5Vh8Dis
Channel Id: undefined
Length: 30min 39sec (1839 seconds)
Published: Fri Dec 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.