A crude signal generator


Senior Member
The attached code (Sig gen 2) uses PWM output of an 8M2 to generate a square wave at 61Hz to 4MHz. Frequency output is altered by 2 momentary push buttons, Up and Down. There are 2 possible sets of frequency outputs - multiples of 1 ,2, 5 or multiples of 1, 1.25, 1.52, 2, 5, 3, 4, 5, 6, 8 approximately - set by #DEFINE longset. In addition to the signal output, there is a serial output for a display showing the current frequency.

The second code (Sig gen with OLED) eliminates the 8M2 and uses the 18M2 of the OLED or LCD display (LCD version is untested) for the PWM output. This raised a problem since the 18M2's PWM is on B.3 or B.6 but the whole of Port B is used to drive the display. Fortunately, there's the Alternate Pin Function which allows PWM on B.6 to be transferred to C.7. Though C.7 is normally the display's RS input but that's easily transferred to C.0 with a small witring adjustment.

I was fortunate to have in the odds and ends box a rotary switch that gives pulses on 2 contacts, depending which way it's turned. It makes choosing a frequency easy. It was previously the channel selector switch on a VHF transceiver. I'm sure there must be a name for that type of switch but I don't know what it is.

The switch inputs need 22k resistors to ground and connect to 5V when switched.