Học liệu

Điều khiển nhiệt độ bằng MSP430

  • 16/06/2019
  • Học liệu

//-------KHAI BAO THU VIEN----------

#include "io430.h"

#include "intrinsics.h"

//-------PHAN DINH NGHIA------------

#define DS18B20_DIR  P2DIR

#define DS18B20_IN  P2IN

#define DS18B20_OUT  P2OUT

#define DS18B20_DQ   BIT6

 

 

#define SKIPROM     0xCC

#define CONVERTTEMP     0x44

#define READSCRATCHPAD  0xBE

 

 

#define delayMicroseconds(n) __delay_cycles(n)//chi ap dung voi DOC16Mhz

 

//--------Khai bao ham su dung----------

unsigned int Reset();

void WriteByte(unsigned char);

unsigned int ReadByte();

void delay(unsigned int);

unsigned int i;

volatile unsigned char Maled[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x00,0x90};

 

void main()

{

     WDTCTL = WDTPW + WDTHOLD;  // Stop watchdog timer

     BCSCTL1 = CALBC1_1MHZ;          // Khai bao bien su dung

        DCOCTL  = CALDCO_1MHZ;

     unsigned char Byte0=0;         

     unsigned char Byte1=0;         

     unsigned char nhiet_do;        

     unsigned char ScratchPad[9];   

    

     while (1)           

     {

     if ( Reset()==1 )

                {

           WriteByte ( SKIPROM );         

           WriteByte ( CONVERTTEMP );          

           }

        if ( Reset()==1 )

           {

           WriteByte ( SKIPROM );         

           WriteByte ( READSCRATCHPAD );       

                }

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

           {

             ScratchPad[i] = ReadByte();

           }

 

    

     Byte0 = ScratchPad[0];

     Byte1 = ScratchPad[1];

 

     nhiet_do = (Byte1<<4) + (Byte0>>4);

     nhiet_do &= 0x7f;

        unsigned int k;

        k=49;

 //------- Phan so sanh nhiet do voi cac nguong nhiet do-------------//

        P2DIR |= (BIT3 + BIT4 + BIT5);

        if (k<30)

                {

                  P2OUT &= ~BIT3;

                  P2OUT |= (BIT4 + BIT5);

                }

         if ((k>=30)&&(k<40))

                {

                  P2OUT &= ~BIT4;

                  P2OUT |= (BIT3 + BIT5);

                }

         if ((k>=40))

                {

                  P2OUT &= ~BIT5;

                  P2OUT |= (BIT3 + BIT4);

                }

//-----------------------------------------------------------------//

//-----------------Phan hien thi nhiet do--------------------------//

        P1DIR = 0xff;

        P2DIR |= (BIT0 + BIT1);

        unsigned int chuc,don_vi;

        chuc=k/10;

        don_vi=k%10;

        P1OUT = Maled[chuc];

        P2OUT |= BIT0;

        P2OUT &= ~BIT1;

        delayMicroseconds( 100 );

        P1OUT = Maled[don_vi];

        P2OUT &= ~BIT0;

        P2OUT |= BIT1;

        delayMicroseconds( 100 );

//-----------------------------------------------------------------//

     }

}

 

//-----------------------------------------------------------------//

//-----------------------------------------------------------------//

unsigned int Reset( void )

{

  unsigned char check;

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

  DS18B20_DIR |= DS18B20_DQ;         // dat DS lam ngo ra      

  DS18B20_OUT &= ~DS18B20_DQ;        // DS18B20_DQ=0                 /**

  DS18B20_DIR &= ~DS18B20_DQ;       

  delayMicroseconds( 70 );           loi

 

  if( 0 == (DS18B20_IN & DS18B20_DQ))

  {

    check = 1;                 

  }

  else

  {

    check = 0;                 

  }

 

  delayMicroseconds( 410 );         

 

  return  check;               

}

//-------------------------------------------------------------------//

//-------------      write byte into DS   ---------------------//

//-------------------------------------------------------------------//

void WriteByte(unsigned char work )

{

  unsigned char n;

  for(n=8; n!=0; n--)

  {

    if ( (work & 0x01) == 1 )

    {

      DS18B20_DIR |= DS18B20_DQ;          

      DS18B20_OUT &= ~DS18B20_DQ;   // DS

      delayMicroseconds( 5 );        //

 

      DS18B20_DIR &= ~DS18B20_DQ;   

      delayMicroseconds( 60);       

    }

    else

    {

      DS18B20_DIR |= DS18B20_DQ;          

      DS18B20_OUT &= ~DS18B20_DQ;  

      delayMicroseconds( 60 );      

 

      DS18B20_DIR &= ~DS18B20_DQ;   

    }

    work >>=1;

  }

}

//---------------------------------------------------------------------//

//--------------     read byte from DS    -----------------------//

//---------------------------------------------------------------------//

unsigned int ReadByte(void)

{

  unsigned char ketqua=0;

  unsigned int n=0;

  unsigned char ketqua_ds=0;

 

  for ( n=8; n>0; n-- )

  {

    DS18B20_DIR |= DS18B20_DQ;            

    DS18B20_OUT &= ~DS18B20_DQ;           

    delayMicroseconds( 5 );    

 

    DS18B20_DIR &= ~DS18B20_DQ;           

    delayMicroseconds( 5 );

 

    ketqua_ds = DS18B20_IN & DS18B20_DQ;  

    delayMicroseconds( 50 );

    ketqua = (ketqua >> 1) | (ketqua_ds << 7);

  }

  return ketqua;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Các tin khác