|
流水灯代码及电路图
- #include<reg51.h>
- #include<intrins.h>
- sbit k1 = P1^0;
- sbit k2 = P1^1;
- sbit k3 = P1^2;
- sbit k4 = P1^3;
- sbit led = P1;
- void delayms(unsigned char ms) //延时子程序
- {
- unsigned char i;
- while(ms--)
- {
- for(i=0;i<120;i++);
- }
- }
- void a() //左右子程序
- {
- unsigned char LED,i;
- LED=0xfe;
- P0=LED;
- for (i=0;i<7;i++)
- { delayms(200);
- LED=LED<<1;
- LED=LED|0x01;
- P0=LED;
-
- }
- }
- void b ( ) //右左子程序
- {
- unsigned char LED,i;
- LED=0x7f;
- P0=LED;
- for (i=0;i<7;i++)
- { delayms(200);
- LED=LED>>1;
- LED=LED|0x80;
- P0=LED;
-
- }
-
- }
- void c( ) //来回子程序
- {
- unsigned char LED,j;
- LED=0xfe;
- P0=LED;
- for (j=0;j<7;j++)
- { delayms(250);
- LED= _crol_(LED,1); //循环左移1位,点亮下一个LED
- P0=LED;
- }
- for (j=0;j<7;j++)
- {delayms(250);
- LED= _cror_(LED,1); //循环右移1位,点亮下一个LED
- P0=LED;
- }
- }
- void d( )
- {
- unsigned char LED,i;
- LED=0xfe;
- P0=LED;
- for (i=0;i<2;i++)
- { delayms(250);
- LED= _crol_(LED,1); //循环左移1位,点亮下一个LED
- P0=LED;
- }
- for (i=0;i<2;i++)
- {delayms(250);
- LED= _cror_(LED,1); //循环右移1位,点亮下一个LED
- P0=LED;
- }
- }
- void main() //主函数
- {
- while(1)
- { if(k1 == 0)
- a () ;
- if(k2 == 0)
- b () ;
- if(k3 == 0)
- c () ;
- if(k4 == 0)
- d () ;
- }
- }
- 秒表代码
- #includereg51.h>
- #define seg1 P0
- #define seg2 P2
- char code TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98}
- void delay(int);
- main()
- {
- Int i;
- While(1)
- {for(i=0;i<100;i++)
- {
- Seg1=TAB[i]/10;delay(1000);
- Seg2=TAB[I]%10;DELAY(1000);
- }
- }
- void delay(int x)
- Int i,j;
- {
- for(i=0;i<x;i++)
- For(j=0;j<120;j++)
- }
- #include<reg51.h>
- #define Seg1 P0
- #define Seg2 P2
- char code TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98}
- void delay(int x)
- int i,j;
- {
- for(i=0;i<x;i++)
- for(j=0;j<120;j++)
- }
- main()
- {
- int i;
- while(1)
- {
- for(i=0;i<100;i++)
- {
- Seg1=TAB[i]/10;
- Seg2=TAB[i]%10;
- delay(1000);
- if(i==99)
- i=0;
- }
- }
- }
- 三个开关
- #include<reg51.h>
- #define Seg1 P0
- #define Seg2 P2
- char code TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};
- //char code MAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};
- void delay(int);
- void a() //左右子程序
- {
- unsigned char TAB,i;
- P0=TAB;
- for (i=0;i<100;i++) ;
- {
- delay (200);
- }
- }
- void b ( ) //右左子程序
- {
- unsigned char TAB,j;
- for (j=0;j<100;j++)
- P0=TAB;
- }
-
- void c( ) //来回子程序
- {
- unsigned char TAB,j;
- P0=TAB;
- for(j=99; j>0;j--)
- { delay (200);
- P0=TAB;
- }
- }
- main()
- {
- int i;
- while(1)
- for(i=0;i<100;i++)
- {
- Seg1=TAB[i/10];
- Seg2=TAB[i%10]; delay(1000);
- if(i==99)
- i=0;
- }
- }
- void delay(int x)
- {int i,j;
- for(i=0;i<x;i++)
- for(j=0;j<120;j++);
- }
- 1234计数显示代码
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- void delayms(uint ms)
- {
- uint j;
- while (ms--)
- { for(j=0;j<120;j++);
- }
- }
- void main(void)
- {
- while(1)
- {
- P2=0xfe;
- P0=0xf9;
- delayms(100);
- P2=0xfd;
- P0=0xa4;
- delayms(100);
- P2=0xfb;
- P0=0xb0;
- delayms(100);
- P2=0xf7;
- P0=0x99;
- delayms(100);
- P2=0xff;
- }
- }
复制代码
|
-
-
流水灯.rar
27.96 KB, 下载次数: 20, 下载积分: 黑币 -5
评分
-
查看全部评分
|