求一单片机C语言程序:两个按键控制两个LED KEY1按一下LED1亮 再按一下LED1灭 KEY2按下LED2亮 LED1灭
我的要求是:两个按键独立控制两个LED按一下KEY1LED1亮再按一下KEY1LED1就灭KEY2也是这样,但是按下KEY2的同时如果LED1亮LED1灭...
我的要求是:两个按键独立控制两个LED 按一下KEY1 LED1亮 再按一下KEY1 LED1就灭 KEY2也是这样,但是 按下KEY2的同时如果LED1亮 LED1灭
展开
4个回答
展开全部
#include<reg51.h>
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit led1 = P1^2;
sbit led2 = P1^3;
void Delay(unsigned int x)
{
unsigned int i, j;
for(i = 0; i < x; i++) for(j = 0; j <= 148; j++);
}
//KEY1按一下LED1亮 再按一下LED1灭
//KEY2按下LED2亮 LED1灭
main()
{
while(1) {
if (!key2) { led1 = 1; led2 = 0;}
else {
if(!key1) {
Delay(10);
if(!key1) {
while(!key1);
led1 = ~led1;
} } } }
}
//--------------------------------
#include<reg51.h>
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit led1 = P1^2;
sbit led2 = P1^3;
void Delay(unsigned int x)
{
unsigned int i, j;
for(i = 0; i < x; i++) for(j = 0; j <= 148; j++);
}
main()
{
while(1) {
if(!key1) {
Delay(10);
if(!key1) {
while(!key1);
led1 = ~led1;//KEY1按一下LED1亮 再按一下LED1灭
} }
if(!key2) {
Delay(10);
if(!key2) {
while(!key2) led1 = 1; //按下KEY2时LED1灭
led2 = ~led2;//KEY2也是这样
} }
}
}
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit led1 = P1^2;
sbit led2 = P1^3;
void Delay(unsigned int x)
{
unsigned int i, j;
for(i = 0; i < x; i++) for(j = 0; j <= 148; j++);
}
//KEY1按一下LED1亮 再按一下LED1灭
//KEY2按下LED2亮 LED1灭
main()
{
while(1) {
if (!key2) { led1 = 1; led2 = 0;}
else {
if(!key1) {
Delay(10);
if(!key1) {
while(!key1);
led1 = ~led1;
} } } }
}
//--------------------------------
#include<reg51.h>
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit led1 = P1^2;
sbit led2 = P1^3;
void Delay(unsigned int x)
{
unsigned int i, j;
for(i = 0; i < x; i++) for(j = 0; j <= 148; j++);
}
main()
{
while(1) {
if(!key1) {
Delay(10);
if(!key1) {
while(!key1);
led1 = ~led1;//KEY1按一下LED1亮 再按一下LED1灭
} }
if(!key2) {
Delay(10);
if(!key2) {
while(!key2) led1 = 1; //按下KEY2时LED1灭
led2 = ~led2;//KEY2也是这样
} }
}
}
展开全部
按一下是有Push and Release 事件,要计算bounce?
要用Interrupt 或是Polling?
既然是单片机,就用interrupt表示,再写下流程图,根据流程图写功能程序
BYTE Key1=0,Key2=0;
IRQ Key1(){
Key1++;
}
IRQ Key2(){
Key2=1;
}
while(1){
if(Key1%2){
LED1=on;
}
else{
LED1=off;
if(Key1>=100) Key1=0; // avoid over 256
}
if(Key2){
LED2=on;
LED1=off;
Key2=0;
}
}
要用Interrupt 或是Polling?
既然是单片机,就用interrupt表示,再写下流程图,根据流程图写功能程序
BYTE Key1=0,Key2=0;
IRQ Key1(){
Key1++;
}
IRQ Key2(){
Key2=1;
}
while(1){
if(Key1%2){
LED1=on;
}
else{
LED1=off;
if(Key1>=100) Key1=0; // avoid over 256
}
if(Key2){
LED2=on;
LED1=off;
Key2=0;
}
}
追问
大哥 有完整点的吗
追答
方案一:
将key1 与 key2 AND gate 到EXT INT pin, 利用中断如上代码逻辑
方案二:
BYTE KeyTrig=0;
ReadKey(){
bit ReadKey1=P0^1;
Sleep(n); // for de-bounce
if(ReadKey1!=P0^1){
Key1++;
KeyTrig|=1;
}
bit ReadKey2=P0^2;
Sleep(n); // for de-bounce
if(ReadKey2!=P0^2){
Key2++;
KeyTrig|=2;
}
}
while(1){
ReadKey();
if(KeyTrig>0){
if(KeyTrig&1){
if(Key1%2){ //Key1==odd
LED1=1;
}
else{ //
LED1=0;
if(Key1>=100) Key1=0;
}
KeyTrig=KeyTrig&0xfe;
}
if(KeyTrig&2){
if(Key2%2){ //Key2==odd
LED2=1;
LED1=0;
}
else{ //
LED2=0
LED1=1;
if(Key2>=100) Key2=0;
}
KeyTrig=KeyTrig&0xfd;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<reg51.h>
sbit key1=P1^0;
sbit key2=P1^1;
sbit led1=P1^2;
sbit led2=P1^3;
main()
{
led1=1;
led2=1;
while(1)
{
if(key1==0)led1=~led1;
if(key2==0)led2=~led2;
}
}
sbit key1=P1^0;
sbit key2=P1^1;
sbit led1=P1^2;
sbit led2=P1^3;
main()
{
led1=1;
led2=1;
while(1)
{
if(key1==0)led1=~led1;
if(key2==0)led2=~led2;
}
}
更多追问追答
追问
不行 按下KEY1或KEY2后 LED闪
追答
#include
sbit key1=P1^0;
sbit key2=P1^1;
sbit led1=P1^2;
sbit led2=P1^3;
main()
{
led1=1;
led2=1;
while(1)
{
if(key1==0)
{
led1=~led1;
while(key1==0);
}
if(key2==0)
{
led2=~led2;
while(key2==0);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
while(1)
{
if(!key1)led1=0; //低电平亮灯
while(key2);
led1=1;
led2=0;
}
{
if(!key1)led1=0; //低电平亮灯
while(key2);
led1=1;
led2=0;
}
更多追问追答
追问
如果再按下KEY2 LED2灭
追答
加两句话就行了
while(1)
{
if(!key1)led1=0; //低电平亮灯
while(key2);
led1=1;
led2=0;
delay();
while(key2);
key2=1;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询