-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarm.asm
306 lines (227 loc) · 7.64 KB
/
Alarm.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
;
;-------------------------------------------------------------------------------
.cdecls C,LIST,"msp430.h" ; Include device header file
;-------------------------------------------------------------------------------
.def RESET ; Export program entry-point to
; make it known to linker.
;-------------------------------------------------------------------------------
.data
time: .word 0, 0, 0 ; Sec, Min, Hr
AMPM: .word 0 ; 0 - am, 1 - pm
alarmOn: .word 0 ; 0 - off, 1 - on
alarm: .word 0, 0, 0 ; Sec, Min, Hr
alarmAMPM: .word 0 ; 0 - am, 1 - pm
timeTillAlarm:
.word 0, 0
; PWM Parameters
; Modify these parameters to give you proper wake up light behavior
lowerDC: .word 20
upperDC: .word 4000
step: .word 1
samplingPr: .word 150
.text ; Assemble into program memory.
.retain ; Override ELF conditional linking
; and retain current section.
.retainrefs ; And retain any sections that have
; references to current section.
;-------------------------------------------------------------------------------
RESET mov.w #__STACK_END,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
;-------------------------------------------------------------------------------
; Main loop here
;-------------------------------------------------------------------------------
debugPin: .set BIT2
debugLED: .set BIT0
alarmLED: .set BIT6
button: .set BIT3
; Configure peripherals
; Set up alarm button
bic.b #button, &P1SEL
bic.b #button, &P1DIR
bis.b #button, &P1IE
bis.b #button, &P1IES
bis.b #button, &P1REN
bis.b #button, &P1OUT
bic.b &button, P1IFG
; Set up LEDS
bic.b #debugLED, &P1SEL
bis.b #debugLED, P1DIR
bic.b #alarmLED, &P1SEL
bis.b #alarmLED, P1DIR
call #ConfigSysClks ; Configure system clock
call #ConfigClockTimer ; Configure timer A1 to generate 1Hz frequency
;call #StartPWMTimer ; Configure timer A0 to start generating PWM
bis.w #GIE, SR
bis.b #debugPin, &P1DIR
loop: bis.w #LPM1+GIE, SR
; update time (Hr Min Sec AM/PM) routine starts here
add.w #1, &time ; Increment seconds count
cmp.w #60, &time ; Compare to full minute
jnz TimeCountFull
clr.w &time ; Clear the seconds count
add.w #1, &time+2 ; Increment the minutes count
cmp.w #60, &time+2 ; Check for full minutes
jnz TimeCountFull
clr.w &time+2 ; Clear the minutes count
add.w #1, time+4 ; Increment hours counts
cmp.w #13, &time+4 ; Compare AM/PM
jnz TimeCountFull
clr.w &time+4 ; Clear the hours count
xor.w #BIT0, &M ; Toggle AM/PM
; -----------------------------------
TimeCountFull:
jmp loop
ConfigSysClks:
clr.b &DCOCTL
mov.b &CALBC1_1MHZ, &BCSCTL1 ; Config MCLK to calibrated 1MHz
mov.b &CALDCO_1MHZ, &DCOCTL
mov.b #DIVS_3, &BCSCTL2 ; Config SMCLK to 1/8MHz
ret
ConfigClockTimer:
mov.w #ID_3+MC_1+TASSEL_2+TACLR, &TA1CTL ; Config Timer Clock to SMCLK/8MHz and count upto CCR0
mov.w #15624, &TA1CCR0 ; Generate a ~1Hz clock.
mov.w #CCIE, &TA1CCTL0 ; Enable CCR0 Interrupt
ret
StartPWMTimer:
mov.w &samplingPr, &TA0CCR0 ; Update PWM sampling period
mov.w &lowerDC, &TA0CCR1 ; Set lower threshold of PWM duty cycle.
; TA0CCR1 cannot start with 0!
bis.b #alarmLED, &P1DIR ; Configure P1.6 to be used as TA0CCR1 output
bis.b #alarmLED, &P1SEL
bic.b #alarmLED, &P1SEL2
bic.b #alarmLED, &P1OUT
mov.w #OUTMOD_7, &TA0CCTL1 ; Use reset/set mode to generate PWM
mov.w #MC_1 + TASSEL_2 + TACLR, &TA0CTL ; Config Timer Clock to SMCLK.
; Start Timer in up mode to count up to CCR0
ret
StopPWMTimer:
bic.b #alarmLED, &P1SEL ; disconnect debug LED from timerA0 CCR0
mov.w #MC_0 + TACLR, &TA0CTL ; clear timerA0 config for PWM
clr.w &TA0CCTL0
clr.w &TA0CCR0
clr.w &TA0CCR1
ret
StartAlarmTimer:
bis.b #BIT0, &P1SEL
bic.b #BIT0, &P1SEL2
bic.b #BIT0, &P1DIR
mov.w #CCIE, &TA0CCTL0
mov.w #ID_0+MC_1+TASSEL_0+TACLR, &TA0CTL
ret
FindTimeTillAlarm:
mov.w &time, R8 ; Move seconds into arbitrary register
push.w &time+2
call #MultBy60
pop.w R7
add.w R7, R8
push.w &time+4
call #MultBy60
call #MultBy60
pop.w R7
add.w R7, R8
mov.w &alarm, R9
push.w &alarm+2
call #MultBy60
pop.w R7
add.w R7, R9
push.w &alarm+4
call #MultBy60
call #MultBy60
pop.w R7
add.w R7, R9
sub.w R8, R9
mov.w R9, &timeTillAlarm
ret
;------------------------------- subroutine MultBy60 ---------------------------
;
; function:
; accept 1 parameter via stack.
; return result of multiplying the parameter with 60 via stack
;
; prerequisit:
; the parameter is passed to the routine via stack
; before calling this subroutine, push the parameter
; to be multiplied onto the stack
;
; postrequisit:
; upon return from subroutine, the result of
; multiplication will be stored at the top of the stack,
; replacing the original parameter passed to this routine.
;
;--------------------------------------------------------------------------------
MultBy60:
push.w R12
mov.w 4(SP), R12
rla R12
rla R12
rla R12
rla R12
rla R12
rla R12
sub.w 4(SP), R12
sub.w 4(SP), R12
sub.w 4(SP), R12
sub.w 4(SP), R12
mov.w R12, 4(SP)
pop.w R12
ret
;----------------- End of subroutine MultBy60 subroutine -----------------------
;-------------------------------------------------------------------------------
; Interrupt Service Routines
;-------------------------------------------------------------------------------
; Port1 ISR
SetAlarmISR:
xor.b #BIT0, &alarmOn ; Toggles on alarm
xor.b #debugLED, &P1OUT
cmp.w #0, &alarmOn ; Check if alarm is on
jeq AlarmOff
call #FindTimeTillAlarm
call #StartAlarmTimer
mov.w &timeTillAlarm, &TA0CCR0
jmp End
AlarmOff:
bic.b #alarmLED, &P1OUT
call #StopPWMTimer
End:
bic.b #button, &P1IFG
reti
; TA1CCR0 ISR
ClockTickTimerISR:
xor.b #BIT1, &P1OUT
; Update PWM duty cycle
cmp.w &TA0CCR1, &upperDC ; update PWM duty cycle
jl endPWM
add.w &step, &TACCR1
bic.b #LPM1, 0(SP) ;
xor.b #debugPin, &P1OUT ; Toggle debug pin
reti
endPWM:
call #StopPWMTimer
bis.b #alarmLED, &P1OUT ; keep the alarm LED on
reti
; TA0CCR0 ISR
DownCntISR:
; Downcount to alarm time routine starts here
mov.w #timeTillAlarm, &TA0CCR0
call #StartPWMTimer
; -------------------------------------------
reti
;-------------------------------------------------------------------------------
; Stack Pointer definition
;-------------------------------------------------------------------------------
.global __STACK_END
.sect .stack
;-------------------------------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------------------------------
.sect ".int02" ; Port1 ISR
.short SetAlarmISR
.sect ".int09" ; TimerA0 CCR0 ISR
.short DownCntISR
.sect ".int13" ; TimerA1 CCRO ISR
.short ClockTickTimerISR
.sect ".reset" ; MSP430 RESET Vector
.short RESET