初次接触51单片机,记录下第一个程序,LED跑马灯,源代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <reg52.h>
sbit led1=P2^0; sbit led2=P2^1; sbit led3=P2^2; sbit led4=P2^3; sbit led5=P2^4; sbit led6=P2^5; sbit led7=P2^6; sbit led8=P2^7;
void delay(int us) { while(us--); }
void main() { while(1) { led1=0; delay(10000); led1=1; delay(10000); led2=0; delay(10000); led2=1; delay(10000); led3=0; delay(10000); led3=1; delay(10000); led4=0; delay(10000); led4=1; delay(10000); led5=0; delay(10000); led5=1; delay(10000); led6=0; delay(10000); led6=1; delay(10000); led7=0; delay(10000); led7=1; delay(10000); led8=0; delay(10000); led8=1; delay(10000); } }
|
改进版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include <reg52.h> #include <intrins.h>
void delay(unsigned int us) { while(us--); }
void main() { while(1) { int i; P2 = 0xfe; for(i=0;i<7;i++) { P2 = _crol_(P2,1); delay(5000); } for(i=0;i<7;i++) { P2 = _cror_(P2,1); delay(5000); } } }
|