BitFlow

Drive a servo

A hobby servo reads its angle from a pulse sent 50 times a second: 1 ms high means one end, 2 ms the other, 1.5 ms the middle. Between pulses the line is low.

Make servo pulse every 20 ms, high for 1 ms plus sw / 15 of a millisecond, so the switches (0–15) sweep the arm from 0° to 180°. On a real board, wire the servo's signal to the pin servo lands on.

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

  • ○A pulse every 20 ms
  • ○sw = 0 gives 1 ms, sw = 15 gives 2 ms, in between in steps
Hints
  1. A counter that restarts every CLK_HZ / 50 cycles.
  2. High while the counter is below CLK_HZ / 1000 + sw * (CLK_HZ / 15000).

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 servo: 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