#include < reg52.h >
#include
sbit P00 = P0^0;
void delay(unsigned int ms)
{
unsigned char i;
while(ms--) for(i = 110; i-- ; i > 0);
}
void main()
{
unsigned char LED = 0xfe, COUN = 0;
while(1) {
P1 = LED;
P2 = COUN;
if (P00 == 1) {
delay(2);
if (P00 == 1) {
while (P00 == 1);
LED = _cror_(LED, 1);
COUN++;
} } } }
单片机检测高电平。1、可以用外部中断,外接激光传感器最好用普通光耦隔离一下,顺便改成低电平触发单片机中断。然后用位移指令移动跑马灯,然后计数值加1. 2、可以用循环检测,也建议用光耦隔离先,在MAIN里面不断检测P00,发现变化后,再次检测以便确认,确认后同样位移一下跑马灯,然后计数加1。
假如LED是8位,输出口为PB
void ledshift(void)
{
static uchar i=0x01;
static counter = 0;
if(P00==1)
{
if(counter++%8==0)
{
i = 0x01;
}
i=i<<1;
PB = i;
}
}