單片機源程序如下:
- #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++); //占空比時間設置
- }
- }
復制代碼
|