MSP430/Backlight project

From Teknologisk videncenter
Jump to: navigation, search

The following code works but need to be documented properly.

; Purpose....: Variable backlight connected to MSP430G2553 
;              P1.6 (Green led on LaunchPad)
; Ver/Date/by: V1.0 / Date 15. mar 2014 / heth@mercantec.dk
; Principle..: Continouous variable PWM signal sent to led. Brightness can be 
;              adjusted by attaching a potentiometer to P1.1 variable from VCC
;              to GND
;              TimerA_0 repeatedly counts from 0 to TACCR0 which is set to 1023
;              ADC10 gets triggeded from TimerA_0 each time it reaches 1023
;              ADC10 Data Transfer module transfers the converted analog value 
;              from P1.1 to TimerA_0 TACCR1. 
;              When TimerA_0 counts from 0 and reaches the value in TACCR1 it 
;              sets the TACCR1 OUT which is connected to P1.6 and counts to
;              1023 and resets TACCR1 out.
; Caveats....: Needs to be commented in more detail!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;RAM setup
RAMSTART        EQU             0x0200          ; RAM start
RAMSIZE         EQU             512             ; Size of RAM in bytes 

;;;;;PORT 1 - Special Function Registers
;;;;;See controllers user guide. P1 and P2 for alternte functions
P1REN           EQU             0x0027          ; Resistor enable 
P1SEL2          EQU             0x0041          ; Port select 2
P1SEL           EQU             0x0026          ; Port select
P1IE            EQU             0x0025          ; Interrupt enable
P1IES           EQU             0x0024          ; Interrupt edge select
P1IFG           EQU             0x0023          ; Interrupt flag 
P1DIR           EQU             0x0022          ; Direction (0=in, 1 = OUT)
P1OUT           EQU             0x0021          ; As output
P1IN            EQU             0x0020          ; As input

;;;;;TimerA_0 SFR
TACTL           EQU             0x0160          ; Timer A Control
TAR             EQU             0x0170          ; Timer A counter
TACCTL0         EQU             0x0162          ; Timer A Capture/Compare control0
TACCR0          EQU             0x0172          ; Timer A capture/compare 0
TACCTL1         EQU             0x0164          ; Timer A Capture/Compare control0
TACCR1          EQU             0x0174          ; Timer A capture/compare 0

;;;;;ADC10 SFR
ADC10AE0        EQU             0x004A
ADC10AE1        EQU             0x004B
ADC10CTL0       EQU             0x01b0
ADC10CTL1       EQU             0x01b2
ADC10MEM        EQU             0x01b4
ADC10SA         EQU             0X01BC
ADC10DTC0       EQU             0x0048
ADC10DTC1       EQU             0x0049

;;;;; WATCH DOG TIMER - Special Function Registers
WDTCTL          EQU             0x0120          ; Watch dog timer

;;;;WATCH DOG TIMER REGISTER VALUES
WDTPW           EQU             0x5A00          ; Watch Dog Timer Password (5A)
WDTHOLD         EQU             0x0080          ; Hold timer. Timer stopped

		ORG		0xC000          ; Where to place code in FLASH 
;Startup code
Reset:		MOV.W	#WDTPW|WDTHOLD, &WDTCTL ; Stop Watch Dog Timer
                MOV.W   #(RAMSTART + RAMSIZE), R1  ; Set stack Pointer to top of RAM
                CALL    #TIMAINIT
                CALL    #ADC10INIT
                
Loop:           jmp     Loop                    ; Your application here !!!        
                
;;;;;Initialize Timer A
TIMAINIT:       ; TAC counts to 1023 because ADC converts from 0 to 1023
                MOV.W   #1023, &TACCR0

                ;;;;;;;TACCTL0 configuration
		;XXXX XXX0 1000 XXXX    ( X = dont care )
		;        | ||||-------> CCIE: 0 = Interrupt Disabled
                ;        | |||--------> OUTMODx: Output mode 100 = Toggle 
		;        |------------> CAP: Capture/compare mode. 0 = Compare
                MOV.W   #0000000010000000b, &TACCTL0
                
                ;;;;;;;TACTL configuration
		;XXXX XX10 1101 XX0X    ( X = dont care )
		;       || ||||   |---> TAIFG: 0 = Interrupt Disabled
		;       || ||||-------> MCx: Mode control. 01 = Up mode. Count to TACCR0
                ;       || ||---------> IDx: Input divider. 11 = divide clock by 8
                ;       ||------------> TASSELx: Clock source = SMCLK          
                MOV.W   #0000001011010000b, &TACTL ; Side 370 i family - Divide by 8

                MOV.W   #011010001000000b, &TACCTL1
                BIS.B   #01000000b, &P1DIR         ; Side 49 i data sheet
                BIS.B   #01000000b, &P1SEL
                BIC.B   #01000000b, &P1SEL2
                RET

ADC10INIT:      BIS.W   #0x10, &ADC10CTL0
                MOV.W   #0001100001110000b, &ADC10CTL0
                MOV.W   #0001100000100000b, &ADC10CTL1
                MOV.B   #0x2,&ADC10AE0
                MOV.B   #00000100b, &ADC10DTC0
                MOV.B   #1, &ADC10DTC1
                MOV.W   #ADC10SA, R6
                MOV.W   #TACCR1, 0(R6)
                BIS.W   #0x04, ADC10CTL1
                BIS.W   #0x02, &ADC10CTL0       ; ENC on (Enable Conversion)
                RET
                
                
;Reset Vector. When CPU resets it loads the 16 bit defined in address 0xFFFE
;to the CPU PC (Program Counter = R0) and starts executing code from that address
                ORG 	        0xFFFE
		DW		Reset
		END