implementing PWM in C to improve performance