Tuesday 27 May 2014

Example of Assembly Program using Interrupt 0, 1 and 2

Example of Circuit 


;When start the program, LED will perform running light
;Interrupt 0 -> Count Up
;Interrupt 1 -> Count Down
;Interrupt 2 -> Addition Operation

;Interrupt service vectors
.org $0000
rjmp Reset ; Reset vector
.org INT0addr
rjmp IntV0 ; INT0 vector (ext. interrupt from pin D2)
.org INT1addr
rjmp IntV1 ; INT1 vector (ext. interrupt from pin D3)
.org INT2addr
rjmp IntV2 ; INT1 vector (ext. interrupt from pin D3)
;---------------------------------------------------------
;
; Register defines for main loop
.def TIME=r16
.def TEMP=r17
;--------------------------------------------------------
;
; Reset vector - just sets up interrupts and service routines and
; then loops forever.
Reset:
LDI R16,$08
OUT SPH,R16
LDI R16,$54
OUT SPL,R16
;Initialize Port
LDI R16,$FF
OUT DDRA,R16
LDI R16,0b11111011
OUT DDRB,R16
LDI R16,0
OUT DDRD,R16
OUT DDRC,R16
; set up int0 and int1 and int2
LDI TEMP,0b11100000; Enable INT0,INT1, INT2
OUT GICR,TEMP
LDI TEMP,0b00001010; Set falling edge trigger for INT0
OUT mcucr,TEMP
LDI TEMP,0x00; Set falling edge trigger for INT1
OUT mcucsr,TEMP
sei ; enable interrupts and off we go!
loop:
LDI R16,0B10000000
OUT PORTB,R16
CALL delay_05
LDI R16,0B01000000
OUT PORTB,R16
CALL delay_05
LDI R16,0B00100000
OUT PORTB,R16
CALL delay_05
LDI R16,0B00010000
OUT PORTB,R16
CALL delay_05
LDI R16,0B00001000
OUT PORTB,R16
CALL delay_05
LDI R16,0B00000010
OUT PORTB,R16
CALL delay_05
LDI R16,0B00000001
OUT PORTB,R16
CALL delay_05
rjmp loop ; Infinite loop - never terminates
;------------------------------------------------------
;
; Int0 vector - decrease count
IntV0:
LDI ZH,HIGH(2*Count_Down)
LDI ZL,LOW(2*Count_Down)
LDI R17,10
Ulang2:
LPM R18,Z
OUT PORTA,R18
CALL delay_05
INC ZL
DEC R17
BRNE Ulang2
RETI
rjmp loop
;-----------------------------------------------------
;
; Int1 vector - increase count
IntV1:
LDI ZH,HIGH(2*Count_Up)
LDI ZL,LOW(2*Count_Up)
LDI R17,10
Ulang1:
LPM R18,Z
OUT PORTA,R18
CALL delay_05
INC ZL
DEC R17
BRNE Ulang1
RETI
rjmp loop

IntV2:
IN R16,PIND
ANDI R16,0b11100000
MOV R17,R16
LSR R17
LSR R17
LSR R17
LSR R17
LSR R17
IN R16,PINC
ANDI R16,0b00000111
ADD R16,R17

Display:
LDI ZH,HIGH(TABLE<<1)
LDI ZL,LOW(TABLE<<1)
ADD ZL,R16
LPM R18,Z
OUT PORTA,R18
RETI
rjmp loop


Count_Up:
.DB $00,$10,$20,$30,$40,$50,$60,$70,$80,$90

Count_Down:
.DB $00,$09,$08,$07,$06,$05,$04,$03,$02,$01

TABLE:
.DB $00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$10,$11,$12,$13,$14

delay_05:
LDI R16,8

outer_loop:
LDI R24,low(3037)
LDI R25,high(3037)

delay_loop:
ADIW R24,1
BRNE delay_loop
DEC R16
BRNE outer_loop
RET

No comments:

Post a Comment