keil写简易计算器代码
时间: 2023-07-10 16:12:41 浏览: 276
以下是使用 Keil 编写的简易计算器代码示例:
```c
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1 = P1 ^ 0;
sbit key2 = P1 ^ 1;
sbit key3 = P1 ^ 2;
sbit key4 = P1 ^ 3;
sbit key5 = P1 ^ 4;
sbit key6 = P1 ^ 5;
sbit key7 = P1 ^ 6;
sbit key8 = P1 ^ 7;
uchar code table[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
void delay(uint i) // 延迟函数
{
while (i--);
}
void display(uchar num) // 数码管显示函数
{
P0 = table[num];
}
void main()
{
uchar num1 = 0, num2 = 0, op = 0, result = 0;
while (1) {
// 检测按键
if (key1 == 0) {
delay(1000);
if (key1 == 0) {
num1 = num1 * 10 + 1;
display(num1);
}
while (!key1);
}
if (key2 == 0) {
delay(1000);
if (key2 == 0) {
num1 = num1 * 10 + 2;
display(num1);
}
while (!key2);
}
if (key3 == 0) {
delay(1000);
if (key3 == 0) {
num1 = num1 * 10 + 3;
display(num1);
}
while (!key3);
}
if (key4 == 0) {
delay(1000);
if (key4 == 0) {
op = '+';
num2 = num1;
num1 = 0;
display(op);
}
while (!key4);
}
if (key5 == 0) {
delay(1000);
if (key5 == 0) {
num1 = num1 * 10 + 4;
display(num1);
}
while (!key5);
}
if (key6 == 0) {
delay(1000);
if (key6 == 0) {
num1 = num1 * 10 + 5;
display(num1);
}
while (!key6);
}
if (key7 == 0) {
delay(1000);
if (key7 == 0) {
num1 = num1 * 10 + 6;
display(num1);
}
while (!key7);
}
if (key8 == 0) {
delay(1000);
if (key8 == 0) {
result = num1 + num2;
display(result);
}
while (!key8);
}
}
}
```
这个代码示例使用了单片机上的按键和数码管,实现了简单的加法运算。具体实现过程请参考代码注释。
阅读全文