วันอังคารที่ 10 กุมภาพันธ์ พ.ศ. 2558

SOURCE CODEหยอดเหรียญเครื่องซักผ้าPIC18F2455 EEPROM CCS UPDATE1



SOURCE CODEหยอดเหรียญเครื่องซักผ้าPIC18F2455 EEPROM CCS UPDATE1
10/2/2558 SONGCHAI PRAPATRUNGSEE
SOURCE CODEหยอดเหรียญเครื่องซักผ้าPIC18F2455 EEPROM CCS UPDATE1

#include "D:\SONGCHAI1_7\SONGCHAI_PROJECT\WATCHING_MATCHINE\CAPER_PIC18F2455\CAPER_PIC18F2455_14.h"

/* 7-8-2555 BY SONGCHAI PRAPATRANGSEE
COIN_B FAST 30MS, MEDIUM 50MS, SLOW 100MS */

//INSTRUC CYCLE 0.2US
//1 MACHINE CYCLE =CRYSTAL/4
//INTERRUPT EVERY 5000US
//20MHZ

#byte INTCON2=0X0FF1
#bit RBPU=INTCON2.7//PORTB PULLUP

#byte PORTA=0XF80
#byte PORTB=0XF81
#byte PORTC=0XF82
//#byte PORTD=0XF83

#byte TRISA=0XF92
#byte TRISB=0XF93
#byte TRISC=0XF94
//#byte TRISD=0XF95

#bit COIN_B=PORTA.2 //sbit =COMMAND, COIN_B=VARIABLE NAME, P3^5=PIN 3.5
#bit SETCOIN_B=PORTB.7
#bit SETLEVEL_B=PORTA.0
#bit POWER=PORTC.0
#bit LEVEL=PORTC.1
#bit START=PORTC.2
#bit GROUND_COIN=PORTA.3
#bit STB_NUMBER=PORTA.5
#bit DG1=PORTC.6
#bit DG0=PORTC.7

int8 NCX;//COIN X BATH
int8 LVX;//LEVEL WATER X TIME
int8 PLX;////PLUS +5, +10

//int8 DIGIT[]={0X10>>1,0XB6>>1,0X28>>1,0X22>>1,0X86>>1,
//      0X42>>1,0X40>>1,0X36>>1,0X00>>1,0X02>>1,0XFF};//TOD-5261HMR-B 
int8 DIGIT[]={~0X3F,~0X06,~0X5B,~0X4F,~0X66,
              ~0x6D,~0X7D,~0X07,~0X7F,~0X6F};//~ALL SEGMENT
                 
int8 DGIT,NUMBER,BLINK_T,N;
int8 DATA0,DATA1,i,j,PLUS,PL,NUMSAVE;
int8 SETCOIN_T,SELECTCOIN_T,SETLEVEL_T,SELECTLEVEL_T;
int16 TIME,COIN_T;
int1 BLINK_W,COL0_W,MATCHINE_W,CHECK_W,SETCOIN_W;
int1 SET5_W,SET10_W,UP5_W,UP10_W,SAVECOIN_W;
int1 SETLEVEL_W,SAVELEVEL_W,SAVE_W,NORMAL_W;
int1 COIN_L,NUMBER_L,CHECK_L,FIRST_L,FIRSTUP_L;
int1 FIRST2_L;
int1 BLINK1_W,BLINK3_W,BLINK2_W;

void CONVERT_DEC_TO_UNIT(int8 NUMBER)
{
   int8 NUM;
 
   NUM=((NUMBER%100)/10);//SIB
   DATA1=DIGIT[NUM];

   NUM=NUMBER%10;//NAUY
   DATA0=DIGIT[NUM];
}
void CUT_ZERO()
{
  if(DATA1==DIGIT[0])
  {
    DATA1=0XFF;  
  }       
}

void DISPLAY(NUMBER)
{
  CONVERT_DEC_TO_UNIT(NUMBER);
  CUT_ZERO();
  switch (DGIT)
  {
    case 0:    
      DG0=DG1=1;    
      DG0=0;

      PORTB=DATA0;

      DGIT=1;
      break;
    case 1:
      DG0=DG1=1;    
      DG1=0;

      PORTB=DATA1;
      DGIT=0;
      break;
    default:
      DGIT=0;
      break;
  }
}

