⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 left_right.c

📁 用单片机实现左右移动的KeilC程序,单片机初学入门的最佳选择
💻 C
字号:
//A,B,C,D,分别为P1.1 P1.2 P1.3 P1.4
//STB为P1.7  EN为P1.0  CLK为P3.1 SDA为P3.0
#include <reg52.h>
sbit STB=P1^7;
sbit EN=P1^0;
#define uchar unsigned char
#define uint unsigned int
uchar yid,h;			   //YID为移动计数器,H为行段计数器。
uint zimuo;		           //字模计数器
void sbuf_out();			    //16段扫描
void in_data(void);			//调整数据
void rxd_data(void);		//发送数据
void Large_word(void);
void Small_word(void);

uchar choice,choice1,Flag;
uchar BUFF[12];			   //缓存  定义为5*2+2,如果是显示3个字则为3*2+2;右移时缓存加大

uchar code hanzi[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*--  宽度x高度=16x16  --*/
//图片
0x00,0x00,0x38,0x3C,0x7E,0x7E,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0x7F,0xFE,0x3F,0xFC,0x1F,0xF8,0x0F,0xF0,0x07,0xE0,0x03,0xC0,0x00,0x00,

/*欢   CBBB6 */
0x01,0x00,0x01,0x00,0x01,0x3F,0x7F,0xA0,0x20,0xA1,0x12,0x52,0x02,0x14,0x02,0x08,
0x02,0x18,0x06,0x18,0x05,0x24,0x09,0x24,0x18,0x82,0x70,0x61,0x20,0x1C,0x00,0x00,

/*迎   CD3AD */
0x00,0x02,0x01,0x84,0x3E,0x6C,0x22,0x24,0x22,0x20,0x22,0x20,0x22,0x27,0x22,0x24,
0x22,0xA4,0x2A,0x64,0x12,0x24,0x02,0x04,0x02,0x04,0x00,0x0A,0x7F,0xF1,0x00,0x00,

/*光   CB9E2 */
0x00,0x80,0x08,0x84,0x18,0x98,0x08,0xB0,0x04,0x90,0x20,0x80,0x7F,0xFE,0x02,0x20,
0x02,0x20,0x02,0x20,0x02,0x20,0x42,0x10,0x42,0x10,0x42,0x08,0x7C,0x04,0x00,0x02,

/*临   CC1D9 */
0x00,0x80,0x01,0x90,0x00,0x90,0x7F,0x92,0x04,0x52,0x08,0x52,0x08,0x32,0x20,0x92,
0x7F,0x92,0x24,0x92,0x24,0x92,0x24,0x92,0x24,0x92,0x3F,0x90,0x20,0x90,0x00,0x00,

/*中   CD6D0 */
0x00,0x80,0x00,0x80,0x10,0x84,0x3F,0xFC,0x10,0x84,0x10,0x84,0x10,0x84,0x10,0x84,
0x10,0x84,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,

/*山   CC9BD */
0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x20,0x84,0x20,0x84,0x20,0x84,
0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x3F,0xFE,0x20,0x04,0x00,0x00,

/*焯   CECCC */
0x04,0x08,0x04,0x08,0x7C,0x08,0x04,0x28,0x3F,0xA8,0x20,0x9A,0x3F,0x8A,0x20,0x89,
0x20,0x88,0x3F,0x88,0x04,0x14,0x04,0x24,0x7F,0xC2,0x04,0x02,0x04,0x01,0x04,0x00,

/*科   CBFC6 */
0x10,0x60,0x11,0x1E,0x12,0x10,0x12,0x10,0x10,0x7F,0x11,0x18,0x12,0x38,0x12,0x54,
0x70,0x14,0x1E,0x12,0x11,0xD1,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,

/*电   CB5E7 */
0x00,0x80,0x00,0x80,0x00,0x80,0x1F,0xFC,0x10,0x84,0x10,0x84,0x1F,0xFC,0x10,0x84,
0x10,0x84,0x10,0x84,0x1F,0xFC,0x10,0x84,0x40,0x80,0x40,0x80,0x7F,0x00,0x00,0x00,

/*子   CD7D3 */
0x00,0x00,0x0F,0xFC,0x04,0x00,0x02,0x00,0x01,0x00,0x00,0x80,0x00,0x80,0x20,0x80,
0x7F,0xFF,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0xA0,0x00,0x40,

/*有   CD3D0 */
0x00,0x80,0x00,0x80,0x7F,0xFE,0x00,0x40,0x00,0x20,0x0F,0xF0,0x08,0x18,0x08,0x14,
0x0F,0xF2,0x08,0x11,0x08,0x10,0x0F,0xF0,0x08,0x10,0x09,0x10,0x0E,0x10,0x04,0x10,

/*限   CCFDE */
0x00,0x00,0x1F,0xDF,0x10,0x49,0x1F,0xC9,0x10,0x45,0x10,0x45,0x1F,0xC9,0x01,0x51,
0x12,0x51,0x0A,0x55,0x04,0x49,0x04,0x41,0x08,0x41,0x71,0x41,0x20,0xC1,0x00,0x41,

/*公   CB9AB */
0x00,0x00,0x02,0x20,0x02,0x60,0x02,0x20,0x04,0x10,0x08,0x10,0x18,0x08,0x70,0x44,
0x20,0xC2,0x00,0x40,0x04,0x20,0x08,0x10,0x1F,0x88,0x18,0xFC,0x08,0x08,0x00,0x00,

/*司   CCBBE */
0x00,0x00,0x3F,0xFC,0x20,0x00,0x20,0x00,0x27,0xFE,0x20,0x00,0x20,0x00,0x23,0xFC,
0x22,0x04,0x22,0x04,0x23,0xFC,0x22,0x04,0x20,0x04,0x28,0x00,0x10,0x00,0x00,0x00,




0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

	

};
//关行段码的时候要把EN信号关掉,让74HC138不工作;
unsigned  code sw[16]={
0x61,0x63,0x65,0x67,0x69,0x6b,0x6d,0x6f,
0x71,0x73,0x75,0x77,0x79,0x7b,0x7d,0x7f,
}; /*16行段码*/


