书上原来是汇编程序,我给修改了用PORTD输出扫描到的按键码,可以正常的显示,按下某个按键,就会显示出某个按键码,延时结束后,LED就会灭掉。而修改成C程序,按下某个按键,可以正常显示按键码,但是延时结束后,仍然显示,直到按新的按键,才会发生变化。可以帮忙修改一下么?谢谢了!
另外,新手,尽量讲的详细一些,谢谢!
Proteus仿真电路以及汇编、C程序
ourdev_556000.rar(文件大小:19K) (原文件名:按键中断仿真.rar)
Proteus仿真图片
(原文件名:仿真.jpg)
C源程序,这些都包含在附件中,这个是给不想下附件的看的。
//=============头文件及配置===================
#include<pic.h>
__CONFIG(HS & PROTECT & PWRTEN & BOREN & WDTDIS);
//============全局变量====================
unsigned char KeyCode[8];
unsigned char KeySaved=0x00;
unsigned char KeyCurrent=0x00;
//===========函数声明======================
void KeyCheck(unsigned char KeySaved,unsigned char KeyCurrent);
void Delay(void);
//===========主函数========================
main()
{
TRISB=0xF0;//PORTB高4位输入,低4位输出
TRISD=0x00;//PORTD全输出
PORTD=0x00;//PORTD清零
PORTB=0x00;//PORTB清零,PORTB=B'11110000'
RBPU=0;//在C里,NOT_RBPU位即RBPU位,开启PORTB的内部弱上拉
RBIE=1;//开启PORTB端口引脚状态变化中断
GIE=1;//开启总中断
while(1)
{
PORTD=0x00;
PORTB=0x00;
KeyCheck(KeySaved,KeyCurrent);//按键检测
}
}
//==============================================
void interrupt RbInt(void)
{
unsigned char ScanCode=0xEF;//定义按键扫描
unsigned int k;
if(RBIE && RBIF)//判断中断使能和中断标志位
{
RBIF=0;//清零中断标志位
if((PORTB & 0xF0)!=0xF0)//如果有按键按下
{
for(k=0;k<5;k++)
{
PORTB=ScanCode;//输出初始扫描值,PORTB的引脚上逻辑电平高4为不变
if((PORTB & 0xF0)!=0xF0)
{
KeyCode[KeyCurrent]=PORTB;
break;
}
else
{
ScanCode=ScanCode>>1;//扫描码循环右移,这个移的乱七八糟的
//不知道是仿真的问题,还是移位的问题
//原理就是低4为依次循环输出0,而其他位为高
}
}
}
KeyCurrent++;//当前按键存储偏移位置加1
KeyCurrent=KeyCurrent & 0x07;//确保按键存储偏移位置小于8
}
}
//==============================================
void KeyCheck(unsigned char KeySaved,unsigned char KeyCurrent)//按键检测函数
{
if(KeyCurrent!=KeySaved)//如果当前按键偏移位置和已有的按键偏移位置不相等,则有新的按键
{
PORTD=KeyCode[KeySaved];//读保存的按键码
Delay();//延时显示
KeySaved = KeyCurrent;//已有按键偏移位置加1,与当前按键偏移位置同步
//KeySaved = KeySaved & 0x07;//确保按键已存储偏移位置小于8
}
else PORTD=0x00;
}
//==============================================
void Delay(void)//延时
{
int n;
for(n=0;n<=5000;n++)
continue;
}
//==============================================
#define KEY_VERT 0b010001 //verticle bits
#define KEY_HORI 0b100110 //horizontal bits
KEY_VERT basically says that the columns are connected to RA0 and RA4; and KEY_HORI says that the rows are connected to RA5, RA2 and RA1.
the rest of the code is the same.
so when keypad "7" was pushed, key_detect() reports that the RA5 row and RA4 column is pushed (set). thus the value of 48 shows up on the lcd.
(原文件名:16F684 Keypad 2.PNG)
this approach beats out the traditional scanning approach by a long shot, :)
一周热门 更多>