#int_TIMER1
void  TIMER1_isr(void)
{
  set_timer1(65535-25000+51);
  restart_wdt();
 
  if(SETCOIN_B==0&&FIRST_L==0)
  {   
    if(++SETCOIN_T>=20)//100MS
    {
      SETCOIN_T=0;
      FIRST_L=1;
      SETCOIN_W=1;
      NORMAL_W=1;
    
      TIME=0;
      CHECK_W=0;
      BLINK1_W=0;
      BLINK2_W=0;//BLINK NUMBER
      NUMBER_L=0;
      NUMBER=0;
      GROUND_COIN=1;
    }
    }else if(SETCOIN_B==1)
    {
         SETCOIN_T=0;
    }else if(SETCOIN_B==0)
    {
         SELECTCOIN_T=0;
    }
   
  if(SETLEVEL_B==0&&FIRST_L==0)
  {
     if(++SETLEVEL_T>=20)//100MS
     {
        SETLEVEL_T=0;
        FIRST_L=1;
        SETLEVEL_W=1;
        NORMAL_W=1;
       
        TIME=0;
        CHECK_W=0;
        BLINK1_W=0;
        BLINK2_W=0;//BLINK NUMBER
        NUMBER_L=0;
        NUMBER=0;
        GROUND_COIN=1;
     }
  }else if(SETLEVEL_B==1)
  {
      SETLEVEL_T=0;
  }else if(SETLEVEL_B==0)
  {
      SELECTLEVEL_T=0;
  }
////////////////////////////////////////////////////////////////
  if(SETCOIN_W==1)//BLINK5 BLINK10
  {      
    if(BLINK1_W==0)//BLINK 5
    {
      if(SETCOIN_B==1)
      {
          if(++SELECTCOIN_T>=20)//100MS
          {
             SELECTCOIN_T=0;
             SETCOIN_W=0;
             SET5_W=1;             
          }
      }
      DISPLAY(5);

      if(++BLINK_T>=200)//500MS
      {
        BLINK_T=0;
        BLINK1_W=1;
      }   
    }else
    if(BLINK1_W==1)//BLINK 10
    {
       if(SETCOIN_B==1)
       {
         if(++SELECTCOIN_T>=20)//100MS
         {
            SELECTCOIN_T=0;
            SETCOIN_W=0;
            SET10_W=1;             
         }
       }
       DISPLAY(10);
        
       if(++BLINK_T>=200)//500MS
       {
         BLINK_T=0;
         BLINK1_W=0;      
       }                                                                       
    }
  }
///////////////////////////////////////////////////////////////
  if(SETLEVEL_W==1)
  {
       if(FIRST2_L==0)
       {
          FIRST2_L=1;
          i=1;    
       }
         
       if(SETLEVEL_B==1)
       {
         if(++SELECTLEVEL_T>=200)//1000MS
         {
            SELECTLEVEL_T=0;
            SETLEVEL_W=0;                      
            SAVE_W=1;      
            SAVELEVEL_W=1;                     
         }
       }else
       {       
         if(i>9)i=1;
         if(++SETLEVEL_T>=200)//1S
         {
           SETLEVEL_T=0;
           i++;           
         }      
       }
       DISPLAY(i);          
  }
///////////////////////////////////////////////////////////////
  if(NORMAL_W==0)//NORMAL MODE
  {
    if(CHECK_W==1)
    {  
      if(STB_NUMBER==0)
      {
        if(++TIME>=1000)//200=1SEC
        {
          TIME=0;
          CHECK_W=0;
          BLINK1_W=0;
          BLINK2_W=0;//BLINK NUMBER
          NUMBER_L=0;
          NUMBER=0;
          GROUND_COIN=1;
        }    
      }
    }

    if(COIN_B==0&&COIN_L==0)
    {
      if(++COIN_T>=4)//DELAY 20MS
      {
        COIN_T=0;
        COIN_L=1;
        CHECK_L=0;
        BLINK1_W=1;
        BLINK3_W=0;
        BLINK_T=0;
 
        NUMBER=NUMBER+PLX;
      }           
    }else if(COIN_B==1)
    {   
      COIN_T=0;
      COIN_L=0;
    }
 
    if(BLINK1_W==0)//BLINK0 BLINK0
    {
      if(COL0_W==0)
      {
        DISPLAY(0);
        if(++BLINK_T>=100)//500MS
        {
          BLINK_T=0;
          COL0_W=1;
        }   
      }else
      if(COL0_W==1)
      {
        DG0=DG1=1;    
        DG1=0;

        PORTB=DIGIT[0];
   
        if(++BLINK_T>=100)//500MS
        {
          BLINK_T=0;
          COL0_W=0;
        }    
      }  
    }else
    if(BLINK1_W==1)//BLINK0 STOP
    {  
         if(BLINK2_W==0)
         {
            if(++BLINK_T>=100)
            {  
               BLINK_T=0;
               BLINK3_W=~BLINK3_W;
            }
            if(BLINK3_W==0)
            {
               DISPLAY(NUMBER);
            }else DG0=DG1=1;
         }else
         {
            DISPLAY(NUMBER);
         }
                 
      if(NUMBER>=NCX&&NUMBER_L==0)
      {
        NUMBER_L=1;
        BLINK2_W=1;
        MATCHINE_W=1;    
      }           
   }
  }
////////////////////////////////////////////////////////////////
  if(SET5_W==1)//MODE 5BATH BLINK 5
  {
    if(BLINK1_W==0)
    {
      if(SETCOIN_B==0)
      {
        if(++SETCOIN_T>=20)//100MS
        {
           SETCOIN_T=0;
           UP5_W=1;
           SET5_W=0;                                         
        }
      }
      DISPLAY(5);

      if(++BLINK_T>=100)//500MS
      {
        BLINK_T=0;
        BLINK1_W=1;
      }   
    }else//BLINK SPACE
    {
      DG0=DG1=1;
     
      if(++BLINK_T>=100)//500MS
      {
        BLINK_T=0;
        BLINK1_W=0;
        if(N++>=10)//N TIME EXIT
        {
          N=0;
          SETCOIN_W=0;
          SET5_W=0;
          FIRST_L=0;
          NORMAL_W=0;
        }                    
      }   
    }             
  }
 
  if(UP5_W==1)
  {
    if(FIRSTUP_L==0)
    {
      FIRSTUP_L=1;
      NUMSAVE=5;        
    }
   
    DISPLAY(NUMSAVE);
   
    if(SETCOIN_B==1)
    {
      if(++SELECTCOIN_T>=200)//1SEC
      {
        SELECTCOIN_T=0;
        SAVE_W=1;
        PLX=5;//+5
       
        SAVECOIN_W=1;
      }
     }else
     {
       if(++TIME>=200)
       {
          TIME=0;
          NUMSAVE=NUMSAVE+10;
          if(NUMSAVE>=100)NUMSAVE=5;
       }
     }    
  }
    
/////////////////////////////////////////////////////////
  if(SET10_W==1)//MODE 10BATH BLINK10
  {
      if(BLINK1_W==0)
      {
         if(SETCOIN_B==0)
         {
           if(++SETCOIN_T>=20)//100MS
           {
              SETCOIN_T=0;
              UP10_W=1;
              SET10_W=0;                                         
           }
         }
         DISPLAY(10);
 
         if(++BLINK_T>=100)//500MS
         {
           BLINK_T=0;
           BLINK1_W=1;
         }   
      }else//BLINK SPACE
      {       
          DG0=DG1=1;
         
          if(++BLINK_T>=100)//500MS
          {
              BLINK_T=0;
              BLINK1_W=0;
              if(N++>=10)//N TIME EXIT
              {
                  N=0;
                  SETCOIN_W=0;
                  SET10_W=0;
                  FIRST_L=0;
                  NORMAL_W=0;
              }                    
          }   
      }             
  }
 
  if(UP10_W==1)
  {
       if(FIRSTUP_L==0)
       {
          FIRSTUP_L=1;
          NUMSAVE=10;       
       }
      
       DISPLAY(NUMSAVE);
      
       if(SETCOIN_B==1)
       {
          if(++SELECTCOIN_T>=200)//1SEC
          {
               SELECTCOIN_T=0;
              
               SAVE_W=1;
               PLX=10;//+10
            
               SAVECOIN_W=1;
          }
       }else
       if(++TIME>=200)
       {
         TIME=0;
         NUMSAVE=NUMSAVE+10;
         if(NUMSAVE>=100)NUMSAVE=10;
       }    
  }
}

