51单片机 流水灯 蜂鸣器应用
1. 闹铃小程序:
/*======================================================*/
/* 时间:2015年8月2日 16:42:05 */
/* 功能:蜂鸣器1:闹钟 */
/* 目的:51单片机学习 */
/* ^_^……! */
/*======================================================*/
#include
#include "commLib.h"
int main(void)
{
unsigned int cnt;
while(1)
{
cnt = 4;
while (cnt--)
{
P2 = 0xF7;
delay(100);
P2 = 0xFF;
delay(100);
}
delay(600);
}
return 0;
}
2. 流水灯和蜂鸣器相结合:
/*======================================================*/
/* 时间:2015年7月27日 21:34:44 */
/* :2015年7月29日 21:40:17 */
/* :2015年8月2日 18:15:38 */
/* 功能:流水灯1 + 蜂鸣器 */
/* 目的:51单片机学习 */
/* ^_^……! */
/*======================================================*/
#include
#include "commLib.h"
#define SHIFT_LEFT 0 // 左移
#define SHIFT_RIGHT 1 // 右移
sbit beep = P2^3; // 蜂鸣器
int main(void)
{
char shiftFlag = SHIFT_LEFT;
unsigned char led = 0xFE;
while (1)
{
P1 = led;
beep = 0; // 蜂鸣器发声
delay(50);
beep = 1; // 蜂鸣器关闭
// 此时最左边的灯亮,0在最低位,所以要向高位移,即向左移
if (0xFE == led)
{
shiftFlag = SHIFT_LEFT;
}
// 此时最右边的灯亮,0在最高位,所以要向低位移,即向右移
else if (0x7F == led)
{
shiftFlag = SHIFT_RIGHT;
}
switch(shiftFlag)
{
// 左移
case SHIFT_LEFT:
led = ccCrol(led, 1);
break;
// 右移
case SHIFT_RIGHT:
led = ccCror(led, 1);
break;
}
//延时100ms
delay(50);
}
return 0;
}