]> www.average.org Git - sensor-light.git/blob - msp430/main.c
streamline logic, adjust ambient light value
[sensor-light.git] / msp430 / main.c
1 #include <msp430.h> 
2
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};
6
7 #define PWM_ORDER 10
8 #define PWM_HALF 5
9 #define LIGHT_THRESHOLD 600
10 #define TIME_ON 16
11         
12 #ifdef ADCSC /* Let us hope that this is a "new" model */
13 # define BIT_RL BIT0
14 # define BIT_GL BIT1
15 # define PBTN(x) P2##x
16 # define BIT_BTN BIT3
17 # define HAVE_BTN2
18 # define BIT_BTN2 BIT7
19 #else
20 # define BIT_RL BIT0
21 # define BIT_GL BIT6
22 # define PBTN(x) P1##x
23 # define BIT_BTN BIT3
24 # define BIT_BTN2 0
25 #endif
26
27 int main(void)
28 {
29         int Duty_Cycle = 0;
30         int Increment = 1;
31         unsigned int Time_Count = 0;
32         unsigned int Time_Left = 5;
33         unsigned int Time_Indicate = 2;
34
35         WDTCTL = WDTPW | WDTHOLD;       // stop watchdog timer
36         // Configure GPIO Out
37         P1DIR |= BIT_RL|BIT_GL|BIT2;    // Set LEDs & PWM to output direction
38         P1OUT &= ~(BIT_RL|BIT_GL);      // LEDs off
39 #ifdef P1SEL1
40         P1SEL1 |= BIT2;                 // PWM out
41 #else
42         P1SEL |= BIT2;                  // PWM out
43 #endif
44
45         // Configure GPIO In
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
51
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
57
58         // Configure ADC10
59
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
68 #else
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
72 #endif
73
74         // Timer and PWM
75
76 #ifndef TASSEL__SMCLK
77 # define TASSEL__SMCLK TASSEL_2
78 # define MC__UP MC_1
79 # define MC__CONTINUOUS MC_2
80 # define TA0CCR2 TA0CCR1
81 # define TA0CCTL2 TA0CCTL1
82 #endif
83
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
90
91         //Configure timer A1 for counting time
92         TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE;
93                 // SMCLK, no divider, continuous mode, interrupt enable
94
95 #ifdef LOCKLPM5
96         // Disable the GPIO power-on default high-impedance mode to activate
97         // previously configured port settings
98         PM5CTL0 &= ~LOCKLPM5;
99 #endif
100
101         while(1)
102         {
103                 unsigned int events;
104
105                 _disable_interrupts();
106                 events = irq_events;
107                 irq_events = 0;
108                 _enable_interrupts();
109
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
118 #ifdef ADCENC
119                         ADCCTL0 |= ADCENC | ADCSC;
120 #else
121                         ADC10CTL0 |= ENC + ADC10SC;
122 #endif
123                 }
124
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
129                         Time_Indicate = 5;
130                         if (ADC_Result < LIGHT_THRESHOLD)
131                                 continue;
132                         Time_Left = TIME_ON;
133                         Increment = 1;
134                 }
135
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
139                         Time_Indicate = 5;
140                         if (Duty_Cycle > PWM_HALF) {
141                                 Time_Left = 0;
142                                 Increment = -1;
143                         } else {
144                                 Time_Left = TIME_ON;
145                                 Increment = 1;
146                         }
147                 }
148
149                 // Timer event (100 ms) changed duty cycle and flashes red led
150                 if (events & 1<<ev_tmr) {
151                         if (Time_Indicate) {
152                                 Time_Indicate--;
153                                 if (!Time_Indicate)
154                                         P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off
155                         }
156                         if (Time_Count++ > 20) {
157                                 Time_Count = 0;
158                                 if (Time_Left)
159                                         Time_Left--;
160                                 else if (Duty_Cycle > 1)
161                                         Increment = -1;
162                         }
163                         if (Increment > 0) {
164                                 if (++Duty_Cycle >= PWM_ORDER) {
165                                         Duty_Cycle = PWM_ORDER;
166                                         Increment = 0;
167                                 }
168                         } else if (Increment < 0) {
169                                 if (--Duty_Cycle < 1) {
170                                         Duty_Cycle = 0;
171                                         Increment = 0;
172                                 }
173                         } else
174                                 continue;
175                         if (Duty_Cycle)
176                                 TA0CCR2 = 1 << (Duty_Cycle - 1);
177                         else
178                                 TA0CCR2 = 0;
179                 }
180                 __bis_SR_register(LPM0_bits | GIE);
181                 __no_operation();
182         }
183         return 0; /* not reached */
184 }
185
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)
192 #else
193 #error Compiler not supported!
194 #endif
195 {
196         switch(__even_in_range(TA1IV,TA1IV_TAIFG))
197         {
198                 case TA1IV_NONE:
199                         break;  // No interrupt
200                 case TA1IV_TACCR1:
201                         break;  // CCR1 not used
202                 case TA1IV_TACCR2:
203                         break;  // CCR2 not used
204                 case TA1IV_TAIFG:
205                         irq_events |= 1<<ev_tmr;
206                         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
207                         break;
208                 default:
209                         break;
210         }
211 }
212
213 #ifndef ADC_VECTOR
214 # define ADCMEM0 ADC10MEM
215 # define ADC_VECTOR ADC10_VECTOR
216 #endif
217
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)
224 #else
225 #error Compiler not supported!
226 #endif
227 {
228 #ifdef ADCIV_NONE
229         switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
230         {
231                 case ADCIV_NONE:
232                         break;
233                 case ADCIV_ADCOVIFG:
234                         break;
235                 case ADCIV_ADCTOVIFG:
236                         break;
237                 case ADCIV_ADCHIIFG:
238                         break;
239                 case ADCIV_ADCLOIFG:
240                         break;
241                 case ADCIV_ADCINIFG:
242                         break;
243                 case ADCIV_ADCIFG:
244 #endif
245                         ADC_Result = ADCMEM0;
246                         irq_events |= 1<<ev_adc;
247                         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
248 #ifdef ADCIV_NONE
249                         break;
250                 default:
251                         break;
252         }
253 #endif
254 }
255
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)
262 #else
263 #error Compiler not supported!
264 #endif
265 {
266         if (P2IFG & BIT4) {
267                 irq_events |= 1<<ev_pir1;
268                 P2IFG &= ~BIT4; // Clear P2.4 IFG
269         }
270         if (P2IFG & BIT5) {
271                 irq_events |= 1<<ev_pir2;
272                 P2IFG &= ~BIT5; // Clear P2.5 IFG
273         }
274 #if (PBTN() == P1)
275         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
276 }
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)
283 #else
284 #error Compiler not supported!
285 #endif
286 {
287 #endif /* (PBTN() == P1) */
288         if (PBTN(IFG) & BIT_BTN) {
289                 irq_events |= 1<<ev_btn1;
290                 PBTN(IFG) &= ~BIT_BTN;  // Clear button IFG
291         }
292 #ifdef HAVE_BTN2
293         if (PBTN(IFG) & BIT_BTN2) {
294                 irq_events |= 1<<ev_btn2;
295                 PBTN(IFG) &= ~BIT_BTN2; // Clear button 2 IFG
296         }
297 #endif
298         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
299 }