softdog.c
;汇编
ERRORP SEGMENT CODE
PUBLIC error
RSEG ERRORP
error:
CLR EA
MOV DPTR,#ERR1
PUSH DPL
PUSH DPH
RETI
ERR1:
CLR A
PUSH ACC
PUSH ACC
RETI
END
//以下程序只是一个范例
void error(void);
//定时器0,清除定时器1的计时
void int_t0(void) interrupt 1 {
TL0=TL0+68;TH0=0xfd; //700
TH1=0xfb;
}
//定时器1,中断作为看门狗
void int_t1(void) interrupt 3 {
error(); //复位
}
unsigned char adds;
unsigned char b_job0[5][3]; //job0用到的数据,如A/D采集的数据,一共5组,每组3个,其中一个有效,其余2个备用
unsigned char b_job1[5][3];
unsigned char b_job2[5][3];
void job0(void) {
unsigned char i;
adds=0;
for (i=0;i<5;i++) { //数据采集部分,此处简化过程
b_job0[i][0]=b_job0[i][1]=b_job0[i][2]=123;
}
while (1) {}
}
void job1(void) {
unsigned char i;
adds=1;
for (i=0;i<5;i++) { //数据采集部分,此处简化过程
b_job0[i][0]=b_job0[i][1]=b_job0[i][2]=23;
}
while (1) {}
}
void job2(void) {
unsigned char i;
adds=2;
for (i=0;i<5;i++) { //数据采集部分,此处简化过程
b_job0[i][0]=b_job0[i][1]=b_job0[i][2]=12;
}
while (1) {}
}
//为了在复位时不把b_power清零,连接时必须和nostart.obj连接
void main(void) {
unsigned int b_power;
unsigned char b_test_ram,i,j;
TMOD=0x11;
TH0=0xfd;TH1=0xfb;
ET0=TR0=1;
ET1=TR1=1;
EA=1;
if (b_power!=0x1234) { //b_power不等于0x1234表示刚开机
b_power=0x1234;
adds=0; //第一次执行job0
} else { //软件复位处理程序,主要是根据产生复位的地址来继续执行
//RAM数据错误检测和恢复,3中取2相等法
for (i=0;i<5;i++) {
for (j=0;j<2;j++) {
b_test_ram=job0[i][j];
if (b_test_ram==job0[i][j+1]) break;
b_test_ram=job0[i][j+1];
}
if (j==2) break;
}
if (i!=5) {}//处理job0数据出错
for (i=0;i<5;i++) {
for (j=0;j<2;j++) {
b_test_ram=job1[i][j];
if (b_test_ram==job1[i][j+1]) break;
b_test_ram=job1[i][j+1];
}
if (j==2) break;
}
if (i!=5) {}//处理job1数据出错
for (i=0;i<5;i++) {
for (j=0;j<2;j++) {
b_test_ram=job2[i][j];
if (b_test_ram==job2[i][j+1]) break;
b_test_ram=job2[i][j+1];
}
if (j==2) break;
}
if (i!=5) {}//处理job2数据出错
switch (adds) {
case 0:job0();break;
case 1:job1();break;
case 2:job2();break;
}
}
while (1) {}
}
|