วันเสาร์ที่ 16 พฤษภาคม พ.ศ. 2558

ประหยัดรอม ตอน4DEBUGหาข้อผิดพลาด



ประหยัดรอม ตอน4DEBUGหาข้อผิดพลาด
13/5/2558 SONGCHAI PRAPATRUNGSEE
ประหยัดรอม ตอน4DEBUGหาข้อผิดพลาด

#include <REGX52.H>
#include <intrins.H>
/* USE 11.0592MHZ WRITE 24256 WORK(AT LEAST 5MS)
   IF USE 12MHZ WRITE 24256 NOT WORK
   AT TIME VALUE 0XEDFF
   DELAY WRITE 24256 AT LEAST 5MS(EDFF) =WORK
   DELAY READ 24256 AT LEAST 4.167MS(F10F) =WORK */

/* SAME AS 2416
   DELAY WRITE 2416 AT LEAST 5MS(EDFF) =WORK
   BUT SIMULATE WITH PROTEUS 5MS NOT WORK
   10MS WORK */

//1MATCHINE CYCLE =1.085US
//INTERRUPT EVERY 100HZ =10MS
//100 =1SEC

/*i++ =CODE 1139 BYTE
i-- =CODE 1136 BYTE
SAVE CODE 3 BYTE */

#define X 256-10//OVF256-3 =13US(CRYSTAL12MHZ)
/* -4=14US -5=15US -255=265US -256=267US */

//#define  's' 0xf0;
sbit COIN_B=P0^0;   //sbit =COMMAND, COIN1_B=VARIABLE NAME, P3^5=PIN 3.5
sbit DISPLAY1_B=P0^1;
sbit DISPLAY2_B=P0^2;
sbit CLEAR1_B=P0^3;
sbit CLEAR2_B=P0^4;

//sbit SDA=P0^6;//REAL
//sbit SCL=P0^7;//REAL
sbit SDA=P2^0;//SIMULATE
sbit SCL=P2^1;//SIMULATE

sbit DG0=P3^0;
sbit DG1=P3^1;
sbit DG2=P3^2;
sbit DG3=P3^3;

unsigned char DATA[]={0XEB,0X28,0XB3,0XBA,0X78,
  0XDA,0XDB,0XA8,0XFB,0XFA,};    

char DGIT,N;
char SET_L;

unsigned char COIN1_T;
unsigned int TOTAL_T,B_T;

unsigned int COINSAVE_T,COIN_T;
unsigned int DISPLAY1_T,DISPLAY2_T;
unsigned int CLEAR1_T,CLEAR2_T;
unsigned int BLINK_T;     
bit COIN1_W;
bit DOWN_L;

unsigned char DATA0,DATA1,DATA2,DATA3;
unsigned char COIN1,COIN2,COINN;
unsigned char COIN,SAVE_COIN;

bit MAXCOIN1_W,MAXCOIN2_W;
bit DISPLAY1_W,DISPLAY2_W;
bit DISPLAYCLEAR1_W,DISPLAYCLEAR2_W;
bit CLEAR1_W,CLEAR2_W;
bit COINSAVE_W,COIN_W=1;

