X-Git-Url: http://www.average.org/gitweb/?p=sensor-light.git;a=blobdiff_plain;f=msp430%2Fmain.c;h=6b075ad91d6b9753f86cb258216d88227842d174;hp=543022962a06b114721d3c7bb0cfadd34d40b06b;hb=68eddb43a02efafa7d42778a58659313199c8228;hpb=c289f198e917c6a6726e8ead0937a36662e3efff diff --git a/msp430/main.c b/msp430/main.c index 5430229..6b075ad 100644 --- a/msp430/main.c +++ b/msp430/main.c @@ -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,6 +24,14 @@ 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; @@ -114,10 +122,6 @@ int main(void) P1OUT |= BIT_GL; // Set green LED on if (events & 1< 0) { - Time_Left = TIME_ON; - continue; - } // Sampling and conversion start #ifdef ADCENC ADCCTL0 |= ADCENC | ADCSC; @@ -131,8 +135,6 @@ int main(void) if (events & 1< 20) { + if (Time_Count++ > 10) { Time_Count = 0; if (Time_Left) Time_Left--; @@ -167,8 +169,8 @@ int main(void) 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) { @@ -178,10 +180,8 @@ int main(void) } } else continue; - if (Duty_Cycle) - TA0CCR2 = 1 << (Duty_Cycle - 1); - else - TA0CCR2 = 0; + + TA0CCR2 = expon2(Duty_Cycle); } __bis_SR_register(LPM0_bits | GIE); __no_operation();