第二讲 零基础入门(二)
认识原理图
左侧接地,电平为0,如果按键按下,右侧引脚电平也是0
引脚接的是IO口P34到P37,检测按键是否按下,就是在检测对应的IO口是否为0
用按键控制LED的亮灭
#include <stdio.h>
#include <REGX52.H>
/*变量声明区域*/
unsigned char Key_Val,Key_Down,Key_Up,Key_Old;
/*按键读取函数*/
unsigned char Key_Read()
{
unsigned char temp=0;
if(P3_4==0) temp=1;
if(P3_5==0) temp=2;
if(P3_6==0) temp=3;
if(P3_7==0) temp=4;
return temp;
}
void main ()
{
while(1)
{
Key_Val=Key_Read();//读取键码值
Key_Down=Key_Val&(Key_Val^Key_Old);//检测下降沿
Key_Up=~Key_Val&(Key_Val^Key_Old);//检测上升沿
Key_Old=Key_Val;//扫描辅助变量
if(Key_Down==1)
P1_0=0;
if(Key_Up==2)
P1_0=1;
if(Key_Old==3)
P1_1=0;
else
P1_1=1;
}
}
其中Key_Down代表按键按下 Key_Up代表按键松开 Key_Old代表一直按着
用按键控制流水灯的开始和暂停
#include <stdio.h>
#include <REGX52.H>
#include <intrins.h>
/*变量声明区域*/
unsigned char Key_Val,Key_Down,Key_Up,Key_Old;
unsigned char ucLed=0xfe;
bit System_Flag;
/*延时函数*/
void Delay(unsigned char xms) //@12.000MHz
{
while(xms--)
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
/*按键读取函数*/
unsigned char Key_Read()
{
unsigned char temp=0;
if(P3_4==0) temp=1;
if(P3_5==0) temp=2;
if(P3_6==0) temp=3;
if(P3_7==0) temp=4;
return temp;
}
void main ()
{
while(1)
{
Key_Val=Key_Read();//读取键码值
Key_Down=Key_Val&(Key_Val^Key_Old);//检测下降沿
Key_Up=~Key_Val&(Key_Val^Key_Old);//检测上升沿
Key_Old=Key_Val;//扫描辅助变量
if(System_Flag==1)
{
ucLed=_crol_(ucLed,1);
P1=ucLed;
Delay(100);
}
if(Key_Down==1)
System_Flag=1;
if(Key_Down==2)
System_Flag=0;
}
}
优化代码
#include <stdio.h>
#include <REGX52.H>
#include <intrins.h>
/*变量声明区域*/
unsigned char Key_Val,Key_Down,Key_Up,Key_Old;
unsigned char ucLed=0xfe;
bit System_Flag;
/*延时函数*/
void Delay(unsigned char xms) //@12.000MHz
{
while(xms--)
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
/*按键读取函数*/
unsigned char Key_Read()
{
unsigned char temp=0;
if(P3_4==0) temp=1;
if(P3_5==0) temp=2;
if(P3_6==0) temp=3;
if(P3_7==0) temp=4;
return temp;
}
void main ()
{
while(1)
{
Key_Val=Key_Read();//读取键码值
Key_Down=Key_Val&(Key_Val^Key_Old);//检测下降沿
Key_Up=~Key_Val&(Key_Val^Key_Old);//检测上升沿
Key_Old=Key_Val;//扫描辅助变量
if(System_Flag==1)
{
ucLed=_crol_(ucLed,1);
P1=ucLed;
Delay(100);
}
switch(Key_Down)
{
case 1:
System_Flag=1;
break;
case 2:
System_Flag=0;
break;
}
}
}
矩阵键盘
本质上是先选择行,再选择列
#include <stdio.h>
#include <REGX52.H>
#include <intrins.h>
/*变量声明区域*/
unsigned char Key_Val,Key_Down,Key_Up,Key_Old;
unsigned char ucLed=0xfe;
bit System_Flag;
/*延时函数*/
void Delay(unsigned char xms) //@12.000MHz
{
while(xms--)
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
/*按键读取函数*/
unsigned char Key_Read()
{
unsigned char temp=0;
P3_0=0;P3_1=1;P3_2=1;P3_3=1;
if(P3_4==0) temp=1;
if(P3_5==0) temp=2;
if(P3_6==0) temp=3;
if(P3_7==0) temp=4;
P3_0=1;P3_1=0;P3_2=1;P3_3=1;
if(P3_4==0) temp=5;
if(P3_5==0) temp=6;
if(P3_6==0) temp=7;
if(P3_7==0) temp=8;
P3_0=1;P3_1=1;P3_2=0;P3_3=1;
if(P3_4==0) temp=9;
if(P3_5==0) temp=10;
if(P3_6==0) temp=11;
if(P3_7==0) temp=12;
P3_0=1;P3_1=1;P3_2=1;P3_3=0;
if(P3_4==0) temp=13;
if(P3_5==0) temp=14;
if(P3_6==0) temp=15;
if(P3_7==0) temp=16;
return temp;
}
void main ()
{
while(1)
{
Key_Val=Key_Read();//读取键码值
Key_Down=Key_Val&(Key_Val^Key_Old);//检测下降沿
Key_Up=~Key_Val&(Key_Val^Key_Old);//检测上升沿
Key_Old=Key_Val;//扫描辅助变量
if(System_Flag==1)
{
ucLed=_crol_(ucLed,1);
P1=ucLed;
Delay(100);
}
switch(Key_Down)
{
case 1:
System_Flag=1;
break;
case 2:
System_Flag=0;
break;
}
}
}
用矩阵键盘实现对流水灯的控制
用矩阵键盘实现对流水灯的控制和加减速
#include <stdio.h>
#include <REGX52.H>
#include <intrins.h>
/*变量声明区域*/
unsigned char Key_Val,Key_Down,Key_Up,Key_Old;
unsigned char ucLed=0xfe;
unsigned int Time=500;
bit System_Flag;
/*延时函数*/
void Delay(unsigned char xms) //@12.000MHz
{
while(xms--)
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
/*按键读取函数*/
unsigned char Key_Read()
{
unsigned char temp=0;
P3_0=0;P3_1=1;P3_2=1;P3_3=1;
if(P3_4==0) temp=1;
if(P3_5==0) temp=2;
if(P3_6==0) temp=3;
if(P3_7==0) temp=4;
P3_0=1;P3_1=0;P3_2=1;P3_3=1;
if(P3_4==0) temp=5;
if(P3_5==0) temp=6;
if(P3_6==0) temp=7;
if(P3_7==0) temp=8;
P3_0=1;P3_1=1;P3_2=0;P3_3=1;
if(P3_4==0) temp=9;
if(P3_5==0) temp=10;
if(P3_6==0) temp=11;
if(P3_7==0) temp=12;
P3_0=1;P3_1=1;P3_2=1;P3_3=0;
if(P3_4==0) temp=13;
if(P3_5==0) temp=14;
if(P3_6==0) temp=15;
if(P3_7==0) temp=16;
return temp;
}
void main ()
{
while(1)
{
Key_Val=Key_Read();//读取键码值
Key_Down=Key_Val&(Key_Val^Key_Old);//检测下降沿
Key_Up=~Key_Val&(Key_Val^Key_Old);//检测上升沿
Key_Old=Key_Val;//扫描辅助变量
if(System_Flag==1)
{
ucLed=_crol_(ucLed,1);
P1=ucLed;
Delay(Time);
}
switch(Key_Down)
{
case 1:
System_Flag=1;
break;
case 2:
System_Flag=0;
break;
case 3:
Time+=100;
break;
case 4:
Time-=100;
break;
}
}
}


