Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;-------------------------------------------------------------------------------
- ; MSP430 Assembler Code Template for use with TI Code Composer Studio
- ;
- ; Ben Spencer Luv Jain Meet Patel Darren Reed
- ;-------------------------------------------------------------------------------
- .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 208
- alarm: .word 0, 0, 0 ; Sec, Min, Hr
- alarmAMPM: .word 0 ; 0 - am, 1 - pm
- timeTillAlarm:
- .word 0, 0 ; 218
- tempTime: .word 0, 0, 0 ; Sec, Min, Hr 222
- timeSeconds:
- .word 0 ; 228
- ; PWM Parameters
- ; Modify these parameters to give you proper wake up light behavior
- lowerDC: .word 20
- upperDC: .word 4000
- step: .word 1
- samplingPr: .word 5000
- .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
- alarmLED: .set BIT6
- button: .set BIT3
- call #ConfigSysClks ; Configure system clock
- call #ConfigClockTimer ; Configure timer A1 to generate 1Hz frequency
- call #StartPWMTimer ; Configure timer A0 to start generating PWM
- bis.b #debugPin, &P1DIR
- bis.b #BIT0 + BIT6, &P1DIR ; Configure P1.0(LEDR) and P1.6 (LEDG) as output pin
- bic.b #BIT3, &P1DIR
- bis.b #BIT3, &P1REN
- bis.b #BIT3, &P1OUT
- loop:
- bis.w #LPM1+GIE, SR
- ; update time (Hr Min Sec AM/PM) routine starts here
- bit.w #BIT3, &P1IN ; Did somebody press the button?
- jnz SetupAlarm
- cmp.w #0, TA0CCR0 ; Is the Timer 0?
- jeq LoadTimer ; Attempt to load the rest of the time
- cmp.w #0, TA0CCR0 ; Is the Timer still 0?
- jeq LetTheDogsOut ; Set off the Alarm
- bis.b #BIT0 + BIT6, &P1OUT
- inc.w &0x200 ; Increment sec
- cmp.w #60, &0x200 ; Check if 60 sec
- jeq IncMin ; Increment minute if at 60 sec
- jmp NextSec
- EqualSec:
- call #IncMin
- NextSec:
- cmp.w #60, &0x202 ; Check if 60 min
- jeq IncHour ; Increment hour if at 60 min
- cmp.w #12, &0x204 ; Check if 12 hours
- jeq IncAMPM
- ; -----------------------------------
- jmp loop
- LetTheDogsOut: ; Trigger the alarm
- bis.b #BIT0 + BIT6, &P1OUT
- ret
- SetupAlarm:
- inc.w &0x208 ; Set up the alarm to begin properly
- cmp #2, &0x208
- jeq AlmOff
- cmp #1, &0x208
- jeq StartAlarm
- ret
- StartAlarm:
- call FindTimeTillAlarm ; Find time till alarm
- call LoadTimer ; Load seconds values into the alarm countdown
- call StartAlarmTimer ; Start the Alarm Timer
- ret
- AlmOff:
- mov.w #0, &0x208 ; Turn off the Alarm
- ret
- IncMin:
- inc.w &0x202 ; Increase minutes if 60 sec
- mov.w #0, &0x200 ; Reset sec to 0
- ret ; Return to main loop
- IncHour:
- inc.w &0x204 ; Increase the hour if minutes if 60 min
- mov.w #0, &0x202 ; Reset minutes to 0
- ret
- IncAMPM:
- inc.w &0x206 ; Move from AM to PM or PM to 2
- mov.w #0, &0x204 ; Set hours to 0
- cmp.w #2, &0x206 ; If we pass midnight
- jeq ToAM ; Reset to AM
- ret
- ToAM:
- mov.w #0, &0x206 ; If AMPM is 2, set to AM cuz 2 aint no thang baby gurl
- ret
- ; -------------------------------------
- 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 #7812, &TA1CCR0 ; Generate a ~1Hz clock.
- mov.w #CCIE, &TA1CCTL0 ; Enable CCR0 Interrupt
- ret
- StartPWMTimer:
- bic.w #CCIE, &TA0CCTL0
- 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:
- cmp.w #1, &0x216 ; Store the extra 12 hours for an am or pm cycle in the first timeTillAlarm
- jeq AddPm
- clr.w &0x224 ; Convert the alarm minutes into seconds and store in tempTime min
- add.w &0x212, &0x224
- push.w &0x224
- call #MultBy60
- pop.w &0x224
- clr.w &0x226 ; Convert the alarm hours into seconds and store in tempTime hours
- add.w &0x214, &0x226
- push.w &0x226
- call #MultBy60
- call #MultBy60
- pop.w &0x226
- add.w &0x210, &0x220 ; Add alarm seconds
- add.w &0x224, &0x220 ; Add alarm seconds
- add.w &0x226, &0x220 ; Add alarm seconds
- clr.w &0x224 ; Convert the current minutes into seconds and store in tempTime min
- add.w &0x202, &0x224
- push.w &0x224
- call #MultBy60
- pop.w &0x224
- clr.w &0x226 ; Convert the current hours into seconds and store in tempTime hours
- add.w &0x204, &0x226
- push.w &0x226
- call MultBy60
- call MultBy60
- pop.w &0x226
- sub.w &0x200, &0x220 ; Add alarm seconds
- sub.w &0x224, &0x220 ; Add alarm seconds
- sub.w &0x226, &0x220 ; Add alarm seconds
- sub.w #30, &0x220 ; The timer will reach 0 30 seconds before the alarm goes off so that the light may begin to fade
- ret
- AddPm:
- cmp.w #1, &0x206
- jeq Nope
- mov.w #43200, &0x218
- ret
- Nope:
- ret
- LoadTimer:
- cmp.w #0, &0x218
- jne LoadOne ;Load the first portion of seconds if not equal to zero
- cmp.w #0, TA0CCR0
- jeq LoadTwo ;Load the second portion of seconds if not equal to zero
- ret
- LoadOne:
- mov.w &0x218, TA0CCR0
- clr.w &0x218
- ret
- LoadTwo:
- mov.w &0x220, TA0CCR0
- clr.w &0x220
- 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:
- reti
- ; TA1CCR0 ISR
- ClockTickTimerISR:
- bic.w #LPM1, 0(SP)
- xor.b #debugPin, &P1OUT ; Toggle debug pin
- ; Update PWM duty cycle
- cmp.w &TA0CCR1, &upperDC ; update PWM duty cycle
- jl endPWM
- add.w &step, &TACCR1
- reti
- endPWM:
- call #StopPWMTimer
- bis.b #alarmLED, &P1OUT ; keep the alarm LED on
- reti
- ; TA0CCR0 ISR
- DownCntISR:
- ; Downcount to alarm time routine starts here
- ; -------------------------------------------
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement