Making Square Waves with RISC-V
A function generator is a handy piece of equipment to have around an electronics workbench, but you don’t necessarily need a fancy benchtop instrument for your hobby projects. In fact, as electronics hobbyist Alun Morris recently demonstrated, you can build a very capable square wave generator around an inexpensive microcontroller. Called Mega Hurts, the pocket-sized device can generate signals at frequencies ranging from 1 Hz to 30 MHz with an adjustable duty cycle, and it has a simple user interface that makes dialing in just the right signal a breeze.
At the core of the device is a CH32V203F6P6 microcontroller. This RISC-V chip comes equipped with 32 KB of flash memory and 10 KB of RAM, which is more than sufficient for the task at hand. And since the square wave is generated entirely in hardware with the help of one of the microcontroller’s 16-bit timers, the processor is free to tend to other matters, like updating the user interface.
A 128x64-pixel OLED display and a rotary encoder with an integrated push button round out the user interface. These components, along with a USB-C port and an optional BNC connector for the signal output, can be housed in a small 3D-printed enclosure.
While the microcontroller is capable of running at 144 MHz, Morris opted to use a 120 MHz timer clock to make it possible to generate common frequencies like 5 MHz and 10 MHz exactly relative to the clock source. There is a trade-off between frequency and duty cycle resolution, however. At 30 MHz, for instance, there are only four timer counts to work with per period, so the duty cycle can only be set to 25, 50, or 75 percent. The slew rate of the microcontroller’s output also leaves something to be desired at these higher frequencies.
Morris, with an assist from Claude, developed firmware that makes it easy to dial in the desired signal characteristics. The main menu presents eight frequency bands to get users in the right ballpark quickly. From there, rotating the encoder adjus




Comments
No comments yet. Be the first to share your thoughts.