新闻  |   论坛  |   博客  |   在线研讨会
STC单片机IO管脚毁坏动态自检程序
tvb2058 | 2008-01-15 19:15:30    阅读:3284   发布文章


/*---------------------------------------------------------
        STC单片机IO管脚毁坏动态自检程序
HotPower@126.com   2005.6.6          于西安大雁塔村队部
----------------------------------------------------------*/
#i nclude <REGX52.h>
#i nclude <stdio.h>
#i nclude <absacc.h>
#i nclude <intrins.h>
#i nclude <string.h>

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

#define WdtTime_71_1mS   0x30 //71.1mS
#define WdtTime_141_2mS  0x31 //141.2mS
#define WdtTime_284_4mS  0x32 //284.4mS
#define WdtTime_568_8mS  0x33 //568.8mS
#define WdtTime_1_1377S  0x34 //1.1377S
#define WdtTime_2_755S   0x35 //2.755S

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


/*------------------------------------------------
AT89S5X
--------------------------------------------------*/
sfr   AUXR      = 0x8e;
sfr   WDTRST    = 0xa6;
sfr16 TIMEER2   = 0xcc;
sfr16 RCAP      = 0xca;
/*------------------------------------------------
STC89C5XRC
--------------------------------------------------*/
sfr   WDT_CONTR = 0x0e1;

//全局变量定义
typedef struct Systemstruct{//系统数据结构
  unsigned char TimeCount;
  unsigned int  RamTest;
  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;//启动定时器
/*----------------------------------
      定时器0定时参数设置
----------------------------------*/
  TL0 = 0;
  TH0 = 0;
  TR0 = 1;//启动定时器0
/*----------------------------------
      定时器1定时参数设置
----------------------------------*/
  TMOD = 0x20;
  TH1 =  0xfb;//Fosc=18.432MHz  th1=tl1=0xfb bps=9600
  TL1 =  TH1;
  SCON = 0x58;
  PCON = 0x80;//2*bps=9600*2=19200
  TR1 = 1;//启动定时器1
/*----------------------------------
      定时器2定时参数设置
----------------------------------*/
  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喂狗
}


/*------------------------------------
              P0端口测试
主要测试管脚接电源或地及相临管脚的短路
注意:此函数必须配合P0口上拉电阻!!!
-------------------------------------*/
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
}


/*------------------------------------
              P1端口测试
主要测试管脚接电源或地及相临管脚的短路
-------------------------------------*/
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
}

/*------------------------------------
              P2端口测试
主要测试管脚接电源或地及相临管脚的短路
-------------------------------------*/
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
}


/*------------------------------------
              P3端口测试
主要测试管脚接电源或地及相临管脚的短路
-------------------------------------*/
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!!!\n");
      SystemBuffers.SioTxBuff[1] += err - 1;
      SystemBuffers.SioTxBuff[3] += pin;
      SystemBuffers.SioTxCount = strlen(SystemBuffers.SioTxBuff);
      SystemBuffers.SioTxSum = SystemBuffers.SioTxCount;
      TI = 1;
    }
  }
}



/*------------------------------------
         定时器T0中断服务程序
------------------------------------*/
void t0proc() interrupt TF0_VECTOR using 1
{
}

/*------------------------------------
         定时器T1中断服务程序
------------------------------------*/
void t1proc() interrupt TF1_VECTOR using 1
{
}

/*------------------------------------
         定时器T2中断服务程序(5mS)
------------------------------------*/
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 --;
    }
  }
}

/*------------------------------------
         外部INT0中断服务程序
------------------------------------*/
void int0proc() interrupt IE0_VECTOR using 0
{
}

/*------------------------------------
         外部INT1中断服务程序
------------------------------------*/
void int1proc() interrupt IE1_VECTOR using 0
{
}

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客