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

1. Setup timer0 to create an interrupt that happens every 4mS. In the interrupt

ID: 2079743 • Letter: 1

Question

1. Setup timer0 to create an interrupt that happens every 4mS. In the interrupt routine, add one to the dumy0 variable we created.

// PORT Arduino smt-pin function what we use it for
// label
// PORTB.0 0x01 8 12 1 CLKO/ICP1 Trig
// PORTB.1 0x02 9 13 0 OC1A Echo
// PORTB.2 0x04 10 14 1 OC1B LED D3
// PORTB.3 0x08 11 15 0 MOSI/OC2A MOSI
// PORTB.4 0x10 12 16 0 MISO MISO
// PORTB.5 0x20 13 17 1 SCK LED D1
// PORTB.6 0x40 7 0 XTAL1 XTAL
// PORTB.7 0x80 8 0 XTAL2 XTAL

#define SET_DDRB 0x25

// PORTC.0 0x01 A0 23 1 ADC0 JP11-2
// PORTC.1 0x02 A1 24 1 ADC1 JP11-3
// PORTC.2 0x04 A2 25 1 ADC2 JP11-4
// PORTC.3 0x08 A3 26 1 ADC3 JP11-5
// PORTC.4 0x10 A4 27 0 ADC4 N/C
// PORTC.5 0x20 A5 28 0 ADC5 JP12-2
// PORTC.6 0x40 29 0 RESET RESET

#define SET_DDRC 0x0F

// PORTD.0 0x01 0 30 0 Rx RX SERIAL Data
// PORTD.1 0x02 1 31 1 Tx TX SERIAL Data
// PORTD.2 0x04 2 32 1 INT0 Servo 0
// PORTD.3 0x08 3 1 1 OC2B/INT1 Servo 1
// PORTD.4 0x10 4 2 1 XCK/T0 Servo 2
// PORTD.5 0x20 5 9 1 OC0B/T1 Servo 3
// PORTD.6 0x40 6 10 1 OC0A/AIN0 Servo 4
// PORTD.7 0x80 7 11 1 AIN1 Servo 5

#define SET_DDRD 0xFE

// ADC6 19 0 ADC6 N/C
// ADC7 22 0 ADC7 Vin (battery voltage)


volatile uint8_t dumy0; // for debug
volatile uint16_t dumy16; // for debug

void busyWait(){
volatile uint32_t i;
for(i=0;i<50000;i++){
}
}

void outChar(char c){
while((UCSR0A & 0x20) == 0);

UDR0 = c;

}

void outCR(){
outChar(0x0D);
outChar(0x0A);
}

void printHex4(uint8_t n){
n &= 0x0F;
n += '0';
if(n > '9'){
n+=7;
}
outChar(n);
}

void printHex8(uint8_t n){
printHex4(n >> 4);
printHex4(n);
}

void printHex16(uint16_t n){
printHex8(n >> 8);
printHex8(n);
}

void printStatus(){
printHex16(dumy16);
outChar(' ');
printHex8(dumy0);
outCR();
}

int main(){
// uint16_t i;
DDRB = SET_DDRB;
DDRC = SET_DDRC;
DDRD = SET_DDRD;

UBRR0H = 0;
UBRR0L = 103;
UCSR0B = 0x10 | 0x08 | 0x01; // same as 0x19

dumy0 = 0;
while(1){
// PORTC = 0x00;
PINC = 0xFF;
PINB = 0x20;
dumy16 += 123;
printStatus();
busyWait();
  
}
}

Explanation / Answer

as per the following formula, with a clock frequency of 32 kHz and 8-bit counter, the maximum delay possible is of 8 ms. This is quite low (for us, but not for the MCU). Hence for a delay of 6 ms, we need a timer count of 191. This can easily be achieved with an 8-bit counter (MAX = 255).Thus, what we need to do is quite simple. We need to keep a track of the counter value. As soon as it reaches 191, we toggle the LED value and reset the counter.

Right now, we will concentrate on the highlighted bits. The other bits will be discussed as and when necessary. By selecting these three Clock Select Bits, CS02:00, we set the timer up by choosing proper prescaler. The possible combinations

#include <avr/io.h>

  

void timer0_init()

{

    // set up timer with no prescaling

    TCCR0 |= (1 << CS00);

  

    // initialize counter

    TCNT0 = 0;

}

  

int main(void)

{

    // connect led to pin PC0

    DDRC |= (1 << 0);

  

    // initialize timer

    timer0_init();

  

    // loop forever

    while(1)

    {

        // check if the timer count reaches 191

        if (TCNT0 >= 191)

        {

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

            TCNT0 = 0;            // reset counter

        }

    }

}