I do not know about your code, but something else is wrong based on the oscilloscope view.
When the PWM is not happening the sine wave looks good.
When the PWM is working the sine is not as high. Which is what I would expect from PWM, reducing the amplitude based on the percentage on when compared to the time of the off and on cycle.
It looks like you are starting with a sine wave and trying to modify it.
Normally someone starts with a constant voltage and uses PWM to make it look like a sine wave with a non-zero crossing.