bit COIN1_L,COIN_L;
bit DISPLAY1_L,DISPLAY2_L,CLEAR1_L,CLEAR2_L;
bit ER04_W;
void ERROR(unsigned char DIGIT1,DIGIT2,DIGIT3,DIGIT4)
{
                        BLINK_T++;//64
                        if(BLINK_T<=100)//65 500MS
                        {
                              switch (DGIT)//70
                              {
                                    case 0://71
                                    DG0=DG1=DG2=DG3=1;//72 DG2 DG1 OFF    
                                    DG0=0;//73 DG1 ON
                                   
                                    P1=DIGIT4;//74 NUMBER2

                                    DGIT=1;//75
                                    break;//76
                                    case 1://77
                                    DG0=DG1=DG2=DG3=1;//78 DG2 DG1 OFF    
                                    DG1=0;//79 DG2 ON

                                    P1=DIGIT3;//80 NUMBER5
                                    DGIT=2;//81
                                    break;//82
      case 2://77
                                    DG0=DG1=DG2=DG3=1;//78 DG2 DG1 OFF    
                                    DG2=0;//79 DG2 ON

                                    P1=DIGIT2;//80 NUMBER5
                                    DGIT=3;//81
                                    break;//82   
      case 3://77
                                    DG0=DG1=DG2=DG3=1;//78 DG2 DG1 OFF    
                                    DG3=0;//79 DG2 ON

                                    P1=DIGIT1;//80 NUMBER5
                                   
                                    DGIT=0;//81
                                    break;//82                                       
                              }                                              
                        }else
                        if(BLINK_T<=200)
                        {
                              if(BLINK_T==200)
                              {
                                    BLINK_T=0;
                                    if(N++>5)
                                    {
                                     P1=255;
                                          P3=255;
                                          TR2=0;
                                    }
                              }
                              DG0=DG1=DG2=DG3=1;//89 OFF
                        }                            
}

void DELAY_T0M2(unsigned char TIME)
{
  //TIMER0 MODE2 8BIT AUTO RELOAD
  //TH0=255;//HI BYTE FOR AUTO RELOAD TO LOW BYTE

  TF0=0;//TIMER0 OVERFLOW
            //TH0=254;
            TL0=TIME;//LOW BYTE
            TR0=1;//TIMER START

  while(TF0==0);
  TR0=0;//TIMER STOP
}

void CONVERT_DEC_TO_UNIT(unsigned int NUMBER)
{
   unsigned char NUM;
   NUM=((NUMBER%10000)/1000);//PAN
   DATA3=DATA[NUM];

   NUM=((NUMBER%1000)/100);//ROY
   DATA2=DATA[NUM];
 
   NUM=((NUMBER%100)/10);//SIB
   DATA1=DATA[NUM];

   NUM=NUMBER%10;//NAUY
   DATA0=DATA[NUM];
}

void CUT_ZEROS()
{
  if(DATA3==DATA[0])
  {  
    DATA3=0X00;
    if(DATA2==DATA[0])
    {  
      DATA2=0X00;
      if(DATA1==DATA[0])
      {  
        DATA1=0X00;                          
      }                        
    }                        
  }       
}

void DISPLAY_COIN(unsigned char NUMBER)
{
  CONVERT_DEC_TO_UNIT(NUMBER);
  CUT_ZEROS();
  switch (DGIT)
  {
     case 0:
       //DG0=DG1=DG2=DG3=0;//REAL    
       //DG0=1;
       DG0=DG1=DG2=DG3=1;//SIMULATE    
       DG0=0;
      
       P1=DATA0;

       DGIT=1;
       break;
     case 1:
       //DG0=DG1=DG2=DG3=0;//REAL    
       //DG1=1;
       DG0=DG1=DG2=DG3=1;//SIMULATE    
       DG1=0;
        
       P1=DATA1;
       DGIT=2;
       break;
     case 2:
       //DG0=DG1=DG2=DG3=0;//REAL    
       //DG2=1;
       DG0=DG1=DG2=DG3=1;//SIMULATE    
       DG2=0;
      
       P1=DATA2;

       DGIT=3;
       break;
     case 3:
       //DG0=DG1=DG2=DG3=0;//REAL    
       //DG3=1;
       DG0=DG1=DG2=DG3=1;//SIMULATE    
       DG3=0;
      
       P1=DATA3;
       DGIT=0;
       break;
     default:
       DGIT=0;
       break;
  }
}

void I2C_START()
{
  SCL=1;
       DELAY_T0M2(X);
  SDA=1;
  DELAY_T0M2(X);
  SDA=0;
  DELAY_T0M2(X);    
  SCL=0;
  DELAY_T0M2(X);
}
void I2C_STOP()
{
  SDA=0;
       DELAY_T0M2(X);     
  SCL=1;
  DELAY_T0M2(X);          
  SDA=1;
       DELAY_T0M2(X);     
}
void I2C_TX(unsigned char DATTX)
{
  char i;
      for (i=8;i>0;i--)
      {
            if ((DATTX&0x80)==0x80)SDA=1;
            else SDA=0;
       DELAY_T0M2(X);
            SCL=1;
                        DELAY_T0M2(X);
            SCL=0;
                        DELAY_T0M2(X);
            DATTX<<=1;
      }
      SDA=1;//SEND ACK FOR 24CXX PULL LOW
            DELAY_T0M2(100);
            if(SDA==1)
  {
                   ER04_W=1;
            }
           
      SCL=1;
            DELAY_T0M2(X);
      SCL=0;
            DELAY_T0M2(X);
}

