基于51单片机+按键键盘+lcd1602显示+蜂鸣器设计的智能计算器软件源码
时间: 2023-12-03 22:00:24 浏览: 174
基于51单片机+按键键盘+LCD1602显示+蜂鸣器设计的智能计算器软件源码.rar
5星 · 资源好评率100%
智能计算器软件源码:
```c
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit k0 = P3^2;
sbit k1 = P3^3;
sbit k2 = P3^4;
sbit k3 = P3^5;
sbit beep = P2^3;
uchar code table[] = {
'0','1','2','3','+',
'4','5','6','-', 0,
'7','8','9','*', 0,
'.','=','C','/', 0
};
void delay(uint z){
uint x,y;
for(x=z;x>0;x--){
for(y=110;y>0;y--);
}
}
void key_scan(){
uchar temp,i;
P0 = 0xf0;
temp = P0;
temp &= 0xf0;
if(temp != 0xf0){
delay(5);
temp = P0;
temp &= 0xf0;
if(temp != 0xf0){
temp >>= 4;
switch(temp){
case 0x0e:
P2 = 0xfe;
for(i=0;i<20;i++){
if(!k0) break;
delay(10);
}
if(i == 20){
beep = 0;
}
break;
case 0x0d:
P2 = 0xfd;
for(i=0;i<20;i++){
if(!k1) break;
delay(10);
}
if(i == 20){
beep = 0;
}
break;
case 0x0b:
P2 = 0xfb;
for(i=0;i<20;i++){
if(!k2) break;
delay(10);
}
if(i == 20){
beep = 0;
}
break;
case 0x07:
P2 = 0xf7;
for(i=0;i<20;i++){
if(!k3) break;
delay(10);
}
if(i == 20){
beep = 0;
}
break;
default:
break;
}
P2 = 0xff;
}
}
}
void write_com(uchar com){
P2 = 0;
P0 = com;
_nop_();
P2 = 0x40;
delay(2);
P2 = 0;
}
void write_data(uchar date){
P2 = 0x80;
P0 = date;
_nop_();
P2 = 0xc0;
delay(2);
P2 = 0;
}
void init(){
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void main(){
uchar n=0,s=0,c=0;
init();
while(1){
key_scan();
if(!k0){
beep = 0;
write_data(table[n]);
delay(500);
beep = 1;
n++;
n %= 20;
}
if(!k1){
beep = 0;
write_data(table[s]);
delay(500);
beep = 1;
s++;
s %= 20;
}
if(!k2){
beep = 0;
write_com(0x18);
c++;
if(c > 15){
c = 0;
write_com(0x18);
}
beep = 1;
}
if(!k3){
beep = 0;
write_com(0x01);
n = 0;
s = 0;
c = 0;
beep = 1;
}
}
}
```
这是基于51单片机,使用按键键盘和LCD1602显示屏设计的智能计算器软件源码。其中使用了4个按键进行控制,按键K0~K3分别对应数字和运算符的输入、切换显示内容、清空屏幕和清零操作。源码实现通过按键输入数字和运算符,将输入内容显示在LCD1602上。同时还实现了蜂鸣器功能,以便在按下按键时发出提示音。
阅读全文