新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STC單片機(jī)IO管腳毀壞動(dòng)態(tài)自檢程序

STC單片機(jī)IO管腳毀壞動(dòng)態(tài)自檢程序

作者: 時(shí)間:2016-11-23 來(lái)源:網(wǎng)絡(luò) 收藏
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

#pragma NOAREGS
//STC ISP_V2.0 PCB的晶振為18.432MHz

#define WdtTime_71_1mS0x30 //71.1mS
#define WdtTime_141_2mS0x31 //141.2mS
#define WdtTime_284_4mS0x32 //284.4mS
#define WdtTime_568_8mS0x33 //568.8mS
#define WdtTime_1_1377S0x34 //1.1377S
#define WdtTime_2_755S0x35 //2.755S

#define T2_5mS -1536*5//5ms 18.432MHz



sfrAUXR= 0x8e;
sfrWDTRST= 0xa6;
sfr16 TIMEER2= 0xcc;
sfr16 RCAP= 0xca;

sfrWDT_CONTR = 0x0e1;

//全局變量定義
typedef struct Systemstruct{//系統(tǒng)數(shù)據(jù)結(jié)構(gòu)
unsigned char TimeCount;
unsigned intRamTest;
unsigned char SioTxCount;
unsigned char SioTxSum;
unsigned char SioRxCount;
unsigned char SioTxBuff[32];
//unsigned char SioRxBuff[32];
}SystemData;

data SystemData SystemBuffers;//申請(qǐng)系統(tǒng)數(shù)據(jù)結(jié)構(gòu)






void MainInit(void);//系統(tǒng)初始化
void SystemInit(void);//系統(tǒng)初始化
void SystemSetup(void);//系統(tǒng)設(shè)置
void SystemIoInit(void);//系統(tǒng)接口初始化
void TimeInit(void);//定時(shí)器定時(shí)參數(shù)設(shè)置
void UserSetup(void);//用戶運(yùn)行環(huán)境設(shè)置
void ClrWdt(void);//喂狗
void IoPinTest(void);
unsigned char Port0Test(void);
unsigned char Port1Test(void);
unsigned char Port2Test(void);
unsigned char Port3Test(void);



void main(void)
{
MainInit();//系統(tǒng)初始化
while (1) {//主循環(huán)
IE|= 0xb2;//保證中斷可靠EA,ET2,ES,ET0
TCON |= 0x55;//保證定時(shí)器開(kāi)
PCON |= SMOD_ + GF0_ + IDL_;//進(jìn)入空閑狀態(tài)并喂軟件狗
_nop_();
_nop_();
}
}

void MainInit(void) using 0//系統(tǒng)初始化
{
ClrWdt();//清除看門(mén)狗計(jì)數(shù)器
SystemIoInit();//系統(tǒng)接口初始化
SystemInit();//系統(tǒng)上電初始化
SystemSetup();//系統(tǒng)運(yùn)行環(huán)境設(shè)置
UserSetup();//用戶運(yùn)行環(huán)境設(shè)置
}

void SystemInit(void) using 0//系統(tǒng)初始化
{
if (SystemBuffers.RamTest != 0x55aa) {//內(nèi)存初始化
SystemBuffers.RamTest = 0x55aa;
}
else {
}
}

void SystemSetup(void) using 0//系統(tǒng)設(shè)置
{
TimeInit();
}

void SystemIoInit(void) using 0
{
IE = 0x00;//關(guān)閉中斷
P2 = 0xff;//P2口初始化
P0 = 0xff;//P0口初始化
P1 = 0xff;//P1口初始化
P3 = 0xff;//P3口初始化
}


void TimeInit() using 0
{
//TCON = 0x55;//啟動(dòng)定時(shí)器

TL0 = 0;
TH0 = 0;
TR0 = 1;//啟動(dòng)定時(shí)器0

TMOD = 0x20;
TH1 =0xfb;//Fosc=18.432MHzth1=tl1=0xfb bps=9600
TL1 =TH1;
SCON = 0x58;
PCON = 0x80;//2*bps=9600*2=19200
TR1 = 1;//啟動(dòng)定時(shí)器1

TIMEER2 = T2_5mS;
RCAP= T2_5mS;
TR2= 1;//啟動(dòng)定時(shí)器2
}

