#include <STC89C5xRC.H> // 包含STC89C54RD+头文件
// 定义LED点亮模式表格(存储到程序存储区)
unsigned char code TAB[8] = {
0x01, // 00000001 - P0.0高电平点亮LED1
0x02, // 00000010 - P0.1高电平点亮LED2
0x04, // 00000100 - P0.2高电平点亮LED3
0x08, // 00001000 - P0.3高电平点亮LED4
0x10, // 00010000 - P0.4高电平点亮LED5
0x20, // 00100000 - P0.5高电平点亮LED6
0x40, // 01000000 - P0.6高电平点亮LED7
0x80 // 10000000 - P0.7高电平点亮LED8
};
//---------------------------------------
// 延时函数(约1ms@12MHz)
//---------------------------------------
void Delay1ms(unsigned int xms) {
unsigned char i, j;
while(xms--) {
i = 2;
j = 239;
do {
while (--j);
} while (--i);
}
}
//---------------------------------------
// 主函数
//---------------------------------------
void main(void) {
unsigned char index = 0; // LED模式索引
while(1) {
P0 = TAB[index]; // 输出当前LED模式到P0口
Delay1ms(500); // 延时约500ms
// 更新索引,循环0-7
if(++index >= 8) {
index = 0;
}
}
}
|