Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.89 KB | None | 0 0
  1. ;-------------------------------------------------------------------------------
  2. ; MSP430 Assembler Code Template for use with TI Code Composer Studio
  3. ;
  4. ; Ben Spencer Luv Jain Meet Patel Darren Reed
  5. ;-------------------------------------------------------------------------------
  6. .cdecls C,LIST,"msp430.h" ; Include device header file
  7.  
  8. ;-------------------------------------------------------------------------------
  9. .def RESET ; Export program entry-point to
  10. ; make it known to linker.
  11. ;-------------------------------------------------------------------------------
  12. .data
  13.  
  14. time: .word 0, 0, 0 ; Sec, Min, Hr
  15. AMPM: .word 0 ; 0 - am, 1 - pm
  16.  
  17. alarmOn: .word 0 ; 0 - off, 1 - on 208
  18.  
  19. alarm: .word 0, 0, 0 ; Sec, Min, Hr
  20. alarmAMPM: .word 0 ; 0 - am, 1 - pm
  21.  
  22. timeTillAlarm:
  23. .word 0, 0 ; 218
  24.  
  25. tempTime: .word 0, 0, 0 ; Sec, Min, Hr 222
  26.  
  27. timeSeconds:
  28. .word 0 ; 228
  29. ; PWM Parameters
  30. ; Modify these parameters to give you proper wake up light behavior
  31. lowerDC: .word 20
  32. upperDC: .word 4000
  33. step: .word 1
  34. samplingPr: .word 5000
  35.  
  36. .text ; Assemble into program memory.
  37. .retain ; Override ELF conditional linking
  38. ; and retain current section.
  39. .retainrefs ; And retain any sections that have
  40. ; references to current section.
  41.  
  42. ;-------------------------------------------------------------------------------
  43. RESET mov.w #__STACK_END,SP ; Initialize stackpointer
  44. StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
  45.  
  46.  
  47. ;-------------------------------------------------------------------------------
  48. ; Main loop here
  49. ;-------------------------------------------------------------------------------
  50.  
  51. debugPin: .set BIT2
  52. alarmLED: .set BIT6
  53.  
  54. button: .set BIT3
  55.  
  56.  
  57. call #ConfigSysClks ; Configure system clock
  58. call #ConfigClockTimer ; Configure timer A1 to generate 1Hz frequency
  59.  
  60. call #StartPWMTimer ; Configure timer A0 to start generating PWM
  61.  
  62.  
  63. bis.b #debugPin, &P1DIR
  64.  
  65. bis.b #BIT0 + BIT6, &P1DIR ; Configure P1.0(LEDR) and P1.6 (LEDG) as output pin
  66.  
  67. bic.b #BIT3, &P1DIR
  68. bis.b #BIT3, &P1REN
  69. bis.b #BIT3, &P1OUT
  70.  
  71. loop:
  72. bis.w #LPM1+GIE, SR
  73.  
  74. ; update time (Hr Min Sec AM/PM) routine starts here
  75.  
  76. bit.w #BIT3, &P1IN ; Did somebody press the button?
  77. jnz SetupAlarm
  78.  
  79. cmp.w #0, TA0CCR0 ; Is the Timer 0?
  80. jeq LoadTimer ; Attempt to load the rest of the time
  81. cmp.w #0, TA0CCR0 ; Is the Timer still 0?
  82. jeq LetTheDogsOut ; Set off the Alarm
  83.  
  84. bis.b #BIT0 + BIT6, &P1OUT
  85.  
  86. inc.w &0x200 ; Increment sec
  87.  
  88. cmp.w #60, &0x200 ; Check if 60 sec
  89. jeq IncMin ; Increment minute if at 60 sec
  90. jmp NextSec
  91. EqualSec:
  92. call #IncMin
  93. NextSec:
  94. cmp.w #60, &0x202 ; Check if 60 min
  95. jeq IncHour ; Increment hour if at 60 min
  96.  
  97. cmp.w #12, &0x204 ; Check if 12 hours
  98. jeq IncAMPM
  99.  
  100. ; -----------------------------------
  101.  
  102. jmp loop
  103.  
  104.  
  105. LetTheDogsOut: ; Trigger the alarm
  106. bis.b #BIT0 + BIT6, &P1OUT
  107. ret
  108.  
  109. SetupAlarm:
  110. inc.w &0x208 ; Set up the alarm to begin properly
  111. cmp #2, &0x208
  112. jeq AlmOff
  113.  
  114. cmp #1, &0x208
  115. jeq StartAlarm
  116. ret
  117.  
  118. StartAlarm:
  119. call FindTimeTillAlarm ; Find time till alarm
  120. call LoadTimer ; Load seconds values into the alarm countdown
  121. call StartAlarmTimer ; Start the Alarm Timer
  122. ret
  123.  
  124. AlmOff:
  125. mov.w #0, &0x208 ; Turn off the Alarm
  126. ret
  127.  
  128. IncMin:
  129. inc.w &0x202 ; Increase minutes if 60 sec
  130. mov.w #0, &0x200 ; Reset sec to 0
  131. ret ; Return to main loop
  132.  
  133. IncHour:
  134. inc.w &0x204 ; Increase the hour if minutes if 60 min
  135. mov.w #0, &0x202 ; Reset minutes to 0
  136. ret
  137.  
  138. IncAMPM:
  139. inc.w &0x206 ; Move from AM to PM or PM to 2
  140. mov.w #0, &0x204 ; Set hours to 0
  141. cmp.w #2, &0x206 ; If we pass midnight
  142. jeq ToAM ; Reset to AM
  143. ret
  144. ToAM:
  145. mov.w #0, &0x206 ; If AMPM is 2, set to AM cuz 2 aint no thang baby gurl
  146. ret
  147.  
  148. ; -------------------------------------
  149.  
  150. ConfigSysClks:
  151. clr.b &DCOCTL
  152. mov.b &CALBC1_1MHZ, &BCSCTL1 ; Config MCLK to calibrated 1MHz
  153. mov.b &CALDCO_1MHZ, &DCOCTL
  154. mov.b #DIVS_3, &BCSCTL2 ; Config SMCLK to 1/8MHz
  155.  
  156. ret
  157.  
  158. ConfigClockTimer:
  159. mov.w #ID_3+MC_1+TASSEL_2+TACLR, &TA1CTL ; Config Timer Clock to SMCLK/8MHz and count upto CCR0
  160. mov.w #7812, &TA1CCR0 ; Generate a ~1Hz clock.
  161. mov.w #CCIE, &TA1CCTL0 ; Enable CCR0 Interrupt
  162. ret
  163.  
  164. StartPWMTimer:
  165. bic.w #CCIE, &TA0CCTL0
  166.  
  167. mov.w &samplingPr, &TA0CCR0 ; Update PWM sampling period
  168. mov.w &lowerDC, &TA0CCR1 ; Set lower threshold of PWM duty cycle.
  169. ; TA0CCR1 cannot start with 0!
  170.  
  171. bis.b #alarmLED, &P1DIR ; Configure P1.6 to be used as TA0CCR1 output
  172. bis.b #alarmLED, &P1SEL
  173. bic.b #alarmLED, &P1SEL2
  174.  
  175. bic.b #alarmLED, &P1OUT
  176.  
  177. mov.w #OUTMOD_7, &TA0CCTL1 ; Use reset/set mode to generate PWM
  178. mov.w #MC_1 + TASSEL_2 + TACLR, &TA0CTL ; Config Timer Clock to SMCLK.
  179. ; Start Timer in up mode to count up to CCR0
  180. ret
  181.  
  182. StopPWMTimer:
  183. bic.b #alarmLED, &P1SEL ; disconnect debug LED from timerA0 CCR0
  184.  
  185. mov.w #MC_0 + TACLR, &TA0CTL ; clear timerA0 config for PWM
  186. clr.w &TA0CCTL0
  187. clr.w &TA0CCR0
  188. clr.w &TA0CCR1
  189.  
  190. ret
  191.  
  192. StartAlarmTimer:
  193. bis.b #BIT0, &P1SEL
  194. bic.b #BIT0, &P1SEL2
  195. bic.b #BIT0, &P1DIR
  196.  
  197. mov.w #CCIE, &TA0CCTL0
  198. mov.w #ID_0+MC_1+TASSEL_0+TACLR, &TA0CTL
  199.  
  200. ret
  201.  
  202. ;-----------------------------------------------------
  203. FindTimeTillAlarm:
  204. cmp.w #1, &0x216 ; Store the extra 12 hours for an am or pm cycle in the first timeTillAlarm
  205. jeq AddPm
  206.  
  207. clr.w &0x224 ; Convert the alarm minutes into seconds and store in tempTime min
  208. add.w &0x212, &0x224
  209. push.w &0x224
  210. call #MultBy60
  211. pop.w &0x224
  212.  
  213. clr.w &0x226 ; Convert the alarm hours into seconds and store in tempTime hours
  214. add.w &0x214, &0x226
  215. push.w &0x226
  216. call #MultBy60
  217. call #MultBy60
  218. pop.w &0x226
  219.  
  220. add.w &0x210, &0x220 ; Add alarm seconds
  221. add.w &0x224, &0x220 ; Add alarm seconds
  222. add.w &0x226, &0x220 ; Add alarm seconds
  223.  
  224.  
  225. clr.w &0x224 ; Convert the current minutes into seconds and store in tempTime min
  226. add.w &0x202, &0x224
  227. push.w &0x224
  228. call #MultBy60
  229. pop.w &0x224
  230.  
  231. clr.w &0x226 ; Convert the current hours into seconds and store in tempTime hours
  232. add.w &0x204, &0x226
  233. push.w &0x226
  234. call MultBy60
  235. call MultBy60
  236. pop.w &0x226
  237.  
  238. sub.w &0x200, &0x220 ; Add alarm seconds
  239. sub.w &0x224, &0x220 ; Add alarm seconds
  240. sub.w &0x226, &0x220 ; Add alarm seconds
  241.  
  242. sub.w #30, &0x220 ; The timer will reach 0 30 seconds before the alarm goes off so that the light may begin to fade
  243. ret
  244.  
  245. AddPm:
  246. cmp.w #1, &0x206
  247. jeq Nope
  248. mov.w #43200, &0x218
  249. ret
  250.  
  251. Nope:
  252. ret
  253.  
  254. LoadTimer:
  255. cmp.w #0, &0x218
  256. jne LoadOne ;Load the first portion of seconds if not equal to zero
  257.  
  258. cmp.w #0, TA0CCR0
  259. jeq LoadTwo ;Load the second portion of seconds if not equal to zero
  260. ret
  261.  
  262. LoadOne:
  263. mov.w &0x218, TA0CCR0
  264. clr.w &0x218
  265. ret
  266. LoadTwo:
  267. mov.w &0x220, TA0CCR0
  268. clr.w &0x220
  269. ret
  270. ;-----------------------------------------------------
  271.  
  272.  
  273. ;------------------------------- subroutine MultBy60 ---------------------------
  274. ;
  275. ; function:
  276. ; accept 1 parameter via stack.
  277. ; return result of multiplying the parameter with 60 via stack
  278. ;
  279. ; prerequisit:
  280. ; the parameter is passed to the routine via stack
  281. ; before calling this subroutine, push the parameter
  282. ; to be multiplied onto the stack
  283. ;
  284. ; postrequisit:
  285. ; upon return from subroutine, the result of
  286. ; multiplication will be stored at the top of the stack,
  287. ; replacing the original parameter passed to this routine.
  288. ;
  289. ;--------------------------------------------------------------------------------
  290. MultBy60:
  291. push.w R12
  292.  
  293. mov.w 4(SP), R12
  294. rla R12
  295. rla R12
  296. rla R12
  297. rla R12
  298. rla R12
  299. rla R12
  300.  
  301. sub.w 4(SP), R12
  302. sub.w 4(SP), R12
  303. sub.w 4(SP), R12
  304. sub.w 4(SP), R12
  305.  
  306. mov.w R12, 4(SP)
  307.  
  308. pop.w R12
  309. ret
  310. ;----------------- End of subroutine MultBy60 subroutine -----------------------
  311.  
  312. ;-------------------------------------------------------------------------------
  313. ; Interrupt Service Routines
  314. ;-------------------------------------------------------------------------------
  315.  
  316. ; Port1 ISR
  317. SetAlarmISR:
  318.  
  319. reti
  320.  
  321.  
  322. ; TA1CCR0 ISR
  323. ClockTickTimerISR:
  324. bic.w #LPM1, 0(SP)
  325. xor.b #debugPin, &P1OUT ; Toggle debug pin
  326.  
  327. ; Update PWM duty cycle
  328. cmp.w &TA0CCR1, &upperDC ; update PWM duty cycle
  329. jl endPWM
  330. add.w &step, &TACCR1
  331. reti
  332.  
  333. endPWM:
  334. call #StopPWMTimer
  335. bis.b #alarmLED, &P1OUT ; keep the alarm LED on
  336.  
  337. reti
  338.  
  339. ; TA0CCR0 ISR
  340. DownCntISR:
  341. ; Downcount to alarm time routine starts here
  342.  
  343. ; -------------------------------------------
  344. reti
  345.  
  346.  
  347.  
  348.  
  349. ;-------------------------------------------------------------------------------
  350. ; Stack Pointer definition
  351. ;-------------------------------------------------------------------------------
  352. .global __STACK_END
  353. .sect .stack
  354.  
  355. ;-------------------------------------------------------------------------------
  356. ; Interrupt Vectors
  357. ;-------------------------------------------------------------------------------
  358.  
  359. .sect ".int02" ; Port1 ISR
  360. .short SetAlarmISR
  361.  
  362. .sect ".int09" ; TimerA0 CCR0 ISR
  363. .short DownCntISR
  364.  
  365. .sect ".int13" ; TimerA1 CCRO ISR
  366. .short ClockTickTimerISR
  367.  
  368. .sect ".reset" ; MSP430 RESET Vector
  369. .short RESET
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement