X-Git-Url: http://www.average.org/gitweb/?p=sensor-light.git;a=blobdiff_plain;f=msp430%2Fmain.c;h=dab85428e6635833fe526b2d73f8059004799f01;hp=5d03cca04d33314f26f55b56db6791904824b907;hb=098228675c00cff635ca4f2c8f306a19b7ceea20;hpb=058ef70c8107e74aca0f3e3f7291fc8e75143e9c;ds=sidebyside diff --git a/msp430/main.c b/msp430/main.c index 5d03cca..dab8542 100644 --- a/msp430/main.c +++ b/msp430/main.c @@ -4,6 +4,21 @@ static volatile unsigned int ADC_Result; static volatile unsigned int irq_events = 0; enum {ev_btn1 = 0, ev_btn2, ev_pir1, ev_pir2, ev_tmr, ev_adc, ev_MAX}; +#ifdef ADCSC /* Let us hope that this is a "new" model */ +# define BIT_RL BIT0 +# define BIT_GL BIT1 +# define PBTN(x) P2##x +# define BIT_BTN BIT3 +# define HAVE_BTN2 +# define BIT_BTN2 BIT7 +#else +# define BIT_RL BIT0 +# define BIT_GL BIT6 +# define PBTN(x) P1##x +# define BIT_BTN BIT3 +# define BIT_BTN2 0 +#endif + int main(void) { int Duty_Cycle = 1; @@ -13,42 +28,41 @@ int main(void) WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer // Configure GPIO Out - P1DIR |= BIT0|BIT1|BIT2; // Set P1.0&1&2/LEDs to output direction - P1OUT &= ~(BIT0|BIT1); // P1.0&1 LEDs off - P1SEL1 |= BIT2; // P1.2 PWM out + P1DIR |= BIT_RL|BIT_GL|BIT7; // Set LEDs & PWM to output direction + P1OUT &= ~(BIT_RL|BIT_GL); // LEDs off + P1SEL1 |= BIT7; // PWM out // Configure GPIO In - P2DIR &= ~(BIT3|BIT7); // Buttons - P2OUT |= BIT3|BIT7; // Pull up - P2REN |= BIT3|BIT7; // Enable pull-up - P2IES |= BIT3|BIT7; // INT on Hi->Lo edge - P2IE |= BIT3|BIT7; // INT enable - - P2DIR &= ~(BIT2|BIT5); // PIR Sensors - P2OUT &= ~(BIT2|BIT5); // Pull down - P2REN |= BIT2|BIT5; // Enable pull-down - P2IES &= ~(BIT2|BIT5); // INT on Lo->Hi edge - P2IE |= BIT2|BIT5; // INT enable - P2IFG = 0; // ??? Needed? + PBTN(DIR) &= ~(BIT_BTN|BIT_BTN2); // Buttons + PBTN(OUT) |= BIT_BTN|BIT_BTN2; // Pull up + PBTN(REN) |= BIT_BTN|BIT_BTN2; // Enable pull-up + PBTN(IES) |= BIT_BTN|BIT_BTN2; // INT on Hi->Lo edge + PBTN(IE) |= BIT_BTN|BIT_BTN2; // INT enable + + P2DIR &= ~(BIT4|BIT5); // PIR Sensors + P2OUT &= ~(BIT4|BIT5); // Pull down + P2REN |= BIT4|BIT5; // Enable pull-down + P2IES &= ~(BIT4|BIT5); // INT on Lo->Hi edge + P2IE |= BIT4|BIT5; // INT enable // Configure ADC A7 pin SYSCFG2 |= ADCPCTL7; // Configure ADC10 - ADCCTL0 |= ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks - ADCCTL1 |= ADCSHP; // ADCCLK = MODOSC; sampling timer - ADCCTL2 |= ADCRES; // 10-bit conversion results - ADCMCTL0 |= ADCINCH_7; // A7 ADC input select; Vref=AVCC - ADCIE |= ADCIE0; // Enable ADC conv complete interrupt + ADCCTL0 |= ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks + ADCCTL1 |= ADCSHP; // ADCCLK = MODOSC; sampling timer + ADCCTL2 |= ADCRES; // 10-bit conversion results + ADCMCTL0 |= ADCINCH_7; // A7 ADC input select; Vref=AVCC + ADCIE |= ADCIE0; // Enable ADC conv complete interrupt // Configure timer A0 for PWM - TA0CCR0 = 10000-1; // PWM Period - TA0CCTL2 = OUTMOD_7; // CCR2 reset/set - TA0CCR2 = 500; // CCR2 PWM duty cycle - TA0CTL = TASSEL__SMCLK | MC__UP | TACLR; // SMCLK, up mode, clear TAR + TA0CCR0 = 10000-1; // PWM Period + TA0CCTL2 = OUTMOD_7; // CCR2 reset/set + TA0CCR2 = 500; // CCR2 PWM duty cycle + TA0CTL = TASSEL__SMCLK | MC__UP | TACLR; // SMCLK, up mode, clear TAR //Configure timer A1 for counting time - TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE; // SMCLK, no divider, continuous mode + TA1CTL |= TASSEL__SMCLK | MC__CONTINUOUS | TACLR | TAIE; // SMCLK, no divider, continuous mode // Disable the GPIO power-on default high-impedance mode to activate // previously configured port settings @@ -58,10 +72,10 @@ int main(void) { unsigned int events; - _disable_interrupt(); + _disable_interrupts(); events = irq_events; irq_events = 0; - _enable_interrupt(); + _enable_interrupts(); // Button 2 or PIR events initiate light measurement and tuns on green led if (events & (1< 1000) { // Time_Count = 0; - // __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0 + // __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0 //} } @@ -185,7 +199,7 @@ void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void) case ADCIV_ADCIFG: ADC_Result = ADCMEM0; irq_events |= 1<