If the frequency needs to be accurate you would be much better off using a timer interrupt.