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

preview_player
Показать описание
Programming embedded systems is difficult. You need to make the most out of the limited processing power you have. The worst thing you could do is waste precious processing cycles by sleeping, waiting for an event to occur. Instead, you should be using INTERRUPTS. Interrupts are a great way for a device to be pre-empted and ran when an event or timer triggers.

In this video, I'll teach you how to write code that produces a timer interrupt, in baremetal AVR C, without using the Arduino API or libraries.

🔥🔥🔥 SOCIALS 🔥🔥🔥
Рекомендации по теме
Комментарии
Автор

Your videos are extremely helpful and educational. I can not express my appreciation for your work in words. Thank you very much. I have a small request, if it is possible please archive your twitch streams on youtube.

nimcompoo
Автор

6:55 "ISR" is the "Interrupt Service Routine", ie the code that runs when the CPU gets an Interrupt.
what you meant there is called an "IRQ" which is the "Interrupt Request" signal that goes to the CPU and causes it to run the ISR (if interrupts are enabled).

proxy
Автор

As a convenience and to exemplify the behavior of the timer:

LEDHZ = 1
TCNT = 65535 - (F_CPU/1024)/LEDHZ

aajpeter
Автор

I was just searching for this for a school project! Great timing!

gregtasi
Автор

I'm a full stack developer who did embedded systems as well in varsity and enjoyed it, however due to the nature of my work I haven't been writing much C since college but this channel has gotten me practicing again and my skills have level up 10 fold..Thank you so much for your A - Grade content 🔥🔥keep it up and I'll keep recommending you 🙏 also can we get some esp8266 mesh network projects?

Frost_Byte_Tech
Автор

Really you should use OCR1A and OCR1B registers for the timer value and set CTC mode for timer 1, rather than setting TCNT1=0 in the ISR.
That will get the hardware to reset the timer to zero for you immediately it matches the programmed OCR value. As your code stands there is some latency between the interrupt being triggered and your ISR getting called, so you will not have “exactly” 15KHz (or whatever frequency division of the CPU clock you are aiming for). Worse, if other interrupts preempt your service routine, you will also introduce drift with a manual reset.

stuartmcconnachie
Автор

Great Video, but i think you should always mention or show the specific part in the manual where you get the needen port and interrupt specifications from, its very hard to follow these low level stuff otherwise for beginners. Reading the documentation in the video would also teach your audience how to read these docs properly. Which is a higly needed skill to have if you are programming bare metal stuff.

CooperDuper
Автор

Oh damn, your makefile... I recall taking some massive makefile for raw AVR programming and distilling it down to a nice hand written makefile like that and it was so much cleaner. But I lost that thing years ago and didn't want to recreate it 🤣, very glad to see you have a MUCH nicer starting place

ZenoTasedro
Автор

i didn't know your channel, but i instantly loved the fact that you started the video saying: "low level GANG".

henokvanni
Автор

Wow reminds me of my university days learning how to program a Microchip in assembly.

cornevanzyl
Автор

Thanks for this. Can you please do this same thing with another processor, say an SAMD21 or RP2040? I have used up all 6 ISRs on the 328P and need more, but can't understand the datasheets of the Cortex M0+ processor.

vegansynths
Автор

i always learned to do it with timer compare that way you dont have to do the subtract but its just a different way of doing the same thing :)

dualityy
Автор

another way to the same would be to use the output compare registers and then listen for an Output Compare Match interrupt instead of listening for the overflow of the counter.

alejandrojara
Автор

I don't know why I'm watching this, I have no clue what's even going on lol

Awkwardzana
Автор

You can also use the CTC modes and set the OCR1A or IC(i forgot the rest of the register name) and use their corresponding ISRs to do this task without fussing around with the timer counter itself.

ljaworski
Автор

Thanks I have some code I did ages back using the timer registers but I don't think I understood what I was doing as well as I do now with your explanation. Still... it did at least work!

WistrelChianti
Автор

Cool baremental programming technique! I think one the pros of interrupt-driven is power save, maybe you can upload another video to explain it.

好多鱼-ln
Автор

It would be worth including a comment about how the interrupt flag gets cleared. In many micros you have to do it explicitly but I am guessing the Atmel clears it automatically when you reload the timer. A common mistake used to be forgetting to clear the interrupt flag.

ruffrecords
Автор

great tutorial, hope to see more like this !!

SaliyaRuchiranga
Автор

Great vid, but I did notice one problem, the Egyptian Braces in your int main :P

TokyoScarab