void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_ON);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!
  set_timer1(65535-25000+51);
  RBPU=0;
  TRISA=0B11110111;//1=INPUT, 0=OUTPUT
  TRISB=0B10000000;
  TRISC=0B00111000;
  PORTB=0XFF;
 
  NCX=read_eeprom(1);//NUMBER COIN
  PLX=read_eeprom(2);//PLUS +5,+10
  LVX=read_eeprom(3);//LEVEL

  GROUND_COIN=1;
 
  while(1)
  {
     if(MATCHINE_W==1)
    {
        GROUND_COIN=0;
    
        POWER=1;
        delay_ms(1000);
            
        POWER=0;
        delay_ms(1000);
        for(j=0;j<LVX;j++)
        {
          LEVEL=1;
          delay_ms(1000);     
          LEVEL=0;
          delay_ms(1000);
        }
       
        START=1;
        delay_ms(1000);

        START=0;
        delay_ms(1000);
       
        MATCHINE_W=0;
        CHECK_W=1;
    }
   
    if(SAVE_W==1)
    {           
        if(SAVELEVEL_W==1)
        {
             SAVELEVEL_W=0;
           
             write_eeprom(3,i);      
        
             LVX=i;               
             DELAY_MS(1000);           
        }else
        if(SAVECOIN_W==1)
        {
            SAVECOIN_W=0;
           
            write_eeprom(1,NUMSAVE);
            write_eeprom(2,PLX);//+
           
            NCX=NUMSAVE;
            
            DELAY_MS(1000);
                   
            UP5_W=0;
            UP10_W=0;                   
        }
                             
        for(i=0;i<100;i++)
        {
            DG0=DG1=1;
            DG0=0;  
           
            PORTB=0X04>>1;//A
            delay_ms(5);
                   
            DG0=DG1=1;
            DG1=0;
           
            PORTB=0X42>>1;//S
            delay_ms(5);
        }
           
        FIRST_L=0;
        FIRSTUP_L=0;
        NUMSAVE=0;
        SAVE_W=0;           
        TIME=0;   
        NORMAL_W=0;                     
    }     
  }
}

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