On Fri, 2 Sep 2011 11:05:44 -0700 (PDT), xavierpereira wrote: :: As the Duty Cycle will be the Function of the Input Volatge and :: voltage :: reference(ie 12 V ). :: I have to set this Duty Cycle Value to the PWM pin.. I need a :: formula or some hints to write this algorithim OK, let's think about this. What voltage is going to be at the input pin of the ADC? I'm assuming the max voltage is 3v3 or is a DSPIC a 5v part?=20 Once you've got that and you know what the maximum count is and minimum=20 assuming you are referencing the ADC as 0 -> Max Vdd - all you then have to= =20 do is work out what voltage points you are going to make valid, that is are= =20 you only responding to per volt or per half volt etc, once you've decided=20 that all you need to do is map those counts that equal the voltage to PWM=20 values. You could use a look up table, but you could also have a either an IF ...=20 Else... code block or if many values perhaps a Case block which would be=20 something like: Case: nought_counts to count_equals_1v;=20 duty_cycle =3D some_value;=20 sub_change_duty(); =20 break; repeat for other voltage counts. You haven't mentioned what the Mosfet will be switching, so how you decide= =20 how to allocate the duty cycle to the voltage input will have a great=20 bearing on what ADC count you assign to what duty cycle value. Hoping this won't confuse you, but here is some code for an 8 bit device=20 that increments the duty cycle of a PWM as a soft start to drive a fan=20 motor derived from a humidity reading. void trip_check(trips *pTrip) { if (rh_adj < pTrip->humidity ) { //if current humidity < trip humidity switch on with soft start CCP1CON=3D0x2C; //PWM module1 on, output pin RC2 =09 if (++duty1 > 30) { duty1=3D31; } CCPR1L=3Dduty1; } else { if(!(duty1 =3D=3D 0)) duty1--; CCPR1L=3Dduty1; #asm movf duty1,W btfsc STATUS,Z ;Is duty zero? movwf CCP1CON ;Yes so turn CCP1 off=09 #asmend Ignore the fact that part of it is in assembler that is purely because the= =20 compiler produces more assembler code than I like. Note that (if I recall=20 correctly) 31 is about the maximum figure that CCPR1L can take for the=20 16F87x series, you'll need to check your datasheet for your dsPic to see=20 what the maximum number is. There is a code example for the dsPIC30F4012 which is for driving a=20 brushless motor, it takes the ADC value (as a speed control) and applies=20 that as a duty cycle to change the speed of the motor. http://ww1.microchip.com/downloads/en/DeviceDoc/CE003_Sinusoidal_BLDC_01090 8.zip Strip away the bits you don't want and the general idea if what you need to= =20 do is there. Colin -- cdb, colin@btech-online.co.uk on 3/09/2011 =20 Web presence: www.btech-online.co.uk =20 =20 Hosted by: www.justhost.com.au =20 =20 This email is to be considered private if addressed to a named individual= =20 or HR department, and public if addressed to a blog, forum or news=20 article. =20 =20 =20 --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .