Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Motion detection using MSP430g2131

Status
Not open for further replies.

maulik_suthar

Junior Member level 1
Joined
Aug 28, 2011
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,472
i have this code built as i am not much used to programming with ADC's so plz suggest changes in code required. i want to get led glowing for 3 seconds wen it detects motion then should go off.



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include  <msp430g2131.h>   
   
#define   LED_OUT         BIT6                  // Bit location for LED   
 
#define   THRESHOLD       10                    // Threshold for motion   
   
static unsigned int result_old = 0;             // Storage for last conversion
void main(void)   
{   
  WDTCTL = WDTPW+WDTHOLD;      
  ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE;   // ADC10ON, interrupt enabled, Sample Timer = /16
  ADC10CTL1 = INCH_1;                // select A1 for sampling
  P1DIR |= BIT6;                     // set LED to OUT
  P1IES &= BIT1;                     // high-low edge initially
  P1IFG &= ~BIT1;                    // prevent immediate interrupt
  P1IE |= BIT1;                      // enable interrupts on P1.1
  _BIS_SR(LPM4_bits + GIE);          // enable GPIO interrupts and send into LPM4
}  
// interrupt for P1
#pragma vector = PORT1_VECTOR
__interrupt void P1_ISR() 
{
  unsigned int result_new;
  ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
  __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
  
  result_new = ADC10MEM;
 
  if (result_new > result_old)              // Get difference between samples   
    result_old = result_new - result_old;   
   else   
     result_old = result_old - result_new;   
   
  if (result_old > THRESHOLD)               // If motion detected...   
     P1OUT |= BIT6;                      // Turn LED on   
   
  result_old = ADC10MEM;                    // Save last conversion   
    
} 
 
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)
}



Use CODE Or SYNTAX Tags When Posting Your Code
 
Last edited by a moderator:

i have this code built as i am not much used to programming with ADC's so plz suggest changes in code required. i want to get led glowing for 3 seconds wen it detects motion then should go off.



Code C - [expand]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#include <msp430g2131.h> #define LED_OUT BIT6 // Bit location for LED #define THRESHOLD 10 // Threshold for motion static unsigned int result_old = 0; // Storage for last conversion void main(void) { WDTCTL = WDTPW+WDTHOLD; ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled, Sample Timer = /16 ADC10CTL1 = INCH_1; // select A1 for sampling P1DIR |= BIT6; // set LED to OUT P1IES &= BIT1; // high-low edge initially P1IFG &= ~BIT1; // prevent immediate interrupt P1IE |= BIT1; // enable interrupts on P1.1 _BIS_SR(LPM4_bits + GIE); // enable GPIO interrupts and send into LPM4 } // interrupt for P1 #pragma vector = PORT1_VECTOR __interrupt void P1_ISR() { unsigned int result_new; ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit result_new = ADC10MEM; if (result_new > result_old) // Get difference between samples result_old = result_new - result_old; else result_old = result_old - result_new; if (result_old > THRESHOLD) // If motion detected... P1OUT |= BIT6; // Turn LED on result_old = ADC10MEM; // Save last conversion } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) }



Use CODE Or SYNTAX Tags When Posting Your Code

Answer : You have forgotten to include delay!!! and who is going to switch off? two thins you didnt do.. a infinte while loop is missing and also why is P1 port u r using it as interrupt? that to as external one!!! do recheck the code and see whether their is syntax errors.
 

i have this availiable device its p1.1 is attached with o/p of the PIR sensor. so hav to make it as an input.
where to put delay? at start? can i use WDT for delay
 

Oki.. are you using launch pad? first of all, TH EPIR output should be given to input pin. that means if pin1.1 is connected then the change the direction to input direction. Now if any of your embedded project has o work then u should hav an infinite loop. run this one in infinte loop

unsigned int result_new;

while(1){
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit

result_new = ADC10MEM;

if (result_new > result_old) // Get difference between samples
result_old = result_new - result_old;
else
result_old = result_old - result_new;

if (result_old > THRESHOLD) // If motion detected...
P1OUT |= BIT6; // Turn LED on

result_old = ADC10MEM; // Save last conversion

// give delay here. it can be hardware delay or software delay. u wil hav to find amount of iteractions u req to do to get that delay.
//switch off the led here. it wil help u rerun infinite times.
}
 

sir,
thankx...
but, after reciving sample i have to start conversion, so can i put an interrupt for reciving signal, then starting the conversion in ISR

- - - Updated - - -

and is the configuration for adc done right for continous sampling.
m not using launchpad, its a prototype device for motion sensing
 

if my suggestions helped please click helped me option. good luck. any clarifications you can ask me.
 

no, it didnt helped. plz give suggetions for using adc for sampling
 

have you downloaded sample codes? use single channel continous mode for fetching the input from the device. default sampling rate would do the job. dont give any reference for ADC. I guess G2131 has 10 bit adc. So the sampled output will be in ADC10MEM0. collect the data from there. and write the code.
 

m trying that...i got it configured right...still its not working..i want sampling to start when ip comes to pin1.1..so can i use port interrupts with adc interrupt
 

configure pin1.1 as interrupt pin. u can do it.. initialise adc in main function. after that when interrupt occurs, just enable the adc inside the interrupt. it wil work. however i would like to ask one question, why do u want the sampling to start after input is reached? cant it be before? this question i m asking bcoz, for first few samples the adc converted data will be garbage. so u will get error results. the stability and performance of whole system wil be deteriorated. so i suggest u to rethink of the logic.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top