可供初学者学习,代码清晰易懂,八个按钮,四个不同跑马灯形式,四个调节频率。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include"reg51.h"
- code unsigned char leddat1[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- code unsigned char leddat2[]={0x01,0x80,0x02,0x40,0x04,0x20,0x08,0x10};
- code unsigned char leddat3[]={0x01,0x02,0x04,0x08,0x80,0x40,0x20,0x10};
- code unsigned char leddat4[50]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x81,0x82,
- 0x84,0x88,0x90,0xA0,0xC0,0xC1,0xC2,0xC4,0xC8,0xD0,
- 0xE0,0xE1,0xE2,0xE4,0xE8,0xF0,0xF1,0xF2,0xF4,0xF8,
- 0xF9,0xFA,0xFC,0xFD,0xFE,0xFF,0xFF,0x00,0xFF,0x00};
- void delay(unsigned int n)
- {
- unsigned int i=0,j=0;
- for(i=0;i<n;i++)
- {
- for(j=0;j<120;j++);
- }
- }
- void led1()
- {
- int i=0;
- for(i=0;i<8;i++)
- {
- P2=~leddat1[i];
- delay(400);
- }
- }
- void led2()
- {
- int i=0;
- for(i=0;i<8;i++)
- {
- P2=~leddat2[i];
- delay(400);
- }
- }
- void led3()
- {
- int i=0;
- for(i=0;i<8;i++)
- {
- P2=~leddat3[i];
- delay(400);
- }
- }
- void led4()
- {
- int i=0;
- for(i=0;i<50;i++)
- {
- P2=~leddat4[i];
- delay(400);
- }
- }
- void ledF1()
- {
- int i=0;
- for(i=0;i<8;i++)
- {
- P2=~leddat1[i];
- delay(100);
- }
- }
- void ledF2()
- {
- int i=0;
- for(i=0;i<8;i++)
- {
- P2=~leddat1[i];
- delay(400);
- }
- }
- void ledF3()
- {
- int i=0;
- for(i=0;i<8;i++)
- {
- P2=~leddat1[i];
- delay(1000);
- }
- }
- void ledF4()
- {
- int i=0;
- for(i=0;i<8;i++)
- {
- P2=~leddat1[i];
- delay(2000);
- }
- }
- void main()
- {
- while(1)
- {
- if(P1==~0x01)
- {led1();}
- if(P1==~0x02)
- {led2();}
- if(P1==~0x04)
- {led3();}
- if(P1==~0x08)
- {led4();}
- if(P1==~0x10)
- {ledF1();}
- if(P1==~0x20)
- {ledF2();}
- if(P1==~0x40)
- {ledF3();}
- if(P1==~0x80)
- {ledF4();}
- }
- }
复制代码
所有资料51hei提供下载:
MCS-51单片机完成频率、花色可调的跑马灯.zip
(51.08 KB, 下载次数: 41)
|