程序想实现的功能是
1,按更改键 准备键入数值
2.按数字键,比如5,第一个数码管显示5 ,其他为0
3按第二个数字键,比如3 数码管显示53,以此类推
但是现在的结果是 第一步可以实现,第二部 会所有数码管显示5 第三部按了没反应,得在按以此更改键 才可以输入数值,代码如下 帮忙看看吧 菜鸟一个
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar key , disp_buf , temp;
//unsigned char l_tmpdate[8]={15,11,10,0,0,14,2,12};
unsigned char l_tmpdate[8]={7,6,5,4,2,3,1,0};
unsigned long freq = 10000;
uchar count_i = 0;
sbit LS138A = P2^2; //定义138译码器的输入A脚由P2.2控制
sbit LS138B = P2^3; //定义138译码器的输入脚B由P2.3控制
sbit LS138C = P2^4; //定义138译码器的输入脚C由P2.4控制
void delay(uchar x);
int keyscan(void);
void display(unsigned char *lp,unsigned char lc);
void diszf();
//code unsigned char LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C}; //共阳极
code unsigned char LED7Code[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C}; //共阴极
//code unsigned char key_table[13]={0xed,0x7e,0x7d,0x7b,
// 0xbe,0xbd,0xbb,0xde,
// 0xdd,0xdb,0x77,0xb7,
// 0Xff};
// 0 1 2 3 4 5 6 7 8 9 a b
code unsigned char key_table[]={0x7b,0xed,0xdd,0xbd,0xeb,0xdb,0xbb,0xe7,0xd7,0xb7,0x7d,0x77};
// 1 2 3 up 0x01 0x02 0x03 0x0a
// 4 5 6 dn 对应16进制码 0x04 0x05 0x06 0x0b
// 7 8 9 M1 0x07 0x08 0x09 0x0d
// *c 0 #ok M1 0x0c 0x00 0x0e 0x0f
void delay(uchar x)
{ uchar j;
while((x--)!=0) //CPU执行x*125次
{ for(j=0;j<125;j++)
{;}
}
}
int keyscan(void) //执行一下会得到一个对应的键值
{
uchar i,scancode;
P0 = 0xf0; // 发全0行扫描码
if ((P0&0xf0)!=0xf0) // 若有键按下
{
delay(1); // 延时去抖动
scancode = 0xfd;
// key = 0xff;
for(i=0;1<3;++i)
{
P0 = scancode;
if ((P0&0xf0)!=0xf0) // 延时后再判断一次,去除抖动影响
{
key = P0;
break;
}
scancode = _crol_(scancode,1);
}
}
return(0); // 无键按下,返回值为0
}
int main()
{
int i , num ;
num = 0xff;
P0 = 0x00; //键盘置位
// P1 = 0xff; //数码管置位
delay(10);
while(1)
{
keyscan();
for(i=0;i<13;i++)
{ //查表获得相应的16进制值存放l_key变量中
if (key==key_table[i])
{
num=i;
break;
}
}
// P1 = LED7Code[num];
display(l_tmpdate,8); //输出获取的键值码
if(num==0x01){ //1按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=1;break;
case 2: freq=freq*10+1;break;
case 3: freq=freq*10+1;break;
case 4: freq=freq*10+1;break;
case 5: freq=freq*10+1;break;
case 6: freq=freq*10+1;break;
case 7: freq=freq*10+1;break;
case 8: freq=freq*10+1;break;
}
count_i++;
}
diszf();
}
if(num==0x02){ //2按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=2;break;
case 2: freq=freq*10+2;break;
case 3: freq=freq*10+2;break;
case 4: freq=freq*10+2;break;
case 5: freq=freq*10+2;break;
case 6: freq=freq*10+2;break;
case 7: freq=freq*10+2;break;
case 8: freq=freq*10+2;break;
}
count_i++;
}
diszf();
}
if(num==0x03){ //3按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=3;break;
case 2: freq=freq*10+3;break;
case 3: freq=freq*10+3;break;
case 4: freq=freq*10+3;break;
case 5: freq=freq*10+3;break;
case 6: freq=freq*10+3;break;
case 7: freq=freq*10+3;break;
case 8: freq=freq*10+3;break;
}
count_i++;
}
diszf();
}
if(num==0x04){ //4按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=4;break;
case 2: freq=freq*10+4;break;
case 3: freq=freq*10+4;break;
case 4: freq=freq*10+4;break;
case 5: freq=freq*10+4;break;
case 6: freq=freq*10+4;break;
case 7: freq=freq*10+4;break;
case 8: freq=freq*10+4;break;
}
count_i++;
}
diszf();
}
if(num==0x05){ //5按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=5;break;
case 2: freq=freq*10+5;break;
case 3: freq=freq*10+5;break;
case 4: freq=freq*10+5;break;
case 5: freq=freq*10+5;break;
case 6: freq=freq*10+5;break;
case 7: freq=freq*10+5;break;
case 8: freq=freq*10+5;break;
}
count_i++;
}
diszf();
}
if(num==0x06){ //6按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=6;break;
case 2: freq=freq*10+6;break;
case 3: freq=freq*10+6;break;
case 4: freq=freq*10+6;break;
case 5: freq=freq*10+6;break;
case 6: freq=freq*10+6;break;
case 7: freq=freq*10+6;break;
case 8: freq=freq*10+6;break;
}
count_i++;
}
diszf();
}
if(num==0x07){ //7按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=7;break;
case 2: freq=freq*10+7;break;
case 3: freq=freq*10+7;break;
case 4: freq=freq*10+7;break;
case 5: freq=freq*10+7;break;
case 6: freq=freq*10+7;break;
case 7: freq=freq*10+7;break;
case 8: freq=freq*10+7;break;
}
count_i++;
}
diszf();
}
if(num==0x08){ //8按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=8;break;
case 2: freq=freq*10+8;break;
case 3: freq=freq*10+8;break;
case 4: freq=freq*10+8;break;
case 5: freq=freq*10+8;break;
case 6: freq=freq*10+8;break;
case 7: freq=freq*10+8;break;
case 8: freq=freq*10+8;break;
}
count_i++;
}
diszf();
}
if(num==0x09){ //9按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=9;break;
case 2: freq=freq*10+9;break;
case 3: freq=freq*10+9;break;
case 4: freq=freq*10+9;break;
case 5: freq=freq*10+9;break;
case 6: freq=freq*10+9;break;
case 7: freq=freq*10+9;break;
case 8: freq=freq*10+9;break;
}
count_i++;
}
diszf();
}
if(num==0x00){ //0按键判断
num=0xff;
if(count_i && count_i<9){
switch(count_i){
case 1: freq=0;break;
case 2: freq=freq*10;break;
case 3: freq=freq*10;break;
case 4: freq=freq*10;break;
case 5: freq=freq*10;break;
case 6: freq=freq*10;break;
case 7: freq=freq*10;break;
case 8: freq=freq*10;break;
}
count_i++;
}
diszf();
}
/*#######################################
if(num==0x0a){ //步进按键
num=0xff;
freq+=100;
diszf();
ad9850_reset();
ad9850_wr(0x00,freq);//串行写freqHz频率程序
}
if(num==0x0b){ //步退按键
num=0xff;
freq-=100;
diszf();
ad9850_reset();
ad9850_wr(0x00,freq);//串行写freqHz频率程序
}
##############################################*/
if(num==0x0a){ //更改按键
num=0xff;
count_i=1;
freq=0;
// ad9850_reset();
diszf();
}
if(num==0x0b){ //确认输出按键
num=0xff;
count_i=0;
diszf();
// ad9850_reset();
// ad9850_wr(0x00,freq);//串行写freqHz频率程序
}
//#################################################
/*if(num==0x0d){ //**1按键
num=0xff;
count_i=0;
freq=38000000;
diszf();
ad9850_reset();
ad9850_wr(0x00,freq);//串行写freqHz频率程序
}
if(num==0x0f){ //**2按键,弄个蛙鸣的频率试试
num=0xff;
count_i=0;
freq=7023000;
diszf();
ad9850_reset();
ad9850_wr(0x00,freq);//串行写freqHz频率程序
}
####################################################*/
}
}
void display(unsigned char *lp,unsigned char lc)//显示
{
unsigned char i; //定义变量
// P1=0; //端口2为输出
// P1=P1&0xF8; //将P1口的前3位输出0,对应138译门输入脚,全0为第一位数码管
for(i=0;i<lc;i++)
{ //循环显示
// P1=LED7Code[lp[i]]; //查表法得到要显示数字的数码段
// delay(1); //延时5个空指令
switch(i) //使用switch 语句控制位选 也可以是用查表的方式 学员可以试着自己修改
{
case 0:LS138A=0; LS138B=0; LS138C=0; P1=LED7Code[lp[i]]; break;
case 1:LS138A=1; LS138B=0; LS138C=0; P1=LED7Code[lp[i]]; break;
case 2:LS138A=0; LS138B=1; LS138C=0; P1=LED7Code[lp[i]]; break;
case 3:LS138A=1; LS138B=1; LS138C=0; P1=LED7Code[lp[i]]; break;
case 4:LS138A=0; LS138B=0; LS138C=1; P1=LED7Code[lp[i]]; break;
case 5:LS138A=1; LS138B=0; LS138C=1; P1=LED7Code[lp[i]]; break;
case 6:LS138A=0; LS138B=1; LS138C=1; P1=LED7Code[lp[i]]; break;
case 7:LS138A=1; LS138B=1; LS138C=1; P1=LED7Code[lp[i]]; break;
}
delay(1000);
// if(i==7) //检测显示完8位否,完成直接退出,不让P1口再加1,否则进位影响到第四位数据
// break;
P1=0; //清0端口,准备显示下位
// a++; //下一位数码管
}
}
void diszf()//处理转换给数码管显示字符
{
bit nums=1;
unsigned long tmp;
l_tmpdate[0]=freq/10000000;
tmp=freq/10000000;
if(tmp==0 && nums)
l_tmpdate[0]=0;
else
nums=0;
tmp=freq%10000000;
l_tmpdate[1]=tmp/1000000;
if(l_tmpdate[1]==0 && nums)
l_tmpdate[1]=0;
else
nums=0;
tmp=tmp%1000000;
l_tmpdate[2]=tmp/100000;
if(l_tmpdate[2]==0 && nums)
l_tmpdate[2]=0;
else {
nums=0; }
tmp=tmp%100000;
l_tmpdate[3]=tmp/10000;
if(l_tmpdate[3]==0 && nums)
l_tmpdate[3]=0;
else {
nums=0; }
tmp=tmp%10000;
l_tmpdate[4]=tmp/1000;
if(l_tmpdate[4]==0 && nums)
l_tmpdate[4]=0;
else {
nums=0; }
tmp=tmp%1000;
l_tmpdate[5]=tmp/100;
if(l_tmpdate[5]==0 && nums)
l_tmpdate[5]=0;
else {
nums=0; }
tmp=tmp%100;
l_tmpdate[6]=tmp/10;
if(l_tmpdate[6]==0 && nums)l_tmpdate[6]=0;
else {
nums=0; }
tmp=tmp%10;
l_tmpdate[7]=tmp%10;
} |
|