วันเสาร์ที่ 4 เมษายน พ.ศ. 2558

หยอดเหรียญเครื่องซักผ้าAT89C2051 ตอน1ไฟล์.C



หยอดเหรียญเครื่องซักผ้าAT89C2051 ตอน1ไฟล์.C
4/4/2558 SONGCHAI PRAPATRUNGSEE
หยอดเหรียญเครื่องซักผ้าAT89C2051 ตอน1ไฟล์.C

/* 4/4/2558 BY SONGCHAI PRAPATRANGSEE
COIN_B FAST 20MS, MEDIUM 50MS, SLOW 100MS
IF USE FAST 20MS =DELAY MAX 20MS*0.8=16MS
IF EXCEED CAN NOT WORK */

//INTERRUPT EVERY 2000US=2MS
//CRYSTAL 4 MHZ

#include <REG2051.H>

/*SET PRICE BLINK END(STB_NUMBER)===*/
#define CMM0 15//PRICE 5,15...95BAHT
#define CMM1 20//PRICE 10,20...90BAHT
#define D1M0 DATA[1]//BLINK 0=5,1=15...9=95BAHT
#define D1M1 DATA[2]//BLINK 1=10,2=20...9=90BAHT
#define END 1/*SET0=LOW END,SET1=HIGH END AT P3^4 =STB_NUMBER*/

//sbit =COMMAND, COIN_B=VARIABLE NAME, P3^5=PIN 3.5
sbit MODE_B=P1^0;
sbit COIN_B=P3^5;   
sbit POWER=P3^7;//PIN 11
sbit START=P3^2;//PIN 6
sbit GROUND_COIN=P3^3;
sbit STB_NUMBER=P3^4;
sbit DG1=P3^0;
sbit DG2=P3^1;

/*ARRAY 0-9 ====================*/
char DATA[]={0X11,0XB7,0X29,0X23,0X87,//13 
  0X43,0X41,0X37,0X01,0X03,};//14 

/*DECLARATION GLOBAL VARIABLE===*/
bit BLINK_W,MATCHINE_W,CHECK_W;
bit COIN_L,NUMBER_L,CHECK_L,CX_L;
bit PON_L=1,POFF_L,STON_L,STOFF_L;

unsigned char CX;
unsigned char DG,NUMBER,CN,BLINK_T;
unsigned int TIME;
unsigned char DIGIT1,DIGIT2,i;

/*FUNCTION=====================*/
void CONVERT_DEC_TO_UNIT(unsigned char NUMBER)
{
   unsigned char NUM;

   NUM=((NUMBER%100)/10);//SIB
   DIGIT1=DATA[NUM];

   NUM=NUMBER%10;//NAUY
   DIGIT2=DATA[NUM];
}
void CUT_ZERO()
{
   if(DIGIT1==DATA[0])
    DIGIT1=0XFF;        
}

void DELAY_MS(unsigned int TIME)
{
  unsigned int i,j;
  for(i=TIME;i>0;i--)
  {
      for(j=34;j>0;j--);
  }
}

