X-Git-Url: http://www.average.org/gitweb/?p=sensor-light.git;a=blobdiff_plain;f=msp430%2Fmain.c;h=fc80ae9b04fdbeeb4e180960ee4b055c34d745db;hp=03786aa4b5cb1ab5383644151f2d92310d73eb56;hb=662310e4999c78fb786068643f100e76b9294c68;hpb=c9ef989a5af20427a69004b565b48e7e30316326 diff --git a/msp430/main.c b/msp430/main.c index 03786aa..fc80ae9 100644 --- a/msp430/main.c +++ b/msp430/main.c @@ -6,8 +6,8 @@ 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 600 -#define TIME_ON 16 +#define LIGHT_THRESHOLD 800 +#define TIME_ON 160 #ifdef ADCSC /* Let us hope that this is a "new" model */ # define BIT_RL BIT0 @@ -24,12 +24,19 @@ 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<>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_Left = 50; unsigned int Time_Indicate = 2; WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer @@ -153,16 +160,15 @@ int main(void) if (!Time_Indicate) P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off } - if (Time_Count++ > 20) { - Time_Count = 0; - if (Time_Left) - Time_Left--; - else if (Duty_Cycle > 1) - Increment = -1; + if (Time_Left) { + Time_Left--; + if (!Time_Left) + if (Duty_Cycle) + Increment = -1; } 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) { @@ -170,12 +176,10 @@ int main(void) Duty_Cycle = 0; Increment = 0; } - } else + } else // Increment _was_ zero - no change! continue; - if (Duty_Cycle) - TA0CCR2 = 1 << (Duty_Cycle - 1); - else - TA0CCR2 = 0; + + TA0CCR2 = expon2(Duty_Cycle); } __bis_SR_register(LPM0_bits | GIE); __no_operation();