]> www.average.org Git - sensor-light.git/blob - msp430/main.c
make PWM duty cycle change more smoothly
[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 static int expon2(int duty)
28 {
29         int shift = duty>>1;
30         int comp = 1<<shift;
31         int extra = (duty & 1) ? comp>>1 : 0;
32         return (duty ? comp|extra : 0);
33 }
34
35 int main(void)
36 {
37         int Duty_Cycle = 0;
38         int Increment = 1;
39         unsigned int Time_Count = 0;
40         unsigned int Time_Left = 5;
41         unsigned int Time_Indicate = 2;
42
43         WDTCTL = WDTPW | WDTHOLD;       // stop watchdog timer
44         // Configure GPIO Out
45         P1DIR |= BIT_RL|BIT_GL|BIT2;    // Set LEDs & PWM to output direction
46         P1OUT &= ~(BIT_RL|BIT_GL);      // LEDs off
47 #ifdef P1SEL1
48         P1SEL1 |= BIT2;                 // PWM out
49 #else
50         P1SEL |= BIT2;                  // PWM out
51 #endif
52
53         // Configure GPIO In
54         PBTN(DIR) &= ~(BIT_BTN|BIT_BTN2);       // Buttons
55         PBTN(OUT) |= BIT_BTN|BIT_BTN2;          // Pull up
56         PBTN(REN) |= BIT_BTN|BIT_BTN2;          // Enable pull-up
57         PBTN(IES) |= BIT_BTN|BIT_BTN2;          // INT on Hi->Lo edge
58         PBTN(IE)  |= BIT_BTN|BIT_BTN2;          // INT enable
59
60         P2DIR &= ~(BIT4|BIT5);          // PIR Sensors
61         P2OUT &= ~(BIT4|BIT5);          // Pull down
62         P2REN |= BIT4|BIT5;             // Enable pull-down
63         P2IES &= ~(BIT4|BIT5);          // INT on Lo->Hi edge
64         P2IE  |= BIT4|BIT5;             // INT enable
65
66         // Configure ADC10
67
68 #ifdef ADCPCTL4 /* Newer model */
69         SYSCFG2 |= ADCPCTL4|ADCPCTL5;   // disconnect pin 4 and 5 from GPIO
70         ADCCTL0 |= ADCSHT_2 | ADCON;    // ADCON, S&H=16 ADC clks
71         ADCCTL1 |= ADCSHP;              // ADCCLK = MODOSC; sampling timer
72         ADCCTL2 |= ADCRES;              // 10-bit conversion results
73         ADCMCTL0 |= ADCINCH_4;          // A4 ADC input select; Vref=AVCC
74         ADCIE |= ADCIE0;                // Enable ADC conv complete interrupt
75         // channel 5 is unused, reserved for measuring current
76 #else
77         ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADCON, S&H=16 ADC clks
78         ADC10CTL1 = INCH_4;             // A4 ADC input select
79         // channel 5 is unused, reserved for measuring current
80 #endif
81
82         // Timer and PWM
83
84 #ifndef TASSEL__SMCLK
85 # define TASSEL__SMCLK TASSEL_2
86 # define MC__UP MC_1
87 # define MC__CONTINUOUS MC_2
88 # define TA0CCR2 TA0CCR1
89 # define TA0CCTL2 TA0CCTL1
90 #endif
91
92         // Configure timer A0 for PWM
93         TA0CCR0 = 1 << PWM_ORDER;       // PWM Period 2^10 ca. 1 kHz
94         TA0CCR2 = 0;                    // CCR1 PWM duty cycle
95         TA0CCTL2 = OUTMOD_7;            // CCR1 reset/set
96         TA0CTL = TASSEL__SMCLK | MC__UP | TACLR;// SMCLK, up mode
97                 // SMCLK, no divider, up mode, no interrupt, clear TAR
98
99         //Configure timer A1 for counting time
100         TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE;
101                 // SMCLK, no divider, continuous mode, interrupt enable
102
103 #ifdef LOCKLPM5
104         // Disable the GPIO power-on default high-impedance mode to activate
105         // previously configured port settings
106         PM5CTL0 &= ~LOCKLPM5;
107 #endif
108
109         while(1)
110         {
111                 unsigned int events;
112
113                 _disable_interrupts();
114                 events = irq_events;
115                 irq_events = 0;
116                 _enable_interrupts();
117
118                 // Button 2 or PIR events initiate light measurement
119                 // and tuns on green or red led
120                 if (events & (1<<ev_btn2|1<<ev_pir1|1<<ev_pir2)) {
121                         if (events & 1<<ev_pir1)
122                                 P1OUT |= BIT_GL;        // Set green LED on
123                         if (events & 1<<ev_pir2)
124                                 P1OUT |= BIT_RL;        // Set red LED on
125                         // Sampling and conversion start
126 #ifdef ADCENC
127                         ADCCTL0 |= ADCENC | ADCSC;
128 #else
129                         ADC10CTL0 |= ENC + ADC10SC;
130 #endif
131                 }
132
133                 // End of light measurement. Set new Duty_Cycle,
134                 // zero increment and turn off green led
135                 if (events & 1<<ev_adc) {
136                         P1OUT ^= (BIT_GL|BIT_RL); // Flip green and red LEDs
137                         Time_Indicate = 5;
138                         if (ADC_Result < LIGHT_THRESHOLD)
139                                 continue;
140                         Time_Left = TIME_ON;
141                         Increment = 1;
142                 }
143
144                 // Button 1 sets non-zero increment (and toggles it)
145                 if (events & 1<<ev_btn1) {
146                         P1OUT |= (BIT_GL|BIT_RL); // Set green and red LEDs on
147                         Time_Indicate = 5;
148                         if (Duty_Cycle > PWM_HALF) {
149                                 Time_Left = 0;
150                                 Increment = -1;
151                         } else {
152                                 Time_Left = TIME_ON;
153                                 Increment = 1;
154                         }
155                 }
156
157                 // Timer event (100 ms) changed duty cycle and flashes red led
158                 if (events & 1<<ev_tmr) {
159                         if (Time_Indicate) {
160                                 Time_Indicate--;
161                                 if (!Time_Indicate)
162                                         P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off
163                         }
164                         if (Time_Count++ > 10) {
165                                 Time_Count = 0;
166                                 if (Time_Left)
167                                         Time_Left--;
168                                 else if (Duty_Cycle > 1)
169                                         Increment = -1;
170                         }
171                         if (Increment > 0) {
172                                 if (++Duty_Cycle >= (PWM_ORDER<<1)) {
173                                         Duty_Cycle = PWM_ORDER<<1;
174                                         Increment = 0;
175                                 }
176                         } else if (Increment < 0) {
177                                 if (--Duty_Cycle < 1) {
178                                         Duty_Cycle = 0;
179                                         Increment = 0;
180                                 }
181                         } else
182                                 continue;
183
184                         TA0CCR2 = expon2(Duty_Cycle);
185                 }
186                 __bis_SR_register(LPM0_bits | GIE);
187                 __no_operation();
188         }
189         return 0; /* not reached */
190 }
191
192 // TIMER interrupt routine
193 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
194 #pragma vector = TIMER1_A1_VECTOR
195 __interrupt void Timer_A (void)
196 #elif defined(__GNUC__)
197 void __attribute__ ((interrupt(TIMER1_A1_VECTOR))) Timer_A (void)
198 #else
199 #error Compiler not supported!
200 #endif
201 {
202         switch(__even_in_range(TA1IV,TA1IV_TAIFG))
203         {
204                 case TA1IV_NONE:
205                         break;  // No interrupt
206                 case TA1IV_TACCR1:
207                         break;  // CCR1 not used
208                 case TA1IV_TACCR2:
209                         break;  // CCR2 not used
210                 case TA1IV_TAIFG:
211                         irq_events |= 1<<ev_tmr;
212                         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
213                         break;
214                 default:
215                         break;
216         }
217 }
218
219 #ifndef ADC_VECTOR
220 # define ADCMEM0 ADC10MEM
221 # define ADC_VECTOR ADC10_VECTOR
222 #endif
223
224 // ADC interrupt service routine
225 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
226 #pragma vector=ADC_VECTOR
227 __interrupt void ADC_ISR(void)
228 #elif defined(__GNUC__)
229 void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
230 #else
231 #error Compiler not supported!
232 #endif
233 {
234 #ifdef ADCIV_NONE
235         switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
236         {
237                 case ADCIV_NONE:
238                         break;
239                 case ADCIV_ADCOVIFG:
240                         break;
241                 case ADCIV_ADCTOVIFG:
242                         break;
243                 case ADCIV_ADCHIIFG:
244                         break;
245                 case ADCIV_ADCLOIFG:
246                         break;
247                 case ADCIV_ADCINIFG:
248                         break;
249                 case ADCIV_ADCIFG:
250 #endif
251                         ADC_Result = ADCMEM0;
252                         irq_events |= 1<<ev_adc;
253                         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
254 #ifdef ADCIV_NONE
255                         break;
256                 default:
257                         break;
258         }
259 #endif
260 }
261
262 // GPIO interrupt service routine
263 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
264 #pragma vector=PORT2_VECTOR
265 __interrupt void Port_2(void)
266 #elif defined(__GNUC__)
267 void __attribute__ ((interrupt(PORT2_VECTOR))) Port_2 (void)
268 #else
269 #error Compiler not supported!
270 #endif
271 {
272         if (P2IFG & BIT4) {
273                 irq_events |= 1<<ev_pir1;
274                 P2IFG &= ~BIT4; // Clear P2.4 IFG
275         }
276         if (P2IFG & BIT5) {
277                 irq_events |= 1<<ev_pir2;
278                 P2IFG &= ~BIT5; // Clear P2.5 IFG
279         }
280 #if (PBTN() == P1)
281         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
282 }
283 // GPIO interrupt service routine
284 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
285 #pragma vector=PORT1_VECTOR
286 __interrupt void Port_1(void)
287 #elif defined(__GNUC__)
288 void __attribute__ ((interrupt(PORT1_VECTOR))) Port_1 (void)
289 #else
290 #error Compiler not supported!
291 #endif
292 {
293 #endif /* (PBTN() == P1) */
294         if (PBTN(IFG) & BIT_BTN) {
295                 irq_events |= 1<<ev_btn1;
296                 PBTN(IFG) &= ~BIT_BTN;  // Clear button IFG
297         }
298 #ifdef HAVE_BTN2
299         if (PBTN(IFG) & BIT_BTN2) {
300                 irq_events |= 1<<ev_btn2;
301                 PBTN(IFG) &= ~BIT_BTN2; // Clear button 2 IFG
302         }
303 #endif
304         __bic_SR_register_on_exit(LPM0_bits);   // Wake up
305 }