您的位置 首页 分销

STC单片机IO管脚破坏动态自检程序

#includeREGX52h>#includestdioh>#includeabsacch>#includeintrinsh>#includestringh>pragm

#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{//体系数据结构
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;//请求体系数据结构

void MainInit(void);//体系初始化
void SystemInit(void);//体系初始化
void SystemSetup(void);//体系设置
void SystemIoInit(void);//体系接口初始化
void TimeInit(void);//守时器守时参数设置
void UserSetup(void);//用户运转环境设置
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();//体系初始化
while (1) {//主循环
IE|= 0xb2;//确保中止牢靠EA,ET2,ES,ET0
TCON |= 0x55;//确保守时器开
PCON |= SMOD_ + GF0_ + IDL_;//进入闲暇状况并喂软件狗
_nop_();
_nop_();
}
}

void MainInit(void) using 0//体系初始化
{
ClrWdt();//铲除看门狗计数器
SystemIoInit();//体系接口初始化
SystemInit();//体系上电初始化
SystemSetup();//体系运转环境设置
UserSetup();//用户运转环境设置
}

void SystemInit(void) using 0//体系初始化
{
if (SystemBuffers.RamTest != 0x55aa) {//内存初始化
SystemBuffers.RamTest = 0x55aa;
}
else {
}
}

void SystemSetup(void) using 0//体系设置
{
TimeInit();
}

void SystemIoInit(void) using 0
{
IE = 0x00;//封闭中止
P2 = 0xff;//P2口初始化
P0 = 0xff;//P0口初始化
P1 = 0xff;//P1口初始化
P3 = 0xff;//P3口初始化
}

void TimeInit() using 0
{
//TCON = 0x55;//发动守时器

TL0 = 0;
TH0 = 0;
TR0 = 1;//发动守时器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;//发动守时器1

TIMEER2 = T2_5mS;
RCAP= T2_5mS;
TR2= 1;//发动守时器2
}

void UserSetup(void) using 0//用户运转环境设置
{
SystemBuffers.SioTxCount = 0;
}

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

unsigned char Port0Test(void)
{
unsigned char testval;
P0 = 0x55;//奇数管脚发低电平
_nop_();//延时
testval = P0 ^ 0x55;//比较发送及接纳成果
P0 = 0xaa;//偶数管脚发低电平
_nop_();//延时
testval |= P0 ^ 0xaa;//比较发送及接纳成果(2次的)
P0 = 0xff;//开释P0口,避免外部接入电源
return testval;//测验成功回来0
}

unsigned char Port1Test(void)
{
unsigned char testval;
P1 = 0x55;//奇数管脚发低电平
_nop_();//延时
testval = P1 ^ 0x55;//比较发送及接纳成果
P1 = 0xaa;//偶数管脚发低电平
_nop_();//延时
testval |= P1 ^ 0xaa;//比较发送及接纳成果(2次的)
P1 = 0xff;//开释P1口,避免外部接入电源
return testval;//测验成功回来0
}

unsigned char Port2Test(void)
{
unsigned char testval;
P2 = 0x55;//奇数管脚发低电平
_nop_();//延时
testval = P2 ^ 0x55;//比较发送及接纳成果
P2 = 0xaa;//偶数管脚发低电平
_nop_();//延时
testval |= P1 ^ 0xaa;//比较发送及接纳成果(2次的)
P2 = 0xff;//开释P2口,避免外部接入电源
return testval;//测验成功回来0
}

unsigned char Port3Test(void)
{
unsigned char testval;
P3 = 0x55;//奇数管脚发低电平
_nop_();//延时
testval = P3 ^ 0x55;//比较发送及接纳成果
P3 = 0xaa;//偶数管脚发低电平
_nop_();//延时
testval |= P3 ^ 0xaa;//比较发送及接纳成果(2次的)
P3 = 0xff;//开释P1口,避免外部接入电源
return testval;//测验成功回来0
}

void IoPinTest(void)
{
unsigned char i, err = 0, pin = 0;
//if (pin = Port0Test()) err = 1;//假如P0口上拉可敞开此行
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_) {//中止是从主循环内跳入的才干喂狗
ClrWdt();//铲除看门狗计数器
PCON &= ~GF0_;//铲除标志
}
IoPinTest();
}

void sioproc() interrupt SIO_VECTOR using 1
{
//unsigned char i;
if (RI) {//接纳中止
RI = 0;
}
if (TI) {//发送中止
TI = 0;
if (SystemBuffers.SioTxCount) {//答应串口发送数据
SBUF = SystemBuffers.SioTxBuff[SystemBuffers.SioTxSum – SystemBuffers.SioTxCount];//串口显现
SystemBuffers.SioTxCount –;
}
}
}

void int0proc() interrupt IE0_VECTOR using 0
{
}

void int1proc() interrupt IE1_VECTOR using 0
{
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/fenxiao/260867.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部