Stop Wasting Time, Use AVR Timer Interrupts | Baremetal AVR Programming Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
low level gang what is up in this video we're going to talk about interrupts and by the end of the video you'll be able to write an interrupt service routine or isr for the atmega328p bare metal without using the arduino ide or associated libraries if you haven't already hit that subscribe button and go follow me on twitch i'll be doing short form tutorials here and longer form projects and some gaming over there interrupts are an effective way of getting code to execute out of band from your main program when certain events happen instead of sleeping the cpu for a thousand milliseconds or pulling for a sensor to go off using interrupts you can as the name implies interrupt the code flow of the cpu to handle something important in this video we'll use the interrupt for timers on the atmega328p to blink an led on and off every second without using sleep okay so let's get coding here you see i have a basic main.c shell file no important code there that'll be the code file that we actually write our program into so get that ready and then also i have a make file here i've done this in previous arduino bare metal tutorials before but i'll kind of go over at a high level what this makefile does essentially you use the avrgcc program to build your c file into an avr compliant you know binary format for this chip and then you have to use the avr obs copy tool to convert that elf that it creates into an ihex file we're going to call that ihex file main.hex and then finally we use the avrdude avr flash burning tool to use the arduino bootloader spec and write our main.hex file to the flash pointed to by the device that lives at this path right so this is the usb port that happens when i plug my arduino into my computer right and just this is the default but it's good to specify it the baud rate is uh 115 200. so pretty standard stuff and just to test it it all works i can say make builds and i can say make burn and it converts that binary to an ihex file uses avr dude and then we get prompted with the the burn of the file and i'll put my camera show you guys real quick that now i've burned that flash file to my device and there's nothing happening there's no leds blinking there's nothing happening so we're good we're at a good default state so in this file like i said in the previous part of the video we're going to be making a timer interrupt happen at a one second interval to blink an led this is different than the normal way you do this where you use a loop and sleep right an interrupt completely preempts the instruction counter of the arduino and says hey this is more important so we're going to set that up in the following way when doing any kind of avr programming you first want to include the avr.io or the io.h header that'll explain to the compiler where all of the ports and data direction register stuff is so we can actually turn the led on and off and then obviously we're doing a an interrupt tutorial so we need to include the interrupt dot h header that describes the compiler where to find files for interrupt service routines right so if we do an interrupt we need to tell the board what to do during that interrupt and that is called an isr or an interrupt service routine okay so now in our main file you know we need to first enable the led to be turned on and the way we do that is we say that the data direction register for port b turn on the bit uh bv ddb5 so you know pin 13 which is the pin on the arduino that the the led lives is actually on uh port b port five and we're going to turn that bit on so now it's known as an output right so this enables pin 13 to be written to okay so now gets to the actual timer part right so when you're setting up timers there are various registers inside the arduino that describe to you bits you can set to make the timer operate in a certain kind of way we're going to be using an overflow timer that will flip in isr every time a certain internal value is overflowed right and that overflow value is called t counter one we're gonna set it to zero right now this is actually the wrong value we're going to change that and i'll show you the math on how to change that in a minute but for right now we're going to set the default overflow value to zero so basically what's happening is every predetermined interval the computer is incrementing this value by one right right now the chip's processor is operating at 16 megahertz we don't want to count this up every 16 megahertz because that's actually way too fast we can't get the granularity of time that we want to one second using 16 megahertz so we need to actually down scale that by what's called a prescaler so we'll enable the prescaler on the timer by using the timer control register for timer 1 part b and i'll show you the data sheet and have the data sheet in the description below and basically what we can do is we can set a prescaler value and i'll show you what that means in a second using this line of code cs10 and cs12 so what this does is it sets the prescaler to 1024 so basically instead of the counter ticking every 16 megahertz it ticks 16 megahertz divided by 1024 so we have to count to 1024 before we actually tick up right so now our effective clock rate is 16 megahertz over 1024 which is about 15625 and that that number is very important we got to hold on to this and keep that in our head for a little bit okay so 1565 hertz is our new effective clock rate okay so now that we've pre-scaled the timer to get it into a granular time control that we want we now need to set another part of the timer control register to make sure that we're only doing overflow operations you can actually do a lot of cool stuff with timers you can do comparison timers and like uptick down tick timers all we're gonna do is an overflow timer so we need to disable all other features of the timer with this line here this sets basically hey don't enable any other features and then all we have to do now is the timer control register one we need to enable the overflow interrupt so timer overflow interrupt enable one right this allows the cpu to be interrupted by the overflow condition of this counter and then finally now that we've enabled that interrupt in our controller we need to say sei or set interrupts enabled before or set enable interrupts rather before we ran this line of code we enabled the timer interrupt but the entire cpu wasn't able to be interrupted it would not have responded to a single isr now at this point after this line it is able to be interrupted and now we're going to say an infinite loop where nothing happens right so essentially this is going to run infinitely and do nothing there will be knobs in here and all it will respond to is this interrupt triggering the cpu to do something so the question is what does it do right where do we actually send the processor to go so what we have to do actually is describe an isr or an interrupt service routine that is the isr for the timer 1 overflow vector oh not prescaler so this function is what's going to get ran when this counter gets overflowed to five three six right so i'll describe that here this has a max value of six five five three oh three six or a three five rather so when it gets to six five five three six it will actually overflow and trigger our isr and this is the point in the code where we're going to actually flip the bit that changes the led right so port b is equal to our x4 equals so we're going to flip it right we're going to go 1 to zero or zero to one the bit for uh port b five right and then we have to reset our counter to some value and again zero is the wrong value we just need to set it we need to set it back so we don't set it back the timing will be off okay so in theory if i flash this to the board it will make the led flip on and off at some rate the problem is our goal here goal is to flip led every second this is not going to be every second right because if i said the effective clock rate is this times a second 1565 hertz right but we want to go and make it a second that's not how the math is going to play out the way the math actually plays out to get it to be a second is the max value of the counter six five five three five minus the effective clock rate divided by the prescaler i'll take this and i'll copy it down here too i'm going to pound to find the clock rate as well cpu is 16. because what's happening is we need to pre-empt the timer to only have this many clock rate left to go right if that's the clock rate per second we only want to count that many times before the clock goes off before the timer goes off so we say max value minus the clock rate divided by the prescaler and again the pre-schedule right now is 1024. so this effectively this gets us a number around 49 000 that is the default timer for the chip to take so essentially set the timer to some default value enable an overflow interrupt let the interop run put the board into an infinite loop and then once that interrupt gets triggered we go here to the isr and xor the led and set that bit either on or off so we're gonna make it real quick and test it out and boom there we go our led is now blinking on and off using an isr or an interrupt service routine to handle the overflow of a timer in the cpu on the arduino guys if you liked that video if you learned something or if you just you know wanna wanna help me out hit like hit subscribe follow me on twitch and i'll see y'all in the next video take care [Music] you
Info
Channel: Low Level Learning
Views: 43,714
Rating: undefined out of 5
Keywords: arduino, avr, timer, interrupts, baremetal, raspberry pi, pico, rpi, microcontroller, maker, craft, hobby, electronics, wires, temperature, safety, project, board, electric, leds, led, thonny, python, micropython, os, ide, onewire, ds18b20, circuitpython, review, launch, measure, probe, rp2040, specs, specifications, how to, guide, programming, Pico emulation, retro games raspberry pi pico, etaprime, eta prime, raspberry pi pico, arm cortex m0+, low cost
Id: 5HgQkHzQc3o
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Sat Apr 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.