chtyise 发表于 2019-4-13 07:49 谢谢,你的建议可行! |
wulin 发表于 2019-4-13 15:15 膜拜大神,你的代码简单好用!!! |
| 代码不够有条理 |
| 给你写一个可变速花样流水灯示例。没有用如何难懂的语句,你一看就该明白。 #include <reg52.h> //#include "STC90C5xAD.h" #include "intrins.h" #define u16 unsigned int #define u8 unsigned char sbit key1=P3^2;//速度选择 sbit key2=P3^3;//花样选择 //花样流水灯数组 u8 code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; u8 code table2[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; u8 code table3[]={0x00,0x81,0xc3,0xe7,0xff,0x7e,0x3c,0x18}; u8 code table4[]={0x18,0x3c,0x7e,0xff,0xe7,0xc3,0x81,0x00}; u8 a=1,b=1,c=0;//速度变量、花样变量、选通变量 bit d=0; //计时标志 u16 tt=0;//中断计数变量 void delay(u16 z) { u16 x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void init() { TMOD=0x01; TH0=(65536-1000)/256;//1ms TL0=(65536-1000)%256;//1ms TR0=1; EA=1; ET0=1; } void main() { init(); while(1) { if(!key1) { delay(10); if(!key1) { a++; if(a>=5) a=1; while(!key1); } } if(!key2) { delay(10); if(!key2) { b++; if(b>=5) b=1; while(!key2); } } if(d==1) { d=0; if(b==1) P1=table1[c]; if(b==2) P1=table2[c]; if(b==3) P1=table3[c]; if(b==4) P1=table4[c]; c++; if(c>=8) c=0; } } } void t0() interrupt 1 { TH0=(65536-1000)/256;//1ms TL0=(65536-1000)%256;//1ms tt++; if(tt*a>=300) { tt=0; d=1; } } |
| 中断0里面switch可以去掉,只要改变a值就行了; mod函数里面的while(1) 死循环,要加判断:a值改变的时候要跳出循环。 也可以去掉mod里面的while(1),把那个switch语句放主函数while(1)里应该就可以 |
|
本帖最后由 wulin 于 2019-4-13 17:51 编辑 给你写一个可变速花样流水灯示例。没有用任何难懂的语句,你一看就该明白。 #include <reg52.h> //#include "STC90C5xAD.h" #include "intrins.h" #define u16 unsigned int #define u8 unsigned char sbit key1=P3^2;//速度选择 sbit key2=P3^3;//花样选择 //花样流水灯数组 u8 code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; u8 code table2[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; u8 code table3[]={0x00,0x81,0xc3,0xe7,0xff,0x7e,0x3c,0x18}; u8 code table4[]={0x18,0x3c,0x7e,0xff,0xe7,0xc3,0x81,0x00}; u8 a=1,b=1,c=0;//速度变量、花样变量、选通变量 bit d=0; //计时标志 u16 tt=0;//中断计数变量 void delay(u16 z) { u16 x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void init() { TMOD=0x01; TH0=(65536-1000)/256;//1ms TL0=(65536-1000)%256;//1ms TR0=1; EA=1; ET0=1; } void main() { init(); while(1) { if(!key1) { delay(10); if(!key1) { a++; if(a>=5) a=1; while(!key1); } } if(!key2) { delay(10); if(!key2) { b++; if(b>=5) b=1; while(!key2); } } if(d==1) { d=0; if(b==1) P1=table1[c]; if(b==2) P1=table2[c]; if(b==3) P1=table3[c]; if(b==4) P1=table4[c]; c++; if(c>=8) c=0; } } } void t0() interrupt 1 { TH0=(65536-1000)/256;//1ms TL0=(65536-1000)%256;//1ms tt++; if(tt*a>=300) { tt=0; d=1; } } |
|
中断0里面switch可以去掉,只要改变a值就行了; mod函数里面的while(1) 死循环,要加判断:a值改变的时候要跳出循环。 也可以去掉mod里面的while(1),把那个switch语句放主函数while(1)里 |