51单片机,流星灯程序修改

2019-03-24 20:05发布

这是网上找的51单片机流星灯程序,软件模拟PWM,拖尾效果,是P0的IO口8路输出的,如果,我要改为16路输出,怎样改呢,求高手指点,谢谢~,原程序如下:  
#include<reg52.h>     //head file
#define uchar unsigned char//for convenient
#define led P0      // led data port
bit flag=0;       //direction flag uchar code lshift[3]={0x07,0x03,0x01};
uchar code shift[7]={0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe};
//imitate PWM data
uchar code sta0[100]={  0x0f,0x8f,0x8f,0x8f,0x8f,0x8f,0x8f,0x8f,0x8f,0x8f,
                        0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,
      0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,
      0xcf,0xcf,0xcf,0xef,0xef,0xef,0xef,0xef,0xef,0xef,
      0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,
      0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,
      0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,
      0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,
      0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,
      0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef       
        };
 //functions statement
void delay(uchar ms); 
uchar exchg(uchar dat);
//main function
void main()    
{
  uchar i,j,k,x,sdata; 
  while(1) 
  {   for(k=1;k<8;k++)
    for(i=0;i<22;i++)
   for(j=0;j<100;j++)
     {
      sdata=(sta0[j]>>k)|shift[k-1];
      if(flag==1)  led=exchg(sdata); 
   else led=sdata;    
  }   for(x=1;x<4;x++)
    for(i=0;i<22;i++)
   for(j=0;j<100;j++)
     {
      sdata=( sta0[j]<<(4-x) )|lshift[x-1];
      if(flag==1)  led=exchg(sdata); 
   else led=sdata;
  }
  
 
 }  }
void delay(uchar ms)
{
   uchar n;
  while(ms--)
  {
    for(n=125;n>0;n--);
  }
}
uchar exchg(uchar dat) //对字节的高位和低位进行互换!
{
   uchar temp;
   temp=
   ((dat&0x01)<<7)|
   ((dat&0x02)<<5)|
   ((dat&0x04)<<3)|
   ((dat&0x08)<<1)|
   ((dat&0x10)>>1)|
   ((dat&0x20)>>3)|
   ((dat&0x40)>>5)|
   ((dat&0x80)>>7);
return temp;
} 此帖出自小平头技术问答
0条回答

一周热门 更多>