用51单片机实现一个简单计算器的功能
发布网友
发布时间:2022-04-29 09:10
我来回答
共2个回答
热心网友
时间:2022-06-25 06:38
下面是我以前用来显示9999秒倒计时的原程序,仅供学习,版权所有,侵权不究。 #include <REGX51.H> #include <./stdint.h> #include <ABSACC.H> #include<AT89X51.H> #include <INTRINS.H> //------------------------------ #define TH_10MS (65536-50000)/256 #define TL_10MS (65536-50000)%256 //------------------------------------------ #define led12 XBYTE[0xf7ff] //led1与led2数码管 #define led34 XBYTE[0xefff] //led3与led4数码管 #define led_light P3_7 #define keyst P1_0 #define keyclk P1_1 volatile struct _clock { uint8_t t_s;//秒 uint8_t t_ms;//分秒 uint8_t led_lightf : 1;//中间灯 uint8_t keyf_st : 1; //开停键标志 uint8_t keyf_clk : 1; //清0键标志 uint8_t clockf_st : 1; //开始计时标志 #define STOP 0 #define START 1 uint8_t : 0; }clock; //-----------------delay----------------------------- void delay_10ms() { uint8_t i, j; for(i=0; i<100; i++) for(j=0; j<100; j++); } //-------------------10进制显示----------------------- void display_val(uint8_t data_l, uint8_t data_r) { uint8_t led_tmp12, led_tmp34; //取右边分秒 led_tmp12 = (data_r/10)%10; led_tmp12 <<=4; led_tmp12 |= (uint8_t)(data_r%10); //取左边秒 led_tmp34 = (data_l/10)%10; led_tmp34 <<=4; led_tmp34 |= data_l%10; led12 = led_tmp12;//送显示 led34 = led_tmp34; } //------------------------初始化-------------- void clr_data() { clock.t_s = 99; clock.t_ms = 99; clock.led_lightf = 0; clock.keyf_st = 0; clock.keyf_clk = 0; clock.clockf_st = 0; TR0 = 0; //关定时器 display_val(99,99); led_light = 0; } void init_sys() { clr_data(); //定时器0设置 TMOD = 0x01; //定时器0设为MAOD1 TCON = 0x00; //清标志位 TH0 = TH_10MS; TL0 = TL_10MS; //TR0 = 1; //开定时器 //IE = 0x8a; //开定时中断0,1与总开关 EA = 1; ET0 = 1; } //--------------------键盘扫描----------------- void key_scan() { bit keyf; keyf = keyst & keyclk; if(keyf) goto error; delay_10ms(); keyf = keyst & keyclk; if(keyf) goto error; clock.keyf_st = !keyst; clock.keyf_clk = !keyclk; do { keyf = keyst & keyclk; } while(!keyf); error: return ; } //--------------------键盘处理------------------- void key_deal() { if(clock.keyf_st) { if(clock.clockf_st == STOP) TR0 = 1; else TR0 = 0; clock.clockf_st = ~clock.clockf_st; clock.keyf_st = 0; } if(clock.keyf_clk) { clr_data(); clock.keyf_clk = 0; } } // -------------------main------------------------ void main() { init_sys(); while(1) { key_scan();//键盘扫描 key_deal();//键盘处理 } } //----------------------定时中断0----------------- void t0_interrupt() interrupt 1 { static t0_count = 0 , t_1s = 0; TR0 = 0; t_1s++; if(t_1s==20) { t_1s=0; clock.t_ms--; if(clock.t_ms == -1) clock.t_ms = 99; t0_count++; if(t0_count == 100) { t0_count = 0; clock.t_s--; if(clock.t_s == -1) clock.t_s = 99; } display_val(clock.t_s, clock.t_ms); clock.led_lightf = ~clock.led_lightf; led_light = clock.led_lightf; } TH0 = TH_10MS; TL0 = TL_10MS; TR0 = 1; }
热心网友
时间:2022-06-25 06:39
给你一个参考程序,这个是可行的哦,你看看这个程序
#include <</font>reg51.h>
#include <</font>intrins.h>
#include <</font>ctype.h>
#include <</font>stdlib.h>
#define uchar unsigned char
#define uint unsigned int
uchar operand1[9], operand2[9];
uchar operator;
void delay(uint);
uchar keyscan();
void disp(void);
void buf(uint value);
uint compute(uint va1,uint va2,uchar optor);
uchar code table[] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xff};
uchar dbuf[8] = {10,10,10,10,10,10,10,10};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan()
{
uchar skey;
P1 = 0xfe;
while((P1 & 0xf0) != 0xf0)
{
delay(3);
while((P1 & 0xf0) != 0xf0)
{
switch(P1)
{
case 0xee: skey = '7'; break;
case 0xde: skey = '8'; break;
case 0xbe: skey = '9'; break;
case 0x7e: skey = '/'; break;
default: skey = '#';
}
while((P1 & 0xf0) != 0xf0)
;
}
}
P1 = 0xfd;
while((P1 & 0xf0) != 0xf0)
{
delay(3);
while((P1 & 0xf0) != 0xf0)
{
switch(P1)
{
case 0xed: skey = '4'; break;
case 0xdd: skey = '5'; break;
case 0xbd: skey = '6'; break;
case 0x7d: skey = '*'; break;
default: skey = '#';
}
while((P1 & 0xf0) != 0xf0)
;
}
}
P1 = 0xfb;
while((P1 & 0xf0) != 0xf0)
{
delay(3);
while((P1 & 0xf0) != 0xf0)
{
switch(P1)
{
case 0xeb: skey = '1'; break;
case 0xdb: skey = '2'; break;
case 0xbb: skey = '3'; break;
case 0x7b: skey = '-'; break;
default: skey = '#';
}
while((P1 & 0xf0) != 0xf0)
;
}
}
P1 = 0xf7;
while((P1 & 0xf0) != 0xf0)
{
delay(3);
while((P1 & 0xf0) != 0xf0)
{
switch(P1)
{
case 0xe7: skey = '$'; break;
case 0xd7: skey = '0'; break;
case 0xb7: skey = '='; break;
case 0x77: skey = '+'; break;
default: skey = '#';
}
while((P1 & 0xf0) != 0xf0)
;
}
}
return skey;
}
void main()
{
uint value1, value2, value;
uchar ckey, cut1 = 0, cut2 = 0;
uchar operator;
uchar i, bool = 0;
init:
buf(0);
disp();
value = 0;
cut1 = cut2 = 0;
bool = 0;
for(i = 0;i <</font> 9;i++)
{
operand1[i] = '\0';
operand2[i] = '\0';
}
while(1)
{
ckey = keyscan();
if(ckey != '#')
{
if(isdigit(ckey))
{
switch(bool)
{
case 0:
operand1[cut1] = ckey;
operand1[cut1+1] = '\0';
value1 = atoi(operand1);
cut1++;
buf(value1);
disp();
break;
case 1:
operand2[cut2] = ckey;
operand2[cut2+1] = '\0';
value2 = atoi(operand2);
cut2++;
buf(value2);
disp();
break;
default: break;
}
}
else if(ckey=='+'||ckey=='-'||ckey=='*'||ckey=='/')
{
bool = 1;
operator = ckey;
buf(0);
dbuf[7] = 10;
disp();
}
else if(ckey == '=')
{
value = compute(value1,value2,operator);
buf(value);
disp();
while(1)
{
ckey = keyscan();
if(ckey == '$')
goto init;
else
{
buf(value);
disp();
}
}
}
else if(ckey == '$')
{ goto init;}
}
disp();
}
}
uint compute(uint va1,uint va2,uchar optor)
{
uint value;
switch(optor)
{
case '+' : value = va1+va2; break;
case '-' : value = va1-va2; break;
case '*' : value = va1*va2; break;
case '/' : value = va1/va2; break;
default : break;
}
return value;
}
void buf(uint val)
{
uchar i;
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;
}
void disp(void)
{
uchar bsel, n;
bsel=0x01;
for(n=0;n<</font>8;n++)
{
P2=bsel;
P0=table[dbuf[n]];
bsel=_crol_(bsel,1);
delay(3);
P0=0xff;
}
}