unsigned char I2C_RX()
{
      unsigned char i,DATRX=0;
  SDA=1;//IF THIS ROW NONE DISPLAY TEMPERATURE 0.00       
      for(i=8;i>0;i--)
      {
            DATRX<<=1;    
            SCL=1;
    DELAY_T0M2(X);              
            if(SDA)//SDA DEFAULT=1
            {
                   DATRX|=0X01;
            }
            else
            {
                   DATRX&=0XFE;
            }
                        DELAY_T0M2(X);
            SCL=0;
                        DELAY_T0M2(X);      
      }
  SDA=1;//SEND ACK1 FOR24CXX PULL LOW
  DELAY_T0M2(X);
            SCL=1;
  DELAY_T0M2(X);
            SCL=0;
            DELAY_T0M2(X);
      return DATRX;
}

void WRITE_24CXX(unsigned char ADDRESS,DATWR)
{    
      I2C_START();
     
  I2C_TX(0XA0);//Control=Bit7654,Address=bit321(A2A1A0),ReadWrite=bit0
      I2C_TX(ADDRESS);
     
      I2C_TX(DATWR);

      I2C_STOP();
}
unsigned int READ_24CXX(unsigned char ADDRESS)
{
      unsigned char DATRX;
     
      I2C_START();

  I2C_TX(0XA0);//Control=Bit7654,Address=bit321(A2A1A0),ReadWrite=bit0           
      I2C_TX(ADDRESS);

      I2C_START();
      I2C_TX(0XA1);
     
  DATRX=I2C_RX();

      I2C_STOP();

      return DATRX;
}

