#include <reg51.h>void Delay10ms(unsignedint c); // 延時10msvoid main()
{
while(1)
{
P0 = 0x00; // 設置 P0 爲低電平
Delay10ms(50); // 延時
P0 = 0xff; // 設置 P0 爲高電平
Delay10ms(50); // 延時
}
}
void Delay10ms(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>void Delay10ms(unsignedint c); // 延時10msvoid main()
{
unsignedchar count = 0x00;
while(1)
{
P0 = count;
Delay10ms(50); // 延時
count++;
}
}
void Delay10ms(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_
*/void Delay10ms(unsignedint c); // 延時10msvoid main()
{
unsignedchar LED;
LED = ~0xfe; // 0xfe = 1111 1110while(1)
{
P0 = LED;
Delay10ms(50); // 延時
LED <<= 1;
if (P0 == 0x00)
{
LED = ~0xfe;
}
// LED = _crol_(LED, 1);
}
}
void Delay10ms(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>void Delay10ms(unsignedint c); // 延時10msunsignedchar LED;
void main()
{
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;
}
}
}
void Delay10ms(unsignedint c)
{
unsignedchar a, b;
for (; c > 0; c--)
{
for (b = 38; b > 0; b--)
{
for (a = 130; a > 0; a--);
}
}
}