51单片机c语言看门狗程序怎么写看门狗在51单片机电路里的作用是防止程序“跑飞”、“死机”后,系统不动作,而采取复位的办法“唤醒”系统。
89S51、89S52系列单片机自带有看门狗功能,片内数据区A6H寄存器具有看门狗功能,使用很简单:
i
cludereg51hsfrWDTRST0xA6voidmai
WDTRST0x1E初始化看门狗WDTRST0xE1初始化看门狗forWDTRST0x1E喂狗指令
WDTRST0xE1喂狗指令
可见,你只要在程序的大循环体内加一条喂狗指令就行。但这种看门狗功能有限,不是很可靠的,它依靠晶振工作,一旦晶振不起振,就无效了。
实践中多采用外部看门狗的方法,可以选用的芯片很多:MAX708、MAX813、X25045具体编程就要看芯片的参考资料了。
例如:X25045是SPI总线的看门狗芯片,复位端和单片机复位端连接,SPI数据输入你可以选择合适的IO接口。
WRENWRDIRDSRWRSRREADWRITE
0x06
设置写允许位
0x04
复位写允许位
0x05
读状态寄存器
0x01
写状态寄存器
0x030x0b读操作时内部EEPROM页地址
0x020x0a写操作时内部EEPROM页地址
#i
cludereg51hsbitCSP27sbitSOP26sbitSCKP25sbitSIP24
fdefi
eWREN0x06defi
eWRDI0x04defi
eRDSR0x05defi
eWRSR0x01defi
eREAD00x03defi
eREAD10x0bdefi
eWRITE00x02defi
eWRITE10x0adefi
eucharu
sig
edcharucharReadBytereadabytefromdevicebitbDataucharucLoopucharucDataforucLoop0ucLoop8ucLoopSCK1SCK0bDataSOucData1ifbDataucData0x01retur
ucDatavoidWriteByteucharucDatawriteabytetodeviceucharucLoopforucLoop0ucLoop8ucLoopifucData0x800theMSBse
dfirstSI0elseSI1SCK0SCK1ucData1ucharReadRegreadregisterucharucDataCS0WriteByteRDSR
fucDataReadByteCS1retur
ucDataucharWriteRegucharucDatawriteregisterucharucTempucTempReadRegifucTemp0x011thedeviceisbusyretur
0CS0WriteByteWRENwhe
writetheWRENthecsmusthaveahighlevelCS1CS0WriteByteWRSRWriteByteucDataCS1retur
1
voidWriteEpmucharcDataucharcAddressbitbRegio
写入一个字节,cData为写入的数,cAddress为写入地址,bRegio
为页whileReadReg0x011thedeviceisbusyCS0WriteByteWRENwhe
writethewre
thecsmusthaveahighlevelCS1CS0ifbRegio
0
WriteByteWRITE0writethepageaddrelse
WriteByteWRITE1WriteBytecAddressWriteBytecDataSCK0CS1ucharReadEpmucharcAddressbitbRegio
读入一个字节,cAddress为读入地址,bRegio
为页ucharcDatawhileReadReg0x011thedeviceisbusyCS0ifbRegio
0
fWriteByteREAD0else
WriteByteREAD1Wrr