发信人: meiyuan(鼬), 信区: Singlechip
标 题: 求单片机朋友们帮忙指点一个简单的程序
发信站: 饮水思源 (2012年09月26日01:04:52 星期三)
我不知道为什么矩阵键盘没有工作 为什么switch语中的speed子函数不能调用
求好心人指点 好苦恼啊
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void timer1_init();
uchar key_number;
uchar m;
sbit zhongduan=P3^3;
unsigned char const dofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
,
0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F
/*------------------------------------------------
函数声明
------------------------------------------------*/
uchar keyscan(void);//键盘扫描
void delay(uint i);
void speed(uchar m);
/*------------------------------------------------
主函数
------------------------------------------------*/
void DelayMs(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
timer1_init();
while(1)
{
P2=6;
key_number=keyscan(); //调用键盘扫描,
switch(key_number)
{
case 0x7e:P0=dofly[0];speed(0x7e);break;//0
case 0x7d:P0=dofly[1];speed(0x7d);break;//1
case 0x7b:P0=dofly[2];speed(0x7b);break;//2
case 0x77:P0=dofly[3];speed(0x77);break;//3
case 0xbe:P0=dofly[4];speed(0xbe);break;//4
case 0xbd:P0=dofly[5];speed(0xbd);break;//5
case 0xbb:P0=dofly[6];speed(0xbb);break;//6
case 0xb7:P0=dofly[7];speed(0xb7);break;//7
case 0xde:P0=dofly[8];speed(0xde);break;//8
case 0xdd:P0=dofly[9];speed(0xdd);break;//9
case 0xdb:P0=dofly[10];speed(0xdb);break;//a
case 0xd7:P0=dofly[11];speed(0xd7);break;//b
case 0xee:P0=dofly[12];speed(0xee);break;//c
}
}
}
/*------------------------------------------------
键盘扫描程序
------------------------------------------------*/
uchar keyscan(void) //键盘扫描函数,使用行列反转扫描法
{
uchar cord_h,cord_l;//行列值中间变量
P3=0x0f; //行线输出全为0
cord_h=P3&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
DelayMs(20); //去抖
if(cord_h!=0x0f)
{
cord_h=P3&0x0f; //读入列线值
P3=cord_h|0xf0; //输出当前列线值
cord_l=P3&0xf0; //读入行线值
return(cord_h+cord_l);//键盘最后组合码值
}
}
return(0xff); //返回该值
}
void speed(uchar n)
{ n=m;
while(m==0x7e)
{
P1=0xfe;
if(m!=0x7e) break;
DelayMs(600);
P1=0xfd;
if(m!=0x7e) break;
DelayMs(600);
P1=0xfb;
if(m!=0x7e) break;
DelayMs(600);
P1=0xf7;
if(m!=0x7e) break;
DelayMs(600);
P1=0xef;
if(m!=0x7e) break;
DelayMs(600);
P1=0xdf;
if(m!=0x7e) break;
DelayMs(600);
P1=0xbf;
if(m!=0x7e) break;
DelayMs(600);
P1=0x7f;
if(m!=0x7e) break;
DelayMs(600);
}
while(m==0x7d)
{
P1=0xfe;
if(m!=0x7d) break;
DelayMs(100);
P1=0xfd;
if(m!=0x7d)break;
DelayMs(100);
P1=0xfb;
if(m!=0x7d)break;
DelayMs(100);
P1=0xf7;
if(m!=0x7d) break;
DelayMs(100);
P1=0xef;
if(m!=0x7d) break;
DelayMs(100);
P1=0xdf;
if(m!=0x7d)break;
DelayMs(100);
P1=0xbf;
if(m!=0x7d)break;
DelayMs(100);
P1=0x7f;
if(m!=0x7d)break;
DelayMs(100);
}
while((m==0x7b))
{ P0=0xfe;
if(m!=0x7b) break;
DelayMs(4400);
P1=0xfd;
if(m!=0x7b)break;
DelayMs(4400);
P1=0xfb;
if(m!=0x7b)break;
DelayMs(4400);
P1=0xf7;
if(m!=0x7b) break;
DelayMs(4400);
P1=0xef;
if(m!=0x7b) break;
DelayMs(4400);
P1=0xdf;
if(m!=0x7b)break;
DelayMs(4400);
P1=0xbf;
if(m!=0x7b)break;
DelayMs(4400);
P1=0x7f;
if(m!=0x7b)break;
DelayMs(4400);
}
while((m==0x77))
{ P1=0xfe;
if(m!=0x77) break;
DelayMs(4400);
P1=0xfd;
if(m!=0x77)break;
DelayMs(3400);
P1=0xfb;
if(m!=0x77)break;
DelayMs(3400);
P1=0xf7;
if(m!=0x77) break;
DelayMs(3400);
P1=0xef;
if(m!=0x77) break;
DelayMs(3400);
P1=0xdf;
if(m!=0x77)break;
DelayMs(3400);
P1=0xbf;
if(m!=0x77)break;
DelayMs(3400);
P1=0x7f;
if(m!=0x77)break;
DelayMs(3400);
}
while((m==0xbe))
{ P1=0xfe;
if(m!=0xbe) break;
DelayMs(2400);
P1=0xfd;
if(m!=0xbe)break;
DelayMs(2400);
P1=0xfb;
if(m!=0xbe)break;
DelayMs(2400);
P1=0xf7;
if(m!=0xbe) break;
DelayMs(2400);
P1=0xef;
if(m!=0xbe) break;
DelayMs(2400);
P1=0xdf;
if(m!=0xbe)break;
DelayMs(2400);
P1=0xbf;
if(m!=0xbe)break;
DelayMs(2400);
P1=0x7f;
if(m!=0xbe)break;
DelayMs(2400);
}
while((m==0xbd))
{ P1=0xfe;
if(m!=0xbd) break;
DelayMs(1400);
P1=0xfd;
if(m!=0xbd)break;
DelayMs(1400);
P1=0xfb;
if(m!=0xbd)break;
DelayMs(1400);
P1=0xf7;
if(m!=0xbd) break;
DelayMs(1400);
P1=0xef;
if(m!=0xbd) break;
DelayMs(1400);
P1=0xdf;
if(m!=0xbd)break;
DelayMs(1400);
P1=0xbf;
if(m!=0xbd)break;
DelayMs(1400);
P1=0x7f;
if(m!=0xbd)break;
DelayMs(1400);
}
while((m==0xbb))
{ P1=0xfe;
if(m!=0xbb) break;
DelayMs(1100);
P1=0xfd;
if(m!=0xbb)break;
DelayMs(1100);
P1=0xfb;
if(m!=0xbb)break;
DelayMs(1100);
P1=0xf7;
if(m!=0xbb) break;
DelayMs(1100);
P1=0xef;
if(m!=0xbb) break;
DelayMs(1100);
P1=0xdf;
if(m!=0xbb)break;
DelayMs(1100);
P1=0xbf;
if(m!=0xbb)break;
DelayMs(1100);
P1=0x7f;
if(m!=0xbb)break;
DelayMs(1100);
}
while((m==0xb7))
{ P1=0xfe;
if(m!=0xb7) break;
DelayMs(800);
P1=0xfd;
if(m!=0xb7)break;
DelayMs(800);
P1=0xfb;
if(m!=0xb7)break;
DelayMs(800);
P1=0xf7;
if(m!=0xb7) break;
DelayMs(800);
P1=0xef;
if(m!=0xb7) break;
DelayMs(800);
P1=0xdf;
if(m!=0xb7)break;
DelayMs(800);
P1=0xbf;
if(m!=0xb7)break;
DelayMs(800);
P1=0x7f;
if(m!=0xb7)break;
DelayMs(800);
}
while((m==0xde))
{ P1=0xfe;
if(m!=0xde) break;
DelayMs(800);
P1=0xfd;
if(m!=0xde)break;
DelayMs(800);
P1=0xfb;
if(m!=0xde)break;
DelayMs(800);
P1=0xf7;
if(m!=0xde) break;
DelayMs(800);
P1=0xef;
if(m!=0xde) break;
DelayMs(800);
P1=0xdf;
if(m!=0xde)break;
DelayMs(800);
P1=0xbf;
if(m!=0xde)break;
DelayMs(800);
P1=0x7f;
if(m!=0xde)break;
DelayMs(800);
}
while((m==0xdd))
{ P1=0xfe;
if(m!=0xdd) break;
DelayMs(500);
P1=0xfd;
if(m!=0xdd)break;
DelayMs(500);
P1=0xfb;
if(m!=0xdd)break;
DelayMs(500);
P1=0xf7;
if(m!=0xdd) break;
DelayMs(500);
P1=0xef;
if(m!=0xdd) break;
DelayMs(500);
P1=0xdf;
if(m!=0xdd)break;
DelayMs(500);
P1=0xbf;
if(m!=0xdd)break;
DelayMs(500);
P1=0x7f;
if(m!=0xdd)break;
DelayMs(500);
}
while((m==0xdb))
{ P1=0xfe;
if(m!=0xdb) break;
DelayMs(200);
P1=0xfd;
if(m!=0xdb)break;
DelayMs(200);
P1=0xfb;
if(m!=0xdb)break;
DelayMs(200);
P1=0xf7;
if(m!=0xdb) break;
DelayMs(200);
P1=0xef;
if(m!=0xdb) break;
DelayMs(200);
P1=0xdf;
if(m!=0xdb)break;
DelayMs(200);
P1=0xbf;
if(m!=0xdb)break;
DelayMs(200);
P1=0x7f;
if(m!=0xdb)break;
DelayMs(200);
}
while((m==0xd7))
{ P1=0xfe;
if(m!=0xd7) break;
DelayMs(100);
P1=0xfd;
if(m!=0xd7)break;
DelayMs(100);
P1=0xfb;
if(m!=0xd7)break;
DelayMs(100);
P1=0xf7;
if(m!=0xd7) break;
DelayMs(100);
P1=0xef;
if(m!=0xd7) break;
DelayMs(100);
P1=0xdf;
if(m!=0xd7)break;
DelayMs(100);
P1=0xbf;
if(m!=0xd7)break;
DelayMs(100);
P1=0x7f;
if(m!=0xd7)break;
DelayMs(100);
}
}
void timer1_init()
{
TMOD=0x01;//定时器1
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA = 1;
ET0 =1;
TR0 =1;
}
void timer0() interrupt 1
{
key_number=keyscan;
if(key_number!=0xff);
}
--
※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: 180.160.48.13]
标 题: 求单片机朋友们帮忙指点一个简单的程序
发信站: 饮水思源 (2012年09月26日01:04:52 星期三)
我不知道为什么矩阵键盘没有工作 为什么switch语中的speed子函数不能调用
求好心人指点 好苦恼啊
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void timer1_init();
uchar key_number;
uchar m;
sbit zhongduan=P3^3;
unsigned char const dofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
,
0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F
/*------------------------------------------------
函数声明
------------------------------------------------*/
uchar keyscan(void);//键盘扫描
void delay(uint i);
void speed(uchar m);
/*------------------------------------------------
主函数
------------------------------------------------*/
void DelayMs(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
timer1_init();
while(1)
{
P2=6;
key_number=keyscan(); //调用键盘扫描,
switch(key_number)
{
case 0x7e:P0=dofly[0];speed(0x7e);break;//0
case 0x7d:P0=dofly[1];speed(0x7d);break;//1
case 0x7b:P0=dofly[2];speed(0x7b);break;//2
case 0x77:P0=dofly[3];speed(0x77);break;//3
case 0xbe:P0=dofly[4];speed(0xbe);break;//4
case 0xbd:P0=dofly[5];speed(0xbd);break;//5
case 0xbb:P0=dofly[6];speed(0xbb);break;//6
case 0xb7:P0=dofly[7];speed(0xb7);break;//7
case 0xde:P0=dofly[8];speed(0xde);break;//8
case 0xdd:P0=dofly[9];speed(0xdd);break;//9
case 0xdb:P0=dofly[10];speed(0xdb);break;//a
case 0xd7:P0=dofly[11];speed(0xd7);break;//b
case 0xee:P0=dofly[12];speed(0xee);break;//c
}
}
}
/*------------------------------------------------
键盘扫描程序
------------------------------------------------*/
uchar keyscan(void) //键盘扫描函数,使用行列反转扫描法
{
uchar cord_h,cord_l;//行列值中间变量
P3=0x0f; //行线输出全为0
cord_h=P3&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
DelayMs(20); //去抖
if(cord_h!=0x0f)
{
cord_h=P3&0x0f; //读入列线值
P3=cord_h|0xf0; //输出当前列线值
cord_l=P3&0xf0; //读入行线值
return(cord_h+cord_l);//键盘最后组合码值
}
}
return(0xff); //返回该值
}
void speed(uchar n)
{ n=m;
while(m==0x7e)
{
P1=0xfe;
if(m!=0x7e) break;
DelayMs(600);
P1=0xfd;
if(m!=0x7e) break;
DelayMs(600);
P1=0xfb;
if(m!=0x7e) break;
DelayMs(600);
P1=0xf7;
if(m!=0x7e) break;
DelayMs(600);
P1=0xef;
if(m!=0x7e) break;
DelayMs(600);
P1=0xdf;
if(m!=0x7e) break;
DelayMs(600);
P1=0xbf;
if(m!=0x7e) break;
DelayMs(600);
P1=0x7f;
if(m!=0x7e) break;
DelayMs(600);
}
while(m==0x7d)
{
P1=0xfe;
if(m!=0x7d) break;
DelayMs(100);
P1=0xfd;
if(m!=0x7d)break;
DelayMs(100);
P1=0xfb;
if(m!=0x7d)break;
DelayMs(100);
P1=0xf7;
if(m!=0x7d) break;
DelayMs(100);
P1=0xef;
if(m!=0x7d) break;
DelayMs(100);
P1=0xdf;
if(m!=0x7d)break;
DelayMs(100);
P1=0xbf;
if(m!=0x7d)break;
DelayMs(100);
P1=0x7f;
if(m!=0x7d)break;
DelayMs(100);
}
while((m==0x7b))
{ P0=0xfe;
if(m!=0x7b) break;
DelayMs(4400);
P1=0xfd;
if(m!=0x7b)break;
DelayMs(4400);
P1=0xfb;
if(m!=0x7b)break;
DelayMs(4400);
P1=0xf7;
if(m!=0x7b) break;
DelayMs(4400);
P1=0xef;
if(m!=0x7b) break;
DelayMs(4400);
P1=0xdf;
if(m!=0x7b)break;
DelayMs(4400);
P1=0xbf;
if(m!=0x7b)break;
DelayMs(4400);
P1=0x7f;
if(m!=0x7b)break;
DelayMs(4400);
}
while((m==0x77))
{ P1=0xfe;
if(m!=0x77) break;
DelayMs(4400);
P1=0xfd;
if(m!=0x77)break;
DelayMs(3400);
P1=0xfb;
if(m!=0x77)break;
DelayMs(3400);
P1=0xf7;
if(m!=0x77) break;
DelayMs(3400);
P1=0xef;
if(m!=0x77) break;
DelayMs(3400);
P1=0xdf;
if(m!=0x77)break;
DelayMs(3400);
P1=0xbf;
if(m!=0x77)break;
DelayMs(3400);
P1=0x7f;
if(m!=0x77)break;
DelayMs(3400);
}
while((m==0xbe))
{ P1=0xfe;
if(m!=0xbe) break;
DelayMs(2400);
P1=0xfd;
if(m!=0xbe)break;
DelayMs(2400);
P1=0xfb;
if(m!=0xbe)break;
DelayMs(2400);
P1=0xf7;
if(m!=0xbe) break;
DelayMs(2400);
P1=0xef;
if(m!=0xbe) break;
DelayMs(2400);
P1=0xdf;
if(m!=0xbe)break;
DelayMs(2400);
P1=0xbf;
if(m!=0xbe)break;
DelayMs(2400);
P1=0x7f;
if(m!=0xbe)break;
DelayMs(2400);
}
while((m==0xbd))
{ P1=0xfe;
if(m!=0xbd) break;
DelayMs(1400);
P1=0xfd;
if(m!=0xbd)break;
DelayMs(1400);
P1=0xfb;
if(m!=0xbd)break;
DelayMs(1400);
P1=0xf7;
if(m!=0xbd) break;
DelayMs(1400);
P1=0xef;
if(m!=0xbd) break;
DelayMs(1400);
P1=0xdf;
if(m!=0xbd)break;
DelayMs(1400);
P1=0xbf;
if(m!=0xbd)break;
DelayMs(1400);
P1=0x7f;
if(m!=0xbd)break;
DelayMs(1400);
}
while((m==0xbb))
{ P1=0xfe;
if(m!=0xbb) break;
DelayMs(1100);
P1=0xfd;
if(m!=0xbb)break;
DelayMs(1100);
P1=0xfb;
if(m!=0xbb)break;
DelayMs(1100);
P1=0xf7;
if(m!=0xbb) break;
DelayMs(1100);
P1=0xef;
if(m!=0xbb) break;
DelayMs(1100);
P1=0xdf;
if(m!=0xbb)break;
DelayMs(1100);
P1=0xbf;
if(m!=0xbb)break;
DelayMs(1100);
P1=0x7f;
if(m!=0xbb)break;
DelayMs(1100);
}
while((m==0xb7))
{ P1=0xfe;
if(m!=0xb7) break;
DelayMs(800);
P1=0xfd;
if(m!=0xb7)break;
DelayMs(800);
P1=0xfb;
if(m!=0xb7)break;
DelayMs(800);
P1=0xf7;
if(m!=0xb7) break;
DelayMs(800);
P1=0xef;
if(m!=0xb7) break;
DelayMs(800);
P1=0xdf;
if(m!=0xb7)break;
DelayMs(800);
P1=0xbf;
if(m!=0xb7)break;
DelayMs(800);
P1=0x7f;
if(m!=0xb7)break;
DelayMs(800);
}
while((m==0xde))
{ P1=0xfe;
if(m!=0xde) break;
DelayMs(800);
P1=0xfd;
if(m!=0xde)break;
DelayMs(800);
P1=0xfb;
if(m!=0xde)break;
DelayMs(800);
P1=0xf7;
if(m!=0xde) break;
DelayMs(800);
P1=0xef;
if(m!=0xde) break;
DelayMs(800);
P1=0xdf;
if(m!=0xde)break;
DelayMs(800);
P1=0xbf;
if(m!=0xde)break;
DelayMs(800);
P1=0x7f;
if(m!=0xde)break;
DelayMs(800);
}
while((m==0xdd))
{ P1=0xfe;
if(m!=0xdd) break;
DelayMs(500);
P1=0xfd;
if(m!=0xdd)break;
DelayMs(500);
P1=0xfb;
if(m!=0xdd)break;
DelayMs(500);
P1=0xf7;
if(m!=0xdd) break;
DelayMs(500);
P1=0xef;
if(m!=0xdd) break;
DelayMs(500);
P1=0xdf;
if(m!=0xdd)break;
DelayMs(500);
P1=0xbf;
if(m!=0xdd)break;
DelayMs(500);
P1=0x7f;
if(m!=0xdd)break;
DelayMs(500);
}
while((m==0xdb))
{ P1=0xfe;
if(m!=0xdb) break;
DelayMs(200);
P1=0xfd;
if(m!=0xdb)break;
DelayMs(200);
P1=0xfb;
if(m!=0xdb)break;
DelayMs(200);
P1=0xf7;
if(m!=0xdb) break;
DelayMs(200);
P1=0xef;
if(m!=0xdb) break;
DelayMs(200);
P1=0xdf;
if(m!=0xdb)break;
DelayMs(200);
P1=0xbf;
if(m!=0xdb)break;
DelayMs(200);
P1=0x7f;
if(m!=0xdb)break;
DelayMs(200);
}
while((m==0xd7))
{ P1=0xfe;
if(m!=0xd7) break;
DelayMs(100);
P1=0xfd;
if(m!=0xd7)break;
DelayMs(100);
P1=0xfb;
if(m!=0xd7)break;
DelayMs(100);
P1=0xf7;
if(m!=0xd7) break;
DelayMs(100);
P1=0xef;
if(m!=0xd7) break;
DelayMs(100);
P1=0xdf;
if(m!=0xd7)break;
DelayMs(100);
P1=0xbf;
if(m!=0xd7)break;
DelayMs(100);
P1=0x7f;
if(m!=0xd7)break;
DelayMs(100);
}
}
void timer1_init()
{
TMOD=0x01;//定时器1
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA = 1;
ET0 =1;
TR0 =1;
}
void timer0() interrupt 1
{
key_number=keyscan;
if(key_number!=0xff);
}
--
※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: 180.160.48.13]
No comments:
Post a Comment