开始是一样多的,显示会有问题,但是通过按键10直接跳出来就正常,后来数组元素多+了一个才正常了,但是想不通为什么要多加一个才正常,又不是字符串。按键返回值得那个函数就没贴了,包含在anjian.c里面。还有外部函数调用我怎么没反应,delay()、key()使用前声明了extern啊,编译也通过,下载到单片机里没反应。
- #include<reg52.h>
- #include "anjian.c"//按键检测函数key()
- #define uint unsigned int
- #define uchar unsigned char
- #define NUM 4//等待按键个数为NUM
- #define time 10000//按键抖动延时,大概90ms
- uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
- 0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
- uchar str[NUM+1]={0};//按键字符数组,必须加1,否则会乱码!
- void strkey()//按键值赋给str[]数组
- {
- uchar i;
- for(i=0;i<NUM;i++)
- {
- str[i]=key();
- if(str[i]==10)//按键10表示确认
- {
- break;
- }
- if(str[i]==11)//按键11表示取消,数组清零
- {
- for(i=0;i<NUM;i++)
- str[i]=0;
- i=0;
- str[i]=key();
- }
- }
- }
- void main()
- {
- uchar a,x;
- strkey();
- while(1)
- {
- for(x=0;x<4;x++)//显示str[]数组元素到4个数码管
- {
- a=str[x];
- P3=table[a];
- P2=(~(0x01<<x));
- delay(200);
- P2=0xff;
- }
- a=0;
- }
- }
复制代码
|