MSP430 Launchpad DS18B20程序得到结果一直是0 郁闷了 求指教

2019-03-24 13:37发布

代码如下:****************ds18b20.c***************
#include <msp430g2553.h>
#include "1602.h"
#include "ds18b20.h"

#define DQ1 P2OUT |= BIT3
#define DQ0 P2OUT &= ~BIT3
uchar table[4];

/*******************************************
函数名称:DelayNus
功    能:实现N个微秒的延时
参    数:n--延时长度
返回值  :无
说明    :定时器A的计数时钟是1MHz,CPU主频8MHz
           所以通过定时器延时能够得到极为精确的
           us级延时
********************************************/
void DelayNus(uint n)
{
    while (n != 0)
     {
       n--;
     }

}
/*******************************************
函数名称:Init_18B20
功    能:对DS18B20进行复位操作
参    数:无
返回值  :初始化状态标志:1--失败,0--成功
********************************************/
uchar Init_18B20(void)
{
     uchar Error;

     _DINT();
     DQ1;
     DelayNus(20);
     DQ0;
     DelayNus(500);
     DQ1;
     DelayNus(60);
     P2DIR &=~ BIT3;
     _NOP();
     if(P2IN & BIT3)      
     {
         Error = 1;          //初始化失败
         P2DIR |= BIT3;
     }
     else
     {
         Error = 0;          //初始化成功
         P2DIR |= BIT3;
         DQ1;
     }
     _EINT();

     DelayNus(400);
     DQ1;
     return Error;
}
/*******************************************
函数名称:Write_18B20
功    能:向DS18B20写入一个字节的数据
参    数:wdata--写入的数据
返回值  :无
********************************************/
void Write_18B20(uchar wdata)
{
     uchar i;

     _DINT();
     for(i = 0; i < 8;i++)
     {
         DQ0;
         DelayNus(7);            //延时7us
         if(wdata & 0X01)    DQ1;
         else                DQ0;
         wdata >>= 1;
         DelayNus(60);           //延时60us
         DQ1;
     }
     _EINT();
}
/*******************************************
函数名称:Read_18B20
功    能:从DS18B20读取一个字节的数据
参    数:无
返回值  :读出的一个字节数据
********************************************/
uchar Read_18B20(void)
{
     uchar i;
     uchar temp = 0;

     _DINT();
     for(i = 0;i < 8;i++)
     {
         DQ0;
         _NOP();
         _NOP();
         temp >>= 1;
         DQ1;
         DelayNus(7);            //延时7us
         P2DIR &= ~BIT3;
         _NOP();
         if(P2IN & BIT3)   temp |= 0x80;
         DelayNus(45);           //延时45us
         P2DIR |= BIT3;
         //DelayNus(10);           //延时10us
     }
     _EINT();

     return  temp;
}

/*******************************************
函数名称:Skip
功    能:发送跳过读取产品ID号命令
参    数:无
返回值  :无
********************************************/
void Skip(void)
{
     Write_18B20(0xcc);
}
/*******************************************
函数名称:Convert
功    能:发送温度转换命令
参    数:无
返回值  :无
********************************************/
void Convert(void)
{
     Write_18B20(0x44);
}
/*******************************************
函数名称:Read_SP
功    能:发送读ScratchPad命令
参    数:无
返回值  :无
********************************************/
void Read_SP(void)
{
     Write_18B20(0xbe);
}
/*******************************************
函数名称:ReadTemp
功    能:从DS18B20的ScratchPad读取温度转换结果
参    数:无
返回值  :读取的温度数值
********************************************/
uint ReadTemp(void)
{
     uchar temp_low;
     uint  temp;

     temp_low = Read_18B20();      //读低位
     temp = Read_18B20();     //读高位
     temp = (temp<<8) | temp_low;

     return  temp;
}
/*******************************************
函数名称:ReadTemp
功    能:控制DS18B20完成一次温度转换
参    数:无
返回值  :测量的温度数值
********************************************/
uint Do1Convert(void)
{
     uchar i;

     do
     {
         i = Init_18B20();
     }
     while(i);
     Skip();
     Convert();
     for(i = 20;i > 0;i--)  
         DelayNus(60000); //延时800ms以上
     do
     {
         i = Init_18B20();
     }
     while(i);
     Skip();
     Read_SP();
     return ReadTemp();
}

/**************************************************************
函数名称:ShowTemp
功    能:在1602上显示温度值
参    数:无
返回值  :无
**************************************************************/
void ShowTemp()
{
   uint tem;
   tem = Do1Convert();
   table[3] = tem/100 + 0x30;
   table[2] = (tem/10)%10 + 0x30;
   table[1] = tem%10 + 0x30;

   LCD_write_char(10,1,table[3]);
   LCD_write_char(11,1,table[2]);
   LCD_write_char(12,1,table[1]);
}



***************DS18B20.h**************
#ifndef _DS18B20_H
#define _DS18B20_H
typedef unsigned char uchar;
typedef unsigned int  uint;

void DelayNus(unsigned int n);
unsigned char Init_18B20(void);
void Write_18B20(unsigned char wdata);
unsigned char Read_18B20(void);
void Skip(void);
void Convert(void);
void Read_SP(void);
unsigned int ReadTemp(void);
unsigned int Do1Convert(void);
void ShowTemp();
#endif




****************************************** 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
wstt
2019-03-24 19:34
lz比较下论坛上的资料
http://bbs.eeworld.com.cn/thread-315565-1-8.html

一周热门 更多>

相关问题

    相关文章