STC MCU IO pin damage dynamic self-test program

Publisher:创客1992Latest update time:2016-01-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

#pragma NOAREGS
//STC ISP_V2.0 PCB The crystal oscillator is 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



sfr    AUXR       = 0x8e;
sfr    WDTRST     = 0xa6;
sfr16 TIMEER2    = 0xcc;
sfr16 RCAP       = 0xca;

sfr    WDT_CONTR = 0x0e1;

//Global variable definition
typedef struct Systemstruct {//System data structure
  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;/ /Apply for system data structure






void MainInit(void); //System initialization
void SystemInit(void);//System initialization
void SystemSetup(void);//System settings
void SystemIoInit(void);//System interface initialization
void TimeInit(void);//Timer timing parameter settings
void UserSetup(void);/ /User operating environment settings
void ClrWdt(void); //Feed the dog
void IoPinTest(void);
unsigned char Port0Test(void);
unsigned char Port1Test(void);
unsigned char Port2Test(void);
unsigned char Port3Test(void);



void main(void)
{
  MainInit(); //System initialization
  while (1) { //Main loop
    IE    |= 0xb2; //Ensure reliable interrupt EA, ET2, ES, ET0
    TCON |= 0x55; //Ensure timer is on
    PCON |= SMOD_ + GF0_ + IDL_; //Enter idle state and feed the dongle_nop_
    ();
    _nop_();
  }
}

void MainInit(void) using 0 //System initialization
{
  ClrWdt(); //Clear watchdog counter
  SystemIoInit(); //System interface initialization
  SystemInit(); //System power-on initialization
  SystemSetup( );//System operating environment settings
  UserSetup();//User operating environment settings
}

void SystemInit(void) using 0//System initialization
{
  if (SystemBuffers.RamTest != 0x55aa) {//Memory initialization
    SystemBuffers.RamTest = 0x55aa ;
  }
  else {
  }
}

void SystemSetup(void) using 0//System settings
{
  TimeInit();
}

void SystemIoInit(void) using 0
{
  IE = 0x00;//Disable interrupt
  P2 = 0xff;//Initialize P2 port
  P0 = 0xff; //P0 port initialization
  P1 = 0xff; //P1 port initialization
  P3 = 0xff; //P3 port initialization
}


void TimeInit() using 0
{
//   TCON = 0x55; //Start timer

  TL0 = 0;
  TH0 = 0;
  TR0 = 1; //Start timer 0

  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; //Start timer 1

  TIMEER2 = T2_5mS;
  RCAP     = T2_5mS;
  TR2      = 1; //Start timer 2
}

void UserSetup(void) using 0 //User operating environment settings
{
  SystemBuffers.SioTxCount = 0;
}

void ClrWdt(void)   using 0 //Feed the dog
{
  WDT_CONTR = WdtTime_1_1377S; //1.1377S feeds the dog
}



unsigned char Port0Test(void)
{
unsigned char testval;
  P0 = 0x55; //Odd pin sends low level
  _nop_(); //Delay
  testval = P0 ^ 0x55; //Compare the sending and receiving results
  P0 = 0xaa; //Even pin sends low level
  _nop_(); //Delay
  testval |= P0 ^ 0xaa; //Compare the sending and receiving results (2 times)
  P0 = 0xff; //Release P0 port to prevent external power supply
  return testval; //Test successful returns 0
}



unsigned char Port1Test(void)
{
unsigned char testval;
  P1 = 0x55;//Odd pin sends low
  level_nop_();//Delay
  testval = P1 ^ 0x55;//Compare the sending and receiving results
  P1 = 0xaa;//Even pin sends low
  level_nop_();//Delay
  testval |= P1 ^ 0xaa;//Compare the sending and receiving results (2 times)
  P1 = 0xff;//Release P1 port to prevent external power supply
  return testval;//Test successful returns 0
}


unsigned char Port2Test(void)
{
unsigned char testval;
  P2 = 0x55;//Odd pin sends low
  level_nop_();//Delay
  testval = P2 ^ 0x55;//Compare the sending and receiving results
  P2 = 0xaa;//Even pin sends low
  level_nop_();//Delay
  testval |= P1 ^ 0xaa;//Compare the results of sending and receiving (2 times)
  P2 = 0xff;//Release P2 port to prevent external power supply
  return testval;//Test successfully returns 0
}



unsigned char Port3Test(void)
{
unsigned char testval;
  P3 = 0x55;//Odd pin sends low level
  _nop_();//Delay
  testval = P3 ^ 0x55;//Compare the results of sending and receiving
  P3 = 0xaa;//Even pin sends low level
  _nop_();//Delay
  testval |= P3 ^ 0xaa;//Compare the results of sending and receiving (2 times)
  P3 = 0xff;//Release P1 port to prevent external power supply
  return testval;//Test successfully returns 0
}



void IoPinTest(void)
{
unsigned char i, err = 0, pin = 0;
//   if (pin = Port0Test()) err = 1;//If P0 port is pulled up, this line can be opened
  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_) {//The interrupt must be entered from the main loop to feed the dog
    ClrWdt();//Clear the watchdog counter
    PCON &= ~GF0_;//Clear the flag
  }
  IoPinTest();
}


void sioproc() interrupt SIO_VECTOR using 1
{
//unsigned char i;
  if (RI) {//Receive interrupt
    RI = 0;
  }
  if (TI) {//Send interrupt
    TI = 0;
       if (SystemBuffers.SioTxCount) {//Allow serial port to send data
      SBUF = SystemBuffers.SioTxBuff[SystemBuffers.SioTxSum - SystemBuffers.SioTxCount];//Serial port displays
      SystemBuffers.SioTxCount --;
    }
  }
}


void int0proc() interrupt IE0_VECTOR using 0
{
}


void int1proc() interrupt IE1_VECTOR using 0
{
}
Reference address:STC MCU IO pin damage dynamic self-test program

Previous article:STC MCU internal FLASH reading and writing program
Next article:MCU Learning: Introduction to MCU Programming 19

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号