Fun with TI Launchpad : Read LM35 sensor using Launchpad MSP430G2 (MSP430G2553)

Print Friendly, PDF & Email

Small code to read temperature from an LM35 sensor and output the temperature to Serial port. Uses hardware UART module and ADC 10 on launchpad.I do not claim originality for this code.  Serial functions are borrowed from Nathan Zimmerman hardware UART example.

Hardware :TI MSP430G2 Launchpad

Software : Code Composer Studio Version: 4.2.3.00004
Connections are very simple.Hookup the LM35 sensor as follows
Vin->P2.0
GND->P1.4
Dout->P1.5

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <msp430g2553.h>
#include <stdbool.h>
////////////////Defines////////////////
#define LED1        BIT6
#define LED0        BIT0
#define Error       BIT0
#define GND         BIT4
#define DAT         BIT5
#define VCC         BIT0 //port P2.0
char buffer[4];
float temp;
int n_temp,r_temp;
////////////////Function Protos////////////////
void TX(char *tx_message);
static char *i2a(unsigned i, char *a, unsigned r);
char *itoa(int i, char *a, int r);
void ADC_init(void);

static char *i2a(unsigned i, char *a, unsigned r)
{
    if (i/r > 0) a = i2a(i/r,a,r);
    *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
    return a+1;
}

char *itoa(int i, char *a, int r)
{
    if ((r < 2) || (r > 36)) r = 10;
    if (i < 0)
    {
        *a = '-';
        *i2a(-(unsigned)i,a+1,r) = 0;
    }
    else *i2a(i,a,r) = 0;
    return a;
}

void TX(char *tx_message)
{
    unsigned int i=0; //Define end of string loop int
    char *message; // message variable
    unsigned int message_num; // define ascii int version variable
    message = tx_message; // move tx_message into message
    while(1)
    {
        if(message[i]==0) // If end of input string is reached, break loop.
        {break;}
        message_num = (int)message[i]; //Cast string char into a int variable
        UCA0TXBUF = message_num; // write INT to TX buffer
        i++; // increase string index
        __delay_cycles(10000); //transmission delay
        if(i>50) //prevent infinite transmit
        {
            P1OUT |= Error;
            break;  
        }
    } // End TX Main While Loop
} // End TX Function

void ADC_init()
{
    /* Configure ADC  Channel */
    ADC10CTL1 = INCH_5 + ADC10DIV_3 ;         // Channel 5, ADC10CLK/4
    ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE;  //Vcc & Vss as reference
    ADC10AE0 |= BIT5;                         //P1.4 ADC option
   
   
}

////////////////////////////////MAIN PROGRAM LOOP//////////////////////////////////////////////////
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;         // Stop WDT
    BCSCTL1 = CALBC1_1MHZ;            // Set DCO to 1MHz
    DCOCTL = CALDCO_1MHZ;  
    ////////////////USCI setup////////////////
    P1SEL = BIT1 + BIT2;            // Set P1.1 to RXD and P1.2 to TXD
    P1SEL2 = BIT1 + BIT2;            //
    UCA0CTL1 |= UCSSEL_2;            // Have USCI use SMCLK AKA 1MHz main CLK
    UCA0BR0 = 104;                  // Baud: 9600, N= CLK/Baud, N= 10^6 / 9600
    UCA0BR1 = 0;                  // Set upper half of baud select to 0
    UCA0MCTL = UCBRS_1;               // Modulation UCBRSx = 1
    UCA0CTL1 &= ~UCSWRST;             // Start USCI
    ////////////////General GPIO Defines////////////////
    P1DIR |= (LED1+GND+LED0);            //Define GPIOs as outputs else GPIOs are inputs
    P2DIR |=VCC;                
    P1OUT =0;                     //All Outputs off except for Green_LED
    P2OUT|=VCC;
    P1OUT&=~GND;
    //////////////SETUP ADC10 TO READ FROM CH A4 (P1.4)/////////////////////////////////////////////
    ADC_init();    
    __enable_interrupt();                     // Enable interrupts.
    int value=0;
   
////////////////Main Loop////////////////
    while(1)                    
    {
        __delay_cycles(1000);                   // Wait for ADC Ref to settle
        ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
        __bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled    
        value = ADC10MEM;
        temp=value*0.317;  //temperature as float
        //split the float into integer and fraction part
        //C = A % B is equivalent to C = A ? B * (A / B)
        r_temp=((temp*10)-10) * ((temp*10)/10);
        itoa(temp, buffer, 10);
        TX("$LM35:");
        TX(buffer);
        TX(".");
        itoa(r_temp, buffer, 10);
        TX(buffer);
        TX("!\r\n");
        P1OUT ^= LED0;
        _delay_cycles(500000);
         
    } // End While
} // End Main Program

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Return to active mode
  P1OUT ^= LED1;
}
Ishan Karve

About Ishan Karve

Ishan Karve is just an every day normal guy next door who happens to be an Electronics Engineer by profession and dabbles with PHP, Javascript, C++ and python. His interests vary as seasons change.. they change from astronomy to soul searching. This site is just a reflection of what he does to keep his mind engaged when he is not occupied by work and family. He is an extremely objective guy and is always ready for some good arguments.. of course over a glass of 40% proof alcohol.
This entry was posted in DIY, Electronics, MSP-GCC, MSP430, Projects and tagged , , , , , , . Bookmark the permalink.

