// Pins connected to the segments of the two 7-segment displays const byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3}; // Pins connected to the common cathodes of the two 7-segment displays const byte cathodePins[] = {A4, A5}; // Array of 16 binary numbers representing the bit patterns for each hex digit const byte segments[] = { B11111100, // 0 B01100000, // 1 B11011010, // 2 B11110010, // 3 B01100110, // 4 B10110110, // 5 B10111110, // 6 B11100000, // 7 B11111110, // 8 B11110110, // 9 B11101110, // A B00111110, // b B10011100, // C B01111010, // d B10011110, // E B10001110 // F }; // Variables to hold the two binary numbers and the results of bitwise operations byte num1 = B00111100; byte num2 = B00001101; byte resultAnd, resultOr, resultXor, resultNot, resultShiftLeft, resultShiftRight; // Variables to control the timing of the display unsigned long previousMillis = 0; const unsigned long displayInterval = 1000; // display the results for 1 second const unsigned long blinkInterval = 200; // blink off for 200 ms after displaying void setup() { // Set the pins for the segments as output for (byte i = 0; i < sizeof(segmentPins); i++) { pinMode(segmentPins[i], OUTPUT); } // Set the pins for the common cathodes as output for (byte i = 0; i < sizeof(cathodePins); i++) { pinMode(cathodePins[i], OUTPUT); } // Initialize the serial communication for debugging Serial.begin(9600); } void loop() { // Calculate the bitwise results resultAnd = num1 & num2; resultOr = num1 | num2; resultXor = num1 ^ num2; resultNot = ~num1; resultShiftLeft = num1 << 2; resultShiftRight = num1 >> 2; // Display the results on the 7-segment displays unsigned long currentMillis = millis(); if (currentMillis - previousMillis < displayInterval) { displayHex(resultAnd, 0); displayHex(resultOr, 1); displayHex(resultXor, 0); displayHex(resultNot, 1); displayHex(resultShiftLeft, 0); displayHex(resultShiftRight, 1); } else if (currentMillis - previousMillis < displayInterval + blinkInterval) { // Blink off the displays digitalWrite(cathodePins[0], HIGH); digitalWrite(cathodePins[1], HIGH); } else { // Reset the timing and start again previousMillis = currentMillis; } // Print the results to the serial monitor for debugging Serial.print(num1, BIN); Serial.print(" "); Serial.print(operatorSymbol); Serial.print(" "); Serial.print(num2, BIN); Serial.print(" = "); Serial.print(result, HEX); Serial.println(); // Convert the hex result to a string for display on the seven-segment displays char hexString[3]; sprintf(hexString, "%02X", result); // Display the result on the seven-segment displays displayHexNumber(hexString); } void displayHexNumber(char hexString[]) { // Loop through each digit of the hex number for (int digit = 0; digit < 2; digit++) { // Set the cathode pins low to turn on the desired digit digitalWrite(cathodePins[digit], LOW);// Loop through each segment of the seven-segment display for (int segment = 0; segment < 7; segment++) { // Determine the bit value for the current segment byte segmentValue = bitRead(segments[hexString[digit] - '0'], segment); // Set the segment pin high or low depending on the bit value digitalWrite(segmentPins[segment], segmentValue); } // Delay briefly to allow the segment values to stabilize delayMicroseconds(segmentDelay); // Turn off all segment pins to prepare for the next digit for (int segment = 0; segment < 7; segment++) { digitalWrite(segmentPins[segment], LOW); } // Set the cathode pin high to turn off the current digit digitalWrite(cathodePins[digit], HIGH); }} create the arduino circuit of this code
// Pins connected to the segments of the two 7-segment displays
const byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3};
// Pins connected to the common cathodes of the two 7-segment displays
const byte cathodePins[] = {A4, A5};
// Array of 16 binary numbers representing the bit patterns for each hex digit
const byte segments[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
B11101110, // A
B00111110, // b
B10011100, // C
B01111010, // d
B10011110, // E
B10001110 // F
};
// Variables to hold the two binary numbers and the results of bitwise operations
byte num1 = B00111100;
byte num2 = B00001101;
byte resultAnd, resultOr, resultXor, resultNot, resultShiftLeft, resultShiftRight;
// Variables to control the timing of the display
unsigned long previousMillis = 0;
const unsigned long displayInterval = 1000; // display the results for 1 second
const unsigned long blinkInterval = 200; // blink off for 200 ms after displaying
void setup() {
// Set the pins for the segments as output
for (byte i = 0; i < sizeof(segmentPins); i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Set the pins for the common cathodes as output
for (byte i = 0; i < sizeof(cathodePins); i++) {
pinMode(cathodePins[i], OUTPUT);
}
// Initialize the serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Calculate the bitwise results
resultAnd = num1 & num2;
resultOr = num1 | num2;
resultXor = num1 ^ num2;
resultNot = ~num1;
resultShiftLeft = num1 << 2;
resultShiftRight = num1 >> 2;
// Display the results on the 7-segment displays
unsigned long currentMillis = millis();
if (currentMillis - previousMillis < displayInterval) {
displayHex(resultAnd, 0);
displayHex(resultOr, 1);
displayHex(resultXor, 0);
displayHex(resultNot, 1);
displayHex(resultShiftLeft, 0);
displayHex(resultShiftRight, 1);
} else if (currentMillis - previousMillis < displayInterval + blinkInterval) {
// Blink off the displays
digitalWrite(cathodePins[0], HIGH);
digitalWrite(cathodePins[1], HIGH);
} else {
// Reset the timing and start again
previousMillis = currentMillis;
}
// Print the results to the serial monitor for debugging
Serial.print(num1, BIN);
Serial.print(" ");
Serial.print(operatorSymbol);
Serial.print(" ");
Serial.print(num2, BIN);
Serial.print(" = ");
Serial.print(result, HEX);
Serial.println();
// Convert the hex result to a string for display on the seven-segment displays
char hexString[3];
sprintf(hexString, "%02X", result);
// Display the result on the seven-segment displays
displayHexNumber(hexString);
}
void displayHexNumber(char hexString[]) {
// Loop through each digit of the hex number
for (int digit = 0; digit < 2; digit++) {
// Set the cathode pins low to turn on the desired digit
digitalWrite(cathodePins[digit], LOW);// Loop through each segment of the seven-segment display
for (int segment = 0; segment < 7; segment++) {
// Determine the bit value for the current segment
byte segmentValue = bitRead(segments[hexString[digit] - '0'], segment);
// Set the segment pin high or low depending on the bit value
digitalWrite(segmentPins[segment], segmentValue);
}
// Delay briefly to allow the segment values to stabilize
delayMicroseconds(segmentDelay);
// Turn off all segment pins to prepare for the next digit
for (int segment = 0; segment < 7; segment++) {
digitalWrite(segmentPins[segment], LOW);
}
// Set the cathode pin high to turn off the current digit
digitalWrite(cathodePins[digit], HIGH);
}}
create the arduino circuit of this code.
Step by step
Solved in 3 steps with 2 images