This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Interfacing LM35 with MSP430 Launch pad

Other Parts Discussed in Thread: MSP430G2452, LM35
Hi, i was interfacing LM35 to MSP430 launch pad (MSP430g2452), and the LM35 is connected to analog channel A4 of launch pad, The code is mentioned below, I'm getting a constant output as 23 deg C, I have got held up with this code for very long time and the temperature is not changing according to the change in the environment, Pls help me in resolving this issue, Expecting a positive and speedy reply.
Thanking you
#include  "msp430g2452.h"
#include <stdio.h>
#include "adc_lcd16.h"
long temp;
long IntDegF;
long IntDegC;
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  P1OUT = 0x00; 
  P1DIR |= 0xFF; 
  lcdinit(); 
  ADC10CTL1 = INCH_4 + SHS_0 + ADC10SSEL_0 + ADC10DIV_4 + CONSEQ_0 ;        
  ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;
 
  __enable_interrupt();                     // Enable interrupts.
  TACCR0 = 30;                              // Delay to allow Ref to settle           
  TACCTL0 |= CCIE;                          // Compare-mode interrupt.
  TACTL = TASSEL_2 | MC_1;                  // TACLK = SMCLK, Up mode.
  LPM0;                                     // Wait for delay.
  TACCTL0 &= ~CCIE;                         // Disable timer Interrupt
  __disable_interrupt();
  while(1)
  {
    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled
  
    temp = ADC10MEM;
    IntDegF = (temp/6.82)+33.8;
   
    gotoXy(0,0);
    prints("TEMP Deg F= ");
    integerToLcd(IntDegF);
        
 
    temp = ADC10MEM;
    IntDegC=(temp/6.82);
   
    gotoXy(0,1);
    prints("TEMP Deg C= ");
    integerToLcd(IntDegC);
    ADC10CTL0&= ~ENC;
    while(1)
          {
           
           }
    __no_operation();                       // SET BREAKPOINT HERE
  }
}
// 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=TIMER0_A0_VECTOR
__interrupt void ta0_isr(void)
{
  TACTL = 0;
  LPM0_EXIT;                                // Exit LPM0 on return
}
  • Have you checked that you are actually getting an appropriately-varying signal at the correct MSP430 input pin...?

     

  • Andy Neil said:

    Have you checked that you are actually getting an appropriately-varying signal at the correct MSP430 input pin...?

     

    Yes i have checked, Its varying......

  • Karthik R said:
    Yes i have checked, Its varying......

    First you are doing inefficient timer delay for Ref to settle. Simple delay_cycles at default DCO freq can do the job.

    What's that:

     IntDegF = (temp/6.82)+33.8;

    http://en.wikipedia.org/wiki/Integer_(computer_science)

  • hi, this is code regarding delay for Ref to settle,  Can u pls suggest what change has to be made so that code will exe without any problem, Pls help me as i'm new to MCu, 

    __enable_interrupt(); // Enable interrupts.
    TACCR0 = 30; // Delay to allow Ref to settle
    TACCTL0 |= CCIE; // Compare-mode interrupt.
    TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode.
    LPM0; // Wait for delay.
    TACCTL0 &= ~CCIE; // Disable timer Interrupt
    __disable_interrupt();

  • Karthik R said:
    so that code will exe without any problem

    I did not say that your timer-based delay have problem. Perhaps it works. Such delay is just waste of timer peripheral and code memory. As I said - use __delay_cycles(Ncycles); function to do delay here.

    Clearly your code have other problems like:

        IntDegF = (temp/6.82)+33.8;
        IntDegC=(temp/6.82);

    Those lines actually are translated into:

        IntDegF = (temp/6)+33;
        IntDegC=(temp/6);

    You can't do floating point math with integer variables!

    If your code does not work - try examples, see how they do the job, compare to your code, check differences. Debug your code step by step to see that you are getting ADC values you expect, that your math is giving results you expect and so on.

    This is highly unproductive to ask others in the internet to check your code instead of debugging it to see what's actually happening in your uC :D

  • Ilmars said:
    I did not say that your timer-based delay have problem. Perhaps it works. Such delay is just waste of timer peripheral and code memory.

    I disagree. Using timers for delays is the right way to do things. That's what timers have been invented for. Also, a busy-waiting delay with __delay_cycles consumes way more energy than waiting for tiemr expiration in LPM.
    It might be a bit overkill here, but it is good to get used to it from the beginning.

    Ilmars said:
    Clearly your code have other problems like:

        IntDegF = (temp/6.82)+33.8;
        IntDegC=(temp/6.82);

    Those lines actually are translated into:

        IntDegF = (temp/6)+33;
        IntDegC=(temp/6);

    You can't do floating point math with integer variables!

    Well, you're wrong even if you're right.

    First, of course you can do floating point math with integer values (if not, the compiler would throw an error). Whether it gives you the expected result, is a different story.
    The compiler will cast the innteger part to float and then perform the operation, then truncate the result back to integer.

    AFAIK

        IntDegF = (temp/6.82)+33.8;

    translates to

       intDegF = (long) (((float)temp / 6.82)+33.8).

    as the compiler promotes to the larger type of the two operands.

    See here for a nice summary. With citations from the C standard (I know, citing standards is uncommon for me, I usually leave this to others, but this time I wanted to know.)

  • Karthik R said:
    I have got held up with this code for very long time and the temperature is not changing according to the change in the environment


    Karthik R said:
    while(1)[...]
      ADC10CTL0&= ~ENC;
        while(1)
              {
               
               }
        __no_operation();                       // SET BREAKPOINT HERE
      }
    Well,the temperature might change, but your only measure it once, then your code gets caught in this inner while(1) loop and never does a second measurement.
  • Jens-Michael Gross said:

    I have got held up with this code for very long time and the temperature is not changing according to the change in the environment


    Karthik R said:
    while(1)[...]
      ADC10CTL0&= ~ENC;
        while(1)
              {
               
               }
        __no_operation();                       // SET BREAKPOINT HERE
      }
    Well,the temperature might change, but your only measure it once, then your code gets caught in this inner while(1) loop and never does a second measurement.

    [/quote]

    I'm really  sorry for the late reply as i was travelling, can u pls tell , what change has to be made in the code so that i will get continuous reading and display the same, as i'm new to prog language Pls help me in this

    thank you.  

  • Thank you Jens-Michael Gross, for your reply , as u spotted the problem is in the while loop, i removed the loop and the code runs well and it does give the change in temperature according to environment , but i have a small prob , the temp value is not getting stabilized , Pls help me out in this


    thank you

  • Jens-Michael Gross said:

    translates to

       intDegF = (long) (((float)temp / 6.82)+33.8).

    Ups. You are so right :) Thing is that I would never in my life write such kind of [...] type conversion for 16bit uC, especially to convert ADC'ed temperature to integer. So my mind played funny trick here :)

    Jens-Michael Gross said:
    but this time I wanted to know

    LOL. Me either. I suggest this in return.

  • Ilmars said:

    translates to

       intDegF = (long) (((float)temp / 6.82)+33.8).

    Ups. You are so right :) Thing is that I would never in my life write such kind of [...] type conversion for 16bit uC, especially to convert ADC'ed temperature to integer. So my mind played funny trick here :)

    Jens-Michael Gross said:
    but this time I wanted to know

    LOL. Me either. I suggest this in return.

    [/quote]

    Hi,

    I'm sorry , i dint mention the change i have done int the code , the change is mentioned below, i found it on some web site, it works perfectly,  intDegF = ((temp /5)*9/5)+32), but now i want to know how to stabilize the temp value, the value is oscillating too much when der is a change in external atmosphere. Pls help me out in this . 

    Thanking you 

  • Karthik R said:
    the value is oscillating too much

    How much? Please show sample data so we can see what you are talking about. Also specify what variation you expect.

    Karthik R said:
    too much when der is a change in external atmosphere.

    Please specify changes in the atmosphere.

  • HI

    When a soldering iron is kept near the LM35 sensor , the room temperature value (i.e., 30 Deg C) is getting varied or jumping 4 to 5 values up and down to 30 deg C, and the jump is very fast, but it doesnt get stabilised also. I had recorded the video but i'm unable to upload it..... Pls help with this information. 

    Thank you 

  • hey guys. vout of lm35 input to p1.4 of msp???

  • How many lm35 caz temperature sensors can be connected to msp430 g2 development board at a time. Can I connect gps and gsm to msp430 g2 board also Or do I have to connect them on separate msp430 g2 boards Please do reply. Or any other board you know about which can accomodate around 20 lm35 caz temp. sensors. Gps and gsm can be connected on different boards . Recommend boards which can be used to interface gps and gsm if other than msp430 g2 Please do reply....its urgent.

**Attention** This is a public forum