Why the 20mhz requirement? Your loop simply contains a square wave generator, which can produce the same output at 40mhz that it would produce at 20mhz.
Or, use the 40mhz clock, and put a case structure inside the while loop. In the true case put your 20mhz code, leave the false case blank. Flip the value of a boolean each loop cycle, and wire it to the case selector so the code inside only executes every other loop iteration.
Thank you so much for your quick response. I'm very grateful to you.
I need to generate some pulses as a part of my project. With 40MHz the rise time exceeded beyond the limit of the desired one. Another issue is that It is generating triangle waveform instead of square.
Any more suggestion, please.
Why would running the loop at 20mhz instead of 40mhz fix those problems? It sounds like you need to fix the circuit you're driving so it's properly matched to the board outputs. Have you tried disconnecting your circuit and looking at the raw outputs with a scope?