钻石会员
主题
回帖0
积分11256
阅读权限50
注册时间2010-2-18
最后登录1970-1-1
在线时间 小时
|
楼主 |
发表于 2012-7-18 13:22
|
显示全部楼层
网上找了个代码,不知道能不能编译出来
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
sbit DQ=P1^6; //定义DS2431端口
bit DS2431err; //DS2431的错误标志
/*void delay(INT8U cnt)
{
data INT16U i;
for(i=cnt;i>0;i--);
}*/
void delay(unsigned int num) //延时程序
{
while( --num ) ;
}
void Delay() //1us延时
{
_nop_();
_nop_();
}
Init_DS2431(void) //初始化DS2431
{
bit Presece;
DQ=1;
delay(5);
DQ=0; //将DQ信号线拉低
delay(120); //保持DQ低电平480us
DQ=1; //将DQ信号拉高、释放总线
delay(14); //保持DQ高电平70us
Presece = DQ; //保存当前总线状态
delay(140);
return Presece; //返回是否有设备将总线拉低
}
read_bit(void) //从单总线上读取一个数据位
{
bit i;
DQ=0; //启动读时序
Delay(); //1us
Delay(); //1us
Delay(); //1us
Delay(); //1us
DQ=1; //释放总线,等待从机返回数据位
Delay(); //1us
Delay(); //1us
i=DQ;
delay(6); //30us
DQ=1;
return i; //返回总线状态
}
void write_bit(unsigned char bitvalue) //向单总线设备写入一个数据位
{
DQ=0; //启动写时序
Delay(); //1us
Delay(); //1us
Delay(); //1us
Delay(); //1us
Delay(); //1us
if(bitvalue) DQ=1; //写1
delay(5); //等待写时序结束 25us
DQ=1; //释放总线
delay(2); //等待写时序结束 10us
}
void read_byte(void) //从单总线上读一个字节数据
{
unsigned char i,value=0;
EA=0; //关中断
for(i=0;i<8;i++)
{
if(read_bit()) value=value|(0x01<<i); //如果当前读取的数据位为1,将返回字节对应的位置1
}
EA=1;//打开中断
delay(2); //等待释放总线
return value; //返回读取的数据
}
void write_byte(unsigned char bytevalue) //向单总线写一个字节
{
unsigned char i,temp;
EA=0; //关中断
for(i=0;i<8;i++)
{
temp = bytevalue>>i; //将要写的数据字节右移i位
temp = temp &0x01; //取出数据字节的第i位
write_bit(temp); //向总线写一个数据位
}
EA=1;//打开中断
delay(2); //等待写时序结束
}
bit skip_matchRom(void) //发出跳过ROM匹配命令
{
bit tmp=1;
if(Init_DS2431()) return tmp; //如果没有DS2431,返回0
write_byte(0xcc); //发出跳过ROM匹配的命令
tmp = 0;
return tmp;
}
void read_ds2431(unsigned int Readaddr)
{
unsigned char ch;
// WDTCN=0xa5;//喂狗
DS2431err=skip_matchRom(); //发出跳过ROM匹配命令
write_byte(0xF0); //发出读存储器命令
write_byte((unsigned char)Readaddr);
write_byte((unsigned char)(Readaddr>>=8));
ch=read_byte();
// WDTCN=0xa5;//喂狗
return ch;
}
INT8U *read_ds2431_str(unsigned int Readaddr)
{
unsigned char ch[8],i;
WDTCN=0xa5;//喂狗
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[8-i]=read_byte();
WDTCN=0xa5;//喂狗
}
return ch;
}
INT8U write_ds2431(unsigned int Writeaddr, unsigned char *Writedata)
{
unsigned char ch,es,i,hight,low;
unsigned int tem;
// WDTCN=0xa5;//喂狗
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(250); //等待写时序结束 510us
if(read_byte()!=0xAA)
return 1;
// WDTCN=0xa5;//喂狗
return 0;
} |
|