Blinking a LED using 555 timer ic is simple, you can do the similar job using AVR atmega16. This is a simple program, perhaps simplest, and an introduction to Atmega16. To make a led blink you have to set (logic 1) and reset (logic 0) a pin of the controller continuously.
there's a code for blinking a LED connected to any pin of portA of the controller.
#include<avr/io.h>
#include<util/delay.h>
int main(void)
{
DDRA=0xFF; // set portA as out put
while(1) // run forever
{
PORTA=0xFF; //All the pins of portA is set
_delay_ms(1000); //wait for 1sec
PORTA=0x00; //all the pins of portA is reset
_delay_ms(1000); // wait for 1 sec
}
return(0);
}
DDR is data direct resister, DDR determines whether a particular pin of a port will work as an input or as an output, writing logic 1 to DDR makes the pin behave as output pin, writing logic 1 to DDR makes the pin behave as input pin. As we I wrote DDRA=0xFF, it will make all pins of portA work as output pin. Suppose you need only PA2 ( pin3 of portA ) as output pin, you need to write 1 to Bit 0 of data direction resister for portA (DDRA). You can write like this
DDRA=0x01;
or
DDRA|=(1<<PA2);
or
DDRA|=_BV(2);
Blinking LED using avr timers:
The previous program seems very easy, but it has a major drawback, we are making the pins of portA on and
off by writing a software code, which in terns making the CPU of the controller busy all the time. Atmega16 has dedicated hardware for counting, you can set a value up to which the counter will count, after each count a flag bit will set, depending on the value of that flag bit you can set and reset a pin of PortA to toggle a LED, or simply you can run an interrupt service routine every time the flag get set. Remember you have to reset the flag bit each time.
To make a led blink using AVR timer you need to go through this tutorial .
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
int main (void)
{
DDRB |= (1 << 0); // Set LED as output
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
sei(); // Enable global interrupts
OCR1A = 15624; // Set CTC compare value to 1Hz at 1MHz AVR clock, with a prescaler of 64
TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64
for (;;)
{
}
}
ISR(TIMER1_COMPA_vect)
{
PORTB ^= (1 << 0); // Toggle the LED
}
#include <avr/interrupt.h>
int main (void)
{
DDRB |= (1 << 0); // Set LED as output
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
sei(); // Enable global interrupts
OCR1A = 15624; // Set CTC compare value to 1Hz at 1MHz AVR clock, with a prescaler of 64
TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64
for (;;)
{
}
}
ISR(TIMER1_COMPA_vect)
{
PORTB ^= (1 << 0); // Toggle the LED
}
The tutorial and the following data may help you to understand the above code:
Code:
// timer 1
/* tccr1a & tccr1b breakdown
tccr1a
bits:
7 6 5 4 3 2 1 0
com1a1 com1a0 com1b1 com1b0 foc1a foc1b wgm11 wgm10
1 0 1 0 0 0 1 0 = 0xA2
tccr1b
bits:
7 6 5 4 3 2 1 0
icnc1 ices1 - wgm13 wgm12 cs12 cs11 cs10
0 0 0 1 1 0 1 1 = 0x1B
we setup clk/64 prescale, mode 14 fast pwm, clear oc1x on compare match, set oc1x at top
wgm13 wgm12 wgm11 wgm10
0 0 0 0 = mode 0, normal
0 0 0 1 = mode 1, pwm, phase correct, 8-bit
0 0 1 0 = mode 2, pwm, phase correct, 9-bit
0 0 1 1 = mode 3, pwm, phase correct, 10-bit
0 1 0 0 = mode 4, ctc
0 1 0 1 = mode 5, fast pwm, 8-bit
0 1 1 0 = mode 6, fast pwm, 9-bit
0 1 1 1 = mode 7, fast pwm, 10-bit
1 0 0 0 = mode 8, pwm, phase and freq correct
1 0 0 1 = mode 9, pwm, phase and freq correct
1 0 1 0 = mode 10, pwm, phase correct
1 0 1 1 = mode 11, pwm, phase correct
1 1 0 0 = mode 12, ctc
1 1 0 1 = mode 13, reserved!
1 1 1 0 = mode 14, fast pwm
1 1 1 1 = mode 15, fast pwm
cs12 cs11 cs10
0 0 0 = no clock source, timer disabled
0 0 1 = clk / 1
0 1 0 = clk / 8
0 1 1 = clk / 64
1 0 0 = clk / 256
1 0 1 = clk / 1024
1 1 0 = external clock T1, falling edge
1 1 1 = external clock T1, rising edge
com1x1 com1x0 x = either a or b
0 0 = look at datasheet
0 1 = as these functions
1 0 = change for different
1 1 = timer modes
*/
Not so good.....................
ReplyDeleteya..... i appreciate.......u..
DeleteThe first program I managed to compile and run on an atmega. Thanks for it!
ReplyDeleteVajk
Cаn Ӏ sіmply juѕt say what a rеlief
ReplyDeleteto uncover someone that gеnuіnely knows ωhat they're talking about on the internet. You certainly understand how to bring an issue to light and make it important. A lot more people need to check this out and understand this side of the story. I was surprised that you aren't morе popular beсause you ѕurely haνe the gift.
Also visit my wеb-site ... pikavippi
Also visit my page :: pikavippi
Heya i'm for the primary time here. I came across this board and I find It truly helpful & it helped me out much. I hope to give one thing back and help others like you helped me.
ReplyDeletemy web-site ... samsung galaxy s3
Here is my web-site ...
Ιt's actually very difficult in this busy life to listen news on TV, thus I just use the web for that reason, and take the hottest news.
ReplyDeletemy webpage :: get followers in instagram
Thank you for the auspicious writeup. It in fact was
ReplyDeletea amusement account it. Look advanced to more added
agreeable from you! However, how can we communicate?
Also visit my website - broker forex
Keep on working, great job!
ReplyDeletemy blog zulutrade
I'm gone to inform my little brother, that he should also go to see this web site on regular basis to get updated from newest news.
ReplyDeletemy blog: get followers
It's an remarkable paragraph in support of all the internet people; they will take advantage from it I am sure.
ReplyDeleteHere is my blog :: option trading stock
This comment has been removed by the author.
ReplyDeleteQuote:
ReplyDeleteSuppose you need only PA2 ( pin3 of portA ) as output pin, you need to write 1 to Bit 0 of data direction resister for portA (DDRA). You can write like this DDRA=0x01;
or
DDRA|=(1<<PA2);
or
DDRA|=_BV(2);
Should it not be DDRA = 0x04??