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.

Temperature Sensor Project can you help me? thanks so much :((

Other Parts Discussed in Thread: MSP430F2131

MSP430, DS18B20 Temperature Sensor

i do Temperature Sensor Project

 

I have some difficulty in program, i don't understand protocol between msp430 and DS18b20..., you can share a code or some documentation on the protocol?

thanks you !

there are some problem with this Code i don't practice on Proteus

 

#include "msp430f2131.h"

#define DQIN _P2_IN->_BIT.b3

#define DQOUT _P2_OUT->_BIT.b3  

#define E_LCD (_P2_OUT->_BIT.b2)

// #define RW_LCD (_P2_OUT->_BIT.b1)

#define RS_LCD (_P2_OUT->_BIT.b0)

#define P0 (P1OUT)

union reg { 

    struct bit {

      unsigned char b0:1;   

          unsigned char b1:1;     

          unsigned char b2:1;  

          unsigned char b3:1; 

          unsigned char b4:1; 

          unsigned char b5:1; 

          unsigned char b6:1; 

          unsigned char b7:1; 

              }_BIT; 

          unsigned char _BYTE; 

}; 

 

union reg* _P2_DIRECT = (union reg*)0x2A ; 

union reg* _P2_IN = (union reg*)0x28 ; 

union reg* _P2_OUT = (union reg*)0x29 ;

union reg* _P1_DIRECT = (union reg*)0x22 ;

union reg* _P1_OUT = (union reg*)0x21 ;

__intrinsic void __delay_cycles(unsigned long __cycles);

 

  //////////////////////////////////////////////////////////////////////////////

// su dung thach anh noi chu ki thi hanh 1 lenh msp la 62.5ns

//   Reset is 480us, so delay

//   value is (480-24)/16 = 28.5 - we use 29. Presence checked

//   another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.

 

unsigned char ow_reset(void)

{    

unsigned char presence;

 _P2_DIRECT->_BIT.b3 =1;

        DQOUT = 0; //pull DQ line low

__delay_cycles(7680); // leave it low for 480us

DQOUT = 1;       // allow line to return high

__delay_cycles(1) ; // wait for presence

_P2_DIRECT->_BIT.b3 = 0; // khai bao IN de nhan tin hieu co thiet bi ngoai vi

presence = DQIN; // get presence signal

__delay_cycles(8); // wait for end of timeslot

   return(presence); // presence signal returned     

   //     0=presence, 1 = no part

}

//////////////////////////////////////////////////////////////////////////////

// READ_BIT - reads a bit from the one-wire bus. The delay

//   required for a read is 15us, so the DELAY routine won't work.

//   We put our own delay function in this routine in the form of a

//   for() loop.

//

unsigned char read_bit(void)

{

  _P2_DIRECT->_BIT.b3 =1;

DQOUT = 0; // pull DQ low to start timeslot

DQOUT = 1; // then return high

__delay_cycles(240); // delay 15us from start of timeslot

return(DQOUT); // return value of DQ line

}

//////////////////////////////////////////////////////////////////////////////

// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.

//

void write_bit(char bitval)

{

  _P2_DIRECT->_BIT.b3 =1;

DQOUT = 0; // pull DQ low to start timeslot

if(bitval==1) DQOUT =1; // return DQ high if write 1

__delay_cycles(256); // hold value for remainder of timeslot

DQOUT = 1;

}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us

 

//////////////////////////////////////////////////////////////////////////////

// READ_BYTE - reads a byte from the one-wire bus.

//

unsigned char read_byte(void)

{

unsigned char i;

unsigned char value = 0;

for (i=0;i<8;i++)

{

if(read_bit()) value|=0x01<<i;    // reads byte in, one byte at a time and then

// shifts it left

__delay_cycles(256); // wait for rest of timeslot

}

return(value);

}

//////////////////////////////////////////////////////////////////////////////

// WRITE_BYTE - writes a byte to the one-wire bus.

//

void write_byte(char val)

{

unsigned char i;

unsigned char temp;

for (i=0; i<8; i++)            // writes byte, one bit at a time

{

temp = val>>i;          // shifts val right 'i' spaces

temp &= 0x01;              // copy that bit to temp

write_bit(temp);           // write bit in temp into

}

__delay_cycles(256);

}

 

unsigned char Read_Temperature(void)

{

char get[10];

char temp_lsb,temp_msb;

int k;

ow_reset();

write_byte(0xCC);  //Skip ROM

write_byte(0x44);  // Start Conversion

__delay_cycles(256);

ow_reset();

write_byte(0xCC); // Skip ROM

write_byte(0xBE);  // Read Scratch Pad

for (k=0;k<9;k++){get[k]=read_byte();}

temp_msb = get[1]; // Sign byte + lsbit

temp_lsb = get[0]; // Temp data plus lsb

if (temp_msb <= 0x80){temp_lsb = (temp_lsb/2);} // shift to get whole degree

temp_msb = temp_msb & 0x80; // mask all but the sign bit

if (temp_msb >= 0x80) {temp_lsb = (~temp_lsb)+1;} // twos complement

if (temp_msb >= 0x80) {temp_lsb = (temp_lsb/2);}// shift to get whole degree

if (temp_msb >= 0x80) {temp_lsb = ((-1)*temp_lsb);} // add sign bit

return(temp_lsb);

}

// giao tiep hien thi LCD

// giao tiep hien thi LCD

// giao tiep hien thi LCD

// giao tiep hien thi LCD

 

void delay_short()

{

unsigned int i;

for(i=0;i<300;i++);

}//--------------****------------------//

 

//------------Ham tre 2---------------//

void delay(unsigned int time)   // Thoi gian time ms

{    int i,j;          

for(i=0;i<time;i++)

{

for(j=0;j<500;j++)

{

}

}

}

//-----------------******----------------//

 

//--------------Ham kiem tra co ban (busy_flag)--------------??

void kt_ban()  

{

unsigned int x;

for(x=0;x<1000;x++)

{

}

}//-----------*******--------------------// 

 

//---------------Ham ghi lenh giao tiep voi LCD---------------//

void ghi_lenh(unsigned char lenh)

{

kt_ban();

delay(50);

P0 = lenh;

RS_LCD = 0;               // Chon thanh ghi lenh

//RW_LCD = 0;               // Chon che do ghi du lieu tu 8051 vao LCD

E_LCD = 1;

delay_short();

E_LCD = 0;

}//------------*******-----------------//

 

//-----------Ham ghi ky tu ra LCD------------//

void ghi_kytu(unsigned char kytu)

{

kt_ban();

delay(50);

P0 = kytu;

RS_LCD = 1;  // Chon che do doc du lieu tu 8051

//RW_LCD = 0;

E_LCD =1;

delay_short();

E_LCD = 0;

}//------------*******-----------------//

 

 //-----------Ham ghi nhanh ky tu ra LCD------------//

void ghi_nhanh ( unsigned char kytu)

{

delay_short();

P0 = kytu;

RS_LCD = 1;

//RW_LCD = 0;

E_LCD = 1;

delay_short();

E_LCD = 0;

}//--------------***********-------------//

 

//-----------Ham ghi chuoi ky tu ra LCD------------//

void ghi_chuoi(char *str)

{

while(*str)

{

delay(50);

ghi_kytu(*str);

str++;

}

}//-----------********------------//

 

//-----------Ham ghi nhanh chuoi ky tu ra LCD------------//

void ghi_nhanh_chuoi(char *str)

{

while(*str){

ghi_nhanh(*str);

str++;

}

}

//-----------************------------//

 

//-----------Ham ghi so nguyen ra LCD------------//

void ghi_so_nguyen(unsigned char so) 

        unsigned char a,b,c; 

        a=so/100; // Lay phan tram 

        b=(so-100*a)/10; // Lay phan chuc 

        c=(so-100*a-10*b);   // Lay phan don vi 

      ghi_nhanh(a+48); // Doi ra hang tram, ma ascii 

        ghi_nhanh(b+48);   // Doi ra hang chuc, ma ascii 

        ghi_nhanh(c+48);   // Doi ra hang don vi, ma ascii                

//-----------***********------------//

 

//-----------Ham khoi tao LCD------------//

void setting_LCD()

{

ghi_lenh(0x38);

ghi_lenh(0x01); // Xoa man hinh

ghi_lenh(0x0f); // Co dich hien thi 

}

//-----------**********------------//

 

/*********************************/

/* CON DAY LA CHUONG TRINH CHINH */

/* ----------??????--------------*/

/*********************************/

 

 

 

void main( void ) 

  WDTCTL = WDTPW + WDTHOLD;

  _P2_DIRECT->_BIT.b0=1;

  _P2_DIRECT->_BIT.b1 =1;

  _P2_DIRECT->_BIT.b2 =1;

  _P1_DIRECT->_BYTE=0xff;

  int nhietdo;

   while(1)

{

  nhietdo= Read_Temperature();

    setting_LCD();

 

 

ghi_lenh(0x0c);

ghi_so_nguyen(nhietdo);

delay(300);

}

}

 


**Attention** This is a public forum