cosplay001 发表于 2011-12-7 10:51:00

求助DS2431读写程序,望各位有做过的进来赐教!

各位坛友大家好!本人在该论坛逛了好久了,这次是第一次发帖,由于本人学习单片机不久,现在有一个程序问题想请教各位!我在写一个DS2431的读写程序,可是发现怎么也写不了,希望有做过的帮我看看!不胜感激!程序如下:
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
#include <string.h>

sbit DQ=P3^3;         //定义DS2431端口
bitDS2431err;       //DS2431的错误标志
bit read_flag= 0 ;
bit presence;
unsigned char gh;
unsigned char data Writedata={0x00,0x01,0x02,0x03,0x04,0x6,0x07,0x08};
/*******************************************************************/
/*                                                               */
/*us级延时函数                                                   */
/*                                                               */
/*******************************************************************/

void delay(unsigned int num)   //延时程序
{
while( --num ) ;
}

void Delay()          //1us延时

{
        _nop_();
        _nop_();

}
/*******************************************************************/
/*                                                               */
/*初始化DS2431                                                    */
/*                                                               */
/*******************************************************************/
Init_DS2431(void )       //初始化DS2431
{

DQ=1;               
delay(8);       

DQ=0;               //将DQ信号线拉低
delay(100);          //保持DQ低电平480us

DQ=1;                //将DQ信号拉高、释放总线
delay(8);         //保持DQ高电平70us

presence = DQ;       //保存当前总线状态
delay(100);         
DQ=1;
return(presence);   //返回是否有设备将总线拉低
}

/*******************************************************************/
/*                                                               */
/* 读一位(bit)                                                   */
/*                                                               */
/*******************************************************************/
unsigned char read_bit(void)   //从单总线上读取一个数据位
{
bit i;
DQ=0;            //启动读时序
Delay();      //1us

DQ=1;            //释放总线,等待从机返回数据位
Delay();       //1us

i=DQ;
delay(2);              //30us
DQ=1;
return(i);       //返回总线状态
}

/*******************************************************************/
/*                                                               */
/* 写一位(bit)                                                   */
/*                                                               */
/*******************************************************************/
void write_bit(char bitvalue)//向单总线设备写入一个数据位
{
DQ=0;             //启动写时序
Delay();          //1us

if(bitvalue==1) DQ=1;            //写1
delay(5);      //等待写时序结束        25us
DQ=1;             //释放总线
delay(2);      //等待写时序结束        10us

}

/*******************************************************************/
/*                                                               */
/* 读一字节(byte)                                                   */
/*                                                               */
/*******************************************************************/
read_byte(void)          //从单总线上读一个字节数据
{
unsigned char i=0;
unsigned char value=0;                     
for(i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i;//如果当前读取的数据位为1,将返回字节对应的位置1
delay(4);                                           //等待释放总线
}                        
return(value);                        //返回读取的数据
}
/*******************************************************************/
/*                                                               */
/* 写一字节(byte)                                                   */
/*                                                               */
/*******************************************************************/
void write_byte(unsigned char bytevalue)   //向单总线写一个字节
{
unsigned char i=0;
unsigned char temp;                        
for(i=0;i<8;i++)
{
temp = bytevalue>>i;      //将要写的数据字节右移i位
temp&= 0x01;          //取出数据字节的第i位
write_bit(temp);            //向总线写一个数据位
}
delay(5);                   //等待写时序结束
}

/*******************************************************************/
/*                                                               */
/* 跳过ROM(0XCC)                                                   */
/*                                                               */
/*******************************************************************/        
bit skip_matchRom(void)                //发出跳过ROM匹配命令
{
    bit tmp=1;
        if(Init_DS2431()) return tmp;         //如果没有DS2431,返回1
        write_byte(0xcc);                     //发出跳过ROM匹配的命令
        tmp = 0;
        return tmp;
}


/********************************************************************************
** 函数名称: read_ds2431
** 功能描述: 读取DS2431数据
** 输 入:        
** 输 出:   
** 全局变量:
** 调用模块:
**
** 作 者:
** 日 期: 2011年11月28日
**---------------------------------------------------------------------------------
***********************************************************************************/

