1 #include <reg51.h> 2 #define on 0 3 #define off 1 4 typedef unsigned char u8; 5 typedef unsigned int u16; 6 sbit LED0=P2^0; 7 sbit LED1=P2^1; 8 sbit LED2=P2^2; 9 sbit LED3=P2^3; 10 sbit LED4=P2^4; 11 sbit LED5=P2^5; 12 sbit LED6=P2^6; 13 sbit LED7=P2^7; 14 15 void delay(u16 i) 16 { 17 while(i--); 18 } 19 20 void main() 21 { 22 while(1) 23 { 24 LED0=on; 25 delay(1000000); 26 LED0=off; 27 LED1=on; 28 delay(1000000); 29 LED1=off; 30 LED2=on; 31 delay(1000000); 32 LED2=off; 33 LED3=on; 34 delay(1000000); 35 LED3=off; 36 LED4=on; 37 delay(1000000); 38 LED4=off; 39 LED5=on; 40 delay(1000000); 41 LED5=off; 42 LED6=on; 43 delay(1000000); 44 LED6=off; 45 LED7=on; 46 delay(1000000); 47 LED7=off; 48 } 49 }
Tips:
不过例如"LED3=off",
不能写成"P2^3=off".
必须用sbit为引脚取别名,
否则,"P2^3=1"的语句也不容易理解.