当前位置:文档之家› 基于51单片机的看门狗程序

基于51单片机的看门狗程序

/*写入一个字节,cData为写入的数,cAddress为写入地址,bRegion为页*/
{
while((ReadReg()&0x01)==1); //the device is busy
CS=0;
WriteByte(WREN); //when write the wren , the cs must have a high level
CS=1;
CS=0;
if(bRegion==0)
{ WriteByte(WRITE0);} //write the page addr
else
{WriteByte(WRITE1);}
WriteByte(cAddress);
WriteByte(cData);
SCK=0; //
CS=1;
}
uchar ReadEpm(uchar cAddress,bit bRegion)
{
uchar ucLoop;
for(ucLoop=0;ucLoop<8;ucLoop++)
{
if((ucData&0x80)==0) //the MSB send first
{SI=0;}
else
{SI=1;}
Hale Waihona Puke SCK=0;SCK=1;ucData<<=1;
}
}
uchar ReadReg() //read register
/*读入一个字节,cAddress为读入地址,bRegion为页*/
{
uchar cData;
while((ReadReg()&0x01)==1);//the device is busy
CS=0;
if(bRegion==0)
{WriteByte(READ0); }
else
{WriteByte(READ1);}
return 0;
CS=0;
WriteByte(WREN);//when write the WREN, the cs must have a high level
CS=1;
CS=0;
WriteByte(WRSR);
WriteByte(ucData);
CS=1;
return 1;
}
void WriteEpm(uchar cData,uchar cAddress,bit bRegion)
#define READ0 0x03 //
#define READ1 0x0b //
#define WRITE0 0x02 //
#define WRITE1 0x0a //
#define uchar unsigned char
uchar ReadByte() //read a byte from device
基于51单片机的看门狗程序
#include <reg51.h>
sbit CS= P2^7;
sbit SO= P2^6;
sbit SCK= P2^5;
sbit SI= P2^4;
#define WREN 0x06 //
#define WRDI 0x04 //
#define RDSR 0x05 //
#define WRSR 0x01 //
{
bit bData;
uchar ucLoop;
uchar ucData;
for(ucLoop=0;ucLoop<8;ucLoop++)
{
SCK=1;
SCK=0;
bData=SO;
ucData<<=1;
if(bData)
{ ucData|=0x01; }
}
return ucData;
}
void WriteByte(uchar ucData)//write a byte to device
WriteByte(cAddress);
cData=ReadByte();
CS=1;
return cData;
}
main()
{
WriteReg(0x00);//set the watchdog time as 1.4s
CS=1;
CS=0; //reset the watchdog
}
{
uchar ucData;
CS=0;
WriteByte(RDSR);
ucData=ReadByte();
CS=1;
return ucData;
}
uchar WriteReg(uchar ucData) //write register
{
uchar ucTemp;
ucTemp=ReadReg();
if((ucTemp&0x01)==1) //the device is busy
相关主题