学校实训做的,共享一下
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<REGX51.H>
- #include<intrins.h>
- unsigned char code tablex[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
- unsigned char code table0[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff}; //全部led灯一起闪烁
- unsigned char code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //LED从低位往高位移
- unsigned char code table2[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
- unsigned char code table3[]={0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00}; //LED从1个亮到8个都点亮(从低位往高位)
- unsigned char code table4[]={0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff}; //LED从8个全亮到一个都不亮(从低位往高位)
- unsigned char led00[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff}; //
- unsigned char led01[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char led02[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char led10[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char led11[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char led12[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char led20[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char led21[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char led22[]={0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
- unsigned char i,j; //定义循环变量
- unsigned int tt=200; //定义时间指数
- unsigned int go=0,to=0; //定义跳转变量
- //***************************************************************************
- void delay(unsigned int time) //延时函数
- {
- unsigned int x,y;
- for(x=time;x>0;x--)
- for(y=110;y>0;y--);
- }
- //————————————————————————————————————————————————————————————————————————————————————————————————————
- void INT0ear()interrupt 0
- {
- switch(tt)
- {
- case 200:tt=400;break;
- case 400:tt=600;break;
- case 600:tt=800;break;
- case 800:tt=1000;break;
- case 1000:tt=200;break;
- }
- }
- void INT1ear()interrupt 2
- {
- go=go++;
- if(go==5)go=0;
- switch(go)
- {
- case 0:for(j=0;j<8;j++)
- {
- led00[j]=table0[j];led01[j]=table0[j];led02[j]=table0[j];
- led10[j]=table0[j];led11[j]=table0[j];led12[j]=table0[j];
- led20[j]=table0[j];led21[j]=table0[j];led22[j]=table0[j];
- P3_4=0;P3_5=0;P3_6=0;P3_7=0;
- }break;
- case 1:for(j=0;j<8;j++)
- {
- led00[j]=table1[j];led01[j]=table1[j];led02[j]=table1[j];
- led10[j]=table1[j];led11[j]=table1[j];led12[j]=table1[j];
- led20[j]=table1[j];led21[j]=table1[j];led22[j]=table1[j];
- P3_4=1;P3_5=0;P3_6=0;P3_7=0;
- }break;
- case 2:for(j=0;j<8;j++)
- {
- led00[j]=table2[j];led01[j]=table2[j];led02[j]=table2[j];
- led10[j]=table2[j];led11[j]=table2[j];led12[j]=table2[j];
- led20[j]=table2[j];led21[j]=table2[j];led22[j]=table2[j];
- P3_4=0;P3_5=1;P3_6=0;P3_7=0;
- }break;
- case 3:for(j=0;j<8;j++)
- {
- led00[j]=table3[j];led01[j]=table3[j];led02[j]=table3[j];
- led10[j]=table3[j];led11[j]=table3[j];led12[j]=table3[j];
- led20[j]=table3[j];led21[j]=table3[j];led22[j]=table3[j];
- P3_4=1;P3_5=1;P3_6=0;P3_7=0;
- }break;
- case 4:for(j=0;j<8;j++)
- {
- led00[j]=table4[j];led01[j]=table4[j];led02[j]=table4[j];
- led10[j]=table4[j];led11[j]=table4[j];led12[j]=table4[j];
- led20[j]=table4[j];led21[j]=table4[j];led22[j]=table4[j];
- P3_4=0;P3_5=0;P3_6=1;P3_7=0;
- }break;
- }
- }
- //————————————————————————————————————————————————————————————————————————————————————————————————————
- void main()
- {
- EA=1;EX0=1;IT0=1;EX1=1;IT1=1; //打开总中断开关,打开外部中断,设置初始状态
- P3_4=0;
- P3_5=0;
- P3_6=0;
- P3_7=0;
- while(1)
- {
- for(i=0;i<8;i++)
- {
- P0=led00[i];
- P1=led01[i];
- P2=led02[i];
- delay(tt);
- for(i=0;i<8;i++)
- {
- }
- P0=led10[i];
- P1=led11[i];
- P2=led12[i];
- delay(tt);
- }
- for(i=0;i<8;i++)
- {
- P0=led20[i];
- P1=led21[i];
- P2=led22[i];
- delay(tt);
- }
- }
- }
复制代码
所有资料51hei提供下载:
51跑马灯.zip
(20.32 KB, 下载次数: 21)
|