/* LAB2: Addends generator for FPGA BCD adder Programming & Hardware By Epiapoq Started 29-03-2021 Modified 30-03-2021 Version 1.0 Reference: https://github.com/whatuptkhere/paralleloslam/blob/master/Paralleloslam.ino */
int firstByte = 0; // the first addend from serial port int secondByte = 0; // the second addend from serial port int sum = 0; // the sum int cb = 0; // check bit, which is used to check the number of the incomming addend
/* each addend of BCD adder consists of only 4 bits, so here just define the lower nibble of the firstByte or secondByte */ constint firstByte_0 = 4; // LSB of first addend constint firstByte_1 = 5; constint firstByte_2 = 6; constint firstByte_3 = 7; // MSB of first addend constint secondByte_0 = 8; // LSB of second addend constint secondByte_1 = 9; constint secondByte_2 = 10; constint secondByte_3 = 11; // MSB of second addend
voidsetup(){ Serial.begin(9600); // open serial port, set data rate to 9600 bps pinMode(firstByte_0, OUTPUT); pinMode(firstByte_1, OUTPUT); pinMode(firstByte_2, OUTPUT); pinMode(firstByte_3, OUTPUT); pinMode(secondByte_0, OUTPUT); pinMode(secondByte_1, OUTPUT); pinMode(secondByte_2, OUTPUT); pinMode(secondByte_3, OUTPUT); }
voidloop(){ if (Serial.available() > 0 && (cb == 0)) { // read the first addend firstByte = Serial.read() - 48; /* serial in from monitor and parallel out to FPGA */ digitalWrite(firstByte_0, firstByte & 0x01); // send LSB of first addend digitalWrite(firstByte_1, firstByte & 0x02); digitalWrite(firstByte_2, firstByte & 0x04); digitalWrite(firstByte_3, firstByte & 0x08); Serial.print(firstByte); Serial.print('+'); cb = 1; } elseif ((Serial.available() > 0) && (cb == 1)) { // read the second addend secondByte = Serial.read() - 48;
/* serial in from monitor and parallel out to FPGA */ digitalWrite(secondByte_0, secondByte & 0x01); digitalWrite(secondByte_1, secondByte & 0x02); digitalWrite(secondByte_2, secondByte & 0x04); digitalWrite(secondByte_3, secondByte & 0x08); Serial.print(secondByte); Serial.print('='); sum = firstByte + secondByte; Serial.print(sum); Serial.println('\n'); cb = 0; } }