void UserSetup(void) using 0//用戶運(yùn)行環(huán)境設(shè)置
{
SystemBuffers.SioTxCount = 0;
}

void ClrWdt(void)using 0//喂狗
{
WDT_CONTR = WdtTime_1_1377S;//1.1377S喂狗
}



unsigned char Port0Test(void)
{
unsigned char testval;
P0 = 0x55;//奇數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval = P0 ^ 0x55;//比較發(fā)送及接收結(jié)果
P0 = 0xaa;//偶數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval |= P0 ^ 0xaa;//比較發(fā)送及接收結(jié)果(2次的)
P0 = 0xff;//釋放P0口,防止外部接入電源
return testval;//測(cè)試成功返回0
}



unsigned char Port1Test(void)
{
unsigned char testval;
P1 = 0x55;//奇數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval = P1 ^ 0x55;//比較發(fā)送及接收結(jié)果
P1 = 0xaa;//偶數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval |= P1 ^ 0xaa;//比較發(fā)送及接收結(jié)果(2次的)
P1 = 0xff;//釋放P1口,防止外部接入電源
return testval;//測(cè)試成功返回0
}


unsigned char Port2Test(void)
{
unsigned char testval;
P2 = 0x55;//奇數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval = P2 ^ 0x55;//比較發(fā)送及接收結(jié)果
P2 = 0xaa;//偶數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval |= P1 ^ 0xaa;//比較發(fā)送及接收結(jié)果(2次的)
P2 = 0xff;//釋放P2口,防止外部接入電源
return testval;//測(cè)試成功返回0
}



unsigned char Port3Test(void)
{
unsigned char testval;
P3 = 0x55;//奇數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval = P3 ^ 0x55;//比較發(fā)送及接收結(jié)果
P3 = 0xaa;//偶數(shù)管腳發(fā)低電平
_nop_();//延時(shí)
testval |= P3 ^ 0xaa;//比較發(fā)送及接收結(jié)果(2次的)
P3 = 0xff;//釋放P1口,防止外部接入電源
return testval;//測(cè)試成功返回0
}



void IoPinTest(void)
{
unsigned char i, err = 0, pin = 0;
//if (pin = Port0Test()) err = 1;//如果P0口上拉可開(kāi)放此行
if (pin = Port1Test()) err = 2;
if (pin = Port2Test()) err = 3;
if (pin = Port3Test()) err = 4;
if (err) {
if ((TI == 0) && (SystemBuffers.SioTxCount == 0)) {
for (i = 0; i < 8; i++) {
if (pin & 1) break;
pin >>= 1;
}
strcpy(SystemBuffers.SioTxBuff, "P0_0 is Bad?。?!");
SystemBuffers.SioTxBuff[1] += err - 1;
SystemBuffers.SioTxBuff[3] += pin;
SystemBuffers.SioTxCount = strlen(SystemBuffers.SioTxBuff);
SystemBuffers.SioTxSum = SystemBuffers.SioTxCount;
TI = 1;
}
}
}




void t0proc() interrupt TF0_VECTOR using 1
{
}


void t1proc() interrupt TF1_VECTOR using 1
{
}


void t2proc() interrupt TF2_VECTOR using 0
{
TF2 = 0;
if (PCON & GF0_) {//中斷是從主循環(huán)內(nèi)跳入的才能喂狗
ClrWdt();//清除看門(mén)狗計(jì)數(shù)器
PCON &= ~GF0_;//清除標(biāo)志
}
IoPinTest();
}


void sioproc() interrupt SIO_VECTOR using 1
{
//unsigned char i;
if (RI) {//接收中斷
RI = 0;
}
if (TI) {//發(fā)送中斷
TI = 0;
if (SystemBuffers.SioTxCount) {//允許串口發(fā)送數(shù)據(jù)
SBUF = SystemBuffers.SioTxBuff[SystemBuffers.SioTxSum - SystemBuffers.SioTxCount];//串口顯示
SystemBuffers.SioTxCount --;
}
}
}


void int0proc() interrupt IE0_VECTOR using 0
{
}


void int1proc() interrupt IE1_VECTOR using 0
{
}


評(píng)論


技術(shù)專區(qū)

關(guān)閉