}
}
/*************************************************
*function: delay for a moment
*input: time type:unsigned int
*output: none
**************************************************/
void delay(uInt time)
{
uInt i;
uChar j;
for(i=0;i<time;i++)
for(j=0;j<125;j++);
}
/**************************************************
*function: let the led light from left to right one by one
*input: none
*output: none
***************************************************/
void LeftToRight()
{
uChar i;
uChar temp=0xfe;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp<<1)|1;
delay(500);
}
}
/**************************************************
*function: let the led light from right to left one by one
*input: none
*output: none
***************************************************/
void RightToLeft()
{
uChar i;
uChar temp=0x7f;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp>>1)|0x80;
delay(500);
}
}
/***************************************************
*function: let the led light from both edge to middle one by one
*input: none
*output: none
****************************************************/
void BothToMid()
{
uChar temp[4]={0x7e,0xbd,0xdb,0xe7};
uChar i;
for(i=0;i<4;i++)
{
P2=temp[i];
delay(1000);
}
}
/****************************************************
*function: let the led light from middle to both edge one by one
*input: none
*output: none
*****************************************************/
void MidToBoth()
{
uChar temp[4]={0xe7,0xdb,0xbd,0x7e,};
uChar j;
for(j=0;j<4;j++)
{
P2=temp[j];
delay(1000);
}
}
/*****************************************************
*function :wait for a moment to let the led become lighter or darker
*input: timer type:unsigned char
*output: none
*******************************************************/
void delay1(uChar timer)
{
uChar i;
while(timer--)
{
for(i=0;i<1;i++);
}
}
/******************************************************
*function:let the led become lighter and lighter
*input:none
*output:none
********************************************************/
void Lighter()
{
void delay1(uChar);
uChar maxNum;
uChar timer;
unsigned char j;
P2=0xff;
maxNum=250;
timer=0;
- /***************************************************************************************************
- *AT89C52RC单片机操作8个led灯
- *菜鸟一号
- *2012.5.8
- ****************************************************************************************************/
- #include<reg52.h>
- //sbit LED=P2^0;
- typedef unsigned int uInt;
- typedef unsigned char uChar;
- void delay(uInt); //the function to delay for a moment
- void BothToMid();
- void MidToBoth();
- void LeftToRight();
- void RightToLeft();
- void Lighter();
- void Darker();
- /************************************************main()********************************************/
- void main()
- {
- void BothToMid();
- void MidToBoth();
- void LeftToRight();
- void RightToLeft();
- void Lighter();
- void Darker();
- while(1)
- {
-
- BothToMid();
- MidToBoth();
- LeftToRight();
- RightToLeft();
- Lighter();
- Darker();
- delay(500);
-
- }
- }
- /*************************************************
- *function: delay for a moment
- *input: time type:unsigned int
- *output: none
- **************************************************/
- void delay(uInt time)
- {
- uInt i;
- uChar j;
- for(i=0;i<time;i++)
- for(j=0;j<125;j++);
- }
- /**************************************************
- *function: let the led light from left to right one by one
- *input: none
- *output: none
- ***************************************************/
- void LeftToRight()
- {
- uChar i;
- uChar temp=0xfe;
- for(i=0;i<8;i++)
- {
- P2=temp;
- temp=(temp<<1)|1;
- delay(500);
- }
- }
- /**************************************************
- *function: let the led light from right to left one by one
- *input: none
- *output: none
- ***************************************************/
- void RightToLeft()
- {
- uChar i;
- uChar temp=0x7f;
- for(i=0;i<8;i++)
- {
- P2=temp;
- temp=(temp>>1)|0x80;
- delay(500);
- }
- }
- /***************************************************
- *function: let the led light from both edge to middle one by one
- *input: none
- *output: none
- ****************************************************/
- void BothToMid()
- {
- uChar temp[4]={0x7e,0xbd,0xdb,0xe7};
- uChar i;
- for(i=0;i<4;i++)
- {
- P2=temp[i];
- delay(1000);
- }
- }
- /****************************************************
- *function: let the led light from middle to both edge one by one
- *input: none
- *output: none
- *****************************************************/
- void MidToBoth()
- {
-
- uChar temp[4]={0xe7,0xdb,0xbd,0x7e,};
- uChar j;
- for(j=0;j<4;j++)
- {
- P2=temp[j];
- delay(1000);
- }
- }
- /*****************************************************
- *function :wait for a moment to let the led become lighter or darker
- *input: timer type:unsigned char
- *output: none
- *******************************************************/
- void delay1(uChar timer)
- {
- uChar i;
- while(timer--)
- {
- for(i=0;i<1;i++);
- }
- }
- /******************************************************
- *function:let the led become lighter and lighter
- *input:none
- *output:none
- ********************************************************/
- void Lighter()
- {
- void delay1(uChar);
- uChar maxNum;
- uChar timer;
- unsigned char j;
- P2=0xff;
- maxNum=250;
- timer=0;
-
- for(j=0;j<maxNum;j++)
- {
- timer++;
- delay1(timer);
- P2=0xff;
- timer=~timer;
- delay1(timer);
- P2=0x00;
- timer=~timer;
-
- }
- }
- /*********************************************************
- *function:let the led become darker and darker
- *input:none
- *output:none
- **********************************************************/
- void Darker()
- {
- void delay1(uChar);
- uChar maxNum;
- uChar timer;
- unsigned char j;
- P2=0x00;
- maxNum=250;
- timer=0;
-
- for(j=0;j<maxNum;j++)
- {
- timer++;
- delay1(timer);
- P2=0x00;
- timer=~timer;
- delay1(timer);
- P2=0xff;
- timer=~timer;
-
- }
- }
复制代码楼主的代码格式还是很好看的~~~赞同!!!顶你!
*AT89C52RC单片机操作8个led灯
*菜鸟一号
*2012.5.8
****************************************************************************************************/
#include<reg52.h>
//sbit LED=P2^0;
typedef unsigned int uInt;
typedef unsigned char uChar;
void delay(uInt); //the function to delay for a moment
void BothToMid();
void MidToBoth();
void LeftToRight();
void RightToLeft();
void Lighter();
void Darker();
/************************************************main()********************************************/
void main()
{
void BothToMid();
void MidToBoth();
void LeftToRight();
void RightToLeft();
void Lighter();
void Darker();
while(1)
{
BothToMid();
MidToBoth();
LeftToRight();
RightToLeft();
Lighter();
Darker();
delay(500);
}
}
/*************************************************
*function: delay for a moment
*input: time type:unsigned int
*output: none
**************************************************/
void delay(uInt time)
{
uInt i;
uChar j;
for(i=0;i<time;i++)
for(j=0;j<125;j++);
}
/**************************************************
*function: let the led light from left to right one by one
*input: none
*output: none
***************************************************/
void LeftToRight()
{
uChar i;
uChar temp=0xfe;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp<<1)|1;
delay(500);
}
}
/**************************************************
*function: let the led light from right to left one by one
*input: none
*output: none
***************************************************/
void RightToLeft()
{
uChar i;
uChar temp=0x7f;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp>>1)|0x80;
delay(500);
}
}
/***************************************************
*function: let the led light from both edge to middle one by one
*input: none
*output: none
****************************************************/
void BothToMid()
{
uChar temp[4]={0x7e,0xbd,0xdb,0xe7};
uChar i;
for(i=0;i<4;i++)
{
P2=temp[i];
delay(1000);
}
}
/****************************************************
*function: let the led light from middle to both edge one by one
*input: none
*output: none
*****************************************************/
void MidToBoth()
{
uChar temp[4]={0xe7,0xdb,0xbd,0x7e,};
uChar j;
for(j=0;j<4;j++)
{
P2=temp[j];
delay(1000);
}
}
/*****************************************************
*function :wait for a moment to let the led become lighter or darker
*input: timer type:unsigned char
*output: none
*******************************************************/
void delay1(uChar timer)
{
uChar i;
while(timer--)
{
for(i=0;i<1;i++);
}
}
/******************************************************
*function:let the led become lighter and lighter
*input:none
*output:none
********************************************************/
void Lighter()
{
void delay1(uChar);
uChar maxNum;
uChar timer;
unsigned char j;
P2=0xff;
maxNum=250;
timer=0;
for(j=0;j<maxNum;j++)
{
timer++;
delay1(timer);
P2=0xff;
timer=~timer;
delay1(timer);
P2=0x00;
timer=~timer;
}
}
/*********************************************************
*function:let the led become darker and darker
*input:none
*output:none
**********************************************************/
void Darker()
{
void delay1(uChar);
uChar maxNum;
uChar timer;
unsigned char j;
P2=0x00;
maxNum=250;
timer=0;
for(j=0;j<maxNum;j++)
{
timer++;
delay1(timer);
P2=0x00;
timer=~timer;
delay1(timer);
P2=0xff;
timer=~timer;
}
}
这代码格式挺好的,细节很重要啊,相信你!
一周热门 更多>