Assignment#2
Assignment#2
Assignment#2
April 2, 2013
Design Assignment #2
Code:
.org 0x0000
jmp main
.org 0x001A
jmp TIMER1_OVRFLOW
.org 0x001E
jmp TIMER0_COMPB
.org 0x0038
main:
ldi r16, high(RAMEND) ;initialize stack
out SPH, r16
ldi r16, low(RAMEND)
out SPL, r16
ldi r16, 0x03 ;set PB0 and PB1 as output
out DDRB, r16
ldi r16, 0x02 ;set timer1 prescaler to 1/8, TCCR1A unchanged so
sts TCCR1B, r16 ;Timer1 is in normal mode
ldi r16, 0x01 ;set TOIR1 to enable overflow interrupt
sts TIMSK1, r16
ldi r16, 0xF9 ;set OCR0A so Timer0 counts from 0 to 249
out OCR0A, r16
ldi r16, 0x09 ;set OCR0B to 9 so compare raises an interrupt on tenth
out OCR0B, r16 ;falling edge from Timer1
ldi r16, 0x02 ;set Timer0 to CTC mode
out TCCR0A, r16
ldi r16, 0x06 ;set Timer0 prescaler to external clock on falling edge
out TCCR0B, r16
ldi r16, 0x04 ;set OCIE0B to enable interrupt on OCR0B match
sts TIMSK0, r16
sei ;enable global interrupt
ldi r16, 0x3F ;set PC0 to PC5 to output
out DDRC, r16
ldi r16, 0xC0 ;set PD6 and PD7 to output
out DDRD, r16
loop:
in r16, TCNT0 ;get Timer0 count
out PORTC, r16 ;display count bits [5..0]
out PORTD, r16 ;display count bits [7..6]
jmp loop
TIMER1_OVRFLOW:
push r16 ;save registers
push r17
ldi r16, 0x02 ;toggle PB1
in r17, PORTB
eor r17, r16
out PORTB, r17
pop r17 ;restore registers
pop r16
reti ;return from interrupt and re-enable interrupt
TIMER0_COMPB:
push r16 ;save registers
push r17
in r16, OCR0B ;increment OCR0B by 10 to raise interrupt again on next
ldi r17, 10 ;match with OCR0B
add r16, r17
brcc not_overflow
ldi r16, 9
not_overflow:
out OCR0B, r16
ldi r16, 0x01 ;toggle PB0
in r17, PORTB
eor r17, r16
out PORTB, r17
pop r17 ;restore registers
pop r16
reti ;return from interrupt and re-enable interrupt
Code Flowchart: