You could always set up a timer interrupt to output the frequency.

i.e. for 40Hz, set a timer to the correct value so it will overflow in 12.5mS. When it overflows, that triggers the interrupt, and get the interrupt to toggle a digital output and then subtract 12.5mS from the timer value. This will ensure a pretty accurate 40Hz.