3 static volatile unsigned int ADC_Result;
4 static volatile unsigned int irq_events = 0;
5 enum {ev_btn1 = 0, ev_btn2, ev_pir1, ev_pir2, ev_tmr, ev_adc, ev_MAX};
11 unsigned int Time_Count = 0;
12 unsigned int Time_Left = 5;
14 WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
16 P1DIR |= BIT0|BIT1|BIT2; // Set LEDs & PWM to output direction
17 P1OUT &= ~(BIT0|BIT1); // P1.0&1 LEDs off
18 P1SEL1 |= BIT2; // P1.2 PWM out
21 P2DIR &= ~(BIT3|BIT7); // Buttons
22 P2OUT |= BIT3|BIT7; // Pull up
23 P2REN |= BIT3|BIT7; // Enable pull-up
24 P2IES |= BIT3|BIT7; // INT on Hi->Lo edge
25 P2IE |= BIT3|BIT7; // INT enable
27 P2DIR &= ~(BIT2|BIT5); // PIR Sensors
28 P2OUT &= ~(BIT2|BIT5); // Pull down
29 P2REN |= BIT2|BIT5; // Enable pull-down
30 P2IES &= ~(BIT2|BIT5); // INT on Lo->Hi edge
31 P2IE |= BIT2|BIT5; // INT enable
33 // Configure ADC A7 pin
37 ADCCTL0 |= ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks
38 ADCCTL1 |= ADCSHP; // ADCCLK = MODOSC; sampling timer
39 ADCCTL2 |= ADCRES; // 10-bit conversion results
40 ADCMCTL0 |= ADCINCH_7; // A7 ADC input select; Vref=AVCC
41 ADCIE |= ADCIE0; // Enable ADC conv complete interrupt
43 // Configure timer A0 for PWM
44 TA0CCR0 = 10000-1; // PWM Period
45 TA0CCTL2 = OUTMOD_7; // CCR2 reset/set
46 TA0CCR2 = 500; // CCR2 PWM duty cycle
47 TA0CTL = TASSEL__SMCLK | MC__UP | TACLR; // SMCLK, up mode, clear TAR
49 //Configure timer A1 for counting time
50 TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE; // SMCLK, no divider, continuous mode
52 // Disable the GPIO power-on default high-impedance mode to activate
53 // previously configured port settings
60 _disable_interrupts();
65 // Button 2 or PIR events initiate light measurement and tuns on green led
66 if (events & (1<<ev_btn2|1<<ev_pir1|1<<ev_pir2)) {
71 ADCCTL0 |= ADCENC | ADCSC; // Sampling and conversion start
72 P1OUT |= BIT1; // Set P1.1 LED on
75 // End of light measurement, set new Duty_Cycle and zero increment and tuns off green led
76 if (events & 1<<ev_adc) {
77 P1OUT &= ~BIT1; // Clear P1.1 LED off
86 // Button 1 sets non-zero increment (and toggles it)
87 if (events & 1<<ev_btn1) {
88 if (Duty_Cycle > 5000) {
97 // Timer event (100 ms) changed duty cycle and flashes red led
98 if (events & 1<<ev_tmr) {
99 if (Time_Count++ > 10) {
104 else if (Duty_Cycle > 1)
109 else if (Increment > 0)
111 else if (Increment < 0)
113 if (Duty_Cycle < 1) {
117 if (Duty_Cycle > (10000-1)) {
118 Duty_Cycle = 10000-1;
121 TA0CCR2 = Duty_Cycle;
123 __bis_SR_register(LPM0_bits | GIE);
126 return 0; /* not reached */
129 // TIMER interrupt routine
130 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
131 #pragma vector = TIMER1_A1_VECTOR
132 __interrupt void Timer_A (void)
133 #elif defined(__GNUC__)
134 void __attribute__ ((interrupt(TIMER1_A1_VECTOR))) Timer_A (void)
136 #error Compiler not supported!
139 switch(__even_in_range(TA1IV,TA1IV_TAIFG))
142 break; // No interrupt
144 break; // CCR1 not used
146 break; // CCR2 not used
148 irq_events |= 1<<ev_tmr;
149 __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0
154 //if (Time_Count++ > 1000) {
156 // __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0
160 // ADC interrupt service routine
161 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
162 #pragma vector=ADC_VECTOR
163 __interrupt void ADC_ISR(void)
164 #elif defined(__GNUC__)
165 void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
167 #error Compiler not supported!
170 switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
176 case ADCIV_ADCTOVIFG:
185 ADC_Result = ADCMEM0;
186 irq_events |= 1<<ev_adc;
187 __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0
194 // GPIO interrupt service routine
195 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
196 #pragma vector=PORT2_VECTOR
197 __interrupt void Port_2(void)
198 #elif defined(__GNUC__)
199 void __attribute__ ((interrupt(PORT2_VECTOR))) Port_2 (void)
201 #error Compiler not supported!
205 irq_events |= 1<<ev_btn1;
206 P2IFG &= ~BIT3; // Clear P1.3 IFG
209 irq_events |= 1<<ev_btn2;
210 P2IFG &= ~BIT7; // Clear P1.3 IFG
213 irq_events |= 1<<ev_pir1;
214 P2IFG &= ~BIT2; // Clear P1.4 IFG
217 irq_events |= 1<<ev_pir2;
218 P2IFG &= ~BIT5; // Clear P1.7 IFG
220 __bic_SR_register_on_exit(LPM3_bits); // Exit LPM3