42 Responses to Fun with TI Launchpad : Read LM35 sensor using Launchpad MSP430G2 (MSP430G2553)

  1. Rob says:

    Hello Ishan,

    Your projects are very impressive and professional.

    Rob.

  2. Kumar says:

    Hi,

    Nice explained code. I am trying this example and would like to know how to make it work.

    I am using TMP35 similar to lm35 sensor. The program runs without any errors. But how do i see result on terminal or console. Is there any command i need to issue.

    Can you explain what i need to do.
    Thanks

    • Ishan Karve Ishan Karve says:

      I am using hyperterminal @ 9600 8N1

      • Kumar says:

        I tried with same settings but still i cannot see any result. Do i need to connect the micro crystal oscillator since i hv not soldered that.

      • Kumar says:

        is there any other hardware connections apart from lm35. Do i need to change rx ,tx connections.

        • Ishan Karve Ishan Karve says:

          Oh yes.. there is one minor change.. If u are having the new launchpad shipped with MSP430G2553 and u are using the same MCU, then there is a small catch.. MSP430G2553 has a hardware UART. So the launchpad jumpers need to be tweaked a bit.

          STD Configuration is like this

          VCC —
          TXD —
          RXD —
          RST —
          TEST —

          Change it to
          VCC ||
          TXD ||
          RXD —
          RST —
          TEST —

          I hope this resolves your query.

  3. Michal says:

    What modifications need this code to work in IAR? I try it in IAR and I get error “Udefined external _delay_cycles referred in main”.

  4. vankhoa says:

    i don’t understand what you mean when you multiply temp=value *0.317, you noted that because temp is a float type but i still don’t get it, can you explain it to me, thanks a lot.

  5. Michal says:

    Firstly
    r_temp=((temp*10)-10) * ((temp*10)/10); <— this return wrong results
    http://www.java2s.com/Code/C/Math/Splitfloatingpointvalueintofractionalandintegerpartshowtousemodf.htm

    This code above works fine (maybe not efficient but works)

  6. Rajesh says:

    Hi Ishan,
    Can you post a pic of hard UART with the TX and RX jumpers in place
    along with the UART hardware(Max232), it would be helpful.

    • Ishan Karve Ishan Karve says:

      Dear Rajesh
      The code that i posted works for Launchpad with MSP430G2553. It doesnot require any level translator like max232 as it has a USB FET interface.
      For hardware uart here is a quick illustration

      STD Jumper Configuration is like this

      VCC –
      TXD –
      RXD –
      RST –
      TEST –

      Change it to
      VCC ||
      TXD ||
      RXD –
      RST –
      TEST –

      I hope this resolves your query.

  7. Rajesh says:

    Hi Ishan,
    Thanks for the help Cris crossing the Tx and Rx Helped able to receive the data.

  8. Rajesh says:

    Hi Ishan,

    What is the maximum baud you are able to attain with 2553. I tried for 128000 bud but could not succeed, UCA0BR0 = 7;, UCA0MCTL = UCBRS_7;

    Any help for baud above 1MBPS

  9. acolytesumatra says:

    hi,
    can you take a picture of the std configuration jumper because i don’t understand it.
    is this in degree Celsius? or degree Fahrenheit?

  10. John Chlark says:

    Hi,
    can you post a diagram of the std configuration because i couldn’t understand it.
    is this in degree celsius? or in degree fahrenheit?

  11. Riva says:

    How to get the temperature value in degree celsius using this code? Please send the formula for that…….

  12. Riva says:

    I got output………. Thank you ………………………

  13. Gabriel says:

    Hello, how do I see the temperature output? I see nothing in the console. Thanks for the reply.

  14. John Bran says:

    How would you go about displaying LM35 sensors. I tried connecting the output of another LM35 to pin 1.7 and adding
    define DAT2 BIT 7
    ///////////////////
    void ADC2_init function
    ////////////////////
    in main, I copied everything inside it and placed it in a while loop that ends after a certain amount of x. Under that while loop I put an exact copy but this one uses ADC2-init and all BIT5 are replaced with BIT7.

    From testing it, I keep getting the same value for both sensors on the hyperterminal. It’s almost as if the ADCmem is never cleared

  15. Kartik Haria says:

    Heyy, Ishan. I am new to working with MSP430 using Code Composer Studio. i Would like to know why did u set the
    ADC10AE0 |= BIT5;
    when you had to connect P1.4 to ground?

  16. Paul says:

    How would I change this code so I can use P1.7 as my analogue input and directly tap off the VCC and GND to power a TMP35. I like to use the rest of P2.x and P1.x as outputs to drive a set of seven segment displays and only have P1.7 used for the analogue input.

    I am new to this analogue input usage so your input would be greatly appreciated.

  17. Paul Kerr Jr says:

    What would I need to change if I wish to use P1.7 for reading the temperature from the output pin of the TMP35. I wish to use the VCC(pin 1) to power the TMP35 and GND(pin 20) for the grounding, rather than using P2.0 and P1.4.

  18. jagravi says:

    it is showing error while i am copiling it in keil file . in the very first line it is showing the errror can you please help for comiling.

  19. kartik a says:

    If I want to run a fan using LM35 using MSP430, what would be the connections to get the output

Leave a Reply

Your email address will not be published. Required fields are marked *