void  Small_word(void) //右移
{
  
        uint  j;
        yid=0;
        zimuo=0;
        for(j=0;j<672;j++)               //显示21个字          
              {
	                  while(yid<16)				       //数据移位。
	                   {
                         
                          sbuf_out();
                          
                          yid++;				//移动一步
	                   }
                    yid=0;
                    zimuo=zimuo+32;			    //后移一个字,
                    if(zimuo>=672)	            //21个字3072个字节;		
                    zimuo=0; 
               choice++;
              if(choice==20) {Flag=1;choice=0;break;}
             }
}


void Large_word(void) //左移
{
       uint  j;
        yid=0;
        zimuo=0;
        for(j=0;j<672;j++)               //显示21个字          
              {
	                  while(yid<16)				       //数据移位。
	                   {
                         
                          sbuf_out();
                          
                          yid++;				//移动一步
	                   }
                    yid=0;
                    zimuo=zimuo+32;			    //后移一个字,
                    if(zimuo>=672)	            //21个字3072个字节;		
                    zimuo=0; 
               choice1++;
              if(choice1==20) {Flag=0;choice1=0;break;}
             }
}



//扫描一行子程序
void hang(uchar i)
{
	EN=1;
	STB=0;
	P1=i;
	STB=1;
	EN=0;
}


/********************************/
void sbuf_out()
{
	for(h=0;h<16;h++)	  //16行扫描
	  {
        
		in_data();				  //调整数据
		rxd_data();				  //串口发送数据
        P1=0xff;		          //关闭显示。
       hang(sw[h]);			     //送段码
	  }
}

/******************************************************/
void in_data(void)
{
          char s;
          if(Flag==0)
          {
  	      for(s=5;s>=0;s--)	    //h为向后先择字节计数器,zimuoo为向后先字计数器
	          {  
                 BUFF[2*s]=hanzi[zimuo+32*s+2*h];//把第一个字模的第一个字节放入BUFF0中,第二个字模的第一个字节放入BUFF2中
	             BUFF[2*s+1]=hanzi[zimuo+1+32*s+2*h];	// 把第一个字模的第二个字节放入BUFF1中,第二个字模的第二个字节放入BUFF3中		 
	          }
          }

         if(Flag==1)
          {
  	      for(s=4;s>=0;s--)	    //h为向后先择字节计数器,zimuoo为向后先字计数器
	          {  
                 BUFF[2*s]=hanzi[zimuo+1+32*s+2*h];//把第一个字模的第一个字节放入BUFF0中,第二个字模的第一个字节放入BUFF2中
	             BUFF[2*s+1]=hanzi[zimuo+32*s+2*h];	// 把第一个字模的第二个字节放入BUFF1中,第二个字模的第二个字节放入BUFF3中		 
	          }
          }



}

/*******************************************************/
void rxd_data(void)		            //串行发送数据
{
  char s;
  uchar inc,tempyid,temp;
  if(yid<8)
    inc=0;
  else 
    inc=1;
   if(Flag==0) //右移
   {
	  for(s=8+inc;s>0+inc;s--)			 //发送8字节数据 s<8+inc  8=4*2;(四个字乘以2);
		{
		  if(yid<8) 
	     tempyid=yid;
	      else 
	    tempyid=yid-8;
	    temp=(BUFF[s]<<tempyid)|(BUFF[s+1]>>(8-tempyid));//h1左移tempyid位后和h2右移8-tempyid相或,取出移位后的数据。
	    temp=255-temp;	  //把数据取反 或者temp=~temp;
        SBUF=temp;//把BUFF中的字节从大到小移位相或后发送输出。
	    while(!TI);TI=0;					 //等待发送中断
        }
   }

   if(Flag==1) //左移
   {
	  for(s=0+inc;s<8+inc;s++)			 //发送8字节数据 s<8+inc  8=4*2;(四个字乘以2);
		{
		  if(yid<8) 
	     tempyid=yid;
	      else 
	    tempyid=yid-8;
	    temp=(BUFF[s]>>tempyid)|(BUFF[s+1]<<(8-tempyid));//h1左移tempyid位后和h2右移8-tempyid相或,取出移位后的数据。
	    temp=255-temp;	  //把数据取反 或者temp=~temp;
        SBUF=temp;//把BUFF中的字节从大到小移位相或后发送输出。
	    while(!TI);TI=0;					 //等待发送中断
        }
   }


	 
	
}


/********************************************/
void main(void)
{  
    EN=1;
    SCON=0;
    
	while(1)
  {
     Small_word();
     Large_word();
  }  
      
  
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -