100% found this document useful (1 vote)
47 views

Assignment#2

The document contains code for an AVR microcontroller that uses two timers to blink LEDs. Timer1 is configured to generate an overflow interrupt every 1/8 second to toggle an LED connected to pin PB1. Timer0 is configured in CTC mode to generate a compare match B interrupt every 1/10 second to increment a counter and toggle an LED on pin PB0. The stack is initialized and global interrupts are enabled to allow interrupt handling.

Uploaded by

liquidmoon1973
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
47 views

Assignment#2

The document contains code for an AVR microcontroller that uses two timers to blink LEDs. Timer1 is configured to generate an overflow interrupt every 1/8 second to toggle an LED connected to pin PB1. Timer0 is configured in CTC mode to generate a compare match B interrupt every 1/10 second to increment a counter and toggle an LED on pin PB0. The stack is initialized and global interrupts are enabled to allow interrupt handling.

Uploaded by

liquidmoon1973
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CPE 301

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:

You might also like