单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <string.h>
-
- void liushui();
- void rjl(); //渐亮
- void rja(); //渐暗
- void huaxin();
- void huaxin2();
- void huaxin3();
- void zkb(unsigned char d);
- void ys1ms(unsigned int s); //延时声明,修改s的数值,就可以改变时间
-
- unsigned char code table[11]={0xff,0xff ,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};
- unsigned char code table6[11]={0xff,0xff ,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff};
- unsigned char code table2[8]={0x7f,0x3f ,0x1f,0x0f,0x07,0x03,0x01,0x00};
- unsigned char code table3[8]={0xfe,0xfc ,0xf8,0xf0,0xe0,0xc0,0x80,0x00};
- unsigned char code table4[9]={0x00,0x01 ,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
- unsigned char code table5[9]={0x00,0x80 ,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
-
- unsigned char i;
- unsigned char j;
- unsigned char k;
- unsigned char m;
-
- int main()
- {
- EA=1;//打开中断,使用中断方便调试查看模式
- EX0=1; //允许外部中断0中断
- EX1=1; //允许外部中断1中断
- PX0=1; // 外部中断0位高优先级
- while(1) //无限循环
- {
- liushui(); ////////////////////
- ys1ms(50);
- huaxin2();
- ys1ms(50);
- rja();
- ys1ms(100);
-
- huaxin(); ////////////////////
- rjl();
- rja();
- ys1ms(100);
-
- huaxin3(); ////////////////////
- rja();
- ys1ms(100);
-
- }
- return 0;
- }
-
- void liushui()
- {
-
- for(i = 9; i > 0; i--)
- {
- P1 = table[ i];
- [ i] ys1ms(50);
- [ i] }
- [ i] for(i = 2; i <= 10; i++)
- [ i] {
- [ i] P0 = table[ i];
- [ i][ i] ys1ms(50);
- [ i][ i] }
- [ i][ i] for(i = 9; i > 0; i--)
- [ i][ i] {
- [ i][ i] P2 = table[ i];
- ys1ms(50);
- }
- for(i = 9; i > 0; i--)
- {
- P3 = table[ i];
- ys1ms(50);
- }
-
- }
-
- void ys1ms(unsigned int s) //定时器精确延时1毫秒函数,改变S数值即改变时间
- {
- TR0=1;
- while(s!=0)
- {
- TH0=-(7629/256);
- TL0=-(7629%256);
- while(TF0!=1);
- TF0=0;
- s--;
- }
- TR0=0;
- }
-
- /****************************************************************************/
- void rjl() //渐亮函数
- {
- unsigned char a, b;//a控制延时长度 b控制循环周期
- P0 = 0xff;
- P1 = 0xff;
- P2 = 0xff;
- P3 = 0xff;
- a = 0;
- for (b=250; b>0; b--) //循环周期设置
- {
- a++;
- zkb (a); //延时长度随a而改变
- P0 = 0xff;
- P1 = 0xff;
- P2 = 0xff;
- P3 = 0xff;
- a = ~a; //a值取反,决定灯灭时的占空比
- zkb (a);
- P0 = 0x00;
- P1 = 0x00;
- P2 = 0x00;
- P3 = 0x00;
- a = ~a; //a值取反 使a回到原值继续循环
- }
- }
- /****************************************************************************/
- void rja() //渐暗函数
- {
- unsigned char a, b;//a控制延时长度 b控制循环周期
- P0 = 0x00;
- P1 = 0x00;
- P2 = 0x00;
- P3 = 0x00; //亮起LED
- a = 0;
- for (b=250; b>0; b--)
- {
- a++;
- zkb (a);
- P0 = 0x00;
- P1 = 0x00;
- P2 = 0x00;
- P3 = 0x00;
- a = ~a;
- zkb (a);
- P0 = 0xff;
- P1 = 0xff;
- P2 = 0xff;
- P3 = 0xff;
- a = ~a;
- }
- }
- void huaxin()
- {
- for(j = 0; j < 2; j++)
- {
- for(i = 0; i < 8; i++)
- {
- P1 = table2[ i];
- P3 = table3[ i];
- ys1ms(50);
- }
- for(i = 0; i < 8; i++)
- {
- P0 = table3[ i];
- P2 = table3[ i];
- ys1ms(50);
- }
- for(i = 0; i < 9; i++)
- {
- P2 = table5[ i];
- P0 = table5[ i];
- ys1ms(50);
- }
- for(i = 0; i < 9; i++)
- {
- P3 = table5[ i];
- P1 = table4[ i];
- ys1ms(50);
- }
- ys1ms(100);
- }
- }
- void huaxin2()
- {
- for(i = 0; i < 8; i++)
- {
- P1 = table2[ i];
- ys1ms(50);
- }
- for(i = 0; i < 8; i++)
- {
- P0 = table3[ i];
- ys1ms(50);
- }
- for(i = 0; i < 8; i++)
- {
- P2 = table2[ i];
-
- ys1ms(50);
- }
- for(i = 0; i < 8; i++)
- {
- P3 = table2[ i];
-
- ys1ms(50);
- }
- }
- void huaxin3()
- {
- k = 0;
- for(m = 0; m < 2; m++)
- {
- for(j = 0; j < 4; j++)
- {
- for(i = 9; i > 0; i--)
- {
- P1 = table[ i];
- P0 = table6[ i];
- P2 = table[ i];
- P3 = table[ i];
- ys1ms(50);
- }
- }
-
- for(i = 0; i < 8; i++)
- {
- P1 = table2[ i];
- P0 = table3[ i];
- P2 = table2[ i];
- P3 = table2[ i];
- ys1ms(50);
- }
- if(k == 0)
- {
- for(i = 0; i < 8; i++)
- {
- P1 = table5[ i];
- P0 = table4[ i];
- P2 = table5[ i];
- P3 = table5[ i];
- ys1ms(50);
- }
- }
- ys1ms(100);
- k++;
- }
- }
- /****************************************************************************/
- void zkb(unsigned char d) //占空比函数,由d决定延时长度
- {
- unsigned char i;
- while( --d != 0)
- {
- for(i = 0; i < 1; i++); //占空比时间设置
- }
- }
复制代码
|