軟件看門(mén)狗 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
//以下程序只是一個(gè)范例
void error(void);
//定時(shí)器0,清除定時(shí)器1的計(jì)時(shí)
void int_t0(void) interrupt 1 {
TL0=TL0+68;TH0=0xfd; //700
TH1=0xfb;
}
//定時(shí)器1,中斷作為看門(mén)狗
void int_t1(void) interrupt 3 {
error(); //復(fù)位
}
unsigned char adds;
unsigned char b_job0[5][3]; //job0用到的數(shù)據(jù),如A/D采集的數(shù)據(jù),一共5組,每組3個(gè),其中一個(gè)有效,其余2個(gè)備用
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++) { //數(shù)據(jù)采集部分,此處簡(jiǎn)化過(guò)程
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++) { //數(shù)據(jù)采集部分,此處簡(jiǎn)化過(guò)程
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++) { //數(shù)據(jù)采集部分,此處簡(jiǎn)化過(guò)程
b_job0[i][0]=b_job0[i][1]=b_job0[i][2]=12;
}
while (1) {}
}
//為了在復(fù)位時(shí)不把b_power清零,連接時(shí)必須和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表示剛開(kāi)機(jī)
b_power=0x1234;
adds=0; //第一次執(zhí)行job0
} else { //軟件復(fù)位處理程序,主要是根據(jù)產(chǎn)生復(fù)位的地址來(lái)繼續(xù)執(zhí)行
//RAM數(shù)據(jù)錯(cuò)誤檢測(cè)和恢復(fù),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數(shù)據(jù)出錯(cuò)
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數(shù)據(jù)出錯(cuò)
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數(shù)據(jù)出錯(cuò)
switch (adds) {
case 0:job0();break;
case 1:job1();break;
case 2:job2();break;
}
}
while (1) {}
}