PORTD = (1 << PD0)|(1 << PD3)|(1 << PD6);
PORTD = (1 << 0)|(1 << 3)|(1 << 6);
PORTD = 0b01001001;
PORTD = 0x49;
//Now, let’s consider the following statements:
DDRC = 0b10110001;
PORTC = 0b10010001;
OUTPUT = 0b10010001; /*This is not a C executable line, this
line is just for explanation*/
#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h"
int main(void)
{
lcd_init(LCD_DISP_ON_CURSOR); /* initialize lcd, display
on, cursor on */
/* for more options for
/* lcd_init(), view lcd.h f
ile
while(1) /* run continuously */
{
lcd_clrscr(); /* clear screen of lcd */
lcd_home(); /* bring cursor to 0,0 */
lcd_puts("hello"); /* type something random */
lcd_gotoxy(0,1); /* go to 2nd row 1st col */
lcd_puts("maxEmbedded"); /* type something random */
_delay_ms(50); /* wait 50ms */
}
}
01 #include <avr/io.h>
02
03 void timer0_init()
04 {
05 // set up timer with prescaler = 1024
06 TCCR0 |= (1 << CS02)|(1 << CS00);
07
08 // initialize counter
09 TCNT0 = 0;
10 }
11
12 int main(void)
13 {
14 // connect led to pin PC0
15 DDRC |= (1 << 0);
16
17 // initialize timer
18 timer0_init();
19
20 // loop forever
21 while(1)
22 {
23 // check if the timer count reaches 124
24 if (TCNT0 >= 124)
25 {
26 PORTC ^= (1 << 0); // toggles the led
27 TCNT0 = 0; // reset counter
28 }
29 }
30 }
01 #include <avr/io.h>
02 #include <avr/interrupt.h>
03
04 // global variable to count the number of overflows
05 volatile uint8_t tot_overflow;
06
07 // initialize timer, interrupt and variable
08 void timer0_init()
09 {
10 // set up timer with prescaler = 256
11 TCCR0 |= (1 << CS02);
12
13 // initialize counter
14 TCNT0 = 0;
15
16 // enable overflow interrupt
17 TIMSK |= (1 << TOIE0);
18
19 // enable global interrupts
20 sei();
21
22 // initialize overflow counter variable
23 tot_overflow = 0;
24 }
25
26 // TIMER0 overflow interrupt service routine
27 // called whenever TCNT0 overflows
28 ISR(TIMER0_OVF_vect)
29 {
30 // keep a track of number of overflows
31 tot_overflow++;
32 }
33
34 int main(void)
35 {
36 // connect led to pin PC0
37 DDRC |= (1 << 0);
38
39 // initialize timer
40 timer0_init();
41
42 // loop forever
43 while(1)
44 {
45 // check if no. of overflows = 12
46 if (tot_overflow >= 12) // NOTE: '>=' is used
47 {
48 // check if the timer count reaches 53
49 if (TCNT0 >= 53)
50 {
51 PORTC ^= (1 << 0); // toggles the led
52 TCNT0 = 0; // reset counter
53 tot_overflow = 0; // reset overflow counter
54 }
55 }
56 }
57 }
#include <avr/io.h>
#include <avr/interrupt.h>
// global variable to count the number of overflows
volatile uint8_t tot_overflow;
// initialize timer, interrupt and variable
void timer0_init()
{
// set up timer with prescaler = 256
TCCR0 |= (1 << CS02);
// initialize counter
TCNT0 = 0;
// enable overflow interrupt
TIMSK |= (1 << TOIE0);
// enable global interrupts
sei();
// initialize overflow counter variable
tot_overflow = 0;
}
// TIMER0 overflow interrupt service routine
// called whenever TCNT0 overflows
ISR(TIMER0_OVF_vect)
{
// keep a track of number of overflows
tot_overflow++;
}
int main(void)
{
// connect led to pin PC0
DDRC |= (1 << 0);
// initialize timer
timer0_init();
// loop forever
while(1)
{
// check if no. of overflows = 12
if (tot_overflow >= 12) // NOTE: '>=' is used
{
// check if the timer count reaches 53
if (TCNT0 >= 53)
{
PORTC ^= (1 << 0); // toggles the led
TCNT0 = 0; // reset counter
tot_overflow = 0; // reset overflow counter
}
}
}
01 #include <avr/io.h>
02 #include <avr/interrupt.h>
03
04 // global variable to count the number of overflows
05 volatile uint8_t tot_overflow;
06
07 // initialize timer, interrupt and variable
08 void timer1_init()
09 {
10 // set up timer with prescaler = 8
11 TCCR1B |= (1 << CS11);
12
13 // initialize counter
14 TCNT1 = 0;
15
16 // enable overflow interrupt
17 TIMSK |= (1 << TOIE1);
18
19 // enable global interrupts
20 sei();
21
22 // initialize overflow counter variable
23 tot_overflow = 0;
24 }
25
26 // TIMER1 overflow interrupt service routine
27 // called whenever TCNT1 overflows
28 ISR(TIMER1_OVF_vect)
29 {
30 // keep a track of number of overflows
31 tot_overflow++;
32
33 // check for number of overflows here itself
34 // 61 overflows = 2 seconds delay (approx.)
35 if (tot_overflow >= 61) // NOTE: '>=' used instead of '=='
36 {
37 PORTC ^= (1 << 0); // toggles the led
38 // no timer reset required here as the timer
39 // is reset every time it overflows
40
41 tot_overflow = 0; // reset overflow counter
42 }
43 }
44
45 int main(void)
46 {
47 // connect led to pin PC0
48 DDRC |= (1 << 0);
49
50 // initialize timer
51 timer1_init();
52
53 // loop forever
54 while(1)
55 {
56 // do nothing
57 // comparison is done in the ISR itself
58 }
59 }