简单计算器的设计方法.docx
- 文档编号:23347110
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:17
- 大小:19.42KB
简单计算器的设计方法.docx
《简单计算器的设计方法.docx》由会员分享,可在线阅读,更多相关《简单计算器的设计方法.docx(17页珍藏版)》请在冰豆网上搜索。
简单计算器的设计方法
简单计算器的设计
#include<reg51.h>#include<intrins.h>
#include<ctype.h>
#include<stdlib.h>
#defineucharunsignedchar
#defineuintunsignedint
ucharoperand1[9],operand2[9];
ucharoperator;
voiddelay(uint);
ucharkeyscan();
voiddisp(void);
voidbuf(uintvalue);
uintcompute(uintva1,uintva2,ucharoptor);
ucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xff};
uchardbuf[8]={10,10,10,10,10,10,10,10};
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
ucharkeyscan()
{
ucharskey;
P1=0xfe;
while((P1&0xf0)!
=0xf0)
{
delay(3);
while((P1&0xf0)!
=0xf0)
{
switch(P1)
{
case0xee:
skey='7';break;
case0xde:
skey='8';break;
case0xbe:
skey='9';break;
case0x7e:
skey='/';break;
default:
skey='#';
}
while((P1&0xf0)!
=0xf0)
;
}
}
P1=0xfd;
while((P1&0xf0)!
=0xf0)
{
delay(3);
while((P1&0xf0)!
=0xf0)
{
switch(P1)
{
case0xed:
skey='4';break;
case0xdd:
skey='5';break;
case0xbd:
skey='6';break;
case0x7d:
skey='*';break;
default:
skey='#';
}
while((P1&0xf0)!
=0xf0)
;
}
}
P1=0xfb;
while((P1&0xf0)!
=0xf0)
{
delay(3);
while((P1&0xf0)!
=0xf0)
{
switch(P1)
{
case0xeb:
skey='1';break;
case0xdb:
skey='2';break;
case0xbb:
skey='3';break;
case0x7b:
skey='-';break;
default:
skey='#';
}
while((P1&0xf0)!
=0xf0)
;
}
}
P1=0xf7;
while((P1&0xf0)!
=0xf0)
{
delay(3);
while((P1&0xf0)!
=0xf0)
{
switch(P1)
{
case0xe7:
skey='$';break;
case0xd7:
skey='0';break;
case0xb7:
skey='=';break;
case0x77:
skey='+';break;
default:
skey='#';
}
while((P1&0xf0)!
=0xf0)
;
}
}
returnskey;
}
voidmain()
{
uintvalue1,value2,value;
ucharckey,cut1=0,cut2=0;
ucharoperator;
uchari,bool=0;
init:
buf(0);
disp();
value=0;
cut1=cut2=0;
bool=0;
for(i=0;i<9;i++)
{
operand1[i]='\0';
operand2[i]='\0';
}
while
(1)
{
ckey=keyscan();
if(ckey!
='#')
{
if(isdigit(ckey))
{
switch(bool)
{
case0:
operand1[cut1]=ckey;
operand1[cut1+1]='\0';
value1=atoi(operand1);
cut1++;
buf(value1);
disp();
break;
case1:
operand2[cut2]=ckey;
operand2[cut2+1]='\0';
value2=atoi(operand2);
cut2++;
buf(value2);
disp();
break;
default:
break;
}
}
elseif(ckey=='+'||ckey=='-'||ckey=='*'||ckey=='/')
{
bool=1;
operator=ckey;
buf(0);
dbuf[7]=10;
disp();
}
elseif(ckey=='=')
{
value=compute(value1,value2,operator);
buf(value);
disp();
while
(1)
{
ckey=keyscan();
if(ckey=='$')
gotoinit;
else
{
buf(value);
disp();
}
}
}
elseif(ckey=='$')
{ gotoinit;}
}
disp();
}
}
uintcompute(uintva1,uintva2,ucharoptor)
{
uintvalue;
switch(optor)
{
case'+':
value=va1+va2; break;
case'-':
value=va1-va2; break;
case'*':
value=va1*va2; break;
case'/':
value=va1/va2; break;
default:
break;
}
returnvalue;
}
voidbuf(uintval)
{
uchari;
if(val==0)
{
dbuf[7]=0;
i=6;
}
else
for(i=7;val>0;i--)
{
dbuf[i]=val%10;
val/=10;
}
for(;i>0;i--)
dbuf[i]=10;
}
voiddisp(void)
{
ucharbsel,n;
bsel=0x01;
for(n=0;n<8;n++)
{
P2=bsel;
P0=table[dbuf[n]];
bsel=_crol_(bsel,1);
delay(3);
P0=0xff;
}
}
音乐播放器
/* 歌曲选择时应按住设置间3秒以上,然后按上或下键进行选择,然后再按设置键退出 */ #include
sbit spek=P3^7; //声音输出端
sbit set=P3^2; //设定及退出键,按奇数次为设定功能;偶数次为退出功能 sbit up=P3^1; //上一曲 sbit down=P3^0; //下一曲
bit flag1,flag2; //按键扫描标志位,新键值标志位
unsigned char f_t,n,i,key,k_max; //音符频率对应的二分之一周期值,节拍常数值,查表用计数器,键值,最大键值 unsigned char *p_music; //指向音乐数据数组的指针 void timer_ms(unsigned char tim); //毫秒级延时函数声明 void timer_us(unsigned char t); //微秒级延时函数 void key_scan(void); //按键扫描函数 void key_manage(); //键值处理函数
//格式为:
频率常数,节拍常数,频率常数,节拍常数
unsigned char code music_table1[]={0x18,0x30,0x1c,0x10,0x20,0x40,0x1c,0x10,0x18,
0x10,0x20,0x10,0x1c,0x10,0x18,0x40,0x1c,0x20,0x20,0x20,0x1c,0x20,0x18,0x20,0x20,0x80, 0xff,0x20,0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20,0x20,0x20,0x26,0x40,0x20,0x20, 0x2b,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0xff,0x20,0x20,0x1c,0x10,0x18,0x10,0x20,0x20, 0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,0x20,0x1c,0x10,0x18,0x10,0x20,0x20,0x26, 0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20, 0x20,0x20,0x26,0x40,0x20,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1c, 0x10,0x20,0x10,0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,0x15, 0x1f,0x05,0x20,0x10,0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20, 0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2b,0x20, 0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1c,0x10,0x18,0x40,0x1c,0x20,0x20,0x20,0x26, 0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00}; //八月桂花香
unsigned char code music_table2[]={0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,
0x26,0x20,0x30,0x20,0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,0x30,0x20,0x30,0x20, 0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10, 0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20, 0x2b,0x40,0x40,0x20,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x1c,0x20, 0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10, 0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10, 0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,0x20,0x10,0x2b,0x10, 0x26,0x30,0x30,0x80,0x00}; //祝你平安
unsigned char code music_table3[]={0x20,40,0x26,20,0x20,20,0x17,80,0x1c,40,0x17,40,0x20,80,0x20, 40,0x2f,20,0x2a,20,0x26,40,0x2a,20,0x2f,20,0x2a,80,0xff,0x20,40,0x26,20,0x20,20,0x17,60,0x19,20, 0x1c,40,0x17,40,0x20,80,0x20,40,0x2a,20,0x26,20,0x24,60,0x32,20,0x2f,80,0xff,0x1c,40,0x17,40,0x17, 80,0x19,40,0x1c,20,0x19,20,0x17,80,0x1c,20,0x19,20,0x17,20,0x1c,20,0x1c,20,0x20,20,0x26,20,0x2f,20, 0x2a,80,0xff,0x20,40,0x26,20,0x20,20,0x17,60,0x19,20,0x1c,40,0x17,40,0x20,80,0x20,40,0x2a,20,0x26, 20,0x24,60,0x32,20,0x2f,80,0xff,0x20,40,0x26,20,0x20,20,0x17,80,0x1c,40,0x17,40,0x20,80,0x20,40, 0x2f,20,0x2a,20,0x26,40,0x2a,20,0x2f,20,0x2a,80,0xff,0x20,40,0x26,20,0x20,20,0x17,60,0x19,20,0x1c, 40,0x17,40,0x20,80,0x20,40,0x2a,20,0x26,20,0x24,60,0x32,20,0x2f,80,0}; //送别
main() {
spek=0; P3=0x7f;
TMOD=0x01; //T0工作于方式1,即16位定时器模式 IE=0x82; //开总中断,T0中断 flag2=0; // flag1=0; key=1;
k_max=3; //最大键值(取值范围1~254) n=0; i=0; p_music=music_table1; //指针赋初值 while
(1) {
switch (p_music[i]) {
case 0x00:
i=0; //歌曲完,则停止0.5S后重新开始 timer_ms(250); //延时0.25ms timer_ms(250); break;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 简单 计算器 设计 方法
![提示](https://static.bdocx.com/images/bang_tan.gif)