Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. /*
  2. Blink 2017-Mar-26 by Peter Hinteregger pfhinter@gmail.com
  3. on Atmega328P @ 16mhz (Makerduino)
  4.  
  5. Section & page references to Atmel 8271 I-AVR 10/2014
  6. */
  7. .cseg
  8. ; =============================================================================
  9. ; IVT for Atmega328 & 328P. 26 vectors * 2 ops = 52 bytes. Table 12-7, page 66
  10.  
  11. jmp RESET ; Reset handler
  12. jmp IRQ0_ISR ; IRQ0
  13. jmp NoInt ; IRQ1
  14. jmp NoInt ; PCINT0
  15. jmp NoInt ; PCINT1
  16. jmp NoInt ; PCINT2
  17. jmp WDT_ISR ; WatchDog
  18.  
  19. ; Timer 2
  20.  
  21. jmp NoInt ; Compare A
  22. jmp NoInt ; Compare B
  23. jmp NoInt ; Overflow
  24.  
  25. ; Timer 1
  26.  
  27. jmp NoInt ; Capture
  28. jmp NoInt ; Compare A
  29. jmp NoInt ; Compare B
  30. jmp NoInt ; Overflow
  31.  
  32. ; Timer 0
  33.  
  34. jmp NoInt ; Compare A
  35. jmp NoInt ; Compare B
  36. jmp NoInt ; Overflow
  37. jmp NoInt ; SPI-STC Transfer complete
  38.  
  39. ; UART
  40.  
  41. jmp NoInt ; RX Compelte
  42. jmp NoInt ; Data Register Empty
  43. jmp NoInt ; TX Complete
  44.  
  45. jmp NoInt ; ADC conversion complete
  46. jmp NoInt ; EPROM ready
  47. jmp NoInt ; Analog comparator
  48. jmp NoInt ; TWI handler
  49. jmp NoInt ; SPR Store program ready handler
  50.  
  51. NoInt: // Just a place for unhandled interrupts to drop into
  52.  
  53. reti
  54.  
  55. .equ T_Seq = (1 << WDCE) | (1 << WDE)
  56.  
  57. IRQ0_ISR:
  58. // Enable WDT with desired pre-scaler of 131072 cycles / tick.
  59.  
  60. wdr
  61. ldi R20, T_Seq ; WDCE or WDE
  62. ldi TEMP, 0b1000110 ; WDIE = 1 and WDP 2:1 = 1, 3 & 0 = 0
  63. sts WDTCSR, R20 ; Begin timed squence
  64. sts WDTCSR, TEMP ; Table 11-2 pg 55
  65.  
  66. // R20 is the count in seconds and LED will blink half as many times
  67.  
  68. ldi R20, 10 ; Blink LED 5 times over 10 seconds
  69. sbi PINB, PINB0 ; Turn LED off, disabling button
  70. reti
  71.  
  72. WDT_ISR:
  73. // Normally in any ISR registers that are altered should be preserved,
  74. // SREG. In this simple example it doesn't matter.
  75.  
  76. dec R20 ; Decrement count
  77. sbi PINB, PINB1 ; Toggle indicator LED
  78. reti
  79.  
  80. .def TEMP = R24 ; Scratch register
  81.  
  82. RESET: // Applications primary entry point.
  83. /*
  84. Stack definately needs to be initialized on POR (11.3 pg 48)
  85. PORF (11.9.1 pg 54) but maybe not for systems with bootloaders
  86. responding to EXTRF as is the case with Arduino
  87. */
  88. ldi TEMP, high (RAMEND)
  89. out SPH, TEMP
  90. ldi TEMP, low (RAMEND)
  91. out SPL, TEMP
  92. /*
  93. Turn off clocks PRTWI, PRTM2, PRTM0, Reserved, PRTM1, PRTSPI, PRUSART0, PRADC
  94. (10.11.3 Power Reduction Register). This is not absolutely essential, but
  95. because so many of my applications are going to be battery dependant,
  96. I'm encorporating it.
  97. */
  98. ldi TEMP, 0b11101111
  99. sts PRR, TEMP ; PRR is @ 0x64, beyond the reach of OUT
  100. /*
  101. This example is going to use PORTB for outputs to 3 LEDs
  102. Arduino pin 19 - PB5 = Onboard LED
  103. 15 - PB1 = Red LED indicator
  104. 14 - PB0 = LED indicator on push button
  105. */
  106. ldi TEMP, 0b100011 ; Set pins 5 1:0 to output on PORTB
  107. out DDRB, TEMP
  108. /*
  109. The push button is going to be rigged like a deadmans switch. While the
  110. indicator LED is on and when the button is released, the LED will go out
  111. and the WDT will start blinking the LED at near 1 sec intervals
  112.  
  113. PWR <- PB0 controls indicator light on button
  114. SIG -> PD2 when released will trigger INT0
  115. GND -> Any of the ground pins.
  116. */
  117. sbi PORTD, PORTD2 ; Pullup INT0 for falling edge.
  118. ldi TEMP, 0b0010 ; Table 31-2 pg 71 INT0 on falling edge
  119. sts EICRA, TEMP ; Set interrupt 0 sense control 69H
  120. ldi TEMP, 1
  121. out EIMSK, TEMP ; Enable INT0 (13.2.2 pg 72)
  122.  
  123. // While waiting for push button, SLEEP will power down MCU.
  124.  
  125. ldi TEMP, 0b101 ; Set SM1 & SE Power Down Mode
  126. out SMCR, TEMP ; 10.11.1 pg 44
  127.  
  128. Loop: // Infinite loop waiting for user to press button
  129.  
  130. sbi PINB, PINB0 ; Enable push button, LED is on
  131. sei ; Re-enable interrupts
  132. sleep ; Wait for hit on INT0
  133.  
  134. or R20, R20 ; ISR modifies R20 so poll register
  135. brne PC - 1 ; until count is exhausted.
  136.  
  137. // Functionally equivalent to code on pg 52, except I didn't reset
  138. // WDRF in MCUSR because WDT isn't set for RESET.
  139.  
  140. cli ; Disable interrupts
  141. wdr
  142. ldi TEMP, T_Seq ; WDCE or WDE
  143. sts WDTCSR, TEMP ; Begin timed sequence
  144. sts WDTCSR, R20 ; Turn off WDT
  145.  
  146. rjmp Loop ; Wait for another press on button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement