c语言怎么实现led自锁程序?

c语言怎么实现led自锁程序?,第1张

sbitkeyLEDP1^0voiddelay(){unsignedchari=20while(i--)}voidmain(){LED=1//熄灭while(1){if(key==0){delay()LED=~LEDdelay()}}

sbit key

LED P1^0

void delay()

{

unsigned char i=20

while(i--)

}

void main ()

{

LED=1//熄灭

while (1)

{

if(key==0)

{

delay()

LED=~LED

delay()

}

}

你的意思是K1按下,D1输出低电平,再按一次K1,D1输出高电平?

这……这里的按键检测就不需要使用延时了,使用while不用怕会阻塞后面的程序执行。事实在公司写程序,在检测按键时我从来不用延时,当然也不是使用这种while等待按键松开的方式。有兴趣可以一起研究一下,呵呵

#include <reg51.h>

#define KEY1 ( 1 << 0 )

#define KEY2 ( 1 << 1 )

#define KEY3 ( 1 << 2 )

#define KEY4 ( 1 << 3 )

#define KEY5 ( 1 << 4 )

#define KEY6 ( 1 << 5 )

sbit D1 = P0 ^ 0

sbit D2 = P0 ^ 1

sbit D3 = P0 ^ 2

sbit D4 = P0 ^ 3

sbit D5 = P0 ^ 4

sbit D6 = P0 ^ 5

void main( void )

{

while(1)

{

P1 = 0xff

if( !(P1&KEY1) )

{

D1 = ~D1

while( !(P1&KEY1) )

}

if( !(P1&KEY2) )

{

D2 = ~D2

while( !(P1&KEY2) )

}

if( !(P1&KEY3) )

{

D3 = ~D3

while( !(P1&KEY3) )

}

if( !(P1&KEY4) )

{

D4 = ~D4

while( !(P1&KEY4) )

}

if( !(P1&KEY5) )

{

D5 = ~D5

while( !(P1&KEY5) )

}

if( !(P1&KEY6) )

{

D6 = ~D6

while( !(P1&KEY6) )

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7833433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存