Demo entry 6356141

C

   

Submitted by KK on Apr 16, 2017 at 10:09
Language: C. Code size: 2.3 kB.

#include<reg51.h>
#include<intrins.h>

#define LED	P2
#define REST  P0 
#define unit unsigned int
sbit kcoin=P3^2;
sbit Beep=P1^5;
sbit rst=P3^3;
sbit wrong=P3^4;      

void IntConfiguration();
void Delayms(unit);
unsigned int coin=0;
     

# define s0 0
# define s1 1
# define s2 2
# define s3 3
# define s4 4
# define s5 5
# define s6 6

unsigned char state=s0;

void main(void)
{
	LED=0Xff;
	REST=0Xff;            
	IntConfiguration();
	state=s0;
	while(1)
	{     
		if(rst==1)
		{rst=0;               //reset
		REST=0Xff;
			wrong=1;
			state=s0;
		} else
		  		switch(state)       //state change
		{
			case s0: if(coin==1)
						 {
						 	state=s1;
						 	coin=0;
						 } else
						 {							   
						 	state=s0;
							LED=0xff;
						 }	  break;
			case s1: if(coin==1)
						 {state=s2;
						 	coin=0;
						 	
						 } else
						 {
						 	state=s1;
							LED=(0xff<<1);
						 }	  break;
			case s2: if(coin==1)
						 {state=s3;
						 	coin=0;
						 	
						 } else
						 {
						 	state=s2;
							LED=(0xff<<2);
						 }	 break;		
	    	case s3: if(coin==1)
						 {state=s4;
						 	coin=0;
						 	
						 } else
						 {
						 	state=s3;
							LED=(0xff<<3);
						 }	  break;
			case s4: if(coin==1)
						 {state=s5;
						 	coin=0;
						 	
						
						 } else
						 {
						 	state=s4;
							LED=(0xff<<4);
						 }	   break;
			case s5: if(coin==1)
						 {state=s0;
						 	REST=REST-1;
               
							 coin=0;
						 }else
							 {
							 	state=s5;
								Beep=1;
								Delayms(500);
								Beep=0;
								Delayms(500);
								LED=(0xff<<5);
								 
							 }	   break;	   	 
						 }	
	}	
}
void IntConfiguration()		 //interrupt
{	
	IT0=1;
	EX0=1;
	IT1=1;
	EX1=1;
	EA=1;
}

void Delayms(unit xms)   
{
    
	unit i,j;          //delay
	for(i=xms;i>0;i--)
	     for(j=110;j>0;j--);

}


void Int0()	interrupt 0		
{
	Delayms(500);	
	if(kcoin==0)       
	{
	    	coin=1;
	}
}

void Int1()	interrupt 2
{
	Delayms(500);	                 //wrong coin beep
	while(wrong==0)
	{       
	 							Beep=1;
								Delayms(500);
								Beep=0;
								Delayms(500);							 
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).