找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3245|回复: 0
收起左侧

430驱动DS18b20驱动不了

[复制链接]
ID:64384 发表于 2014-7-25 17:04 | 显示全部楼层 |阅读模式
板子就接了个32K的晶振,设置DS18B20DE 的信号口选择在P1.6调试了三个程序,debug都读不到数据,附上程序,下面一个是被我屏蔽的程序。望大神指教。
#include<msp430x14x.h>
#define  delay_10uS()  ( _NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP())
#define  delay_6uS()   ( _NOP(),_NOP(),_NOP(),_NOP(),_NOP())
#define  delay_9uS()   ( _NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP())
#define  delay_15uS()   ( _NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP(),_NOP())
#define     uchar   unsigned char
#define     uint    unsigned    int
#define DQ_OUT P1DIR|=BIT6
#define DQ_IN P1DIR&=~BIT6
#define DQ_LOW P1OUT&=~BIT6
#define DQ_HIGH P1OUT|=BIT6
#define DQ_DATA P1IN&BIT6
uint j=0;
uint l=0;
uchar MSB; //温度高字节
uchar LSB; //温度低字节
int t1=0; //温度整数部分数值
uint t2=0; //温度小数部分数值
uchar flag; //负温度标志
void Delayus(uint us)
{
    while(us--)
    {
        _NOP();_NOP();_NOP();_NOP();
        _NOP();_NOP();_NOP();_NOP();
        
    }
}
//初始化DS18B20
void DS18B20Init(void)
{
    DQ_OUT;//设置为输出方向
    DQ_LOW;//拉低总线
    Delayus(50);
    DQ_HIGH;//释放总线
    Delayus(3);
    DQ_IN;//设置为输入方向
    while(DQ_DATA);//等待应答信号
    while(~DQ_DATA);//等待释放总线
    delay_10uS();
    delay_15uS();
}
//读一个字节
uchar ReadByte(void)
{
    uchar i;
    uchar ReadData=0;
    for(i=0;i<8;i++)
    {
        ReadData>>=1;
        DQ_LOW;
        delay_6uS();
        DQ_HIGH;
        delay_9uS();
        DQ_IN;
        _NOP();
        if(DQ_DATA) ReadData|=0x80;
        delay_10uS();
        delay_10uS();
        delay_10uS();
        delay_10uS();
        DQ_OUT;
        DQ_HIGH;
        delay_10uS();
    }
    return ReadData;
}
//写一个字节
void WriteByte(uchar WriteData)
{
    uchar i;
    uchar tmpData;
    for(i=0;i<8;i++)
    {
        DQ_OUT;
        DQ_LOW;
        tmpData=WriteData&0x80;
        delay_6uS();
            if(tmpData)
            {
                 DQ_HIGH;
            }
        else
        {
             DQ_LOW;
        }
        delay_10uS();
        delay_10uS();
        delay_10uS();
        delay_10uS();
        delay_10uS();
      DQ_HIGH;
        WriteData<<=1;
        }
    delay_10uS();
}
//温度计算程序
void GetT()
{
    if((MSB&0xF0)>0)
    { //判断是否为负温度
        flag=1;
    }
    else
    {
         flag=0;
    }
    if(flag)
    { //如果为负温度取反加1
        MSB=~MSB;
        LSB=~LSB+1;
    }
    t1=MSB<<4; //得到温度整数部分
    t1|=(LSB>>4);
    t2=(uint)((LSB&0x0F)*0.0625*10000); //得到温度小数部分并扩大10000 倍
}
void main()
{
    WDTCTL=WDTPW+WDTHOLD;//关闭看门狗
        
    while(1)
    {
        DS18B20Init();
        WriteByte(0xCC); //跳过ROM 配置
        WriteByte(0x44);//启动温度转换
        
        Delayus(1000);
        
        DS18B20Init();
        WriteByte(0xCC); //跳过ROM 配置
        WriteByte(0xBE); //跳过ROM 配置
        LSB=ReadByte(); //读温度数据低字节
        MSB=ReadByte(); //读温度数据高字节
        GetT(); //计算温度
   
    }
}











/*
uchar init_18b20(void)
{
    uchar Error;
    DQ_out;
    _DINT();
    DQ0;
    DelayNus(50);
    DQ1;
    DelayNus(6);
    DQ_in;
    _NOP();
    if(DQ_val)      
    {
        Error = 1;          //初始化失败
    }
    else
    {
        Error = 0;          //初始化成功
    }
    DQ_out;
    DQ1;
    DelayNus(40);  
    return Error;
}
void Write_18B20(uchar wdata)
{
    uchar i;
    for(i = 0; i < 8;i++)
    {
        DQ0;
        delay_6uS();            //延时6us
        if(wdata & 0X01)    DQ1;
        else                DQ0;
        wdata >>= 1;
        DelayNus(5);           //延时50us
        DQ1;
        delay_10uS();           //延时10us
    }
  
}
uchar Read_18B20(void)
{
    uchar i;
    uchar temp = 0;
    for(i = 0;i < 8;i++)
    {
        temp >>= 1;
        DQ0;
         delay_6uS();            //延时6us
        DQ1;
         delay_9uS();            //延时9us
        DQ_in;
        _NOP();
        if(DQ_val)   temp |= 0x80;
        DelayNus(5);           //延时45us
        DQ_out;
        DQ1;
         delay_10uS(); ;           //延时10us
    }
    return  temp;
}
void Convert(void)
{
    Write_18B20(0x44);
}
void Read_SP(void)
{
    Write_18B20(0xbe);
}
void Skip(void)
{
    Write_18B20(0xcc);
}
uint ReadTemp(void)
{
    uchar temp_low;
    uint  temp;
   
    temp_low = Read_18B20();      //读低位
    temp = Read_18B20();     //读高位
    temp = (temp<<8) | temp_low;
   
    return  temp;
}
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();
}
void main(void)
{
    WDTCTL=WDTPW+WDTHOLD;
    P1DIR=0x00;
    P1SEL=0x00;
   
    while(1)
    {
       Do1Convert();  
    }
}
*/

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表