掉电保护和看门狗复位C51程序
时间:08-10
来源:互联网
点击:
这个是一个硬件看门狗和用at24c02保存掉电数据的程序,希望对大家有所帮助已经测试通过的.
#includereg51.h>#include "IIC.h"#define ADDR 0x20 //存储地址sbit led1=P2^7; //掉电现象sbit led2=P2^6; //看门狗复位现象sbit WDI=P2^5; //喂狗位uint watch=100; //喂狗次数uchar wbuf,rbuf; //写数据读数据变量bit flag=0; //掉电标志/*******外部中断,下降沿触发**************/void intinit(){EA=1;EX0=1;IT0=1;}/****************掉电中断*****************/void int0() interrupt 0 //掉电数据保护,灯亮了表示进入中断一次{led1=0;wbuf=0x01; write1char(wbuf,ADDR); //掉电标志wbuf=PSW;write1char(wbuf,ADDR+1); //数据保护wbuf=ACC;write1char(wbuf,ADDR+2);led1=1; }/*************watch dog处理******************/void feeddog(){if(watch>0){WDI=~WDI; //喂狗;watch--;led2=0; //判断是否重启;正常喂狗后灯亮;若是超过1.6s后则复位灯灭;}elsewatch=0;}/////////////////////////////////////////main(){watch=100;led1=1;led2=1;delay(10000);/* rbuf=read1char(ADDR); //判断是否为掉电if(rbuf){PSW=read1char(ADDR+1); //恢复重要数据ACC=read1char(ADDR+2);}wbuf=0;write1char(wbuf,ADDR); //读出数据后把该次的状态清零;防止下次再读;*/intinit();while(1){feeddog();}}/*******************************************IIC*************
**********************************************/#ifndef _IIC_H#define _IIC_H_/***************************************************/#includeintrins.h>#define uchar unsigned char#define uint unsigned int#define SLAVE 0xa0 //IIC器件地址 注意全部接地#define Rslave SLAVE+1sbit SDA=P2^0;sbit SCL=P2^1;////////////////////////////////////////////////////////void delay(uint time){for(time;time>0;time--);}///////////////////////////////////////void start_iic(){SDA=1;SCL=1;delay(10);SDA=0;delay(10);SCL=0;}void stop_iic(){SDA=0;SCL=1;delay(10);SDA=1;delay(10);SCL=0;}void ack_iic(){SDA=0;SCL=1;delay(10);SCL=0;SDA=1;}void nack_iic(){SDA=1;SCL=1;delay(10);SCL=0;SDA=0;}////////////////////////* write 1 byte *//////////////////////void write_byte(uchar ch){uchar i;for(i=0;i8;i++){if(ch0x80)SDA=1;elseSDA=0;SCL=1;delay(10);SCL=0;ch=ch1;}SDA=1;SCL=1;delay(10);if(SDA==1)F0=0;elseF0=1;SCL=0;}///////////////////////////* read 1 byte *////////////////////////uchar read_byte(){uchar i;uchar r=0;SDA=1;for(i=0;i8;i++){r=r1;SCL=1;delay(10);if(SDA==1)r++;SCL=0;}return r;}/////////////////////////* write n byte *////////////////////////////bit write_nbyte(uchar slave,uint addr,uchar *str,uchar numb){uchar i;start_iic();write_byte(slave); //write iic addrif(F0==0)return 0;write_byte(addr); //write data addrif(F0==0)return 0;for(i=0;inumb;i++) //write data{write_byte(*str);if(F0==0)return 0;str++;}stop_iic(); //stop iicreturn(1);}//////////////////*写一个字节*//////////////////////////////bit write1char(uchar ch,uint addr){start_iic(); //产生起始信号write_byte(SLAVE); //发送从器件地址if(F0==0) return 0; //检查应答位write_byte(addr); //发送目的地址if(F0==0) return 0;write_byte(ch); //发送8为数据if(F0==0) return 0;stop_iic(); //停止信号return 1;}////////////////////*读一个字节*////////////////////////////////uchar read1char(uint addr){ uchar ch; start_iic(); write_byte(SLAVE);if(F0==0)return 0;write_byte(addr);if(F0==0)return 0;start_iic(); //再次产生起始信号,不能少write_byte(Rslave); //送读控制字if(F0==0)return 0;ch=read_byte(); //读出指定单元的内容nack_iic(); //非应答信号stop_iic();return (ch); }//////////////////////////////////////////////////////////////////////////////////* read n byte *//////////////////////////////////bit read_nbyte(uchar slave,uint addr,uchar *str,uchar numb){uchar i;start_iic();write_byte(slave); //write iic addrif(F0==0)return 0;write_byte(addr); //write data addrif(F0==0)return 0;start_iic(); //再次产生起始信号,不能少write_byte(Rslave); //送读控制字if(F0==0)return 0;for(i=0;inumb-1;i++) //{*str=read_byte();ack_iic();str++;}*str=read_byte();nack_iic();stop_iic();return(1);}/************************************************************/#endif
- 掉电保护和低功耗设计(11-28)
- 单片机--掉电保护24C02(c51)(11-22)
- 单片机系统中的掉电保护电路(10-16)
- 单片机电路中掉电保护及保护方法(09-20)
- 单片机系统与掉电保护电路的可靠性提高方案(08-17)
- Reset与嵌入式系统应用的稳定性(01-09)