Google

Monday, July 9, 2007

Project 2

Project #2
Flip Flop: Turn the LEDs on, then off, then on, then off

Feature used: PortD, Timer, internal interrupt


.include "m8535def.inc"

.org 0
rjmp main
.org 6
rjmp comparematchtim ;int handler for comparematch Timer1

;---------------ISR ;this routine will be executed if comparematch of Timer1 occurs
comparematchtim:
push r16
in r16, SREG ;store content of SREG to SRAM
push r16 ;store content of r16 to SRAM

in r16, PORTD ;copy content of PORTD register to r16
com r16 ;complement it
out PORTD, r16 ;copy back to PORTD register

ldi r16, high(15625)
out OCR1AH, r16
ldi r16, low(15625) ;comparator register OCR1A = 15625 (2 bytes)
out OCR1AL, r16


pop r16
out SREG, r16 ;load back contents to original location
pop r16
reti

;--------------main program
main:
ldi r16, high(RAMEND) ;stack register initialization
out SPH, r16
ldi r16, low(RAMEND)
out SPL, r16

ldi r16, high(15625)
out OCR1AH, r16
ldi r16, low(15625) ;comparator register OCR1A = 15625 (2 bytes)
out OCR1AL, r16
ldi r16, 0b00010000
out TIMSK, r16 ;enable comparematchA Timer1 interrupt

sei ;enable global interrupt

ldi r16, 0b00001011 ;Timer1 CTC mode, prescaler 64
out TCCR1B, r16

ser r16
out DDRD, r16 ;DDRD = $FF, means all of pins at PortD defined as output
out PORTD, r16 ;PORTD = $FF, means each of pins at PortD assigned to out a '1' logic
;the LEDs must be put in high active logic (cathodes connected to GND)

loop: ;this is called loop forever
rjmp loop ;the program will keep run in this address unless an interrupt occurs

No comments: