You are on page 1of 2

#include<reg52.

h>

sbit Quarter= P0^0;


sbit Nickel = P0^1;
sbit Dime = P0^2;
sbit Release = P0^3;

bit CoinIn()
{
if (Quarter == 0 && Nickel == 0 && Dime == 0 )
return 0;
else return 1;
}

bit Sum75(unsigned char sum)


{
if (sum < 75)
return 0 ;
else
return 1;
}

unsigned char AddCoin(unsigned char sum)


{
if ( Quarter == 1)
sum += 25;
if ( Nickel ==1)
sum +=10;
if ( Dime ==1)
sum += 5;
return sum;
}
unsigned char SubRelease(unsigned char sum)
{
sum = sum - 75;
return sum;
}
void main()
{
unsigned char CurrentSt, Action, NextSt, curr_sum;

CurrentSt = 0;
Action = 0;

while(1)
{
switch (CurrentSt) /* select the type of calculation */
{
case 0: if (CoinIn())
{ Action = 0;
NextSt = 1;
}
else
{ Action = 2;
NextSt = 0;
}
break;
case 1: if (Sum75(curr_sum))
{ Action = 1;
NextSt = 1;
}else
{ Action = 2;
NextSt = 2;
}break;
case 2: if (CoinIn())
{ Action = 0;
NextSt = 1;
}else
{ Action = 2;
NextSt = 2;
} break;

CurrentSt = NextSt;

switch (Action)
{
case 0: curr_sum = AddCoin(curr_sum);
Release = 0;
break;
case 1: curr_sum = SubRelease(curr_sum);
Release = 1;
break;
case 2: break;
}

}
}

You might also like