Let the first small light flash sequentially to the last one, you can use shift to achieve the purpose.
The MCU has a library file with the shift function written, and it is stored in the #include <intrins.h> library file.
1 _crol_ character rotate left
Characters rotate to the left:
#include <reg52.h>
#include <intrins.h>
char tmp;
void delay() //Delay function
{
int i;
for(i=0;i<8888;i++)
;
}
void main()
{
tmp=0xfe;
P1=tmp; //The first small light is on
while(1)
{
tmp=_crol_(tmp,1); //Character shift left
delay();
P1=tmp; //The second small light is on
}
}
Run the program and you can see the first small light, the second small light…flashing in turn.
2 _cror_ characters rotate right
Change the crol in the code tmp=_crol_(tmp,1); to cror.
#include <reg52.h>
#include <intrins.h>
char tmp;
void delay() //Delay function
{
int i;
for(i=0;i<8888;i++)
;
}
void main()
{
tmp=0xfe;
P1=tmp;
while(1)
{
tmp=_cror_(tmp,1); //Character shift right
delay();
P1=tmp;
}
}
After running the program, you can see the first small light, the eighth small light, the seventh small light…flashing in sequence.
3 extension (buzzer)
Add the buzzer, the buzzer will ring once and the light will turn on once:
//flow light and beep
#include <reg51.h>
#include <intrins.h>
unsigned char a,b,k,j;
sbit beep=P2^3;
void delay10ms()
{
for(a=100;a>0;a--)
for(b=225;b>0;b--);
}
void main()
{
k=0xfe;
while(1)
{
delay10ms();
beep=0;
delay10ms();
beep=1;
j=_crol_(k,1);
k=j;
P1=j;
}
}
0