#include "reg52.h"
#include "intrins.h"
sbit BEEP = P2^3;
sbit Led0 = P1^0; //着实费引脚 (8个数码管共阴)
sbit Led1 = P1^1;
sbit Led2 = P1^2;
sbit Led3 = P1^3;
sbit Led4 = P1^4;
sbit Led5 = P1^5;
sbit Led6 = P1^6;
sbit Led7 = P1^7;
unsigned char n;
//严格意义上尽量别用延时
void Delay100ms() //@11.0592MHz 100毫秒延时
{
unsigned char i, j;
i = 180;
j = 73;
do
{
while (--j);
} while (--i);
}
void delay_ms(unsigned char n) //延时是100ms的整数倍
{
while(n>=0)
{
Delay100ms();
n--;
}
}
void Beep() //蜂鸣器响函数,
{
unsigned int i,j;
for(i=0;i<700;i++)
{
BEEP = ~BEEP;
for(j=0;j<300;j++);
BEEP = ~BEEP;
for(j=0;j<10;j++);
}
}
void main()
{
unsigned int i;
/* 第一种效果
while(1)
{
Led0 = Led2 = Led4 = Led6 = 1;//灯亮 0,2,4,6灯亮
Led1 = Led3 = Led5 = Led7 = 0;//灯灭
Beep();
delay_ms(5);//500ms
Led0 = Led2 = Led4 = Led6 = 0;// 灯灭
Led1 = Led3 = Led5 = Led7 = 1;//灯亮
delay_ms(5);//500ms
}
*/
while(1)
{
for(i=0;i<7;i++)
{
P1 = _crol_(P1,1); //右移
Beep();
delay_ms(5);
}
for(i=0;i<7;i++)
{
P1 = _cror_(P1,1);
Beep();
delay_ms(5);
}
}
} |