Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. /*
  2. Name: main.S
  3. Project: d_sema
  4. Author: JCSilva
  5. Creation Date: 2016-05-03
  6. Tabsize: 4
  7. Copyright: License: GNU GPL v2 (see License.txt)
  8. This Revision: $Id$
  9. */
  10.  
  11. #define __SFR_OFFSET 0
  12. #include <avr/io.h>
  13. #include <avr/eeprom.h>
  14.  
  15. /*
  16. EEPROM
  17. */
  18.  
  19. /*
  20. IO Definitions
  21. */
  22. #define RED PB0
  23. #define YELLOW PB1
  24. #define GREEN PB2
  25.  
  26.  
  27. /*
  28. Constants
  29. */
  30. #define WDT_CNTEND 15 /* 32 x 2.5s, end count for WDT timeouts */
  31.  
  32. /* Time system constants */
  33.  
  34. /*
  35. Register difinition, use only C-style comments (avr-as is stupid)
  36. */
  37. ; from R0-R15 only direct addressing
  38. #define zero R0
  39.  
  40. ; from R16-R31 ldi possible
  41. #define wrkreg R16 /* working register */
  42. #define wdt_cnt R17 /* WDT interrupts counter */
  43. #define light_o R18 /* light output */
  44.  
  45. /*
  46. Interrupt Routines
  47. */
  48. ;* Addresses of interrupt routines is defined in io.h (for this processor)
  49. ;.global INT0_vect
  50. ;.global IO_PINS_vect
  51. ;.global TIMER1_COMP_vect
  52. ;.global TIMER1_OVF_vect
  53. ;.global TIMER0_OVF_vect
  54. ;.global EE_RDY_vect
  55. ;.global ANA_COMP_vect
  56. ;.global ADC_vect
  57.  
  58. .text
  59.  
  60. ;*
  61. ;* Interrupt 0
  62. ;*
  63. .global INT0_vect
  64. INT0_vect:
  65. reti
  66.  
  67. ;*
  68. ;* Pin change interrupt
  69. ;*
  70. .global IO_PINS_vect
  71. IO_PINS_vect:
  72. reti
  73.  
  74. ;*
  75. ;* Interrupt Timer 1 Overflow
  76. ;*
  77. .global TIMER1_OVF_vect
  78. TIMER1_OVF_vect:
  79. reti
  80.  
  81. ;*
  82. ;* Interrupt Timer 1 Compare
  83. ;*
  84. .global TIMER1_COMP_vect
  85. TIMER1_COMP_vect:
  86. reti
  87.  
  88. ;*
  89. ;* Interrupt Timer 0 Overflow
  90. ;*
  91. .global TIMER0_OVF_vect
  92. TIMER0_OVF_vect:
  93. reti
  94.  
  95. ;*
  96. ;* EEPROM ready interrupt
  97. ;*
  98. .global EE_RDY_vect
  99. EE_RDY_vect:
  100. reti
  101.  
  102. ;.global ANA_COMP_vect
  103. ;*
  104. ;* Analog Comparator Interrupt
  105. ;*
  106. .global ANA_COMP_vect
  107. ANA_COMP_vect:
  108. reti
  109.  
  110. ;*
  111. ;* ADC Converter, End of Convertion
  112. ;*
  113. .global ADC_vect
  114. ADC_vect:
  115. reti
  116.  
  117. ;*
  118. ;* Main routine
  119. ;*
  120. .global main
  121. main:
  122. in wrkreg, MCUSR ; save MCUSR
  123. clr zero
  124. out MCUSR,zero ; clear it for next
  125. sbrc wrkreg, WDRF ; skip next if not WDReset
  126. rjmp wdt_rst ; WDT reset, check division
  127. ; from here any other reset
  128.  
  129. ; calibrate clock speed - not needed
  130. ; ldi ZH, 0x03 ; Z -> End of Program memory (pmem)
  131. ; ldi ZL, 0xff
  132. ; lpm ; get calibration byte from pmem(0x3ff)
  133. ; out OSCCAL, R0 ; set calibration byte
  134. ; calibrate clock speed - not needed
  135. clr wdt_cnt ; new activation reset counter
  136. cpi light_o, 0b00000100 ; test if green
  137. breq next_light
  138. cpi light_o, 0b00000010 ; test if yellow
  139. breq next_light
  140. set_green:
  141. ldi light_o,0b00000100 ; set light to green
  142. rjmp out_start
  143. next_light:
  144. lsr light_o ; set next light
  145. out_start:
  146. mov wrkreg,light_o
  147. com wrkreg
  148. andi wrkreg,0b00000111
  149. out PORTB, wrkreg ; set light output
  150. out DDRB, light_o
  151. ldi wrkreg, 0x0F ; Enable WDT, reset in 2048K cycles
  152. out WDTCR, wrkreg
  153.  
  154. go_sleep:
  155. ldi wrkreg, 0x30 ; Enable Pull Up, Sleep enable, Power Down mode
  156. out MCUCR, wrkreg
  157. sleep ; else wait for next WDT interrupt
  158. nop
  159.  
  160. wdt_rst:
  161. inc wdt_cnt ; increment wdt_reset_counter
  162. cpi wdt_cnt,WDT_CNTEND ; check number of WDT resets
  163. breq go_sleep
  164. rjmp out_start
  165.  
  166. .section .eeprom
  167. .org 0x00
  168. .ds 1
  169. .byte 0xAA
  170. .byte 0x55
  171. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement