Serial calculator
Put receiving and sending together. The board reads a sum like 3+4 followed by a new line, adds the two digits and sends the answer back as text with \r\n.
The answer can be one digit or two (9+9 is 18), so send the tens digit only when there is one. A small state machine works well: wait for the first digit, the plus, the second digit, the new line, then send.
lab.v
drag to turn · scroll to zoom
Loading the board…
Press Run on the board: your Verilog is turned into gates in this browser and runs here, cycle by cycle.
Goals
- ○One-digit answers are right (3+4 → 7)
- ○Two-digit answers are right (9+8 → 17)
- ○Each answer ends with \r\n
Hints
- Store
aandbas numbers (byte_in - "0") and computesum = a + b(0–18). - To send it: tens is
sum >= 10, ones issum - (tens ? 10 : 0), and each digit goes out as"0" + digit. - Send one character at a time: pulse
sendwhen!busy, as in Say hello.
Pin log
Run the design to see its outputs change.
Serial terminal (type to send on rx)
Put it on a real board
- The clock is set to the board's 27 MHz, and its active-low buttons and LEDs are flipped for you.
Check on my board
Flash this lesson, then connect at 115200 baud. Your browser asks which port: on a Tang Nano, try the higher-numbered one if nothing comes back.