单片机原理与嵌入式系统的实验源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#define LED_PORT0 P0 //用P0口驱动灯,低亮,高灭
#define LED_PORT1 P1 //用P1口驱动灯,低亮,高灭
#define LED_PORT2 P2 //用P2口驱动灯,低亮,高灭
#define LED_PORT3 P3 //用P3口驱动灯,低亮,高灭
void time(unsigned int ucMs);/* 延时单位:毫秒 */
void main(void)
{
unsigned char ucTimes;
#define DELAY_TIME 400 //延时
while(1)
{
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P0口灯
LED_PORT0 = LED_PORT0 - (0x80>>ucTimes);//亮灯需低电平驱动
time(200);
}
LED_PORT0=0xff;//灭P0口灯
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P1口灯
LED_PORT1 = LED_PORT1 - (0x80>>ucTimes);//亮灯需低电平驱动
time(200);
}
LED_PORT1=0xff;//灭P1口灯
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P2口灯
LED_PORT2 = LED_PORT2 - (0x80>>ucTimes);//亮灯需低电平驱动
time(200);
}
LED_PORT2=0xff;//灭P2口灯
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P3口灯
LED_PORT3 = LED_PORT3 - (0x80>>ucTimes);//亮灯需低电平驱动
time(200);
}
LED_PORT3=0xff;//灭P3口灯
LED_PORT3=0x00;
//然后从右往左依次点亮LED
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P1口灯
LED_PORT3 = LED_PORT3 - (0x01<
}
LED_PORT3=0x00;//全亮
LED_PORT3=0x00;
//然后从右往左依次点亮LED
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P1口灯
LED_PORT2 = LED_PORT2 - (0x01<
}
LED_PORT2=0x00;//全亮
LED_PORT3=0x00;
//然后从右往左依次点亮LED
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P1口灯
LED_PORT1 = LED_PORT1 - (0x01<
}
LED_PORT1=0x00;//全亮
LED_PORT3=0x00;
//然后从右往左依次点亮LED
for(ucTimes=0;ucTimes<8;ucTimes++){//循环点亮P0口灯
LED_PORT0 = LED_PORT0 - (0x01<
}
LED_PORT0=0x00;//全亮
}
}
/*********** time C **************/
void time(unsigned int ucMs)//延时单位:ms
{
#define DELAYTIMES 239
unsigned char ucCounter; // 延时设定的循环次数
while (ucMs!=0) {
for (ucCounter=0; ucCounter
}
}