Precisely. Make your life easier!
Anyway, The code you have posted is 100% okay, as to make a software pwm you have to do three things
1)set up a timebase, try also to use an interrupt so jitter will be reduced
2)set up three registers: counter, period,duty.
3)inside the interrupt (or at each timebase event) do the following:
- increase counter
- if counter is greater or equal than period reset counter
- if counter is less or equal than duty out is low/high, otherwise out is high/low