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};
9 #define LIGHT_THRESHOLD 200
12 #ifdef ADCSC /* Let us hope that this is a "new" model */
15 # define PBTN(x) P2##x
18 # define BIT_BTN2 BIT7
22 # define PBTN(x) P1##x
31 unsigned int Time_Count = 0;
32 unsigned int Time_Left = 5;
34 WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
36 P1DIR |= BIT_RL|BIT_GL|BIT2; // Set LEDs & PWM to output direction
37 P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off
39 P1SEL1 |= BIT2; // PWM out
41 P1SEL |= BIT2; // PWM out
45 PBTN(DIR) &= ~(BIT_BTN|BIT_BTN2); // Buttons
46 PBTN(OUT) |= BIT_BTN|BIT_BTN2; // Pull up
47 PBTN(REN) |= BIT_BTN|BIT_BTN2; // Enable pull-up
48 PBTN(IES) |= BIT_BTN|BIT_BTN2; // INT on Hi->Lo edge
49 PBTN(IE) |= BIT_BTN|BIT_BTN2; // INT enable
51 P2DIR &= ~(BIT4|BIT5); // PIR Sensors
52 P2OUT &= ~(BIT4|BIT5); // Pull down
53 P2REN |= BIT4|BIT5; // Enable pull-down
54 P2IES &= ~(BIT4|BIT5); // INT on Lo->Hi edge
55 P2IE |= BIT4|BIT5; // INT enable
59 #ifdef ADCPCTL4 /* Newer model */
60 SYSCFG2 |= ADCPCTL4|ADCPCTL5; // disconnect pin 4 and 5 from GPIO
61 ADCCTL0 |= ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks
62 ADCCTL1 |= ADCSHP; // ADCCLK = MODOSC; sampling timer
63 ADCCTL2 |= ADCRES; // 10-bit conversion results
64 ADCMCTL0 |= ADCINCH_4; // A4 ADC input select; Vref=AVCC
65 ADCIE |= ADCIE0; // Enable ADC conv complete interrupt
66 // channel 5 is unused, reserved for measuring current
68 ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADCON, S&H=16 ADC clks
69 ADC10CTL1 = INCH_4; // A4 ADC input select
70 // channel 5 is unused, reserved for measuring current
76 # define TASSEL__SMCLK TASSEL_2
78 # define MC__CONTINUOUS MC_2
79 # define TA0CCR2 TA0CCR1
80 # define TA0CCTL2 TA0CCTL1
83 // Configure timer A0 for PWM
84 TA0CCR0 = 1 << PWM_ORDER; // PWM Period 2^10 ca. 1 kHz
85 TA0CCR2 = 0; // CCR1 PWM duty cycle
86 TA0CCTL2 = OUTMOD_7; // CCR1 reset/set
87 TA0CTL = TASSEL__SMCLK | MC__UP | TACLR;// SMCLK, up mode
88 // SMCLK, no divider, up mode, no interrupt, clear TAR
90 //Configure timer A1 for counting time
91 TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE;
92 // SMCLK, no divider, continuous mode, interrupt enable
95 // Disable the GPIO power-on default high-impedance mode to activate
96 // previously configured port settings
104 _disable_interrupts();
107 _enable_interrupts();
109 // Button 2 or PIR events initiate light measurement
110 // and tuns on green led
111 if (events & (1<<ev_btn2|1<<ev_pir1|1<<ev_pir2)) {
112 if (Duty_Cycle > 0) {
116 P1OUT |= BIT_GL; // Set green LED on
117 // Sampling and conversion start
119 ADCCTL0 |= ADCENC | ADCSC;
121 ADC10CTL0 |= ENC + ADC10SC;
125 // End of light measurement. Set new Duty_Cycle,
126 // zero increment and turn off green led
127 if (events & 1<<ev_adc) {
128 P1OUT &= ~BIT_GL; // Clear green LED off
131 if (ADC_Result < LIGHT_THRESHOLD)
137 // Button 1 sets non-zero increment (and toggles it)
138 if (events & 1<<ev_btn1) {
139 if (Duty_Cycle > PWM_HALF) {
148 // Timer event (100 ms) changed duty cycle and flashes red led
149 if (events & 1<<ev_tmr) {
150 if (Time_Count++ > 20) {
152 P1OUT |= BIT_RL; // red LED on
155 else if (Duty_Cycle > 1)
157 } else if (Time_Count == 1)
158 P1OUT &= ~BIT_RL; // red LED off
160 if (++Duty_Cycle >= PWM_ORDER) {
161 Duty_Cycle = PWM_ORDER;
164 } else if (Increment < 0) {
165 if (--Duty_Cycle < 1) {
171 TA0CCR2 = 1 << Duty_Cycle;
173 __bis_SR_register(LPM0_bits | GIE);
176 return 0; /* not reached */
179 // TIMER interrupt routine
180 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
181 #pragma vector = TIMER1_A1_VECTOR
182 __interrupt void Timer_A (void)
183 #elif defined(__GNUC__)
184 void __attribute__ ((interrupt(TIMER1_A1_VECTOR))) Timer_A (void)
186 #error Compiler not supported!
189 switch(__even_in_range(TA1IV,TA1IV_TAIFG))
192 break; // No interrupt
194 break; // CCR1 not used
196 break; // CCR2 not used
198 irq_events |= 1<<ev_tmr;
199 __bic_SR_register_on_exit(LPM0_bits); // Wake up
207 # define ADCMEM0 ADC10MEM
208 # define ADC_VECTOR ADC10_VECTOR
211 // ADC interrupt service routine
212 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
213 #pragma vector=ADC_VECTOR
214 __interrupt void ADC_ISR(void)
215 #elif defined(__GNUC__)
216 void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
218 #error Compiler not supported!
222 switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
228 case ADCIV_ADCTOVIFG:
238 ADC_Result = ADCMEM0;
239 irq_events |= 1<<ev_adc;
240 __bic_SR_register_on_exit(LPM0_bits); // Wake up
249 // GPIO interrupt service routine
250 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
251 #pragma vector=PORT2_VECTOR
252 __interrupt void Port_2(void)
253 #elif defined(__GNUC__)
254 void __attribute__ ((interrupt(PORT2_VECTOR))) Port_2 (void)
256 #error Compiler not supported!
260 irq_events |= 1<<ev_pir1;
261 P2IFG &= ~BIT4; // Clear P2.4 IFG
264 irq_events |= 1<<ev_pir2;
265 P2IFG &= ~BIT5; // Clear P2.5 IFG
268 __bic_SR_register_on_exit(LPM0_bits); // Wake up
270 // GPIO interrupt service routine
271 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
272 #pragma vector=PORT1_VECTOR
273 __interrupt void Port_1(void)
274 #elif defined(__GNUC__)
275 void __attribute__ ((interrupt(PORT1_VECTOR))) Port_1 (void)
277 #error Compiler not supported!
280 #endif /* (PBTN() == P1) */
281 if (PBTN(IFG) & BIT_BTN) {
282 irq_events |= 1<<ev_btn1;
283 PBTN(IFG) &= ~BIT_BTN; // Clear button IFG
286 if (PBTN(IFG) & BIT_BTN2) {
287 irq_events |= 1<<ev_btn2;
288 PBTN(IFG) &= ~BIT_BTN2; // Clear button 2 IFG
291 __bic_SR_register_on_exit(LPM0_bits); // Wake up