SOURCE CODEหยอดเหรียญเครื่องซักผ้าแบบประหยัดROM
RAM AT89C2051
27/1/2558 SONGCHAI PRAPATRUNGSEE
SOURCE CODEหยอดเหรียญเครื่องซักผ้าแบบประหยัดROM
RAM AT89C2051
แบบที่1
SWITCH
DELAYแบบแก้ไขง่าย
ใช้แรม30.3ไบท์ รอม682ไบท์
ประหยัดรอมและแรมมากที่สุด
แบบที่2 ใช้SWITCH DELAY
ใช้แรม30.3ไบท์ รอม682ไบท์
ประหยัดรอมและแรมมากที่สุด
แบบที่3 ใช้ if DELAY
แรม30.4ไบท์ รอม673ไบท์
แบบที่4 ใช้SWITCH DELAYเป็นTIMER1
แรม30.3ไบท์ รอม718ไบท์
แบบที่5 ใช้SWITCH DELAYในฟังก์ชันTIMER
แรม30.3ไบท์ รอม730ไบท์
แบบที่1 ดีที่สุด ประหยัดทั้งรอมและแรมและแก้ไขง่าย
แบบที่3 ประหยัดรอมมากกว่าเพราะใช้ใช้แค่ if
else
แต่ถ้าใช้ if else if else จะกินรอมและแรมมากกว่าแบบใช้คำสั่งswitch
จะอธิบายเฉพาะSOURCE
CODEแบบที่1
คำสั่ง #define เป็นคำสั่งเทียม
นั่นหมายถึงไม่กินรอมและแรม
ค่ากำหนดที่สามารถแก้ไขได้
บรรทัดที่1A #define
BLINK 1
หมายความว่า
ถ้าเขียนเป็นลอจิก0 ให้7SEGกระพริบเมื่อลอจิกเป็น0
ถ้าเขียนเป็นลอจิก1 ให้7SEGกระพริบเมื่อลอจิกเป็น1
บรรทัดที่1B #define CMM0 25 หมายความว่า
กำหนดเหรียญสูงสุดในโหมด0 = 25 บาท
บรรทัดที่1C #define CMM1 30 หมายความว่า
กำหนดเหรียญสูงสุดในโหมด1 = 30 บาท
บรรทัดที่1D #define D1M0 DATA[2] หมายความว่า
ตัวเลขหลักสิบในโหมด0เป็นเลข2
ส่วนตัวเลขหลักหน่วยในโหมด0 จะเป็นเลข5ตลอด
บรรทัดที่1E #define
D1M1 DATA[3] หมายความว่า
ตัวเลขหลักสิบในโหมด1เป็นเลข3
ส่วนตัวเลขหลักหน่วยในโหมด1จะเป็นเลข0ตลอด
///////////////////////////////////////////
SOURCE CODE 5 แบบ
แบบที่1ดีที่สุด
แบบที่1 SWITCH
DELAYแบบแก้ไขง่าย
/* 1/2-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>//1
#define BLINK 1//1A
#define CMM0 25//1B 25 BATH
#define CMM1 30//1C 30 BATH
#define D1M0 DATA[2]//1D 25 BATH
#define D1M1 DATA[3]//1E 30 BATH
//sbit =COMMAND, COIN_B=VARIABLE NAME, P3^5=PIN 3.5
sbit MODE_B=P1^0;//2
sbit COIN_B=P3^5;//3
sbit POWER=P3^7;//4
sbit START=P3^2;//5
sbit GROUND_COIN=P3^3;//6
sbit STB_NUMBER=P3^4;//7
sbit DG1=P3^0;//8
sbit DG2=P3^1;//9
bit BLINK_W,MATCHINE_W,CHECK_W;//10
bit COIN_L,NUMBER_L,CHECK_L,CX_L;//11
bit PON_L=1,POFF_L,STON_L,STOFF_L;//12
char DATA[]={0X11,0XB7,0X29,0X23,0X87,//13
0X43,0X41,0X37,0X01,0X03,};//14
unsigned char CX;//15
unsigned char DG,NUMBER,CN,BLINK_T;//16
unsigned int TIME;//17
unsigned char DIGIT1,DIGIT2,i;//18
void CONVERT_DEC_TO_UNIT(unsigned char NUMBER)//19
{
unsigned char
NUM;//20
NUM=((NUMBER%100)/10);//21 SIB
DIGIT1=DATA[NUM];//22
NUM=NUMBER%10;//23 NAUY
DIGIT2=DATA[NUM];//24
}
void CUT_ZERO()//25
{
if(DIGIT1==DATA[0])//26
DIGIT1=0XFF;//27
}
void DELAY_MS(unsigned int TIME)//28
{
unsigned int
i,j;//29
for(i=TIME;i>0;i--)//30
{
for(j=34;j>0;j--);//31
}
}
void T0M1_16BIT()interrupt 1//32 INTERRUPT VECTOR1
{
//TR0=0;//STOP
TIMER0
//TF0=0;//TIMER
OVERFLOW FLAG=CLEAR
TH0=0XFD;//33 HI
BYTE TIMER0
TL0=0X66;//34
LOW BYTE TIMER0
//TR0=1;//START
TIMER0
if(CHECK_W==1)//35
{
if(STB_NUMBER==BLINK)//36***
{
TIME++;//37
if(TIME==2500)//38 500=1SEC
{
TIME=0;//39
CX_L=0;//40
CHECK_W=0;//41
BLINK_W=0;//42
NUMBER_L=0;//43
CN=0;//44
GROUND_COIN=1;//45
}
}else
TIME=0;//45/1 PULSE
}
if(COIN_B==0&&COIN_L==0&&CX_L==0)//46
{
PON_L=1;//47
POFF_L=0;//48
TIME++;//49
if(TIME==8)//50 DELAY 16MS
{
TIME=0;//51
COIN_L=1;//52
CHECK_L=0;//53
BLINK_W=1;//54
if(MODE_B==0)//55
{
NUMBER=++CN*5;//56
CX=CMM0;//57***
}else //58
MODE_B=1
{
NUMBER=++CN*10;//59
CX=CMM1;//60***
}
}
}else
if(COIN_B==1)COIN_L=0;//61
if(BLINK_W==0)//62
{
if(MODE_B==0)//63
{
BLINK_T++;//64
if(BLINK_T<=250&&PON_L==1)//65
500MS
{
if(BLINK_T==250)//66
{
BLINK_T=0;//67
PON_L=0;//68
POFF_L=1;//69
}
switch
(DG)//70
{
case
0://71
DG1=DG2=1;//72 DG2 DG1 OFF
DG1=0;//73
DG1 ON
P1=D1M0;//74 DIGIT1***
DG=1;//75
break;//76
case
1://77
DG1=DG2=1;//78 DG2 DG1 OFF
DG2=0;//79
DG2 ON
P1=DATA[5];//80 DIGIT2
DG=0;//81
break;//82
}
}else//83
if(BLINK_T<=250&&POFF_L==1)//84 500MS
{
if(BLINK_T==250)//85
{
BLINK_T=0;//86
PON_L=1;//87
POFF_L=0;//88
}
DG1=DG2=1;//89 OFF
}
}else //90
MODE_B==1
{
BLINK_T++;//91
if(BLINK_T<=250&&PON_L==1)//92
500MS
{
if(BLINK_T==250)//93
{
BLINK_T=0;//94
PON_L=0;//95
POFF_L=1;//96
}
switch
(DG)//97
{
case
0://98
DG1=DG2=1;//99 DG2 DG1 OFF
DG1=0;//100 DG1 ON
P1=D1M1;//101 DIGIT1***
DG=1;//102
break;//103
case
1://104
DG1=DG2=1;//105 DG2 DG1 OFF
DG2=0;//106 DG2 ON
P1=DATA[0];//107 DIGIT2
DG=0;//108
break;//109
}
}else//110
if(BLINK_T<=250&&POFF_L==1)//111 500MS
{
if(BLINK_T==250)//112
{
BLINK_T=0;//113
PON_L=1;//114
POFF_L=0;//115
}
DG1=DG2=1;//116
OFF
}
}
}else//117
BLINK_W==1
{
if(NUMBER==CX&&NUMBER_L==0)//118
{
CX_L=1;//119
NUMBER_L=1;//120
MATCHINE_W=1;//121
}
CONVERT_DEC_TO_UNIT(NUMBER);//122
CUT_ZERO();//123
switch
(DG)//124
{
case 0://125
DG1=DG2=1;//126 DG2 DG1 OFF
DG2=0;//127 DG2 ON
P1=DIGIT2;//128
DG=1;//129
break;//130
case 1://131
DG2=DG1=1;//132 DG2 DG1 OFF
DG1=0;//133 DG1 ON
P1=DIGIT1;//134
DG=0;//135
break;//136
}
}
}
void main()//137
{
TMOD=0X1;//138
SET TIMER0 MODE1
TCON=0X0;//139
TIMER CONTROLL
IE=0X82;//140
INTERRUPT ENABLE
IP=0X0;//141
INTERRUPT PRIORITY
TH0=0X0;//142 HI
BYTE TIMER0
TL0=0X0;//143
LOW BYTE TIMER0
TR0=1;//144
START TIMER0
POWER=0;//145
START=0;//146
while(1)//147
{
if(MATCHINE_W==1)//148
{
GROUND_COIN=0;//149
POWER=1;//150
DELAY_MS(1000);//151
POWER=0;//152
DELAY_MS(1000);//153
START=1;//154
DELAY_MS(1000);//155
START=0;//156
DELAY_MS(1000);//157
MATCHINE_W=0;//158
CHECK_W=1;//159
}
}
}
/////////////////////////////////////////
แบบที่2 SWITCH
DELAY
/* 25-1-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>//1
//sbit =COMMAND,
COIN_B=VARIABLE NAME, P3^5=PIN 3.5
sbit MODE_B=P1^0;//2
sbit COIN_B=P3^5;//3
sbit POWER=P3^7;//4
sbit START=P3^2;//5
sbit GROUND_COIN=P3^3;//6
sbit STB_NUMBER=P3^4;//7
sbit DG1=P3^0;//8
sbit DG2=P3^1;//9
bit BLINK_W,MATCHINE_W,CHECK_W;//10
bit COIN_L,NUMBER_L,CHECK_L,CX_L;//11
bit PON_L=1,POFF_L,STON_L,STOFF_L;//12
char DATA[]={0X11,0XB7,0X29,0X23,0X87,//13
0X43,0X41,0X37,0X01,0X03,};//14
unsigned char CX;//15
unsigned char DG,NUMBER,CN,BLINK_T;//16
unsigned int TIME;//17
unsigned char DIGIT1,DIGIT2,i;//18
void CONVERT_DEC_TO_UNIT(unsigned char NUMBER)//19
{
unsigned char
NUM;//20
NUM=((NUMBER%100)/10);//21 SIB
DIGIT1=DATA[NUM];//22
NUM=NUMBER%10;//23 NAUY
DIGIT2=DATA[NUM];//24
}
void CUT_ZERO()//25
{
if(DIGIT1==DATA[0])//26
DIGIT1=0XFF;//27
}
void DELAY_MS(unsigned int TIME)//28
{
unsigned int
i,j;//29
for(i=TIME;i>0;i--)//30
{
for(j=34;j>0;j--);//31
}
}
void T0M1_16BIT()interrupt 1//32 INTERRUPT VECTOR1
{
//TR0=0;//STOP TIMER0
//TF0=0;//TIMER OVERFLOW FLAG=CLEAR
TH0=0XFD;//33 HI BYTE TIMER0
TL0=0X66;//34 LOW BYTE TIMER0
//TR0=1;//START TIMER0
if(CHECK_W==1)//35
{
if(STB_NUMBER==1)//36***
{
TIME++;//37
if(TIME==2500)//38 500=1SEC
{
TIME=0;//39
CX_L=0;//40
CHECK_W=0;//41
BLINK_W=0;//42
NUMBER_L=0;//43
CN=0;//44
GROUND_COIN=1;//45
}
}else
TIME=0;//45/1 PULSE
}
if(COIN_B==0&&COIN_L==0&&CX_L==0)//46
{
PON_L=1;//47
POFF_L=0;//48
TIME++;//49
if(TIME==8)//50 DELAY 16MS
{
TIME=0;//51
COIN_L=1;//52
CHECK_L=0;//53
BLINK_W=1;//54
if(MODE_B==0)//55
{
NUMBER=++CN*5;//56
CX=25;//57***
}else
//58 MODE_B=1
{
NUMBER=++CN*10;//59
CX=30;//60***
}
}
}else
if(COIN_B==1)COIN_L=0;//61
if(BLINK_W==0)//62
{
if(MODE_B==0)//63
{
BLINK_T++;//64
if(BLINK_T<=250&&PON_L==1)//65 500MS
{
if(BLINK_T==250)//66
{
BLINK_T=0;//67
PON_L=0;//68
POFF_L=1;//69
}
switch
(DG)//70
{
case 0://71
DG1=DG2=1;//72 DG2 DG1 OFF
DG1=0;//73 DG1 ON
P1=DATA[2];//74 DIGIT1***
DG=1;//75
break;//76
case 1://77
DG1=DG2=1;//78 DG2 DG1 OFF
DG2=0;//79 DG2 ON
P1=DATA[5];//80 DIGIT2
DG=0;//81
break;//82
}
}else//83
if(BLINK_T<=250&&POFF_L==1)//84
500MS
{
if(BLINK_T==250)//85
{
BLINK_T=0;//86
PON_L=1;//87
POFF_L=0;//88
}
DG1=DG2=1;//89 OFF
}
}else //90 MODE_B==1
{
BLINK_T++;//91
if(BLINK_T<=250&&PON_L==1)//92 500MS
{
if(BLINK_T==250)//93
{
BLINK_T=0;//94
PON_L=0;//95
POFF_L=1;//96
}
switch
(DG)//97
{
case 0://98
DG1=DG2=1;//99 DG2 DG1 OFF
DG1=0;//100 DG1 ON
P1=DATA[3];//101 DIGIT1***
DG=1;//102
break;//103
case 1://104
DG1=DG2=1;//105 DG2 DG1 OFF
DG2=0;//106 DG2 ON
P1=DATA[0];//107 DIGIT2
DG=0;//108
break;//109
}
}else//110
if(BLINK_T<=250&&POFF_L==1)//111
500MS
{
if(BLINK_T==250)//112
{
BLINK_T=0;//113
PON_L=1;//114
POFF_L=0;//115
}
DG1=DG2=1;//116 OFF
}
}
}else//117 BLINK_W==1
{
if(NUMBER==CX&&NUMBER_L==0)//118
{
CX_L=1;//119
NUMBER_L=1;//120
MATCHINE_W=1;//121
}
CONVERT_DEC_TO_UNIT(NUMBER);//122
CUT_ZERO();//123
switch (DG)//124
{
case 0://125
DG1=DG2=1;//126 DG2 DG1 OFF
DG2=0;//127 DG2 ON
P1=DIGIT2;//128
DG=1;//129
break;//130
case 1://131
DG2=DG1=1;//132 DG2 DG1 OFF
DG1=0;//133 DG1 ON
P1=DIGIT1;//134
DG=0;//135
break;//136
}
}
}
void main()//137
{
TMOD=0X1;//138 SET TIMER0 MODE1
TCON=0X0;//139 TIMER
CONTROLL
IE=0X82;//140 INTERRUPT
ENABLE
IP=0X0;//141 INTERRUPT
PRIORITY
TH0=0X0;//142 HI BYTE TIMER0
TL0=0X0;//143 LOW BYTE TIMER0
TR0=1;//144 START TIMER0
POWER=0;//145
START=0;//146
while(1)//147
{
if(MATCHINE_W==1)//148
{
GROUND_COIN=0;//149
POWER=1;//150
DELAY_MS(1000);//151
POWER=0;//152
DELAY_MS(1000);//153
START=1;//154
DELAY_MS(1000);//155
START=0;//156
DELAY_MS(1000);//157
MATCHINE_W=0;//158
CHECK_W=1;//159
}
}
}
/////////////////////////////////////////
แบบที่3 IF
DELAY
/* 25-1-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>//1
//sbit =COMMAND,
COIN_B=VARIABLE NAME, P3^5=PIN 3.5
sbit MODE_B=P1^0;//2
sbit COIN_B=P3^5;//3
sbit POWER=P3^7;//4
sbit START=P3^2;//5
sbit GROUND_COIN=P3^3;//6
sbit STB_NUMBER=P3^4;//7
sbit DG1=P3^0;//8
sbit DG2=P3^1;//9
bit BLINK_W,MATCHINE_W,CHECK_W;//10
bit COIN_L,NUMBER_L,CHECK_L,CX_L;//11
bit SWITCH,PON_L=1,POFF_L,STON_L,STOFF_L;//12
char DATA[]={0X11,0XB7,0X29,0X23,0X87,//13
0X43,0X41,0X37,0X01,0X03,};//14
unsigned char CX;//15
unsigned char DG,NUMBER,CN,BLINK_T;//16
unsigned int TIME;//17
unsigned char DIGIT1,DIGIT2,i;//18
void CONVERT_DEC_TO_UNIT(unsigned char NUMBER)//19
{
unsigned char
NUM;//20
NUM=((NUMBER%100)/10);//21 SIB
DIGIT1=DATA[NUM];//22
NUM=NUMBER%10;//23 NAUY
DIGIT2=DATA[NUM];//24
}
void CUT_ZERO()//25
{
if(DIGIT1==DATA[0])//26
DIGIT1=0XFF;//27
}
void DELAY_MS(unsigned int TIME)//28
DELAY_MS HAVE INPUT
{
unsigned int i,j;//29
for(i=TIME;i>0;i--)//30
{
for(j=34;j>0;j--);//31
}
}
void T0M1_16BIT()interrupt 1//32 VECTOR1
{
//TR0=0;//STOP TIMER0
//TF0=0;//TIMER OVERFLOW FLAG=CLEAR
TH0=0XFD;//33 HI BYTE TIMER0
TL0=0X66;//34 LOW BYTE TIMER0
//TR0=1;//START TIMER0
if(CHECK_W==1)//35
{
if(STB_NUMBER==1)//36***
{
TIME++;//37
if(TIME==2500)//38 500=1SEC
{
TIME=0;//39
CX_L=0;//40
CHECK_W=0;//41
BLINK_W=0;//42
NUMBER_L=0;//43
CN=0;//44
GROUND_COIN=1;//45
}
}else
TIME=0;//PULSE//45/1
}
if(COIN_B==0&&COIN_L==0&&CX_L==0)//46
{
PON_L=1;//47
POFF_L=0;//48
TIME++;//49
if(TIME==8)//50 DELAY 16MS
{
TIME=0;//51
COIN_L=1;//52
CHECK_L=0;//53
BLINK_W=1;//54
if(MODE_B==0)//55
{
NUMBER=++CN*5;//56
CX=25;//57***
}else
//58 MODE_B=1
{
NUMBER=++CN*10;//59
CX=30;//60***
}
}
}else
if(COIN_B==1)COIN_L=0;//61
if(BLINK_W==0)//62
{
if(MODE_B==0)//63 25BATH
{
BLINK_T++;//64
if(BLINK_T<=250&&PON_L==1)//65 500MS
{
if(BLINK_T==250)//66
{
BLINK_T=0;//67
PON_L=0;//68
POFF_L=1;//69
}
if(SWITCH==0)//70
SWITCH TEST
{
DG1=DG2=1;//71 DG2 DG1 OFF
DG1=0;//72 DG1 ON
P1=DATA[2];//73
DIGIT1***
SWITCH=1;//74
}else//75 SWITCH=1
{
DG1=DG2=1;//76 DG2 DG1 OFF
DG2=0;//77 DG2 ON
P1=DATA[5];//78 DIGIT2
SWITCH=0;//79
}
}else//80
if(BLINK_T<=250&&POFF_L==1)//81
500MS
{
if(BLINK_T==250)//82
{
BLINK_T=0;//83
PON_L=1;//84
POFF_L=0;//85
}
DG1=DG2=1;//86 OFF
}
}else //87 MODE_B==1
{
BLINK_T++;//88
if(BLINK_T<=250&&PON_L==1)//89 500MS
{
if(BLINK_T==250)//90
{
BLINK_T=0;//91
PON_L=0;//92
POFF_L=1;//93
}
if(SWITCH==0)//94
{
DG1=DG2=1;//95 DG2 DG1 OFF
DG1=0;//96 DG1 ON
P1=DATA[3];//97 DIGIT1***
SWITCH=1;//98
}else//99 SWITCH=1
{
DG1=DG2=1;//100 DG2 DG1 OFF
DG2=0;//101 DG2 ON
P1=DATA[0];//102 DIGIT2
DG=0;//103
SWITCH=0;//104
}
}else//105
if(BLINK_T<=250&&POFF_L==1)//106
500MS
{
if(BLINK_T==250)//107
{
BLINK_T=0;//108
PON_L=1;//109
POFF_L=0;//110
}
DG1=DG2=1;//111 OFF
}
}
}else//112 BLINK_W==1
{
if(NUMBER==CX&&NUMBER_L==0)//113
{
CX_L=1;//114
NUMBER_L=1;//115
MATCHINE_W=1;//116
}
CONVERT_DEC_TO_UNIT(NUMBER);//117
CUT_ZERO();//118
if(SWITCH==0)//119
{
DG2=DG1=1;//120 DG2 DG1 OFF
DG2=0;//121 DG2 ON
P1=DIGIT2;//122
SWITCH=1;//123
}else//124 SWITCH=1
{
DG2=DG1=1;//125 DG2 DG1 OFF
DG1=0;//126 DG1 ON
P1=DIGIT1;//127
SWITCH=0;//128
}
}
}
void main()//137
{
TMOD=0X1;//138 SET TIMER0 MODE1
TCON=0X0;//139 TIMER
CONTROLL
IE=0X82;//140 INTERRUPT
ENABLE
IP=0X0;//141 INTERRUPT
PRIORITY
TH0=0X0;//142 HI BYTE TIMER0
TL0=0X0;//143 LOW BYTE TIMER0
TR0=1;//144 START TIMER0
POWER=0;//145
START=0;//146
while(1)//147
{
if(MATCHINE_W==1)//148
{
GROUND_COIN=0;//149
POWER=1;//150
DELAY_MS(1000);//151
POWER=0;//152
DELAY_MS(1000);//153
START=1;//154
DELAY_MS(1000);//155
START=0;//156
DELAY_MS(1000);//157
MATCHINE_W=0;//158
CHECK_W=1;//159
}
}
}
//////////////////////////////////////////////
แบบที่4 SWITCH DELAYเป็นTIMER
/* 25-1-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>//1
//sbit =COMMAND, COIN_B=VARIABLE NAME, P3^5=PIN 3.5
sbit MODE_B=P1^0;//2
sbit COIN_B=P3^5;//3
sbit POWER=P3^7;//4
sbit START=P3^2;//5
sbit GROUND_COIN=P3^3;//6
sbit STB_NUMBER=P3^4;//7
sbit DG1=P3^0;//8
sbit DG2=P3^1;//9
bit BLINK_W,MATCHINE_W,CHECK_W;//10
bit COIN_L,NUMBER_L,CHECK_L,CX_L;//11
bit PON_L=1,POFF_L,STON_L,STOFF_L;//12
char DATA[]={0X11,0XB7,0X29,0X23,0X87,//13
0X43,0X41,0X37,0X01,0X03,};//14
unsigned char CX;//15
unsigned char DG,NUMBER,CN,BLINK_T;//16
unsigned int TIME;//17
unsigned char DIGIT1,DIGIT2,i;//18
void CONVERT_DEC_TO_UNIT(unsigned char NUMBER)//19
{
unsigned char
NUM;//20
NUM=((NUMBER%100)/10);//21 SIB
DIGIT1=DATA[NUM];//22
NUM=NUMBER%10;//23 NAUY
DIGIT2=DATA[NUM];//24
}
void CUT_ZERO()//25
{
if(DIGIT1==DATA[0])//26
DIGIT1=0XFF;//27
}
void DELAY_T1M1(unsigned char HI_BYTE,LOW_BYTE)//28
{
//IF NOT SET
TMOD, =TIMER1 MODE0 13BIT
//TIMER1 MODE1
16BIT
TH1=HI_BYTE;//29
HI BYTE
TL1=LOW_BYTE;//30 LOW BYTE
TF1=0;//31
TIMER1 OVERFLOW
TR1=1;//32 TIMER
START
while(TF1==0);//33
TR1=0;//34 TIMER
STOP
}
void T0M1_16BIT()interrupt 1//35 INTERRUPT VECTOR1
{
//TR0=0;//STOP
TIMER0
//TF0=0;//TIMER
OVERFLOW FLAG=CLEAR
TH0=0XFD;//36 HI
BYTE TIMER0
TL0=0X66;//37
LOW BYTE TIMER0
//TR0=1;//START
TIMER0
if(CHECK_W==1)//38
{
if(STB_NUMBER==1)//39***
{
TIME++;//40
if(TIME==2500)//41 500=1SEC
{
TIME=0;//42
CX_L=0;//43
CHECK_W=0;//44
BLINK_W=0;//45
NUMBER_L=0;//46
CN=0;//47
GROUND_COIN=1;//48
}
}else
TIME=0;//48/1 PULSE
}
if(COIN_B==0&&COIN_L==0&&CX_L==0)//49
{
PON_L=1;//50
POFF_L=0;//51
TIME++;//52
if(TIME==8)//53
DELAY 16MS
{
TIME=0;//54
COIN_L=1;//55
CHECK_L=0;//56
BLINK_W=1;//57
if(MODE_B==0)//58
{
NUMBER=++CN*5;//59
CX=25;//60***
}else //61
MODE_B=1
{
NUMBER=++CN*10;//62
CX=30;//63***
}
}
}else
if(COIN_B==1)COIN_L=0;//64
if(BLINK_W==0)//65
{
if(MODE_B==0)//66
{
BLINK_T++;//67
if(BLINK_T<=250&&PON_L==1)//68
500MS
{
if(BLINK_T==250)//69
{
BLINK_T=0;//70
PON_L=0;//71
POFF_L=1;//72
}
switch
(DG)//73
{
case
0://74
DG1=DG2=1;//75 DG2 DG1 OFF
DG1=0;//76
DG1 ON
P1=DATA[2];//77 DIGIT1***
DG=1;//78
break;//79
case
1://80
DG1=DG2=1;//81 DG2 DG1 OFF
DG2=0;//82
DG2 ON
P1=DATA[5];//83 DIGIT2
DG=0;//84
break;//85
}
}else//86
if(BLINK_T<=250&&POFF_L==1)//87 500MS
{
if(BLINK_T==250)//88
{
BLINK_T=0;//89
PON_L=1;//90
POFF_L=0;//91
}
DG1=DG2=1;//92
OFF
}
}else //93
MODE_B==1
{
BLINK_T++;//94
if(BLINK_T<=250&&PON_L==1)//95
500MS
{
if(BLINK_T==250)//96
{
BLINK_T=0;//97
PON_L=0;//98
POFF_L=1;//99
}
switch
(DG)//100
{
case
0://101
DG1=DG2=1;//102 DG2 DG1 OFF
DG1=0;//103 DG1 ON
P1=DATA[3];//104 DIGIT1***
DG=1;//105
break;//106
case
1://107
DG1=DG2=1;//108 DG2 DG1 OFF
DG2=0;//109 DG2 ON
P1=DATA[0];//110 DIGIT2
DG=0;//111
break;//112
}
}else//113
if(BLINK_T<=250&&POFF_L==1)//114 500MS
{
if(BLINK_T==250)//115
{
BLINK_T=0;//116
PON_L=1;//117
POFF_L=0;//118
}
DG1=DG2=1;//119
OFF
}
}
}else//120
BLINK_W==1
{
if(NUMBER==CX&&NUMBER_L==0)//121
{
CX_L=1;//122
NUMBER_L=1;//123
MATCHINE_W=1;//124
}
CONVERT_DEC_TO_UNIT(NUMBER);//125
CUT_ZERO();//126
switch
(DG)//127
{
case 0://128
DG1=DG2=1;//129 DG2 DG1 OFF
DG2=0;//130 DG2 ON
P1=DIGIT2;//131
DG=1;//132
break;//133
case 1://134
DG2=DG1=1;//135 DG2 DG1 OFF
DG1=0;//136 DG1 ON
P1=DIGIT1;//137
DG=0;//138
break;//139
}
}
}
void main()//140
{
TMOD=0X11;//141 TIMER0
MODE1& TIMER1 MODE1
TCON=0X00;//142
TIMER CONTROLL
IE=0X82;//143
INTERRUPT ENABLE
IP=0X00;//144
INTERRUPT PRIORITY
TH0=0XED;//145
HI BYTE TIMER0
TL0=0XFF;//146
LOW BYTE TIMER0
TR0=1;//147
START TIMER0
POWER=0;//148
START=0;//149
while(1)//150
{
if(MATCHINE_W==1)//151
{
GROUND_COIN=0;//152
POWER=1;//153
for(i=0;i<100;i++)//154
{
DELAY_T1M1(0XF2,0XFB);//155
}
POWER=0;//156
for(i=0;i<100;i++)//157
{
DELAY_T1M1(0XF2,0XFB);//158
}
START=1;//159
for(i=0;i<100;i++)//160
{
DELAY_T1M1(0XF2,0XFB);//161
}
START=0;//162
for(i=0;i<100;i++)//163
{
DELAY_T1M1(0XF2,0XFB);//164
}
MATCHINE_W=0;//165
CHECK_W=1;//166
}
}
}
///////////////////////////////////////////
แบบที่5 SWITCH
DELAYในฟังค์ชันINTERRUPT
/* 25-1-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>//1
//sbit =COMMAND, COIN_B=VARIABLE NAME, P3^5=PIN 3.5
sbit MODE_B=P1^0;//2
sbit COIN_B=P3^5;//3
sbit POWER=P3^7;//4
sbit START=P3^2;//5
sbit GROUND_COIN=P3^3;//6
sbit STB_NUMBER=P3^4;//7
sbit DG1=P3^0;//8
sbit DG2=P3^1;//9
bit BLINK_W,MATCHINE_W,CHECK_W;//10
bit COIN_L,NUMBER_L,CHECK_L,CX_L;//11
bit PON_L=1,POFF_L,STON_L,STOFF_L;//12
char DATA[]={0X11,0XB7,0X29,0X23,0X87,//13
0X43,0X41,0X37,0X01,0X03,};//14
unsigned char CX;//15
unsigned char DG,NUMBER,CN,BLINK_T;//16
unsigned int TIME;//17
unsigned char DIGIT1,DIGIT2,i;//18
void CONVERT_DEC_TO_UNIT(unsigned char NUMBER)//19
{
unsigned char
NUM;//20
NUM=((NUMBER%100)/10);//21 SIB
DIGIT1=DATA[NUM];//22
NUM=NUMBER%10;//23 NAUY
DIGIT2=DATA[NUM];//24
}
void CUT_ZERO()//25
{
if(DIGIT1==DATA[0])//26
DIGIT1=0XFF;//27
}
void T0M1_16BIT()interrupt 1//28 INTERRUPT VECTOR1
{
//TR0=0;//STOP
TIMER0
//TF0=0;//TIMER
OVERFLOW FLAG=CLEAR
TH0=0XFD;//29 HI
BYTE TIMER0
TL0=0X66;//30
LOW BYTE TIMER0
//TR0=1;//START
TIMER0
if(CHECK_W==1)//31
{
if(STB_NUMBER==1)//32***
{ TIME++;
if(TIME==2500)//33 500=1SEC
{
TIME=0;//34
CX_L=0;
CHECK_W=0;//35
BLINK_W=0;//36
NUMBER_L=0;//37
CN=0;//38
GROUND_COIN=1;//39
}
}else
TIME=0;//39/1 PULSE
}
if(COIN_B==0&&COIN_L==0&&CX_L==0)//40
{
PON_L=1;//41
POFF_L=0;//42
TIME++;//43
if(TIME==8)//44
DELAY 16MS
{
TIME=0;//45
COIN_L=1;//46
CHECK_L=0;//47
BLINK_W=1;//48
if(MODE_B==0)//49
{
NUMBER=++CN*5;//50
CX=25;//51***
}else //52
MODE_B=1
{
NUMBER=++CN*10;//53
CX=30;//54***
}
}
}else
if(COIN_B==1)COIN_L=0;//55
if(BLINK_W==0)//56
{
if(MODE_B==0)//57
{
BLINK_T++;//58
if(BLINK_T<=250&&PON_L==1)//59
500MS
{
if(BLINK_T==250)//60
{
BLINK_T=0;//61
PON_L=0;//62
POFF_L=1;//63
}
switch
(DG)//64
{
case
0://65
DG1=DG2=1;//66
DG2 DG1 OFF
DG1=0;//67
DG1 ON
P1=DATA[2];//68
DIGIT1***
DG=1;//69
break;//70
case
1://71
DG1=DG2=1;//72 DG2 DG1 OFF
DG2=0;//73
DG2 ON
P1=DATA[5];//74 DIGIT2
DG=0;//75
break;//76
}
}else//77
if(BLINK_T<=250&&POFF_L==1)//80 500MS
{
if(BLINK_T==250)//81
{
BLINK_T=0;//82
PON_L=1;//83
POFF_L=0;//84
}
DG1=DG2=1;//85
OFF
}
}else //86
MODE_B==1
{
BLINK_T++;//87
if(BLINK_T<=250&&PON_L==1)//88
500MS
{
if(BLINK_T==250)//89
{
BLINK_T=0;//90
PON_L=0;//91
POFF_L=1;//92
}
switch (DG)//93
{
case
0://94
DG1=DG2=1;//95 DG2 DG1 OFF
DG1=0;//96
DG1 ON
P1=DATA[3];//97 DIGIT1***
DG=1;//98
break;//99
case
1://100
DG1=DG2=1;//101 DG2 DG1 OFF
DG2=0;//102 DG2 ON
P1=DATA[0];//103
DIGIT2
DG=0;//104
break;//105
}
}else//109
if(BLINK_T<=250&&POFF_L==1)//110 500MS
{
if(BLINK_T==250)//111
{
BLINK_T=0;//112
PON_L=1;//113
POFF_L=0;//114
}
DG1=DG2=1;//115
OFF
}
}
}else//116
BLINK_W==1
{
if(NUMBER==CX&&NUMBER_L==0)//117
{
CX_L=1;//118
NUMBER_L=1;//119
MATCHINE_W=1;//120
}
CONVERT_DEC_TO_UNIT(NUMBER);//121
CUT_ZERO();//122
switch (DG)//123
{
case 0://124
DG1=DG2=1;//125 DG2 DG1 OFF
DG2=0;//126 DG2 ON
P1=DIGIT2;//127
DG=1;//128
break;//129
case 1://130
DG1=DG2=1;//131 DG2 DG1 OFF
DG1=0;//132
DG1 ON
P1=DIGIT1;//133
DG=0;//134
break;//135
}
}
if(MATCHINE_W==1)//139
{
GROUND_COIN=0;//140
TIME++;//141
if(TIME==1&&PON_L==1)//142
{
POWER=1;//143
TIME=0;//144
PON_L=0;//145
POFF_L=1;//146
}
if(TIME==500&&POFF_L==1)//147
{
POWER=0;//148
TIME=0;//149
POFF_L=0;//150
STON_L=1;//151
}
if(TIME==500&&STON_L==1)//152
{
START=1;//153
TIME=0;//154
STON_L=0;//155
STOFF_L=1;//156
}
if(TIME==500&&STOFF_L==1)//157
{
START=0;//158
TIME=0;//159
STOFF_L=0;//160
PON_L=1;//161
MATCHINE_W=0;//162
CHECK_W=1;//163
}
}
}
void main()//164
{
TMOD=0X1;//165
SET TIMER0 MODE1
TCON=0X0;//166
TIMER CONTROLL
IE=0X82;//167
INTERRUPT ENABLE
IP=0X0;//168
INTERRUPT PRIORITY
TH0=0X0;//169 HI
BYTE TIMER0
TL0=0X0;//170
LOW BYTE TIMER0
TR0=1;//171
START TIMER0
POWER=0;//172
START=0;//173
while(1);//174
}