《基于51单片机的看门狗程序》由会员分享,可在线阅读,更多相关《基于51单片机的看门狗程序(5页珍藏版)》请在金锄头文库上搜索。
1、基于51单片机的看门狗程序include sbit CS= P27; sbit SO= P26; sbit SCK= P25; sbit SI= P24; #define WREN 0x06 /#define WRDI 0x04 /#define RDSR 0x05 /#define WRSR 0x01 /#define READ0 0x03 /#define READ1 0x0b /#define WRITE0 0x02 /#define WRITE1 0x0a /#define uchar unsigned charuchar ReadByte() /read a byte from de
2、vicebit bData;uchar ucLoop;uchar ucData;for(ucLoop=0;ucLoop8;ucLoop+)SCK=1;SCK=0;bData=SO;ucData=1;if(bData) ucData|=0x01; return ucData;void WriteByte(uchar ucData)/write a byte to deviceuchar ucLoop;for(ucLoop=0;ucLoop8;ucLoop+)if(ucData&0x80)=0) /the MSB send firstSI=0;elseSI=1;SCK=0;SCK=1;ucData
3、=1;uchar ReadReg() /read registeruchar ucData;CS=0;WriteByte(RDSR);ucData=ReadByte();CS=1;return ucData;uchar WriteReg(uchar ucData) /write registeruchar ucTemp;ucTemp=ReadReg();if(ucTemp&0x01)=1) /the device is busyreturn 0;CS=0;WriteByte(WREN);/when write the WREN, the cs must have a high levelCS=
4、1;CS=0;WriteByte(WRSR);WriteByte(ucData);CS=1;return 1;void WriteEpm(uchar cData,uchar cAddress,bit bRegion)/* 写入一个字节,cData为写入的数,cAddress为写入地址,bRegion为页 */while(ReadReg()&0x01)=1); /the device is busyCS=0;WriteByte(WREN); /when write the wren , the cs must have a high levelCS=1;CS=0;if(bRegion=0) Wr
5、iteByte(WRITE0); /write the page addrelseWriteByte(WRITE1); WriteByte(cAddress);WriteByte(cData);SCK=0; /CS=1;uchar ReadEpm(uchar cAddress,bit bRegion)/* 读入一个字节,cAddress为读入地址,bRegion为页 */uchar cData;while(ReadReg()&0x01)=1);/the device is busyCS=0;if(bRegion=0)WriteByte(READ0); elseWriteByte(READ1);WriteByte(cAddress);cData=ReadByte();CS=1;return cData;main()WriteReg(0x00);/set the watchdog time as 1.4sCS=1;CS=0; /reset the watchdog