HW 10 CS 273 Ziad Arafat

  1. If the input clock source operates at 16 MHz, what is the TCNT1 value needed to generate a time delay of 0.5 millisecond in normal mode?

  2. If the clock operates at 10 MHz, what is the OCR0 value needed to generate time delay of 4 microseconds in CTC mode without any prescaler.

  3. Write an assembly program for Timer1 to generate a square wave of 16000 Hz to output on PB3 using a clock frequency of 16 MHz.

16 million clk/sec = 16000 clks/sec * n n = 1000 1000/2 = 500 500 off 500 on
sbi ddrb, 5 // pb5 as an output start: sbi portb, 3 // pb3 = 1 rcall 500delay cbi portb, 3 // pb3 = 0 rcall 500delay jmp start 500delay: ldi r20, 0x01 out tcnt1h, r20 // temp = 0xd8 ldi r20, 0xf8 out tcnt1l, r20 // tcnt1l = 0xf0, tcnt1h = temp ldi r20, 0x0 out tccr1a, r20 // wgm11:10=00 ldi r20, 0x1 out tccr1b, r20 // wgm13:12=00, cs=clk loop: in r20, tifr1 // read tifr1 sbrs r20, tov1 // if tov1 is set skip next instruction jmp loop ldi r20, 1<<tov1 out tifr1, r20 // clear tov1 flag ldi r19, 0 out tccr1b, r19 // stop timer out tccr1a, r19 // ret
  1. What is the advantage of using falling/rising edges instead of high/low voltages to detect events?

  2. What is the signal the following code generates at PORTB pin 5? If it is a periodical signal, what is its period? The clock frequency is 16MHz.

0x100 - 3B = 256 - 59 = 197 = 210 clks off and 210 clks on 420 clks ;; nice