⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sensor0606.c

📁 DS18B20测温程序,应用C语言
💻 C
字号:
#include<msp430x14x.h> 
#define DQ1 P4OUT|=BIT4
#define DQ0 P4OUT&=~BIT4
float   Temper=0.0;
int     temperature=0;
int     temperature1=0;
unsigned char Error = 0;
//unsigned char degree;
//----------------------------------
//功能:us 级别延时
// n=10,则延时10*5+6=56uS
//----------------------------------
void DelayNus(unsigned int n)
{
    while(n--){};
}
//-----------------------------------
//功能:写18B20
//-----------------------------------
void Write_18B20(unsigned char n)
{
    unsigned char i;
    for(i=0;i<8;i++)
    {
        DQ0;
       // DelayNus(1);//延时13us 左右
		_NOP();_NOP();_NOP();_NOP();_NOP();
        if((n&0X01)==0X01) 
         DQ1;
        else 
         DQ0;
        n=n>>1;
        DelayNus(3);//延时50us 以上
        DQ1;
    }
}
//------------------------------------
//功能:读取18B20
//------------------------------------
unsigned char Read_18B20(void)
{
    unsigned char i;
    unsigned char temp;
    for(i=0;i<8;i++)
    {
        temp=temp>>1;
        DQ0;
        _NOP();//延时1us
        DQ1;
         _NOP();_NOP();//延时5us
        _NOP();_NOP();_NOP();
        P4DIR&=~BIT4;
        if((P4IN&BIT4)==0)
        {
            temp=temp&0x7F;
        }
        else
        {
            temp=temp|0x80;
        }
        DelayNus(2);//延时40us
        P4DIR|=BIT4;
        DQ1;
    }
    return(temp);
}
//-----------------------------------
void Init (void)
{ 
    P1OUT=~BIT0;
 //while(1)
 //{  
     //P1OUT=~BIT0;
    DQ1;
    _NOP();
    DQ0;
    DelayNus(51);//延时500us
    //P1OUT=~BIT0;
    DQ1;
    DelayNus(1);//延时16~60us
    //DQ0;
   // DelayNus(15);
   // DQ1;
 
     P4DIR&=~BIT4;
     
    if((P4IN&BIT4)==BIT4)        //0001 1111b=1f
    {
        Error =1;    //失败1
        P4DIR|=BIT4;
       P1OUT=~BIT0;
    }
    else
    {
        Error = 0;//初始化成功
        P1OUT=BIT0;
        DelayNus(5);
        P4DIR|=BIT4;
        DQ1;
     }
 // }
}
//----------------------------------
void Skip(void)
{
    Write_18B20(0xcc);
   
}
//----------------------------------
void Convert (void)
{
    Write_18B20(0x44);
    
}

//----------------------------------
void ReadDo (void)
{
    Write_18B20(0xbe);
}
//----------------------------------

void ReadTemp (void)
{
    
    char temp_low,temp_high; //温度值
    temp_low=Read_18B20(); //读低位
	//P1OUT=temp_low;
    temp_high=Read_18B20(); //读高位
   // temperature=(temp_high&0x0f);
   // temperature<<=8;
   // temperature|=temp_low;
   temperature=(unsigned int)temp_high;
   temperature1=(unsigned int)temp_low;
   
    //Temper=temperature*0.0625;
    //temperature1=(unsigned int)temp_low;
  // return(Temper);
}

void GetTemp(void)
{
        Init();
        Skip();
        Convert();
        DelayNus(60000);
        DelayNus(60000);
        DelayNus(60000);//延时1s以上
        Init();
        Skip();
        ReadDo();
        ReadTemp();
        
}
void Init0(void)//0发
{
 UCTL0=CHAR;   
 UTCTL0=SSEL0;   
 UBR00=0x03; 
 UBR10=0x00; 
 UMCTL0=0x4A; 
 ME1|=UTXE0;  
 //IE1|=UTXIE0; 
 P3SEL|=0x10;  
 P3DIR|=0x10;
 //return;
}
void main(void)
{ 

   WDTCTL=WDTPW+WDTHOLD;
  _DINT();
   Init0();
  P4DIR |=BIT4;
  P1DIR|=BIT0;
    DQ1;
  _EINT();
  while(1)
        {
       // P1OUT=BIT0;
           GetTemp();
        TXBUF0=(unsigned int)(temperature);
        
        //TXBUF0=(unsigned long int)(Temper*1000);
        while((UTCTL0&0x01)==0);
        TXBUF0=(unsigned int)(temperature1);
        
        //TXBUF0=(unsigned long int)(Temper*1000);
        while((UTCTL0&0x01)==0);
        TXBUF0=0x00;
        while((UTCTL0&0x01)==0);
       // P1OUT=BIT0;
       // P4OUT=BIT4;
       // _NOP();
       //  P1OUT=~BIT0;
      // DelayNus(3);
      //DQ1;
      //P1DIR|=BIT0;
    
    //else
    //{
        
    //}
       //DelayNus(1);
        // P4OUT=~BIT4;
        // P1OUT=~BIT0;
         //_NOP();_NOP();_NOP();
       // DelayNus(3);
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -