I'm looking to generate between 50Hz to 150Hz square waves with a PIC18F14K50, which is running with an 8MHz external crystal (XT).
Apart reading an analogue value for the frequency from a pot and displaying the value on a 7-seg display, the PIC doesn't do anything except the square wave.
My options are to either use the PWM module or just write code that alternates the relevant pin between 0
and 1
. I don't necessarily mind doing the latter, except there will be a slight interruption when I periodically (maybe a couple of times per second) fetch the potentiometer value from the ADC.
I've never done PWM before so I looked it up. There is one place where you can type in your MCU's frequency and your desired frequency, and it'll give you the corresponding XC8 code. When I tried this, it seems 8MHz might actually be too fast, and I cannot produce a 50-150Hz square wave with the PWM module. My only option is to move down to 500kHz or to use the manual method.
Is this correct? Can I really not produce a 50-150Hz square wave using the PWM module if my MCU is running at 8MHz? Is there any better way to achieve this square wave?