BitFlow

Switch piano

A scale is a list of frequencies: C 262, D 294, E 330, F 349, G 392, A 440, B 494 and the next C 523 Hz. With the switches as a number from 1 to 8, play that note; at 0, play nothing.

Each note needs its own half-period, CLK_HZ / (2 × frequency). A case statement that turns the switches into a half-period, feeding the counter from the beep lesson, is all it takes.

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

  • ○sw = 0 is silent
  • ○sw = 1–8 plays a note
  • ○Notes are C D E F G A B C, in tune (within 3%)
Hints
  1. case (sw) 4'd1: half = CLK_HZ / 524; 4'd2: half = CLK_HZ / 588; ...
  2. When the switches change, the counter may already be past the new half-period: restart it when it reaches or passes half - 1.

Pin log

Run the design to see its outputs change.

Put it on a real board

  • The Sipeed Tang Nano 9K (Gowin GW1NR-9) has no switches or a buzzer: those ports go to header pins, where you can wire them.
  • The clock is set to the board's 27 MHz, and its active-low buttons and LEDs are flipped for you.
Waveform