C code for generating pwm duty cycle