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 600
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;
33 unsigned int Time_Indicate = 2;
35 WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
37 P1DIR |= BIT_RL|BIT_GL|BIT2; // Set LEDs & PWM to output direction
38 P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off
40 P1SEL1 |= BIT2; // PWM out
42 P1SEL |= BIT2; // PWM out
46 PBTN(DIR) &= ~(BIT_BTN|BIT_BTN2); // Buttons
47 PBTN(OUT) |= BIT_BTN|BIT_BTN2; // Pull up
48 PBTN(REN) |= BIT_BTN|BIT_BTN2; // Enable pull-up
49 PBTN(IES) |= BIT_BTN|BIT_BTN2; // INT on Hi->Lo edge
50 PBTN(IE) |= BIT_BTN|BIT_BTN2; // INT enable
52 P2DIR &= ~(BIT4|BIT5); // PIR Sensors
53 P2OUT &= ~(BIT4|BIT5); // Pull down
54 P2REN |= BIT4|BIT5; // Enable pull-down
55 P2IES &= ~(BIT4|BIT5); // INT on Lo->Hi edge
56 P2IE |= BIT4|BIT5; // INT enable
60 #ifdef ADCPCTL4 /* Newer model */
61 SYSCFG2 |= ADCPCTL4|ADCPCTL5; // disconnect pin 4 and 5 from GPIO
62 ADCCTL0 |= ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks
63 ADCCTL1 |= ADCSHP; // ADCCLK = MODOSC; sampling timer
64 ADCCTL2 |= ADCRES; // 10-bit conversion results
65 ADCMCTL0 |= ADCINCH_4; // A4 ADC input select; Vref=AVCC
66 ADCIE |= ADCIE0; // Enable ADC conv complete interrupt
67 // channel 5 is unused, reserved for measuring current
69 ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADCON, S&H=16 ADC clks
70 ADC10CTL1 = INCH_4; // A4 ADC input select
71 // channel 5 is unused, reserved for measuring current
77 # define TASSEL__SMCLK TASSEL_2
79 # define MC__CONTINUOUS MC_2
80 # define TA0CCR2 TA0CCR1
81 # define TA0CCTL2 TA0CCTL1
84 // Configure timer A0 for PWM
85 TA0CCR0 = 1 << PWM_ORDER; // PWM Period 2^10 ca. 1 kHz
86 TA0CCR2 = 0; // CCR1 PWM duty cycle
87 TA0CCTL2 = OUTMOD_7; // CCR1 reset/set
88 TA0CTL = TASSEL__SMCLK | MC__UP | TACLR;// SMCLK, up mode
89 // SMCLK, no divider, up mode, no interrupt, clear TAR
91 //Configure timer A1 for counting time
92 TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE;
93 // SMCLK, no divider, continuous mode, interrupt enable
96 // Disable the GPIO power-on default high-impedance mode to activate
97 // previously configured port settings
105 _disable_interrupts();
108 _enable_interrupts();
110 // Button 2 or PIR events initiate light measurement
111 // and tuns on green or red led
112 if (events & (1<<ev_btn2|1<<ev_pir1|1<<ev_pir2)) {
113 if (events & 1<<ev_pir1)
114 P1OUT |= BIT_GL; // Set green LED on
115 if (events & 1<<ev_pir2)
116 P1OUT |= BIT_RL; // Set red 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|BIT_RL); // Flip green and red LEDs
130 if (ADC_Result < LIGHT_THRESHOLD)
136 // Button 1 sets non-zero increment (and toggles it)
137 if (events & 1<<ev_btn1) {
138 P1OUT |= (BIT_GL|BIT_RL); // Set green and red LEDs on
140 if (Duty_Cycle > PWM_HALF) {
149 // Timer event (100 ms) changed duty cycle and flashes red led
150 if (events & 1<<ev_tmr) {
154 P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off
156 if (Time_Count++ > 20) {
160 else if (Duty_Cycle > 1)
164 if (++Duty_Cycle >= PWM_ORDER) {
165 Duty_Cycle = PWM_ORDER;
168 } else if (Increment < 0) {
169 if (--Duty_Cycle < 1) {
176 TA0CCR2 = 1 << (Duty_Cycle - 1);
180 __bis_SR_register(LPM0_bits | GIE);
183 return 0; /* not reached */
186 // TIMER interrupt routine
187 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
188 #pragma vector = TIMER1_A1_VECTOR
189 __interrupt void Timer_A (void)
190 #elif defined(__GNUC__)
191 void __attribute__ ((interrupt(TIMER1_A1_VECTOR))) Timer_A (void)
193 #error Compiler not supported!
196 switch(__even_in_range(TA1IV,TA1IV_TAIFG))
199 break; // No interrupt
201 break; // CCR1 not used
203 break; // CCR2 not used
205 irq_events |= 1<<ev_tmr;
206 __bic_SR_register_on_exit(LPM0_bits); // Wake up
214 # define ADCMEM0 ADC10MEM
215 # define ADC_VECTOR ADC10_VECTOR
218 // ADC interrupt service routine
219 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
220 #pragma vector=ADC_VECTOR
221 __interrupt void ADC_ISR(void)
222 #elif defined(__GNUC__)
223 void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
225 #error Compiler not supported!
229 switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
235 case ADCIV_ADCTOVIFG:
245 ADC_Result = ADCMEM0;
246 irq_events |= 1<<ev_adc;
247 __bic_SR_register_on_exit(LPM0_bits); // Wake up
256 // GPIO interrupt service routine
257 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
258 #pragma vector=PORT2_VECTOR
259 __interrupt void Port_2(void)
260 #elif defined(__GNUC__)
261 void __attribute__ ((interrupt(PORT2_VECTOR))) Port_2 (void)
263 #error Compiler not supported!
267 irq_events |= 1<<ev_pir1;
268 P2IFG &= ~BIT4; // Clear P2.4 IFG
271 irq_events |= 1<<ev_pir2;
272 P2IFG &= ~BIT5; // Clear P2.5 IFG
275 __bic_SR_register_on_exit(LPM0_bits); // Wake up
277 // GPIO interrupt service routine
278 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
279 #pragma vector=PORT1_VECTOR
280 __interrupt void Port_1(void)
281 #elif defined(__GNUC__)
282 void __attribute__ ((interrupt(PORT1_VECTOR))) Port_1 (void)
284 #error Compiler not supported!
287 #endif /* (PBTN() == P1) */
288 if (PBTN(IFG) & BIT_BTN) {
289 irq_events |= 1<<ev_btn1;
290 PBTN(IFG) &= ~BIT_BTN; // Clear button IFG
293 if (PBTN(IFG) & BIT_BTN2) {
294 irq_events |= 1<<ev_btn2;
295 PBTN(IFG) &= ~BIT_BTN2; // Clear button 2 IFG
298 __bic_SR_register_on_exit(LPM0_bits); // Wake up