Вы находитесь на странице: 1из 7

- - 7 6 02 (:03109072) (:03109038) (:03109216)

1
.include "m16def.inc" .def INP = r16 .DEF REG1 = r24 .org 0x0000 rjmp reset .org 0x0002 rjmp ISR0 reset: ldi out ldi out ldi out ldi out sei r16, SPH, r16, SPL, high(RAMEND) r16 low(RAMEND) r16 ; used for all kinds of initializations ; code always starts at address 0x00 ; INT0 routine always at address 0x02 ; main program starts here

; setting stack

r24, (1 << ISC01) | (1 << ISC00) MCUCR, r24 ; set INT0 to activate with positive pulse r24, (1 << INT0) ; activate INT0 interrupt GICR, r24 ; activate all interupts ; ; ; ; ; ; INP = 0x00 set B as input initialise PORTA for output of main program initialise counter of main program initialise PORTB

main_counter: clr INP out DDRD, INP ser r26 out DDRA, r26 clr r26 ser r28

out DDRB, r28 clr r28 loop: out PORTA , r26 ldi r24, low(100) ldi r25, high(100) rcall wait_msec inc r26 rjmp loop

; for output of interrupt routine ; initialise counter of interrupts ; show result on leds ; load r25:r24 with 100 ; delay 100 ms ; increase counter ; repeat

ISR0: debounce: ldi REG1, (1 << INTF0) out GIFR, REG1 ; GIFR bit 6 = 0 ldi r24, low(5) ldi r25, high(5) rcall wait_msec ; 5 msec delay ldi REG1, (1 << INTF0) cpi REG1, 1 breq debounce push r26 in r26, SREG push r26 inc r28 in INP, PIND andi INP, 0x01 cpi INP, 0x01 breq continue out PORTB, r28 continue: pop r26 out SREG, r26 pop r26 sei reti .include "delay.asm" ; save counter ; save SREG ; interupts++ ; ; ; ; PB0 = 1? if not return without turning leds on else show counter on leds for output

; reload SREG and r26 contents ; set intrpt mask again ; return where you left of in main program ; delay routines

2
.include "m16def.inc" .def .def .def .def .def INP = r18 bitcount = r19 ledsB = r20 zero_reg = r21 REG1 = r23 ; code always starts at address 0x00 ; INT0 routine always at address 0x04 ; main program starts here

.org 0x0 rjmp reset .org 0x4 rjmp ISR1 reset: ldi r16, high(RAMEND) out SPH, r16 ldi r16, low(RAMEND) out SPL, r16 ser out out clr ldi ldi out ldi out sei r28 DDRC, r28 DDRB, r28 r28 zero_reg, 0x00

; setting stack ; ; ; ; ; initialise PORTC for output of interrupt routine initialise PORTB for input of interrrupt routine initialise counter of INT1 routine set this register to zero for later use

r24, (1 << ISC11) | (1 << ISC10) MCUCR, r24 ; set INT1 to activate with positive pulse r24, (1 << INT1) ; activate INT1 interrupt GICR, r24 ; activate all interupts ; ; ; ; ; initialise PORTA for output of main program initialise counter of main program INP = 0x00 set B as input

main_counter: ser r26 out DDRA, r26 clr r26 clr INP out DDRB, INP loop: out PORTA, r26 ldi r24, low(100) ldi r25, high(100) rcall wait_msec inc r26 rjmp loop ISR1: nop debounce: ldi REG1, (1 << INTF1)

; show result on leds ; load r25:r24 with 100 ; delay 100 ms ; increase counter ; repeat

out GIFR, REG1 ; GIFR bit 6 = 0 ldi r24, low(5) ldi r25, high(5) rcall wait_msec ; 5 msec delay ldi REG1, (1 << INTF1) cpi REG1, 1 breq debounce push r26 in r26, SREG push r26 ldi bitcount, 0x08 ldi ledsB, 0x00 in INP, PINB loop1: rol INP adc ledsB, zero_reg dec bitcount cpi bitcount, 0x00 brne loop1 out PORTC, ledsB pop r26 out SREG, r26 pop r26 sei reti .include "delay.asm" ; save counter ; save SREG ; count all 8 bits ; initialise portB on-leds counter

