51单片机关于LED灯的程序
首先是延时函数。
void SoftWare_Dealy_MS(uint16_t X_ms)
{
uint16_t i;
do
{
i = MAIN_Fosc / 13000;
while (--i)
;
} while (--X_ms);
}
#if (MAIN_Fosc == 11059200UL)
void SoftWare_Dealy_US(uint16_t X_us)
{
while (X_us-- != 0)
{
_nop_();
_nop_();
_nop_();
}
}
#endif
闪烁灯函数。
void USER_Flash_Led(void)
{
P0 = 0x00;
SoftWare_Dealy_MS(500);
P0 = 0XFF;
SoftWare_Dealy_MS(500);
}
跑马灯函数。
void USER_Running_Horse_Led(void)
{
static uint8_t xdata i = 0XFE;
P0 = i;
i = _crol_(i, 1);
SoftWare_Dealy_MS(500);
}
流水灯函数。
void USER_Flowing_Water_Led(void)
{
uint8_t xdata i = 0;
P0 = 0XFF;
SoftWare_Dealy_MS(500);
for (i = 0; i < 8; i++)
{
P0 = P0 << 1;
SoftWare_Dealy_MS(500);
}
}
花样流水灯函数。
void USER_Variety_Flowing_Water_Led(void)
{
static uint8_t xdata i = 0;
static uint8_t xdata LED_HuaYang[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f,
0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f,
0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe,
0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe,
0x00, 0xff, 0x00, 0xff, 0x01};
if (LED_HuaYang[i] != 0x01)
{
P1 = LED_HuaYang[i];
SoftWare_Dealy_MS(500);
i++;
}
else
{
i = 0;
}
}
呼吸灯函数。
void USER_Breating_Led(void)
{
static uint16_t xdata Cycle = 1500;
static uint16_t xdata PWM_Low = 0;
P0 = 0XFF;
SoftWare_Dealy_MS(500);
for (PWM_Low = 1; PWM_Low < Cycle; PWM_Low++)
{
P0 = 0X00;
SoftWare_Dealy_US(PWM_Low);
P0 = 0XFF;
SoftWare_Dealy_US(Cycle - PWM_Low);
}
for (PWM_Low = Cycle - 1; PWM_Low > 0; PWM_Low--)
{
P0 = 0X00;
SoftWare_Dealy_US(PWM_Low);
P0 = 0XFF;
SoftWare_Dealy_US(Cycle - PWM_Low);
}
}
|