Myst3ry 0 Posted February 9, 2011 Share Posted February 9, 2011 Hi guys, I am encountering some problem with my codes, I connected my motion sensor to the MSP and wants it to work this way: When there is motion detected, the LED will remain ON When there is no motion detected for 5 seconds, LED will turn OFF After the LED is off, if there is motion detected, the LED will light up again and reset the timer. this is my code: #include "msp430.h" int SensorCounter = 0; int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P2DIR |= 0xFB; // Set P2.2 to input direction P4DIR |= 0x20; // P4DIR = P4DIR | 0x20 set P4.5 output TACCTL0 = CCIE; // TACCR0 interrupt enabled TACCR0 = 37500; // set value of compare register to 37500 to achieve 1sec blinking rate TACTL = TASSEL_2 + MC_3 + ID_3; // SMCLK, Up/Down Mode, Divide8 __bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ interrupt } // Timer A0 interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { if ( P2IN & 0x04 ) { if ( SensorCounter == 0 ) { P4OUT &= ~0x20; SensorCounter++ ; } else if ( SensorCounter < 6 ) { P4OUT |= 0x20; // P4OUT = P4OUT ^ 0x20 Toggle P4.5 using exclusive-OR SensorCounter++ ; } else if ( SensorCounter > 7 ) { P4OUT &= ~0x20; SensorCounter == 0; } } } Please Advice thanks! Quote Link to post Share on other sites
Myst3ry 0 Posted February 9, 2011 Author Share Posted February 9, 2011 bro RobG adviced me to do this : Start timer for 5sec. Watch for interrupt on port. If there is an interrupt, reset timer or do whatever. When timer expires. Turn off. Go to deep sleep. Another port interrupt can turn on and reset the timer and the cycle continues. But didnt know how Quote Link to post Share on other sites
RobG 1,891 Posted February 9, 2011 Share Posted February 9, 2011 What you need to do is setup Timer A and in the timer interrupt routine, you will turn the LED off. In Port1 interrupt routine you will turn the LED on and reset timer. I didn't test it, but that should work. We have to use counter because in this configuration, we can only get ~0.5s. Another option would be slowing down the clock. We use LPM4 mode when timer is not active, LPM0 when counting 5s delay. Let me know if there are any issues with this. #include "msp430g2231.h" #define DELAY_05s 62500 unsigned char counter = 0; void main(void) { WDTCTL = WDTPW + WDTHOLD; // stop WDT // P1.0 is PIR input P1REN |= BIT0; // enable pull-up P1OUT |= BIT0; // pull-up P1IE |= BIT0; // interrupt enabled P1IES |= BIT0; // hi/lo edge but might be changed depending on your PIR P1IFG &= ~BIT0; // IFG cleared P1DIR |= BIT1; // P1.1 is LED TACCR0 = 0; TACTL = TASSEL_2 + MC_1 + ID_3; // SMCLK, upmode , /8 TACCTL0 = CCIE; __bis_SR_register(LPM4_bits + GIE); // switch to LPM0 with interrupts } // Timer A interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { if(counter == 10) { counter = 0; P1OUT &= ~BIT1; // LED off __bis_SR_register_on_exit(LPM4_bits); // switch from LPM0 to LPM4 } else { counter++; } } // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { P1OUT |= BIT1; // LED on TACCR0 += DELAY_05s; counter = 0; __bic_SR_register_on_exit(LPM4_bits-LPM0_bits); // switch from LPM4 to LPM0 P1IFG &= ~BIT0; // IFG cleared } Quote Link to post Share on other sites
Myst3ry 0 Posted February 9, 2011 Author Share Posted February 9, 2011 Hi bro RogB, i am using P2.5 as the input of the motion sensor so how can i add it into the codes u added? and can i ask, #define DELAY_05s 62500 ---> 62500 is the timing to delay 0.5s? // P1.0 is PIR input --> what is PIR input? Quote Link to post Share on other sites
RobG 1,891 Posted February 9, 2011 Share Posted February 9, 2011 PIR is your motion detector so if you are using P2.5, change all (except the ones that deal with LED) P1 to P2 and instead of BIT0 use BIT5. // P2.5 is PIR input P2REN |= BIT5; // enable pull-up P2OUT |= BIT5; // pull-up P2IE |= BIT5; // interrupt enabled P2IES |= BIT5; // hi/lo edge but might be changed depending on your PIR P2IFG &= ~BIT5; // IFG cleared P1DIR |= BIT1; // P1.1 is LED Also, change interrupt name and P1IFG &= ~BIT0; to P2IFG &= ~BIT5; : #pragma vector=PORT2_VECTOR __interrupt void Port_2(void) Where is your LED connected? Myst3ry 1 Quote Link to post Share on other sites
cde 334 Posted February 9, 2011 Share Posted February 9, 2011 P2.5? P4? What msp430 are you using? Quote Link to post Share on other sites
Myst3ry 0 Posted February 10, 2011 Author Share Posted February 10, 2011 @cde MSP430 eZ430-RF2500 @RobG the green led doesnt seem to off my mistake, im using p2.2 , so i edited it appears this way #include "msp430g2231.h" #define DELAY_05s 62500 unsigned char counter = 0; void main(void) { WDTCTL = WDTPW + WDTHOLD; // stop WDT // P2.2 is PIR input P2REN |= 0x04; // enable pull-up P2OUT |= 0x04; // pull-up P2IE |= 0x04; // interrupt enabled P2IES |= 0x04; // hi/lo edge but might be changed depending on your PIR P2IFG &= ~0x04; // IFG cleared P1DIR |= 0x02; // P1.1 is LED TACCR0 = 0; TACTL = TASSEL_2 + MC_1 + ID_3; // SMCLK, upmode , /8 TACCTL0 = CCIE; __bis_SR_register(LPM4_bits + GIE); // switch to LPM0 with interrupts } // Timer A interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { if(counter == 10) { counter = 0; P1OUT &= ~0x02; // LED off __bis_SR_register_on_exit(LPM4_bits); // switch from LPM0 to LPM4 } else { counter++; } } // Port 1 interrupt service routine #pragma vector=PORT2_VECTOR __interrupt void Port_2(void) { P2IFG &= ~0x04; // LED on TACCR0 += DELAY_05s; counter = 0; __bic_SR_register_on_exit(LPM4_bits-LPM0_bits); // switch from LPM4 to LPM0 P1IFG &= ~0x01; // IFG cleared } erm, bro RobG the program is doing just 5 seconds later den off right? if there is motion detected within this 5 sec, will it reset the timer? please advice thanks Quote Link to post Share on other sites
RobG 1,891 Posted February 10, 2011 Share Posted February 10, 2011 Yes, first you set up the timer to interrupt every 500ms, then you put MCU to sleep with timer clock off. When an interrupt is detected on P2.2, LED is turned on, counter, which is used to increase delay to 5s (10*500ms) is reset back to 0, MCU is still sleeping, but timer's cock is now enabled, finally IFG is cleared so P2.2 can detect another interrupt. Once timer reaches 500ms mark, interrupt occurs, if counter is <10, counter is increased and another timer cycle starts. After 10 interrupts, counter is reset, LED is turned off, and MCU's sleep mode is changed back to "deep" sleep where timer's clock is off. If at any point when counter was < 10 and P2.2 interrupt occurred, counter would be reset which would extend the whole cycle by another 5s. Couple of corrections: #include "msp430g2231.h" #define DELAY_05s 62500 unsigned char counter = 0; void main(void) { WDTCTL = WDTPW + WDTHOLD; // stop WDT // P2.2 is PIR input P2REN |= 0x04; // enable pull-up P2OUT |= 0x04; // pull-up P2IE |= 0x04; // interrupt enabled P2IES |= 0x04; // hi/lo edge but might be changed depending on your PIR P2IFG &= ~0x04; // IFG cleared P1DIR |= 0x02; // P1.1 is LED TACCR0 = DELAY_05s; TACTL = TASSEL_2 + MC_1 + ID_3; // SMCLK, upmode , /8 TACCTL0 = CCIE; __bis_SR_register(LPM4_bits + GIE); // switch to LPM0 with interrupts } // Timer A interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { if(counter == 10) { counter = 0; P1OUT &= ~0x02; // LED off __bis_SR_register_on_exit(LPM4_bits); // switch from LPM0 to LPM4 } else { counter++; } } // Port 1 interrupt service routine #pragma vector=PORT2_VECTOR __interrupt void Port_2(void) { P1OUT |= 0x02; // LED on counter = 0; __bic_SR_register_on_exit(LPM4_bits-LPM0_bits); // switch from LPM4 to LPM0 P2IFG &= ~0x04; // IFG cleared } Quote Link to post Share on other sites
Myst3ry 0 Posted February 10, 2011 Author Share Posted February 10, 2011 Bro RobG, Seems like even if i dun connect the motion sensor to the msp, i stil remain on Even when i connect, it still remain on I guess somewhere in the program has error. as i used a osciloscope to check out the waveform, P2.2 is forever High. Maybe thats the reason why the LED is always ON Quote Link to post Share on other sites
RobG 1,891 Posted February 10, 2011 Share Posted February 10, 2011 I tested it on my LaunchPad by reassigning P1.1 to P1.0 and P2.2 to P1.3 (switch) and it does work as expected. Does the LED turns on the first movement and then stays on or is it always on? P2.2 should be always high except when motion is detected (it is pulled up by internal resistor,) what kind of signal does the sensor output? Is it active high or active low? Quote Link to post Share on other sites
Myst3ry 0 Posted February 10, 2011 Author Share Posted February 10, 2011 I tested it on my LaunchPad by reassigning P1.1 to P1.0 and P2.2 to P1.3 (switch) and it does work as expected. Does the LED turns on the first movement and then stays on or is it always on? P2.2 should be always high except when motion is detected (it is pulled up by internal resistor,) what kind of signal does the sensor output? Is it active high or active low? Initially the LED was turned ON, it stays on, so i went to the osiloscope and check out on the signal on P2.2, it always remained high even when there is no motion detected. The sensor will give a active high when there is movement sensed. Sensor output: http://s1205.photobucket.com/albums/bb4 ... int_00.jpg Quote Link to post Share on other sites
RobG 1,891 Posted February 10, 2011 Share Posted February 10, 2011 Initially the LED was turned ON, it stays on, so i went to the osiloscope and check out on the signal on P2.2, it always remained high even when there is no motion detected. The sensor will give a active high when there is movement sensed. LED should be initially off, so this is one thing to look at. If sensor is active high, the you need to change pull up resistor to pull down and trigger edge to rising edge P2OUT &= ~0x04; // pull-down P2IES &= ~0x04; // lo/hi edge Quote Link to post Share on other sites
Myst3ry 0 Posted February 11, 2011 Author Share Posted February 11, 2011 oh it worked! thanks RogB Quote Link to post Share on other sites
GeekDoc 226 Posted February 11, 2011 Share Posted February 11, 2011 oh it worked! I love those moments! Quote Link to post Share on other sites
rameez rehman 0 Posted February 23, 2011 Share Posted February 23, 2011 Can you please specify which PIR sensor you are using. Also I need help with how to make the hardware connection of the sensor with the target board. Another question is that whether i can connect the PIR (D203b) directly or do i need to to attach the sensor to a PCB first and then interface that with the rf2500 target board. I am a student of software and totally new to hardware, please help in as much detail as possible. Thank you. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.