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 P1.0&1&2/LEDs 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
32 P2IFG = 0; // ??? Needed?
34 // Configure ADC A7 pin
38 ADCCTL0 |= ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks
39 ADCCTL1 |= ADCSHP; // ADCCLK = MODOSC; sampling timer
40 ADCCTL2 |= ADCRES; // 10-bit conversion results
41 ADCMCTL0 |= ADCINCH_7; // A7 ADC input select; Vref=AVCC
42 ADCIE |= ADCIE0; // Enable ADC conv complete interrupt
44 // Configure timer A0 for PWM
45 TA0CCR0 = 10000-1; // PWM Period
46 TA0CCTL2 = OUTMOD_7; // CCR2 reset/set
47 TA0CCR2 = 500; // CCR2 PWM duty cycle
48 TA0CTL = TASSEL__SMCLK | MC__UP | TACLR; // SMCLK, up mode, clear TAR
50 //Configure timer A1 for counting time
51 TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE; // SMCLK, no divider, continuous mode
53 // Disable the GPIO power-on default high-impedance mode to activate
54 // previously configured port settings
66 // Button 2 or PIR events initiate light measurement and tuns on green led
67 if (events & (1<<ev_btn2|1<<ev_pir1|1<<ev_pir2)) {
72 ADCCTL0 |= ADCENC | ADCSC; // Sampling and conversion start
73 P1OUT |= BIT1; // Set P1.1 LED on
76 // End of light measurement, set new Duty_Cycle and zero increment and tuns off green led
77 if (events & 1<<ev_adc) {
78 P1OUT &= ~BIT1; // Clear P1.1 LED off
87 // Button 1 sets non-zero increment (and toggles it)
88 if (events & 1<<ev_btn1) {
89 if (Duty_Cycle > 5000) {
98 // Timer event (100 ms) changed duty cycle and flashes red led
99 if (events & 1<<ev_tmr) {
100 if (Time_Count++ > 10) {
105 else if (Duty_Cycle > 1)
110 else if (Increment > 0)
112 else if (Increment < 0)
114 if (Duty_Cycle < 1) {
118 if (Duty_Cycle > (10000-1)) {
119 Duty_Cycle = 10000-1;
122 TA0CCR2 = Duty_Cycle;
124 __bis_SR_register(LPM0_bits | GIE);
127 retun 0; /* not reached */
130 // TIMER interrupt routine
131 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
132 #pragma vector = TIMER1_A1_VECTOR
133 __interrupt void Timer_A (void)
134 #elif defined(__GNUC__)
135 void __attribute__ ((interrupt(TIMER1_A1_VECTOR))) Timer_A (void)
137 #error Compiler not supported!
140 switch(__even_in_range(TA1IV,TA1IV_TAIFG))
143 break; // No interrupt
145 break; // CCR1 not used
147 break; // CCR2 not used
149 irq_events |= 1<<ev_tmr;
150 __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0
155 //if (Time_Count++ > 1000) {
157 // __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0
161 // ADC interrupt service routine
162 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
163 #pragma vector=ADC_VECTOR
164 __interrupt void ADC_ISR(void)
165 #elif defined(__GNUC__)
166 void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
168 #error Compiler not supported!
171 switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
177 case ADCIV_ADCTOVIFG:
186 ADC_Result = ADCMEM0;
187 irq_events |= 1<<ev_adc;
188 __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0
195 // GPIO interrupt service routine
196 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
197 #pragma vector=PORT2_VECTOR
198 __interrupt void Port_2(void)
199 #elif defined(__GNUC__)
200 void __attribute__ ((interrupt(PORT2_VECTOR))) Port_2 (void)
202 #error Compiler not supported!
206 irq_events |= 1<<ev_btn1;
207 P2IFG &= ~BIT3; // Clear P1.3 IFG
210 irq_events |= 1<<ev_btn2;
211 P2IFG &= ~BIT7; // Clear P1.3 IFG
214 irq_events |= 1<<ev_pir1;
215 P2IFG &= ~BIT2; // Clear P1.4 IFG
218 irq_events |= 1<<ev_pir2;
219 P2IFG &= ~BIT5; // Clear P1.7 IFG
221 __bic_SR_register_on_exit(LPM3_bits); // Exit LPM3