unsigned char read_ds2431(unsigned int Readaddr)
{

        unsigned char ch;
        DS2431err=skip_matchRom();         //发出跳过ROM匹配命令
        write_byte(0xF0);                  //发出读存储器命令
        write_byte((unsigned char)Readaddr);
        write_byte((unsigned char)(Readaddr>>=8));
        ch=read_byte();
        return ch;
}

unsigned char *read_ds2431_str(unsigned int Readaddr)
{
unsigned char ch, i;
        DS2431err=skip_matchRom();//发出跳过ROM匹配命令
        write_byte(0xF0);         //发出读存储器命令
        write_byte((unsigned char)Readaddr);
        write_byte((unsigned char)(Readaddr>>=8));
        for(i=8;i>0;i--)
        {
                ch=read_byte();
        }
        return ch;
}
/*******************************************************************************************
** 函数名称: write_ds2431
** 功能描述: 写数据至ds2431 返回1写入出错,返回0写入正常。
** 输 入:        
** 输 出:   
** 全局变量:
** 调用模块:
**
** 作 者:
** 日 期: 2011年11月28日
**-------------------------------------------------------------------------------------------
********************************************************************************************/
unsigned char write_ds2431(unsigned int Writeaddr, unsigned char *Writedata)
{
unsigned char ch,es,i,hight,low;
unsigned int tem;

        hight=(unsigned char)(Writeaddr>>=4);
        low=(unsigned char)Writeaddr;
        if(skip_matchRom())                   //发出跳过ROM匹配命令
                return 1;
        write_byte(0x0F);         //发送写暂存器命令
        write_byte(low);
        write_byte(hight);
        for(i=0;i<8;i++)
        {
                ch=*Writedata;
                Writedata++;
                write_byte(ch);
        }
        delay(25);

        DS2431err=skip_matchRom();//发出跳过ROM匹配命令
        write_byte(0xAA);         //复制暂存器数据到存储器中
        tem=read_byte();
        tem<<=8;
        tem+=read_byte();
        es=read_byte();
        if(es!=0x07)
                return 1;

        DS2431err=skip_matchRom();//发出跳过ROM匹配命令
        write_byte(0x55);//发出启动转换命令
        write_byte((unsigned char)Writeaddr);
        write_byte((unsigned char)(Writeaddr>>=4));
        write_byte(es);
        for(i=0;i<50;i++)
        delay(100);      //等待写时序结束        510us

        if(read_byte()!=0xAA)
                return 1 ;
        else
                return 0;
}

void init_serialcom( void ) //串口通信初始设定
   {
    SCON = 0x50 ;   //UART为模式1,8位数据,允许接收
    TMOD |= 0x20 ; //定时器1为模式2,8位自动重装
//   PCON |= 0x80 ; //SMOD=1;
    TH1 = 0xFD ;   //Baud:19200 fosc="11".0592MHz
    IE |= 0x90 ;   //Enable Serial Interrupt
    TR1 = 1 ;       // timer 1 run
    TI=1;
       }

//向串口发送一个字符
void send_char_com( unsigned char gh)
         {
            SBUF=gh;
            while (TI== 0);
               TI= 0 ;
          }

//串口接收中断函数
void serial () interrupt 4 using 3
{
    if (RI)
          {
               RI = 0 ;
               gh=SBUF;            
               read_flag= 1 ; //就置位取数标志
            }
}


void main()
{
       init_serialcom();
       Init_DS2431();
        write_ds2431(0x00,Writedata);
        while(1)
        {
      if (read_flag) //如果取数标志已置位,就将读到的数从串口发出
      {
         read_flag= 0 ; //取数标志清0
         send_char_com(read_ds2431(0x00));
      }
        }
}

cosplay001 发表于 2011-12-8 15:02:42

回复【楼主位】cosplay001
-----------------------------------------------------------------------

怎么没人呢?

cosplay001 发表于 2011-12-8 15:03:03

回复【1楼】cosplay001
-----------------------------------------------------------------------

自己顶一下!哈哈!

kiwiwang 发表于 2012-7-3 15:27:43

帮你顶一下

kunkliu 发表于 2013-5-22 12:28:11

{:lol:}{:lol:}{:lol:}{:lol:}{:lol:}{:lol:}{:lol:}{:lol:}
页: [1]
查看完整版本: 求助DS2431读写程序,望各位有做过的进来赐教!