; add carry(current bit) to ledsB

; for output ; reload SREG and r26 contents ; set intrpt mask again ; return where you left of in main program ; delay routines

3
.include "m16def.inc" .DEF REG1 = r24 .org 0x00 rjmp reset .org 0x02 rjmp ISR0 .org 0x10 rjmp ISR_TIMER1_OVF .cseg ; interrupt activation reset: ; main program starts here ldi REG1, high(RAMEND) out SPH, REG1 ldi REG1, low(RAMEND) out SPL, REG1 ; setting stack ldi out ldi out ldi out ldi out sei ; main program main: ldi REG1, 0b00000010 out DDRA, REG1 again: in REG1, PINA andi REG1, 1 cpi REG1, 0 breq again init_timer_1: ldi REG1, high(0xA473) out TCNT1H, REG1 ldi REG1, low(0xA473) out TCNT1L, REG1 leds_on_1: ldi REG1, 0b00000010 out PORTA, REG1 clr REG1 REG1, (1 << ISC01) | (1 << ISC00) MCUCR, REG1 ; set INT0 to activate with positive pulse REG1, (1 << INT0) ; activate INT0 interrupt GICR, REG1 REG1, (1 << CS12) | (0 << CS11) | (1 << CS10) TCCR1B, REG1 ; CK/1024 set timer frequency REG1, (1 << TOIE1) ; activate overflow interrupt for Timer1 TIMSK, REG1 ; activate all interupts ; used for all kinds of initializations ; code always starts at address 0x00 ; INT0 routine always at address 0x02 ; T/C1 overflow vector

; initialise PORTA PA0 as input, PA1 as output ; for output of main program

; PA0 = 0? ; if yes, read again ; initialize TCNT1 to overflow after 3 secs

; turn leds on

rjmp again ; interrupt routine: turn leds ISR0: nop debounce: ldi REG1, (1 << INTF0) out GIFR, REG1 ; ldi r24, low(5) ldi r25, high(5) rcall wait_msec ; ldi REG1, (1 << INTF0) cpi REG1, 1 breq debounce init_timer_2: ; ldi REG1, high(0xA473) out TCNT1H, REG1 ldi REG1, low(0xA473) out TCNT1L, REG1 leds_on_2: ; ldi REG1, 0b00000010 out PORTA, REG1 return: sei clr REG1 rjmp again on

GIFR bit 6 = 0 5 msec delay

initialize TCNT1 to overflow after 3 secs

turn leds on

; timer oveflow interrupt routine: turn leds off ISR_TIMER1_OVF: clr REG1 out PORTA, REG1 sei rjmp again .include "delay.asm" ; delay routines

delay.asm
wait_usec: sbiw r24, 1 nop nop nop nop brne wait_usec ret wait_msec: push r24 push r25 ldi r24, 0xe6 ldi r25, 0x03 rcall wait_usec pop r25 pop r24 sbiw r24, 1 brne wait_msec ret ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 2 1 1 1 1 1 4 cycles (0.250 msec) cycle (0.125 msec) cycle (0.125 msec) cycle (0.125 msec) cycle (0.125 msec) or 2 cycles (0.125 or 0.250 msec) cycles (0.500 msec)

2 cycles (0.250 msec) 2 cycles load register r25:r24 with 998 (1 cycle 0.125 msec) 1 cycle (0.125 msec) 3 cycles (0.375 msec), total delay 998.375 msec 2 cycles (0.250 msec) 2 cycles 2 cycles 1 or 2 cycles (0.125 or 0.250 msec) 4 cycles (0.500 msec)

Вам также может понравиться