Thread: Gas sensor using msp 430.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Gas sensor using msp 430.

    Hi, i'm trying to use 2 input to check the voltage of the sensor

    The condition are, if input 1 lower than 0x24D and higher den 0x07C,output 1 will light up.

    if input 1 lower than 0x307 and higher den 0x24E, output 2 will blink.

    if input 1 is higher than 0x308 it will check input 2 if it is higher than 0x136, if it is, output 3 will blink.

    Below is my code, please help T_T

    Code:
    #include "msp430x22x4.h"
    void main(void)
    {
      int caretaker =0;
      int emergency = 0;
      volatile unsigned int i;
      volatile unsigned int e;
      WDTCTL = WDTPW + WDTHOLD; // Stop WDT
      ADC10CTL0 = SREF_0 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
      TACCR0 = 30; // Delay to allow Ref to settle
      TACCTL0 |= CCIE; // Compare-mode interrupt.
      TACTL = TASSEL_2 + MC_1; // TACLK = SMCLK, Up mode.
      __bis_SR_register(CPUOFF + GIE); // LPM0, TA0_ISR will force exit
      TACCTL0 &= ~CCIE; // Disable timer Interrupt
      ADC10AE0 |= 0x01; // P2.0 ADC option select
      ADC10CTL1 = INCH_0;
      P1DIR |= 0x03; // P1DIR = P1DIR | 0x03 Set P1.0, P1.1 to output direction
      P4DIR |= 0x20; // P4DIR = P4DIR | 0x20 Set P4.5 to output direction
      P4DIR |= 0x10;
      P4DIR |= 0x08;
      for (;;)
      {
        ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
        __bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
        if(ADC10MEM >0x308&& ADC10AE0 == 0x01 )// high,orange,ADC>2.5
        {
          ADC10CTL1 = INCH_1;
          ADC10AE0 |= 0x02;
          TACCR0 = 30; // Delay to allow Ref to settle
          TACCTL0 |= CCIE; // Compare-mode interrupt.
          ADC10CTL0 |= ENC + ADC10SC;
          
        }
         if(ADC10MEM <0x308&& ADC10AE0 == 0x02 )// ADC<2.5
        {
          ADC10CTL0 |= ENC + ADC10SC;
          ADC10CTL0 = SREF_0 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
          ADC10CTL1 = INCH_0;
          ADC10AE0 |= 0x01;
        }
        if(ADC10MEM > 0x136  && emergency<10 && ADC10AE0 == 0x02)
          {
            if(emergency<2)
            {
              P4OUT |=0x20;
            }
            else
            {
              P4OUT &= ~0x20;
            }
            emergency++;
            e=60000;
            do e--;
            while(e !=0);
            P4OUT &= ~0x08;
            P4OUT &= ~0x10;
          }
          if(emergency ==10)
          {
            emergency =0;
          }
        //ADC10CTL0 |= ENC + ADC10SC;
        
        if ( ADC10MEM < 0x307 && ADC10MEM > 0x24E &&caretaker<10 && ADC10AE0 == 0x01 ) // mid,green,1.9>ADC<2.49
        {
          if(caretaker<2)
          {
            P4OUT |= 0x10;
          }
          else
          {
            P4OUT &= ~0x10;
          }
          caretaker++;
          i=60000;
          do i--;
          while(i !=0);
          P4OUT &= ~0x08;
          P4OUT &= ~0x20;
        }
        if(caretaker ==10)
        {
          caretaker =0;
        }
        //ADC10CTL0 |= ENC + ADC10SC;
        
        if (ADC10MEM < 0x24D &&ADC10MEM >0x07C && ADC10AE0 == 0x01 ) // low,red,0.39>ADC<1.89
        {
          P4OUT &= ~0x10;
          P4OUT |= 0x08;
          P4OUT &= ~0x20;
        }
        else
        {
          P4OUT &= ~0x20;
          P4OUT &= ~0x10;
          P4OUT &= ~0x08;
        }
      }
    }
    // 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)
    }
    #pragma vector=TIMERA0_VECTOR
    __interrupt void TA0_ISR(void)
    {
      TACTL = 0;
      LPM0_EXIT; // Exit LPM0 on return
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Help with what? You've described what the code should do. You haven't described what it does.

    Your code is also making use of a lot of names (variables, macros, etc) that are declared in a header file. That header file is non-standard, and you have not given any information about it (other than the name being #include'd). The names themselves are largely meaningless to anyone who is not intimately familiar with that header file.

    Not being mindreaders, people in forums will not be able to help you unless you give relevant information that they can't be expected to know.

    Even worse, that code is using non-standard extensions (e.g. __interrupt) supported by a specific compiler, but you have not identified that compiler, leaving most people to guess how that compiler behaves. The code makes liberal use of "magic values" (0x10, 0x08, etc) which successfully makes the code even harder to understand.

    And main() returns int, not void.


    Try addressing the concerns I've identified above before posting again. In that process, you might have an "aha" moment, and realise what the problem is. If not then, at least, you will be able to ask you question in a way that does not rely on forum members having convenient crystal balls.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about starting with something like this.
    Code:
    unsigned int readInput1 ( void ) {
    }
    unsigned int readInput2 ( void ) {
    }
    void clearOutputs ( void ) {
    }
    void lightOutput1 ( void ) {
    }
    void blinkOutput2 ( void ) {
    }
    
    void main ( ) {
        // prologue
        for ( ; ; ) {
            unsigned int in1 = readInput1();
            clearOutputs();
            if ( in1 >= 0x007c && in1 <= 0x024D ) {
                lightOutput1();
            } else
            if ( in1 >= 0x024E && in1 <= 0x0307 ) {
                blinkOutput2();
            }
        }
    }
    You have function names which map directly to little bits of key phrases in your spec. The point being, that the code starts to read a little bit like your spec.

    Now you can put the 2 to 4 lines of bit twiddle code corresponding to each action in respective functions.






    As opposed to having a 100 (and growing) main() trying to do everything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lab sensor problem in c
    By golden8 in forum C Programming
    Replies: 5
    Last Post: 12-20-2011, 01:55 PM
  2. Water level sensor
    By Aneesha in forum C Programming
    Replies: 97
    Last Post: 07-01-2011, 07:22 PM
  3. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  4. OS sensor
    By Queatrix in forum Windows Programming
    Replies: 1
    Last Post: 07-11-2005, 07:00 PM