单片机简单C语言问题,实现一个个位加法计算器
#include<reg52.h>#defineucharunsignedchar#defineunitunsignedint#defineOPdataP0//十位数据口...
#include <reg52.h>
#define uchar unsigned char
#define unit unsigned int
#define OPdata P0 //十位数据口
#define OPdata1 P1//个位数据口
sbit se0=P3^6;//十位选择
sbit se1=P3^7;//个位选择
sbit Q=P2^6;
sbit W=P2^5;
sbit E=P2^4;
uchar cis; //按下键盘次数
uchar jia1; //加数1
uchar jia2; //加数2
uchar z; //中转数
uchar shi; //十位数
uchar ge; //个位数
uchar s; //译码结果
uchar j; //译码结果
uchar sum; //加法结果
uchar keytemp;
void judge(uchar m) //数码显像管译码
{
switch (m)
{
case 0:
s=0x3F;
break;
case 1:
s=0x06;
break;
case 2:
s=0x5B;
break;
case 3:
s=0x4F;
break;
case 4:
s=0x66;
break;
case 5:
s=0x6D;
break;
case 6:
s=0x7D;
break;
case 7:
s=0x07;
break;
case 8:
s=0x7F;
break;
case 9:
s=0x6F;
break;
}
}
void judge1(uchar h) //十位数码显像管译码
{
switch (h)
{
case 0:
j=0x3F;
break;
case 1:
j=0x06;
break;
}
}
void judgecis()
{
if (cis==1)
{
jia1=z;
ge=z;
shi=0;
}
else if (cis==2)
{
jia2=z;
ge=z;
shi=0;
cis=0;
}
}
void jiafa(uchar m,uchar n) //加法
{
sum=m+n;
if (sum>=10)
{
ge=sum-10;
shi=1;
}
else
{
ge=sum;
shi=0;
}
}
void main()
{
cis=0;
jia1=0;
jia2=0;
shi=0;
ge=0;
s=0;
j=0;
P2=0XFF;
se0=0;//开启十位
se1=0;//开启个位
OPdata=0x3f;//十位
OPdata1=0x3f;//个位
while (1)
{
Q=0;
keytemp=P2;
switch (keytemp)
{
case 0xbb:
z=3;
cis++;
break;
case 0xbd:
z=6;
cis++;
break;
case 0xbe:
z=9;
cis++;
break;
case 0xb7: //等于号
jiafa (jia1,jia2);
cis=0;
break;
}
Q=1;
W=0;
keytemp=P2;
switch (keytemp)
{
case 0xde:
z=8;
cis++;
break;
case 0xdd:
z=5;
cis++;
break;
case 0xdb:
z=2;
cis++;
break;
case 0xd7:
z=0;
cis++;
break;
}
W=1;
E=0;
keytemp=P2;
switch (keytemp)
{
case 0xee:
z=7;
cis++;
break;
case 0xed:
z=4;
cis++;
break;
case 0xeb:
z=1;
cis++;
break;
}
judgecis();
judge(ge);
OPdata1=s;
judge1(shi);
OPdata=j;
E=1;
}
}
问题出在每次按了两次之后,相加的结果总是等于第二个数加第二个数,看了好久都看不出来,我是要崩溃了…… 展开
#define uchar unsigned char
#define unit unsigned int
#define OPdata P0 //十位数据口
#define OPdata1 P1//个位数据口
sbit se0=P3^6;//十位选择
sbit se1=P3^7;//个位选择
sbit Q=P2^6;
sbit W=P2^5;
sbit E=P2^4;
uchar cis; //按下键盘次数
uchar jia1; //加数1
uchar jia2; //加数2
uchar z; //中转数
uchar shi; //十位数
uchar ge; //个位数
uchar s; //译码结果
uchar j; //译码结果
uchar sum; //加法结果
uchar keytemp;
void judge(uchar m) //数码显像管译码
{
switch (m)
{
case 0:
s=0x3F;
break;
case 1:
s=0x06;
break;
case 2:
s=0x5B;
break;
case 3:
s=0x4F;
break;
case 4:
s=0x66;
break;
case 5:
s=0x6D;
break;
case 6:
s=0x7D;
break;
case 7:
s=0x07;
break;
case 8:
s=0x7F;
break;
case 9:
s=0x6F;
break;
}
}
void judge1(uchar h) //十位数码显像管译码
{
switch (h)
{
case 0:
j=0x3F;
break;
case 1:
j=0x06;
break;
}
}
void judgecis()
{
if (cis==1)
{
jia1=z;
ge=z;
shi=0;
}
else if (cis==2)
{
jia2=z;
ge=z;
shi=0;
cis=0;
}
}
void jiafa(uchar m,uchar n) //加法
{
sum=m+n;
if (sum>=10)
{
ge=sum-10;
shi=1;
}
else
{
ge=sum;
shi=0;
}
}
void main()
{
cis=0;
jia1=0;
jia2=0;
shi=0;
ge=0;
s=0;
j=0;
P2=0XFF;
se0=0;//开启十位
se1=0;//开启个位
OPdata=0x3f;//十位
OPdata1=0x3f;//个位
while (1)
{
Q=0;
keytemp=P2;
switch (keytemp)
{
case 0xbb:
z=3;
cis++;
break;
case 0xbd:
z=6;
cis++;
break;
case 0xbe:
z=9;
cis++;
break;
case 0xb7: //等于号
jiafa (jia1,jia2);
cis=0;
break;
}
Q=1;
W=0;
keytemp=P2;
switch (keytemp)
{
case 0xde:
z=8;
cis++;
break;
case 0xdd:
z=5;
cis++;
break;
case 0xdb:
z=2;
cis++;
break;
case 0xd7:
z=0;
cis++;
break;
}
W=1;
E=0;
keytemp=P2;
switch (keytemp)
{
case 0xee:
z=7;
cis++;
break;
case 0xed:
z=4;
cis++;
break;
case 0xeb:
z=1;
cis++;
break;
}
judgecis();
judge(ge);
OPdata1=s;
judge1(shi);
OPdata=j;
E=1;
}
}
问题出在每次按了两次之后,相加的结果总是等于第二个数加第二个数,看了好久都看不出来,我是要崩溃了…… 展开
4个回答
展开全部
你的问题应该出现在键盘处理不完善造成的,稍微修改了一下你试试。
#include <reg52.h>
#define uchar unsigned char
#define unit unsigned int
#define OPdata P0 //十位数据口
#define OPdata1 P1//个位数据口
sbit se0=P3^6;//十位选择
sbit se1=P3^7;//个位选择
sbit Q=P2^6;
sbit W=P2^5;
sbit E=P2^4;
uchar cis; //按下键盘次数
uchar jia1; //加数1
uchar jia2; //加数2
uchar z; //中转数
uchar shi; //十位数
uchar ge; //个位数
uchar s; //译码结果
uchar j; //译码结果
uchar sum; //加法结果
uchar keytemp;
void judge(uchar m) //数码显像管译码
{
switch (m)
{
case 0: s=0x3F; break;
case 1: s=0x06; break;
case 2: s=0x5B; break;
case 3: s=0x4F; break;
case 4: s=0x66; break;
case 5: s=0x6D; break;
case 6: s=0x7D; break;
case 7: s=0x07; break;
case 8: s=0x7F; break;
case 9: s=0x6F; break;
}
}
void judge1(uchar h) //十位数码显像管译码
{
switch (h)
{
case 0: j=0x3F; break;
case 1: j=0x06; break;
}
}
void judgecis()
{
if (cis==1)
{
jia1=z;
ge=z;
shi=0;
}
else if (cis==2)
{
jia2=z;
ge=z;
shi=0;
cis=0;
}
}
void jiafa(uchar m,uchar n) //加法
{
sum=m+n;
if (sum>=10)
{
ge=sum-10;
shi=1;
}
else
{
ge=sum;
shi=0;
}
}
void main()
{
cis=0;
jia1=0;
jia2=0;
shi=0;
ge=0;
s=0;
j=0;
P2=0XFF;
se0=0;//开启十位
se1=0;//开启个位
OPdata=0x3f;//十位
OPdata1=0x3f;//个位
while (1)
{
Q=0;
keytemp=P2;
switch (keytemp)
{
case 0xbb: z=3; cis++;while(P2==0xbb); break;
case 0xbd: z=6; cis++;while(P2==0xbd); break;
case 0xbe: z=9; cis++;while(P2==0xbe); break;
case 0xb7: //等于号
jiafa (jia1,jia2); cis=0; break;
}
Q=1;
W=0;
keytemp=P2;
switch (keytemp)
{
case 0xde: z=8; cis++;while(P2==0xde); break;
case 0xdd: z=5; cis++;while(P2==0xdd); break;
case 0xdb: z=2; cis++;while(P2==0xdb); break;
case 0xd7: z=0; cis++;while(P2==0xd7); break;
}
W=1;
E=0;
keytemp=P2;
switch (keytemp)
{
case 0xee: z=7; cis++;while(P2==0xee); break;
case 0xed: z=4; cis++;while(P2==0xed); break;
case 0xeb: z=1; cis++;while(P2==0xeb); break;
}
judgecis();
judge(ge);
OPdata1=s;
judge1(shi);
OPdata=j;
E=1;
}
}
#include <reg52.h>
#define uchar unsigned char
#define unit unsigned int
#define OPdata P0 //十位数据口
#define OPdata1 P1//个位数据口
sbit se0=P3^6;//十位选择
sbit se1=P3^7;//个位选择
sbit Q=P2^6;
sbit W=P2^5;
sbit E=P2^4;
uchar cis; //按下键盘次数
uchar jia1; //加数1
uchar jia2; //加数2
uchar z; //中转数
uchar shi; //十位数
uchar ge; //个位数
uchar s; //译码结果
uchar j; //译码结果
uchar sum; //加法结果
uchar keytemp;
void judge(uchar m) //数码显像管译码
{
switch (m)
{
case 0: s=0x3F; break;
case 1: s=0x06; break;
case 2: s=0x5B; break;
case 3: s=0x4F; break;
case 4: s=0x66; break;
case 5: s=0x6D; break;
case 6: s=0x7D; break;
case 7: s=0x07; break;
case 8: s=0x7F; break;
case 9: s=0x6F; break;
}
}
void judge1(uchar h) //十位数码显像管译码
{
switch (h)
{
case 0: j=0x3F; break;
case 1: j=0x06; break;
}
}
void judgecis()
{
if (cis==1)
{
jia1=z;
ge=z;
shi=0;
}
else if (cis==2)
{
jia2=z;
ge=z;
shi=0;
cis=0;
}
}
void jiafa(uchar m,uchar n) //加法
{
sum=m+n;
if (sum>=10)
{
ge=sum-10;
shi=1;
}
else
{
ge=sum;
shi=0;
}
}
void main()
{
cis=0;
jia1=0;
jia2=0;
shi=0;
ge=0;
s=0;
j=0;
P2=0XFF;
se0=0;//开启十位
se1=0;//开启个位
OPdata=0x3f;//十位
OPdata1=0x3f;//个位
while (1)
{
Q=0;
keytemp=P2;
switch (keytemp)
{
case 0xbb: z=3; cis++;while(P2==0xbb); break;
case 0xbd: z=6; cis++;while(P2==0xbd); break;
case 0xbe: z=9; cis++;while(P2==0xbe); break;
case 0xb7: //等于号
jiafa (jia1,jia2); cis=0; break;
}
Q=1;
W=0;
keytemp=P2;
switch (keytemp)
{
case 0xde: z=8; cis++;while(P2==0xde); break;
case 0xdd: z=5; cis++;while(P2==0xdd); break;
case 0xdb: z=2; cis++;while(P2==0xdb); break;
case 0xd7: z=0; cis++;while(P2==0xd7); break;
}
W=1;
E=0;
keytemp=P2;
switch (keytemp)
{
case 0xee: z=7; cis++;while(P2==0xee); break;
case 0xed: z=4; cis++;while(P2==0xed); break;
case 0xeb: z=1; cis++;while(P2==0xeb); break;
}
judgecis();
judge(ge);
OPdata1=s;
judge1(shi);
OPdata=j;
E=1;
}
}
展开全部
你的意思是先按1,再按6,最后按=号,结果是12?而不是7?
下面有一行改下。
void judgecis()
{
if (cis==1)
{
jia1=z;
ge=z;
shi=0;
}
else if (cis==2)
{
jia2=z;
ge=z;
shi=0;
cis=0; //把这行去掉试试
}
}
下面有一行改下。
void judgecis()
{
if (cis==1)
{
jia1=z;
ge=z;
shi=0;
}
else if (cis==2)
{
jia2=z;
ge=z;
shi=0;
cis=0; //把这行去掉试试
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个是个小试验项目吧,其实就是做一个4*4键盘的功能?简单就用16个io口分别赋予键值,复杂的就是用4x4键盘逻辑(4个io纵向,4个io横向编程)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这样的程序也不整理再贴出来,看了都累啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询