void T2_16BIT_AUTO()interrupt 5
{
  TF2=0;//TIMER2 OVERFLOW FLAG =CLEAR
  if(ER04_W==1)
  {
   ERROR(DATA[2],DATA[4],DATA[0],DATA[4]);
              return;
            }
     
      if(COIN_W==1)
  {
    DISPLAY_COIN(COINN);
    DISPLAY1_W=0;
  }
 
  if(COIN_B==0&&COIN_L==0)
  {
    if(++COIN_T>=4)//DELAY 20MS
    {     
      COIN_T=0;
      COIN_L=1;
     
      COIN++;
      COINN=COINN+COIN;
      COIN1=COIN1+COIN;
     
      COIN2=COIN2+COIN;
      COIN=0;
      COIN_W=1;
      DISPLAY1_W=0;
      DISPLAY2_W=0;
      COINSAVE_W=1
    }      
  }else if(COIN_B==1)
  {
    COIN_L=0;
    COIN_T=0;
  }
  if(COINSAVE_W==1)
  {
    if(++COINSAVE_T>=40)//200MS
    {
      TR2=0;
      TH2=0X2A;//HI BYTE TIMER2//DBFF=10MS AT 11.0592MHZ
      TL2=0XF8;//LOW BYTE TIMER2
      TR2=1;

      switch(SAVE_COIN)
      {     
        case 0://COIN1_TOTAL
         
          WRITE_24CXX(1,COIN1);
       
          SAVE_COIN=1;
 
          break;
        case 1://COIN1_SAVE        
         
          WRITE_24CXX(2,COIN2);
        
          COINSAVE_W=0;         
          SAVE_COIN=0;
          break; 
      }  
   
    }
  }
   
  if(DISPLAY1_B==0&&DISPLAY1_L==0)
  {
    if(++DISPLAY1_T>=4)//DELAY 20MS
    { //COIN1=5000;
     
      DISPLAY1_T=0;
      DISPLAY1_L=1;
  
      DISPLAY1_W=1;
      DISPLAY2_W=0;
      COIN_W=0;
      COINN=0
    }      
  }else if(DISPLAY1_B==1)
  {
    DISPLAY1_L=0;
    DISPLAY1_T=0;
  }
  if(DISPLAY1_W==1)
  {
    DISPLAY_COIN(COIN1);
   
  }
     
  if(DISPLAY2_B==0&&DISPLAY2_L==0)
  {
    if(++DISPLAY2_T>=4)//DELAY 20MS
    {     
      DISPLAY2_T=0;
      DISPLAY2_L=1;
  
      DISPLAY2_W=1;
      DISPLAY1_W=0;
      COIN_W=0;
      COINN=0
    }      
  }else if(DISPLAY2_B==1)
  {
    DISPLAY2_L=0;
    DISPLAY2_T=0;
  }
  if(DISPLAY2_W==1)
  {
    DISPLAY_COIN(COIN2);   
  }

  if(CLEAR1_B==0&&CLEAR1_L==0)
  {
    if(++CLEAR1_T>=4)//DELAY 20MS
    {
      CLEAR1_T=0;
      CLEAR1_L=1;
      CLEAR1_W=1;
      DISPLAY1_W=1;
      DISPLAY2_W=0;
     
      COIN_W=0;
      COINN=0
    }      
  }else if(CLEAR1_B==1)
  {
    CLEAR1_L=0;
    CLEAR1_T=0;
  }

  if(CLEAR1_W==1)
  {
    CLEAR1_W=0;
    COIN1=0;
       
    TR2=0;
    TH2=0XDB;//HI BYTE TIMER2//DBFF=10MS AT 11.0592MHZ
    TL2=0XFF;//LOW BYTE TIMER2
    TR2=1;
    WRITE_24CXX(1,COIN1);                                            
  }
 
  if(CLEAR2_B==0&&CLEAR2_L==0)
  {
    if(++CLEAR2_T>=4)//DELAY 20MS
    {
      CLEAR2_T=0;
      CLEAR2_L=1;
  
      CLEAR2_W=1;
      DISPLAY2_W=1;
      DISPLAY1_W=0;
      COIN_W=0;
      COINN=0
    }      
  }else if(CLEAR2_B==1)
  {
    CLEAR2_L=0;
    CLEAR2_T=0;
  }
  if(CLEAR2_W==1)
  {
    CLEAR2_W=0;
    COIN2=0;

    TR2=0;
    TH2=0XDB;//HI BYTE TIMER2//DBFF=10MS AT 11.0592MHZ
    TL2=0XFF;//LOW BYTE TIMER2
    TR2=1;

    WRITE_24CXX(2,COIN2);                           
  }  
}
   
void main()
{ TMOD=0X02;
  T2MOD=0;//TIMER2 MODE
  T2CON=0;//TIMER2 CONTROLL
 
  TH2=0XED;//HI BYTE TIMER2//EDFF=5MS AT 11.0592MHZ
  TL2=0XFF;//LOW BYTE TIMER2
  RCAP2H=0XED;//HI BYTE AUTO RELOAD TIMER2
  RCAP2L=0XFF;//LOW BYTE AUTO RELOAD TIMER2
  IE=0XA0;//INTERRUPT ENABLE
  IP=0;//INTERRUPT PRIORITY
       //TH0=255;//HI BYTE TIMER0 MODE2 FOR AUTORELOAD
  //TL0=255;//LOW BYTE TIMER0
  TR2=1;//START TIMER2

  //IF DELAY THIS ROW NOT WORK =BUG OF PROTEUS
  //DELAY_T1M1(0XF1,0X0F);//0XF10F =4.167MS
  COIN1=READ_24CXX(1);
  //DELAY_T1M1(0XED,0XFF);//0XEDFF =5MS
 
  COIN2=READ_24CXX(2)
  //DELAY_T1M1(0XED,0XFF);//0XEDFF =5MS
 
  while(1)
  {

  }
}                   



ไม่มีความคิดเห็น: