欢迎来到天天文库
浏览记录
ID:44713046
大小:73.00 KB
页数:5页
时间:2019-10-25
《基于51单片机的看门狗程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、基于51单片机的看门狗程序#includesbitCS=P2^7;sbitSO=P2^6;sbitSCK=P2^5;sbitSI=P2^4;#defineWREN0x06//#defineWRDI0x04//#defineRDSR0x05//#defineWRSR0x01//#defineREAD00x03//#defineREAD10x0b//#defineWRITE00x02//#defineWRITE10x0a//#defineucharunsignedcharucharReadByte()//readabytefromdevice{bitbData;ucharu
2、cLoop;ucharucData;for(ucLoop=0;ucLoop<8;ucLoop++){SCK=1;SCK=0;bData=SO;ucData<<=1;if(bData){ucData
3、=0x01;}}returnucData;}voidWriteByte(ucharucData)//writeabytetodevice{ucharucLoop;for(ucLoop=0;ucLoop<8;ucLoop++){if((ucData&0x80)==0)//theMSBsendfirst{SI=0;}else{SI=1;}SCK=0;SCK=1;ucData<<=1;}}uchar
4、ReadReg()//readregister{ucharucData;CS=0;WriteByte(RDSR);ucData=ReadByte();CS=1;returnucData;}ucharWriteReg(ucharucData)//writeregister{ucharucTemp;ucTemp=ReadReg();if((ucTemp&0x01)==1)//thedeviceisbusyreturn0;CS=0;WriteByte(WREN);//whenwritetheWREN,thecsmusthaveahighlevelCS=1;CS=0;WriteByte(WRSR
5、);WriteByte(ucData);CS=1;return1;}voidWriteEpm(ucharcData,ucharcAddress,bitbRegion)/*写入一个字节,cData为写入的数,cAddress为写入地址,bRegion为页*/{while((ReadReg()&0x01)==1);//thedeviceisbusyCS=0;WriteByte(WREN);//whenwritethewren,thecsmusthaveahighlevelCS=1;CS=0;if(bRegion==0){WriteByte(WRITE0);}//writethepageadd
6、relse{WriteByte(WRITE1);}WriteByte(cAddress);WriteByte(cData);SCK=0;//CS=1;}ucharReadEpm(ucharcAddress,bitbRegion)/*读入一个字节,cAddress为读入地址,bRegion为页*/{ucharcData;while((ReadReg()&0x01)==1);//thedeviceisbusyCS=0;if(bRegion==0){WriteByte(READ0);}else{WriteByte(READ1);}WriteByte(cAddress);cData=ReadBy
7、te();CS=1;returncData;}main(){WriteReg(0x00);//setthewatchdogtimeas1.4sCS=1;CS=0;//resetthewatchdog}
此文档下载收益归作者所有