利用74LS138、与非门、74LS47和共阳极数码管(或74LS48和共阴极数码管)设计1位全加器。 要求 、 分别表示两个加数, 表示低位的进位, 和 分别表示和位以及向高位的进位,要求利用数码管显示输出结果。例如当输入 、 、 时,数码管应显示为3。
时间: 2023-05-28 18:02:56 浏览: 1298
实验五全加器的设计与应用.pdf
5星 · 资源好评率100%
首先,根据全加器的逻辑电路图,我们可以得到以下真值表:
| A | B | Cin | Cout | S |
| :---: | :---: | :-: | :--: | :---: |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 |
根据真值表,我们可以得到以下逻辑式:
S = A xor B xor Cin
Cout = (A and B) or (Cin and (A xor B))
接下来,我们使用74LS138实现选择器,将输入的A、B、Cin和输出的S、Cout分别选择到不同的与非门中进行计算。具体电路如下图所示:
![1位全加器电路图](https://cdn.luogu.com.cn/upload/image_hosting/3q3q6jtt.png)
其中,74LS138的输入端A0、A1、A2分别连接A、B、Cin,输出端Y0、Y1、Y2、Y3分别连接与非门的输入端,74LS138的使能端G1、G2A、G2B均连接高电平,74LS47的输入端a、b、c、d、e、f、g分别连接与非门的输出端(注意,74LS47的输入端需要接入反向电阻,否则显示的数字会倒过来),74LS47的输出端a、b、c、d、e、f、g分别连接共阳极数码管的a、b、c、d、e、f、g。如果使用74LS48和共阴极数码管,则需要将74LS47改为74LS48,并将共阳极数码管改为共阴极数码管。
最后,我们可以使用以下代码在Arduino上实现该电路的功能:
```c++
const int A_pin = 2;
const int B_pin = 3;
const int Cin_pin = 4;
const int S_pin = 5;
const int Cout_pin = 6;
const int a_pin = 7;
const int b_pin = 8;
const int c_pin = 9;
const int d_pin = 10;
const int e_pin = 11;
const int f_pin = 12;
const int g_pin = 13;
void setup() {
pinMode(A_pin, INPUT);
pinMode(B_pin, INPUT);
pinMode(Cin_pin, INPUT);
pinMode(S_pin, OUTPUT);
pinMode(Cout_pin, OUTPUT);
pinMode(a_pin, OUTPUT);
pinMode(b_pin, OUTPUT);
pinMode(c_pin, OUTPUT);
pinMode(d_pin, OUTPUT);
pinMode(e_pin, OUTPUT);
pinMode(f_pin, OUTPUT);
pinMode(g_pin, OUTPUT);
}
void loop() {
int A = digitalRead(A_pin);
int B = digitalRead(B_pin);
int Cin = digitalRead(Cin_pin);
int S = (A ^ B ^ Cin);
int Cout = ((A & B) | (Cin & (A ^ B)));
digitalWrite(S_pin, S);
digitalWrite(Cout_pin, Cout);
if (S == 0b00000001) {
digitalWrite(a_pin, LOW);
digitalWrite(b_pin, HIGH);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, LOW);
digitalWrite(e_pin, LOW);
digitalWrite(f_pin, LOW);
digitalWrite(g_pin, LOW);
} else if (S == 0b00000010) {
digitalWrite(a_pin, HIGH);
digitalWrite(b_pin, HIGH);
digitalWrite(c_pin, LOW);
digitalWrite(d_pin, HIGH);
digitalWrite(e_pin, HIGH);
digitalWrite(f_pin, LOW);
digitalWrite(g_pin, HIGH);
} else if (S == 0b00000011) {
digitalWrite(a_pin, HIGH);
digitalWrite(b_pin, HIGH);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, HIGH);
digitalWrite(e_pin, LOW);
digitalWrite(f_pin, LOW);
digitalWrite(g_pin, HIGH);
} else if (S == 0b00000100) {
digitalWrite(a_pin, LOW);
digitalWrite(b_pin, HIGH);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, LOW);
digitalWrite(e_pin, LOW);
digitalWrite(f_pin, HIGH);
digitalWrite(g_pin, LOW);
} else if (S == 0b00000101) {
digitalWrite(a_pin, HIGH);
digitalWrite(b_pin, LOW);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, HIGH);
digitalWrite(e_pin, LOW);
digitalWrite(f_pin, HIGH);
digitalWrite(g_pin, HIGH);
} else if (S == 0b00000110) {
digitalWrite(a_pin, HIGH);
digitalWrite(b_pin, LOW);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, HIGH);
digitalWrite(e_pin, HIGH);
digitalWrite(f_pin, HIGH);
digitalWrite(g_pin, HIGH);
} else if (S == 0b00000111) {
digitalWrite(a_pin, HIGH);
digitalWrite(b_pin, HIGH);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, LOW);
digitalWrite(e_pin, LOW);
digitalWrite(f_pin, LOW);
digitalWrite(g_pin, LOW);
} else if (S == 0b00001000) {
digitalWrite(a_pin, HIGH);
digitalWrite(b_pin, HIGH);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, HIGH);
digitalWrite(e_pin, HIGH);
digitalWrite(f_pin, HIGH);
digitalWrite(g_pin, HIGH);
} else {
digitalWrite(a_pin, HIGH);
digitalWrite(b_pin, HIGH);
digitalWrite(c_pin, HIGH);
digitalWrite(d_pin, HIGH);
digitalWrite(e_pin, HIGH);
digitalWrite(f_pin, HIGH);
digitalWrite(g_pin, LOW);
}
}
```
阅读全文