Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Blink 2017-Mar-26 by Peter Hinteregger pfhinter@gmail.com
- on Atmega328P @ 16mhz (Makerduino)
- Section & page references to Atmel 8271 I-AVR 10/2014
- */
- .cseg
- ; =============================================================================
- ; IVT for Atmega328 & 328P. 26 vectors * 2 ops = 52 bytes. Table 12-7, page 66
- jmp RESET ; Reset handler
- jmp IRQ0_ISR ; IRQ0
- jmp NoInt ; IRQ1
- jmp NoInt ; PCINT0
- jmp NoInt ; PCINT1
- jmp NoInt ; PCINT2
- jmp WDT_ISR ; WatchDog
- ; Timer 2
- jmp NoInt ; Compare A
- jmp NoInt ; Compare B
- jmp NoInt ; Overflow
- ; Timer 1
- jmp NoInt ; Capture
- jmp NoInt ; Compare A
- jmp NoInt ; Compare B
- jmp NoInt ; Overflow
- ; Timer 0
- jmp NoInt ; Compare A
- jmp NoInt ; Compare B
- jmp NoInt ; Overflow
- jmp NoInt ; SPI-STC Transfer complete
- ; UART
- jmp NoInt ; RX Compelte
- jmp NoInt ; Data Register Empty
- jmp NoInt ; TX Complete
- jmp NoInt ; ADC conversion complete
- jmp NoInt ; EPROM ready
- jmp NoInt ; Analog comparator
- jmp NoInt ; TWI handler
- jmp NoInt ; SPR Store program ready handler
- NoInt: // Just a place for unhandled interrupts to drop into
- reti
- .equ T_Seq = (1 << WDCE) | (1 << WDE)
- IRQ0_ISR:
- // Enable WDT with desired pre-scaler of 131072 cycles / tick.
- wdr
- ldi R20, T_Seq ; WDCE or WDE
- ldi TEMP, 0b1000110 ; WDIE = 1 and WDP 2:1 = 1, 3 & 0 = 0
- sts WDTCSR, R20 ; Begin timed squence
- sts WDTCSR, TEMP ; Table 11-2 pg 55
- // R20 is the count in seconds and LED will blink half as many times
- ldi R20, 10 ; Blink LED 5 times over 10 seconds
- sbi PINB, PINB0 ; Turn LED off, disabling button
- reti
- WDT_ISR:
- // Normally in any ISR registers that are altered should be preserved,
- // SREG. In this simple example it doesn't matter.
- dec R20 ; Decrement count
- sbi PINB, PINB1 ; Toggle indicator LED
- reti
- .def TEMP = R24 ; Scratch register
- RESET: // Applications primary entry point.
- /*
- Stack definately needs to be initialized on POR (11.3 pg 48)
- PORF (11.9.1 pg 54) but maybe not for systems with bootloaders
- responding to EXTRF as is the case with Arduino
- */
- ldi TEMP, high (RAMEND)
- out SPH, TEMP
- ldi TEMP, low (RAMEND)
- out SPL, TEMP
- /*
- Turn off clocks PRTWI, PRTM2, PRTM0, Reserved, PRTM1, PRTSPI, PRUSART0, PRADC
- (10.11.3 Power Reduction Register). This is not absolutely essential, but
- because so many of my applications are going to be battery dependant,
- I'm encorporating it.
- */
- ldi TEMP, 0b11101111
- sts PRR, TEMP ; PRR is @ 0x64, beyond the reach of OUT
- /*
- This example is going to use PORTB for outputs to 3 LEDs
- Arduino pin 19 - PB5 = Onboard LED
- 15 - PB1 = Red LED indicator
- 14 - PB0 = LED indicator on push button
- */
- ldi TEMP, 0b100011 ; Set pins 5 1:0 to output on PORTB
- out DDRB, TEMP
- /*
- The push button is going to be rigged like a deadmans switch. While the
- indicator LED is on and when the button is released, the LED will go out
- and the WDT will start blinking the LED at near 1 sec intervals
- PWR <- PB0 controls indicator light on button
- SIG -> PD2 when released will trigger INT0
- GND -> Any of the ground pins.
- */
- sbi PORTD, PORTD2 ; Pullup INT0 for falling edge.
- ldi TEMP, 0b0010 ; Table 31-2 pg 71 INT0 on falling edge
- sts EICRA, TEMP ; Set interrupt 0 sense control 69H
- ldi TEMP, 1
- out EIMSK, TEMP ; Enable INT0 (13.2.2 pg 72)
- // While waiting for push button, SLEEP will power down MCU.
- ldi TEMP, 0b101 ; Set SM1 & SE Power Down Mode
- out SMCR, TEMP ; 10.11.1 pg 44
- Loop: // Infinite loop waiting for user to press button
- sbi PINB, PINB0 ; Enable push button, LED is on
- sei ; Re-enable interrupts
- sleep ; Wait for hit on INT0
- or R20, R20 ; ISR modifies R20 so poll register
- brne PC - 1 ; until count is exhausted.
- // Functionally equivalent to code on pg 52, except I didn't reset
- // WDRF in MCUSR because WDT isn't set for RESET.
- cli ; Disable interrupts
- wdr
- ldi TEMP, T_Seq ; WDCE or WDE
- sts WDTCSR, TEMP ; Begin timed sequence
- sts WDTCSR, R20 ; Turn off WDT
- rjmp Loop ; Wait for another press on button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement