Demo entry 6628547

111

   

Submitted by anonymous on Jul 04, 2017 at 13:20
Language: C. Code size: 3.2 kB.

#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int

sbit led = P1^0;
sbit sounder = P1^1;
sbit button = P1^2;
sbit le1 = P3^0;
sbit le2 = P3^1;
unsigned int num = 0;
unsigned int num2 = 0;
unsigned int q,b,g,s;
unsigned int c = 9999;
unsigned int cnt = 0;

uchar code SONG_TONE[]=								  //ÒôÀÖÊý×é
{
 	212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,129,169,190,119,119,126,159,142,159,0
};
uchar code SONG_LONG[]=
{
 	9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0
};
void PlayMusic()											//ÒôÀÖ²¥·Åº¯Êý
{
 	uint i =0,j,k;
	while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
	{
	 	for(j=0;j<SONG_LONG[i]*20;j++)
		{
		 	sounder = ~sounder;
			for(k=0;k<SONG_TONE[i]/3;k++)
			{
			if(button==0)								//Í£Ö¹²¥·Å
			{
				return;
			}
					   
			}
			
		}
		
		i++;
	}
}


void select(int a)									 //Ñ¡Öµ
{
	switch(a)									 
	{
		case 1:P0=0x06; 	break;
		case 2:P0=0x5B;  	break;
		case 3:P0=0x4F;		break;
		case 4:P0=0x66;		break;
		case 5:P0=0x6D;		break;
		case 6:P0=0x7d;		break;
		case 7:P0=0x07;		break;
		case 8:P0=0x7f;		break;
		case 9:P0=0x6f;		break;
		case 0:P0=0x3f;		break;
		default:P0=0xFF;	break;
	}
}


void DelayMS(uint x)
{
 	uchar i;
	while(x--)
	{
	 	for(i=500;i>0;i--);
	}
}

void main()
{
	

	TMOD = 0x01;									  //ÉèÖö¨Ê±Æ÷¹¤×÷ģʽ
	
	TH1 = (65536-45872)/256;						  //50ms
	TL1 = (65536-45872)%256; 
	TH0 = (65536-45872)/256;
	TL0 = (65536-45872)%256;

	EA = 1;										   //ÉèÖÃÖжÏÔÊÐí
	
	ET0 = 1;										//ÉèÖö¨Ê±Æ÷ÖжÏ0 1
	ET1 = 1;

	TR0 = 1;										 //ÔËÐпØÖÆλ
	TR1 = 1;

	led = 1;
	
	while(1)
	{
		q = c/1000;										//·ÖÀëÊý¾Ý
		b = c%1000/100;
		s = c%1000%100/10;
		g = c%1000%100%10;

	   	DelayMS(1);

		P2 = 0xf7; 										//ѡλ
		if(c==9987)									   //Ëø´æλ
		{
			le2 = 1;
			le2 = 0;
		}												
		select(g);										//Ëø´æÖµ
		if(c==9987)
		{
			le1 = 1;
			le1 = 0;
		}
		DelayMS(2);
			
		P2 = 0xfb;
		if(c==9987)
		{
			le2 = 1;
			le2 = 0;
		} 
		select(s);
		if(c==9987)
		{
			le1 = 1;
			le1 = 0;
		}
		DelayMS(2);	
		
		P2 = 0xfd; 
		if(c==9987)
		{
			le2 = 1;
			le2 = 0;
		}
		select(b);
		if(c==9987)
		{
			le1 = 1;
			le1 = 0;
		}
		DelayMS(2);	
				
		P2 = 0xfe; 
		if(c==9987)
		{
			le2 = 1;
			le2 = 0;
		}
		select(q);
		if(c==9987)
		{
			le1 = 1;
			le1 = 0;
		}
		DelayMS(2);	
		

		
	}
	return;
}
 
void timer1() interrupt 3		   //¶¨Ê±Æ÷1ÖжÏ
{
	TH1 = (65536-45872)/256;
	TL1 = (65536-45872)%256;
	
	num++;
	
	if(num==200)
	{  
		if(c!=9987)
		{	
			c--;
			num=0;
		}
	}
}

void timer0() interrupt 1			//¶¨Ê±Æ÷0ÖжÏ
{
	
	TH0 = (65536-45872)/256;
	TL0 = (65536-45872)%256; 

	num2++;
	
	if(num2==20)
	{
		if(c==9987&&cnt<7)							  //LEDÉÁ˸
		{	
			led = !led;
			cnt++;
		}
		if(cnt==7)									//ÉÁ˸Èý´Îºó³£ÁÁ
		{
			led = 0;
			
			while(1)
			{
				PlayMusic();						 //²¥·ÅÒôÀÖ
				if(button==0)						 //Í£Ö¹²¥·Å
				{
					break;
				}
			}
		}
		num2=0;
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).