Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an AVR assembly code that waits for 1 sec using the 8-bit Timer/Counter0 w

ID: 3670376 • Letter: W

Question

Write an AVR assembly code that waits for 1 sec using the 8-bit Timer/Counter0 with the system clock frequency of 16 MHz operating under Normal mode. This is done by doing the following:

(1) Timer/Counter0 is initialized to count for 10 ms and then interrupts on an overflow;

(2) The main part of the program simply loops, and for each iteration, a check is made to see if the loop has reach 100 iterations; and

(3) On each interrupt, Timer/Counter0 is reloaded to interrupt again in 10 ms.

Use the skeleton code shown below:

.include “m128def.inc”

.def mpr = r16

.def counter = r17

.ORG $0000

RJMP Initialize

.ORG $0020 ; Timer/Counter0 overflow interrupt vector

RCALL Reload_Counter

RETI

.ORG $0046 ; End of interrupt vectors

Initialize:

…Your code goes here…

LOOP:

…Your code goes here…

Reload_counter:

…Your code goes here…

RET

Explanation / Answer

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

#include <avr/io.h>

  

// initialize timer, interrupt and variable

void timer1_init()

{

    // set up timer with prescaler = 64 and CTC mode

    TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);

  

    // initialize counter

    TCNT1 = 0;

  

    // initialize compare value

    OCR1A = 24999;

}

  

int main(void)

{

    // connect led to pin PC0

    DDRC |= (1 << 0);

  

    // initialize timer

    timer1_init();

  

    // loop forever

    while(1)

    {

        // check whether the flag bit is set

        // if set, it means that there has been a compare match

        // and the timer has been cleared

        // use this opportunity to toggle the led

        if (TIFR & (1 << OCF1A)) // NOTE: '>=' used instead of '=='

        {

            PORTC ^= (1 << 0); // toggles the led

        }

  

        // wait! we are not done yet!

        // clear the flag bit manually since there is no ISR to execute

        // clear it by writing '1' to it (as per the datasheet)

        TIFR |= (1 << OCF1A);

  

        // yeah, now we are done!

    }

}

So

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

#include <avr/io.h>

  

// initialize timer, interrupt and variable

void timer1_init()

{

    // set up timer with prescaler = 64 and CTC mode

    TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);

  

    // initialize counter

    TCNT1 = 0;

  

    // initialize compare value

    OCR1A = 24999;

}

  

int main(void)

{

    // connect led to pin PC0

    DDRC |= (1 << 0);

  

    // initialize timer

    timer1_init();

  

    // loop forever

    while(1)

    {

        // check whether the flag bit is set

        // if set, it means that there has been a compare match

        // and the timer has been cleared

        // use this opportunity to toggle the led

        if (TIFR & (1 << OCF1A)) // NOTE: '>=' used instead of '=='

        {

            PORTC ^= (1 << 0); // toggles the led

        }

  

        // wait! we are not done yet!

        // clear the flag bit manually since there is no ISR to execute

        // clear it by writing '1' to it (as per the datasheet)

        TIFR |= (1 << OCF1A);

  

        // yeah, now we are done!

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote