The problem with using the built in delay is that CPU clock cycles are essentially wasted. In such a simple program like we had in the previous post, we are not worried about conserving clock cycles but you can imagine that in a very complex program you would want every clock cycle to do something. Once interrupts are set up and enabled, they will "interrupt" the program when a certain condition is met. In the case of delay, a timer counts clock cycles and once a certain number is reached, the program is interrupted and carries out a "service routine" specified by the programmer. Once the service routine is finished, the program returns to the line of code it was executing when the interrupt occurred.
To illustrate the difference between the two types of delays, I wrote a program that does the exact same thing as the program in the previous post but it uses interrupts rather than the built in delay. This program simply makes the built in LEDs turn on and off at a rate that is perceptible to the human eye:
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
DDRB = 0xFF; //PORT B (LEDs) output
TCNT0 = 0; //timer starts at zero
OCR0 = 200; //and counts up to 200
TCCR0 = 0b00001101; //CTC mode, internal clk, 1024 prescaler
sei(); //global interrupt enable
TIMSK = (1<<OCIE0); //timer0 overflow interrupt enable
while(1)
asm("nop"); //stay here....
}
ISR (TIMER0_COMP_vect)
{
PORTB ^= 0xFF; //toggle PORTB (LEDs)
}
So you can load this program using the same method as I showed before and see that the LEDs simply turn on and off. All the connections should be exactly the same.
Some notes on the program: we first set up timer0 by setting TCCR0 to the binary 0001101. Of course we could express this in hex or even decimal but for me, the binary makes it easier to understand whats going on. Using the 8 bits of the TCCR0 register, you can specify whether you want "normal mode" (based on overflow) or "CTC mode" (based on compare match). You can also specify a prescaler value which is multiplies the number of clock cycles needed for a count. In other words, a large prescalar makes a delay longer. For our purposes, we'll set TCCR0 for CTC mode and have a prescaler of 1024. TCNT0 is the register which counts upwards with the clock cycles so we want this to start at zero. OCR0 is the value that TCNT0 will count up to before the interrupt occurs, so we will set this to 200. Note that since this is an 8 bit register, the largest value possible is 255. The sei() command sets the global interrupt bit and TIMSK enables the various different conditions that can cause an interrupt. The one we want is OCIE0 (timer0 overflow) which is actually bit _. Finally, the asm("nop") is not necessary for the program to work but it makes it easier to simulate the program in the AVR Simulator.
Speaking of the AVR Simulator or debugger in AVR Studio, lets take a look at how to make sure the program works without even loading it on the AVR. After you have entered all the code and hit F7 for build, go to Debug on the top menu and select Start Debugging.
So thats about all I'm going to get into. Of course there is a lot more to this subject but this at least illustrates one example.
If the main lights are blinking and very low in voltages then how would people travel. Because a blinking light can be very dangerous.
ReplyDeleteThe information you have shared with us is really interesting. I found lots of information which is very useful. And, I will definitely share this informative blog with my friends and other social sites. Target Promo Codes Keep this up.
ReplyDeletedfds
ReplyDeleteIt is so good that you have shown all these codes. But, now, they all seem to me very complicated. I will try to figure out it.
ReplyDeleteOnline privacy is an important and mainly focused need for everyone worldwide. Norton provides you the best and easily afford secured solutions. Its gives you 360 protection from virus, hackers, malware, and information-seeking programs. if you need a better antivirus at a reasonable price, then you can use Norton Promo Code and coupon codes for maximum saving.
ReplyDeleteNorton 360 protects your all devices from malware, spyware, ransomware, and also from other online threats. Powerful and Popular brand which gives you multilayer protection, online privacy and many more. They store your data and make it safe from hackers. Norton is the new generation protection software and recently update plans. For more information please check our website and protect your home and business devices.
ReplyDeleteNorton antivirus
Bluehost discounts coupons
ReplyDeleteThis blog gives us knowledge about physical education and this blog gives some key information about his coaches. So I am glad to found this types of articles. Hatts off mate you wroke a fabulous article and I would like to thank the admin he or she did a polite job here. Go with link webstagram to know top account on instagram.
ReplyDeletehttps://my.pointloma.edu/ICS/Community/Campus_Groups/Mariner/Public_Page.jnz?portlet=Forums&screen=PostView&screenType=change&id=a1e1d22e-06a4-4eb6-ac20-45514b67d81b
ReplyDeleteNice post, it is a very good interrupt of instruction in a light blink. I am impressed with this program. whatsapp mod
ReplyDeleteMeaningful sharing in the article, rich content! https://www.fencesmilton.com
ReplyDelete