/*TIMER0 MODE1 16BIT============*/
void T0M1_16BIT()interrupt 1//INTERRUPT VECTOR1
{
  /*SET TIMER============*/
  //TR0=0;//STOP TIMER0
  //TF0=0;//TIMER OVERFLOW FLAG=CLEAR
  TH0=0XFD;//HI BYTE TIMER0
  TL0=0X66;//LOW BYTE TIMER0
  //TR0=1;//START TIMER0
     
  /*CHECK FOR END PROCESS*/
  if(CHECK_W==1)
  { 
    if(STB_NUMBER==END)//***
    {
      TIME++;//37
      if(TIME==2500)//500=1SEC
      {
        TIME=0;
        CX_L=0;
        CHECK_W=0;
        BLINK_W=0;
        NUMBER_L=0;
        CN=0;
        GROUND_COIN=1;
      }   
    }else TIME=0;//PULSE
  }

  /*COIN RECEIVE FOR WORK=========*/
  if(COIN_B==0&&COIN_L==0&&CX_L==0)
  {
      PON_L=1;
      POFF_L=0;
      TIME++;
    if(TIME==8)//DELAY 16MS
    {
      TIME=0;
      COIN_L=1;
      CHECK_L=0;
      BLINK_W=1;

    /*MODE 5,10,15...95BAHT
       ELSE MODE 10,20...90BAHT=======*/   
    if(MODE_B==0)
    {
       NUMBER=++CN*5;
       CX=CMM0;//***
    }else //MODE_B=1
   {
      NUMBER=++CN*10;
      CX=CMM1;//***
   }
  } 
  }else if(COIN_B==1)COIN_L=0;

/*BLINK FOR WAIT COIN
   ELSE NO BLINK WHEN COIN RECEIVE*/
  if(BLINK_W==0)
  {
    if(MODE_B==0)
    {
      BLINK_T++;
                               if(BLINK_T<=250&&PON_L==1)//500MS
{
    if(BLINK_T==250)
   {
       BLINK_T=0;
       PON_L=0;
       POFF_L=1;
   }
      /*DISPLAY SEGMENT 2DIGIT==*/
      switch (DG)
       {
        case 0:
        if(D1M0==DATA[0])
      {
        DG=1;
        break;
      }
        DG1=DG2=1;//DG1 DG2 OFF   
        DG1=0;//DG1 ON
       
        P1=D1M0;//DIGIT1***
        
        DG=1;//DG=1 =case 1:
        break;
        case 1:
        DG1=DG2=1;//DG2 DG1 OFF   
        DG2=0;//DG2 ON

        P1=DATA[5];//DIGIT2
        DG=0;
        break;     
       }                                            
      }else
      if(BLINK_T<=250&&POFF_L==1)//500MS
      {
       if(BLINK_T==250)
       {
        BLINK_T=0;
        PON_L=1;
        POFF_L=0;
       }
      DG1=DG2=1;//OFF
     }                           
    }else //MODE_B==1
    {
      BLINK_T++;
 if(BLINK_T<=250&&PON_L==1)//500MS
{
   if(BLINK_T==250)
  {
     BLINK_T=0;
     PON_L=0;
      POFF_L=1;
   }
      /*DISPLAY BLINK SEGMENT 2DIGIT===*/
      switch (DG)
      {
        case 0:
        DG1=DG2=1;//DG2 DG1 OFF   
        DG1=0;//DG1 ON
      
        P1=D1M1;//DIGIT1***

        DG=1;
        break;
        case 1:
        DG1=DG2=1;//DG1 DG2 OFF   
        DG2=0;//DG2 ON

        P1=DATA[0];//DIGIT2
        DG=0;
        break;    
       }                                            
      }else
      if(BLINK_T<=250&&POFF_L==1)//500MS
      {
      if(BLINK_T==250)
     {
        BLINK_T=0;
        PON_L=1;
        POFF_L=0;
     }
      DG1=DG2=1;//OFF
     }     
    } 
  }else//BLINK_W==1
  {
    if(NUMBER==CX&&NUMBER_L==0)
    {
       CX_L=1;
      NUMBER_L=1;
      MATCHINE_W=1;   
    }
    CONVERT_DEC_TO_UNIT(NUMBER);
    CUT_ZERO();
                       
    /*DISPLAY NO BLINK 2DIGIT======*/
    switch (DG)
    {
      case 0:
        DG1=DG2=1;//DG2 DG1 OFF   
        DG2=0;//DG2 ON

        P1=DIGIT2;

        DG=1;
        break;
      case 1:
        DG2=DG1=1;//DG1 DG2 OFF   
        DG1=0;//DG1 ON

        P1=DIGIT1;
        DG=0;
        break;
    }
   }
}

/*FUNCTION MAIN===============*/
void main()
{
  TMOD=0X1;//SET TIMER0 MODE1
  TCON=0X0;//TIMER CONTROLL
  IE=0X82;//INTERRUPT ENABLE
  IP=0X0;//INTERRUPT PRIORITY
  TH0=0X0;//HI BYTE TIMER0
  TL0=0X0;//LOW BYTE TIMER0
  TR0=1;//START TIMER0
  POWER=0;
  START=0;
  while(1)
 {
    /*MATCHINE WORK WHEN RECEIVE COIN MAX*/
    if(MATCHINE_W==1)
    {
      GROUND_COIN=0;
      POWER=1;
      DELAY_MS(1000);
      
      POWER=0;
      DELAY_MS(1000);

      START=1;
      DELAY_MS(1000);
                            
      START=0;
      DELAY_MS(1000);
    
      MATCHINE_W=0;
      CHECK_W=1;
    }
 }
}


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