51单片机数码管、蜂鸣器、点阵、流水灯合集

采用了普中的51单片机开发版,该项目代码实现的功能有先执行15秒倒计时接着蜂鸣器响然后进行学号点阵和花样流水灯,可以通过原理图在仿真软件上进行仿真。原理图如下:

51单片机数码管、蜂鸣器、点阵、流水灯合集_第1张图片



#include 
#include 
typedef unsigned char u8;
typedef unsigned int u16;
unsigned int sex;
unsigned int T0count;
unsigned int T0count1;
#define led P2 
u8 tap[] = {0xfe,0xfb,0xef,0xbf};
u8 tap1[] = {0x7e,0xbd,0xdb,0xe7};
static u8 a;
sbit SRCLK=P3^6;
sbit RCLK=P3^5;
sbit SER=P3^4;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit been=P1^5;
u16 j,k,c;
u8 i;
u8 ledduan1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
u8 ledwei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; 
//改学号在这里改只需要改变一下数组的排列就行了
u8 code ledduan[][8]={{0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00},     //5
                      {0x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00},     //1
                      {0x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00},     //1
	                    {0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00},     //9
                      {0x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00},     //1
                      {0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00},     //2
                      {0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00},     //0
                      {0x00,0x00,0x2a,0x49,0x49,0x49,0x36,0x00},     //3
                      {0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00},     //8
										  {0x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00},     //1
                      {0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00},     //8
                      {0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00},     //2    
											{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,}};   
//................................................................//
u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(u16 i)
{
	while(i--);
}
void Delay()		
{
	u8 i, j;

	for(i=0;i<255;i++)
	 for(j=0;j<255;j++);
}
void zuoyi()
{

  for(a=0;a<7;a++) 
    {
      led=_crol_(led, 1);
      Delay(); 
    }
}
void youyi()
{

   for(a=0;a<7;a++) 
     { 
       led=_cror_(led,1);
       Delay();
     }
}
void zuoyoutonshiyi()
{
	 
   for(a=0;a<4;a++)
    {
			P2 = tap1[a];
			Delay();
	  }
		P2 = 0xff; 
		Delay();
		for(a=0;a<4;a++)
    {
			P2 = tap[a];
			Delay();
	  }
		P2 = 0xff; 
}
void pingshan()
{
   for(a=0;a<2;a++)
	  {
	     P2 = 0x0f;
			 Delay();
			 P2 = 0xf0;
			 Delay();
	  }

}
void diaoyonzihanshu()
{
    zuoyi(); //左移
	  Delay();
    youyi();//右移
	  Delay();
	  zuoyoutonshiyi();//个人样式流水灯
	  Delay();
	  pingshan();//个人样式
}
void Timer0_Init()
{
   TMOD = 0X01;
	 TF0 = 0;
   TR0 =1;
   TH0 = 0XFC;
   TL0 = 0X18;
   ET0 = 1;
	 EA = 1;
	 PT0 =1;

}

void daojishi()  
{
	for(j=0;j<15;j++){
    LSA=1;LSB=1;LSC=1; 
		if(sex<10){
		  P0=smgduan[0];
		}else{
	    P0=smgduan[1];
		}
		P0 = 0x00;
	  LSA=0;LSB=1;LSC=1;
		if(sex<10){
		  P0=smgduan[sex];
		}else{
		  P0=smgduan[sex%10];
		}
		P0 = 0x00;
	}
}
void Hc595SendByte(u8 dat)
{
   u8 a;
   SRCLK=0;
	 RCLK=0;
   for(a=0;a<8;a++)
   {
		SER=dat>>7;
		dat<<=1;
		 
		SRCLK=1;
    _nop_();
		_nop_();
		SRCLK=0;
   }
	 RCLK=1;
	 _nop_();
	 _nop_();
	 RCLK=0; 
}
void xuehaodianzhen()
{
     	for(j=0;j<80;j++)                           
		{
			for(i=0;i<8;i++)
			{
				P0=ledwei[i];
				Hc595SendByte(ledduan[k][i]);
				delay(50);				
				Hc595SendByte(0x00);
			}
		}
		for(i=0;i<8;i++)
			{
				P0=ledwei[i];
				Hc595SendByte(ledduan1[i]);
				delay(50);				
				Hc595SendByte(0x00);
				delay(5000);
			}
		k++;   
}
int main()
{
	sex = 15;
	Timer0_Init();

	k = 0;
	for(i=0;i<8;i++)
			{
				P0=ledwei[i];
				Hc595SendByte(ledduan1[i]);
				delay(50);				
				Hc595SendByte(0x00);
			}
	been = 0x00;
	while(1)
	{   
		
		  daojishi();  
		if(sex == 0){
		  been=~been;
		}
		if(TR0 == 0){
		
		 if(k<13){		
		 for(c=0;c<13;c++){
   	    xuehaodianzhen();
		   }
	  }
    led=~0x01;			
		diaoyonzihanshu();
	  if(k>=13)                                    
		{
			k=0;                                   
		}
		 TR0 = 1;
		}
	} 			
}
void Timer0() interrupt 1
{
   TH0 = 0XFC;
   TL0 = 0X18;
	 T0count++;
	 if(T0count == 1000)
    {
			  T0count=0;
		    sex--;
  
			  if(sex == 1){
		       been=~been;
		    }
		 	  if(sex == -1){
				   sex = 15;
					 TR0 = 0;
				}
				
		}
}
//交作业,改一下点阵学号就没问题了,就是最开始的那个二维数组的排序

你可能感兴趣的:(笔记,51单片机,单片机,嵌入式硬件)