void delay(unsigned int num) //延時程序
{
while( --num ) ;
}
void Delay() //1us延時
{
_nop_();
_nop_();
}
Init_DS2431() //初始化DS2431
{
bit Presece;
DQ=1;
//delay(5);
_nop_();
DQ=0; //將DQ信號線拉低
delay(120); //保持DQ低電平480us 測得483
DQ=1; //將DQ信號拉高、釋放總線
delay(15); //保持DQ高電平70us 測得64
Presece = DQ; //保存當前總線狀態
delay(140); //測得時間為563
DQ=1;
DQ=1;
return Presece; //返回是否有設備將總線拉低
}
read_bit() //從單總線上讀取一個數據位
{
bit i;
DQ=1;
_nop_();
DQ=0; //啟動讀時序
Delay(); //1us
Delay(); //1us
Delay(); //1us
Delay(); //1us
DQ=1; //釋放總線,等待從機返回數據位
Delay(); //1us
Delay(); //1us
i=DQ;
delay(15);
DQ=1;
return i; //返回總線狀態
}
void write_bit(unsigned char bitvalue) //向單總線設備寫入一個數據位
{ DQ=1;
_nop_();
DQ=0; //啟動寫時序
Delay(); //1us
Delay(); //1us
Delay(); //1us
Delay(); //1us
Delay(); //1us
if(bitvalue) DQ=1; //寫1
delay(15);
DQ=1; //釋放總線
delay(2); //等待寫時序結束 10us
}
read_byte() //從單總線上讀一個字節數據
{
int i,value=0;
for(i=0;i<8;i++)
{
if(read_bit()) value=value|(0x01<<i); //如果當前讀取的數據位為1,將返回字節對應的位置1
}
delay(2); //等待釋放總線
return value; //返回讀取的數據
}
void write_byte(unsigned char bytevalue) //向單總線寫一個字節
{
unsigned char i,temp;
for(i=0;i<8;i++)
{
temp = bytevalue>>i; //將要寫的數據字節右移i位
temp = temp &0x01; //取出數據字節的第i位
write_bit(temp); //向總線寫一個數據位
}
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;
}
read_ds2431(unsigned int Readaddr)
{
int ch[8],i;
DS2431err=skip_matchRom(); //發出跳過ROM匹配命令
write_byte(0xF0); //發出讀存儲器命令
write_byte((unsigned char)Readaddr);
write_byte((unsigned char)(Readaddr>>=8));
for(i=0;i<8;i++)
{ ch=read_byte();
printf("%d",ch);}
return ch;
}
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(250); //等待寫時序結束 510us
if(read_byte()!=0xAA)
return 1;
return 0;
}
/***********************************************************
* 函數說明:毫秒級延時程序 *
* 輸入: 需要延時t毫秒 *
* 輸出: 無 *
* 調用函數:無 *
***********************************************************/
void _delay_ms(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<125;j++);
}
/***********************************************************
* 函數說明:寫指令到LCD12864 *
* 輸入: cmd,待輸入的指令 *
* 輸出: 無 *
* 調用函數:無 *
***********************************************************/
void LcdWriteCode(unsigned char cmd)
{
P0=cmd;
lcd_rs=0;
lcd_rw=0;
lcd_e=0;
_delay_ms(1);
lcd_e=1;
}
/***********************************************************
* 函數說明:寫數據到LCD12864 *
* 輸入: dat,待輸入的數據 *
* 輸出: 無 *
* 調用函數:無 *
***********************************************************/
void LcdWriteData(unsigned char dat)
{
P0=dat;
lcd_rs=1;
lcd_rw=0;
lcd_e=0;
_delay_ms(1);
lcd_e=1;
}
/***********************************************************
* 函數說明:LCD初始化 *
* 輸入: 無 *
* 輸出: 無 *
* 調用函數:無 *
***********************************************************/
void LcdInit(void)
{
LcdWriteCode(0x30); //基本指令集
_delay_ms(1);
LcdWriteCode(0x0c); //顯示;關閉光標;關閉反白
_delay_ms(1);
LcdWriteCode(0x01); //清屏
_delay_ms(20);
LcdWriteCode(0x06); //顯示光標位置
}
read_ds2431_str(unsigned int Readaddr)
{
unsigned char ch[8],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[8-i]=read_byte();
}
return ch;
}
main()
{
while(1)
{
{read_ds2431(0x60);
write_ds2431(0x60,0x01);
read_ds2431(0x60);
}
}
}