]> www.average.org Git - sensor-light.git/blobdiff - msp430/main.c
make PWM duty cycle change more smoothly
[sensor-light.git] / msp430 / main.c
index 8df6210c9836f6c8f835315b20666458322d8bfa..6b075ad91d6b9753f86cb258216d88227842d174 100644 (file)
@@ -6,7 +6,7 @@ enum {ev_btn1 = 0, ev_btn2, ev_pir1, ev_pir2, ev_tmr, ev_adc, ev_MAX};
 
 #define PWM_ORDER 10
 #define PWM_HALF 5
-#define LIGHT_THRESHOLD 200
+#define LIGHT_THRESHOLD 600
 #define TIME_ON 16
        
 #ifdef ADCSC /* Let us hope that this is a "new" model */
@@ -24,12 +24,21 @@ enum {ev_btn1 = 0, ev_btn2, ev_pir1, ev_pir2, ev_tmr, ev_adc, ev_MAX};
 # define BIT_BTN2 0
 #endif
 
+static int expon2(int duty)
+{
+       int shift = duty>>1;
+       int comp = 1<<shift;
+       int extra = (duty & 1) ? comp>>1 : 0;
+       return (duty ? comp|extra : 0);
+}
+
 int main(void)
 {
        int Duty_Cycle = 0;
        int Increment = 1;
        unsigned int Time_Count = 0;
        unsigned int Time_Left = 5;
+       unsigned int Time_Indicate = 2;
 
        WDTCTL = WDTPW | WDTHOLD;       // stop watchdog timer
        // Configure GPIO Out
@@ -107,13 +116,12 @@ int main(void)
                _enable_interrupts();
 
                // Button 2 or PIR events initiate light measurement
-               // and tuns on green led
+               // and tuns on green or red led
                if (events & (1<<ev_btn2|1<<ev_pir1|1<<ev_pir2)) {
-                       if (Duty_Cycle > 0) {
-                               Time_Left = TIME_ON;
-                               continue;
-                       }
-                       P1OUT |= BIT_GL;        // Set green LED on
+                       if (events & 1<<ev_pir1)
+                               P1OUT |= BIT_GL;        // Set green LED on
+                       if (events & 1<<ev_pir2)
+                               P1OUT |= BIT_RL;        // Set red LED on
                        // Sampling and conversion start
 #ifdef ADCENC
                        ADCCTL0 |= ADCENC | ADCSC;
@@ -125,9 +133,8 @@ int main(void)
                // End of light measurement. Set new Duty_Cycle,
                // zero increment and turn off green led
                if (events & 1<<ev_adc) {
-                       P1OUT &= ~BIT_GL;       // Clear green LED off
-                       if (Time_Left)
-                               continue;
+                       P1OUT ^= (BIT_GL|BIT_RL); // Flip green and red LEDs
+                       Time_Indicate = 5;
                        if (ADC_Result < LIGHT_THRESHOLD)
                                continue;
                        Time_Left = TIME_ON;
@@ -136,6 +143,8 @@ int main(void)
 
                // Button 1 sets non-zero increment (and toggles it)
                if (events & 1<<ev_btn1) {
+                       P1OUT |= (BIT_GL|BIT_RL); // Set green and red LEDs on
+                       Time_Indicate = 5;
                        if (Duty_Cycle > PWM_HALF) {
                                Time_Left = 0;
                                Increment = -1;
@@ -147,18 +156,21 @@ int main(void)
 
                // Timer event (100 ms) changed duty cycle and flashes red led
                if (events & 1<<ev_tmr) {
-                       if (Time_Count++ > 20) {
+                       if (Time_Indicate) {
+                               Time_Indicate--;
+                               if (!Time_Indicate)
+                                       P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off
+                       }
+                       if (Time_Count++ > 10) {
                                Time_Count = 0;
-                               P1OUT |= BIT_RL; // red LED on
                                if (Time_Left)
                                        Time_Left--;
                                else if (Duty_Cycle > 1)
                                        Increment = -1;
-                       } else if (Time_Count == 1)
-                               P1OUT &= ~BIT_RL; // red LED off
+                       }
                        if (Increment > 0) {
-                               if (++Duty_Cycle >= PWM_ORDER) {
-                                       Duty_Cycle = PWM_ORDER;
+                               if (++Duty_Cycle >= (PWM_ORDER<<1)) {
+                                       Duty_Cycle = PWM_ORDER<<1;
                                        Increment = 0;
                                }
                        } else if (Increment < 0) {
@@ -168,7 +180,8 @@ int main(void)
                                }
                        } else
                                continue;
-                       TA0CCR2 = 1 << Duty_Cycle;
+
+                       TA0CCR2 = expon2(Duty_Cycle);
                }
                __bis_SR_register(LPM0_bits | GIE);
                __no_operation();