#include<reg51.h>voidDelay10ms(unsignedint c);// 延时10msvoidmain(){while(1){
P0 =0x00;// 设置 P0 为低电平Delay10ms(50);// 延时
P0 =0xff;// 设置 P0 为高电平Delay10ms(50);// 延时}}voidDelay10ms(unsignedint c){unsignedchar a, b;for(; c >0; c--){for(b =38; b >0; b--){for(a =130; a >0; a--);}}}
LED灯做二进制加1显示
#include<reg51.h>voidDelay10ms(unsignedint c);// 延时10msvoidmain(){unsignedchar count =0x00;while(1){
P0 = count;Delay10ms(50);// 延时
count++;}}voidDelay10ms(unsignedint c){unsignedchar a, b;for(; c >0; c--){for(b =38; b >0; b--){for(a =130; a >0; a--);}}}
延时实现LED流水灯效果P2口八个灯作跑马灯
#include<reg51.h>// #include <intrins.h>/** intrins.h 方法
_crol_ 字符循环左移
_cror_ 字符循环右移
_irol_ 整数循环左移
_iror_ 整数循环右移
_lrol_ 长整数循环左移
_lror_ 长整数循环右移
_nop_ 空操作8051 NOP 指令
_testbit_ 测试并清零位8051 JBC 指令函数名: _crol_,_irol_,_lrol_
*/voidDelay10ms(unsignedint c);// 延时10msvoidmain(){unsignedchar LED;
LED =~0xfe;// 0xfe = 1111 1110while(1){
P0 = LED;Delay10ms(50);// 延时
LED <<=1;if(P0 ==0x00){
LED =~0xfe;}// LED = _crol_(LED, 1);}}voidDelay10ms(unsignedint c){unsignedchar a, b;for(; c >0; c--){for(b =38; b >0; b--){for(a =130; a >0; a--);}}}
LED灯做跑马灯左右移动
#include<reg51.h>voidDelay10ms(unsignedint c);// 延时10msunsignedchar LED;voidmain(){unsignedchar i;
LED =0xfe;// 0xfe = 1111 1110while(1){for(i =0; i <7; i++){
P0 = LED;Delay10ms(50);
LED <<=1;
LED |=0x01;}for(i =0; i <7; i++){
P0 = LED;Delay10ms(50);
LED >>=1;
LED |=0x80;}}}voidDelay10ms(unsignedint c){unsignedchar a, b;for(; c >0; c--){for(b =38; b >0; b--){for(a =130; a >0; a--);}}}