|
color=#FF0000]/*---------------------------- 下面是我想了解的正确键盘扫描程序 ----------------*/
#include
#include
#include"ch451.h" //这个头文件在下面跟着附带
LEDDisplay(char MODE,uint data1)
{
uint i;
void WriteCh451(uint da);
void Delay();
WriteCh451(CH451_SYSON2);
WriteCh451(CH451_BCD);
if(MODE==0) //突发显示
{
if(1) //如果条件成立时显示
WriteCh451(data1);
}
if(MODE==1) //定周期显示
{
for(i=0;i<10000;i++); //定时周期
WriteCh451(data1);
}
}
/*下面这个,我认为就是把按键所对应的数字字符显示在7段LED上的部分,可它是如何起作用嘀? (红字处语句,不明白!)*/
void WriteCh451(uint da)
{
uint i;
EX1=0; //关闭键盘中断
CH451_LOAD=0; //复位LOAD信号
for(i=0;i<12;i++)
{
if((da&0x1)==1)
CH451_DIN=1;
else
CH451_DIN=0;
CH451_DCLK=0;
da>>=1;
CH451_DCLK=1; //上升沿读取数据
}
CH451_LOAD=1; //加载数据
Delay();
EX1=1; //打开键盘中断
}
void Delay()
{
_nop_();
_nop_();
}
uint ReadCh451()
{
uchar i;
uchar cmd ,keycode;
EX1=0; //禁止键盘中断
cmd=0x07; //获取按键值
CH451_LOAD=0;
for(i=0;i<4;i++)
{
if((cmd&0x1)==1)
CH451_DIN=1;
else
CH451_DIN=0;
CH451_DCLK=0;
cmd>>=1;
CH451_DCLK=1;
}
CH451_LOAD=1;
keycode=0;
for(i=0;i<7;i++)
{
keycode<<=1; //数据移入keycode
keycode|=CH451_DOUT;//从高到低读入451的数据
CH451_DCLK=0; //时钟下降沿通知ch451输出下一位
Delay();
CH451_DCLK=1;
}
IE=0; //清除中断标志
EX1=1;
return(keycode);
}
void IRQKeyRead() interrupt 2
{
uchar key;
uchar key0,key1;
key=ReadCh451();
key=key&0x3f;
key0=key/10;
key1=key%10;
if(key0!=0)
LEDDisplay(0,CH451_DIG1|key0);
else
LEDDisplay(0,CH451_DIG1|0x10);
LEDDisplay(0,CH451_DIG0|key1);
}
void InitSys()
{
EA=1;
//LEDDisplay(0,CH451_DSP);
WriteCh451(CH451_SYSON2);
WriteCh451(CH451_BCD);
EX1=1;
}
void main()
{
LEDDisplay(0,0x0800);//这里排列的8行都是一个自定义函数,我想请问,形参0x0800...之类的是起什么作用?
LEDDisplay(0,0x0900);
LEDDisplay(0,0x0a00);
LEDDisplay(0,0x0b00);
LEDDisplay(0,0x0c00);
LEDDisplay(0,0x0d00);
LEDDisplay(0,0x0e00);
LEDDisplay(1,0x0f00);
for(;;)
{
InitSys();
while(0);
}
}
/*--------------------- #include"ch451.h" 的源程序的--------------*/
#ifndef _CH451_H
#define _CH451_H
typedef unsigned int uint;
typedef unsigned char uchar;
//管脚定义
sbit CH451_DCLK=P1^2; //问题 1: 这4个管脚定义是如何起作用嘀?有什么作用
sbit CH451_DIN=P1^1;
sbit CH451_LOAD=P1^0;
sbit CH451_DOUT=P3^3;
//命令码
#define CH451_NOP 0x0000
#define CH451_RESET 0x0201
#define CH451_LEVEL 0x0100
#define CH451_CLR_BIT 0x0180
#define CH451_SET_BIT 0x01C0
#define CH451_SLEEP 0x0202
#define CH451_LEFTMOV 0x0300
#define CH451_LEFTCYC 0x0301
#define CH451_RIGHTMOV 0x0302
#define CH451_RIGHTCYC 0x0303
#define CH451_SELF_BCD 0x0380
#define CH451_SYSOFF 0x0400
#define CH451_SYSON1 0x0401
#define CH451_SYSON2 0x0403
#define CH451_SYSON2W 0x0423
#define CH451_DSP 0x0500
#define CH451_BCD 0x0580
#define CH451_TWINKLE 0x0600
#define CH451_GET_KEY 0x0700
#define CH451_DIG0 0x0800
#define CH451_DIG1 0x0900
#define CH451_DIG2 0x0a00
#define CH451_DIG3 0x0b00
#define CH451_DIG4 0x0c00
#define CH451_DIG5 0x0d00
#define CH451_DIG6 0x0e00
#define CH451_DIG7 0x0f00
//void LEDDisplay(char MODE,uint data1)
void Delay();
#endif
|
|