Creating a buck converter and need to monitor a current sence voltage and change a pwm duty cycle based on the current sence ADC result. The target output current is user adjustable by a pot and a max current is set by a pot that is internal to the product and set at the factory. These pots are in series so only 1 input is needed. This is protection for a short at the output or the wrong load.

So there are 2 inputs to the pic, the feedback from the current sence resistor and the voltage accross a potential divider for current set. I can set the hardware so that the current sence count and the current limit count are the same value when a given current target is met.

Also, looking at the pbp pro manual I see a max HPWM of 32.767kHz yet the datasheet lists over 200kHz. Will the HPWM command actually allow a frequency of 200kHz? This is on a 18F1320.

Here is what I think will work:

Code:
OSCCON = $70
define osc 8
Define  ADC_BITS        10     	' Set number of bits in result
Define  ADC_CLOCK       3     	' Set clock source (3=rc)
Define  ADC_SAMPLEUS    10    	' Set sampling time in uS

CSET   VAR WORD 'CURRENT SET
CSEN   VAR WORD 'CURRENT SENCE  
DUTY   VAR wORD 'HPWM DUTY CYCLE

ADCON0 = %00011001
ADCON1 = %00001111
ADCON2 = %10111111
TRISA = %00000000
TRISB = %00110011

DUTY = 800 'SET INITIAL OUTPUT CURRENT HIGH TO ENABLE FAST STARTUP 
CSET = 0
CSEN = 0

LOOP:
ADCIN 6, CSET 'READ VALUE OF CURRENT SET POT
ADCIN 4, CSEN   'READ VALUE OF CURRENT SENCE
IF CSEN > CSET Then LET DUTY = DUTY - 1
IF CSEN < CSET THEN LET DUTY = DUTY + 1
HPwm 1,DUTY,32767 'OUTPUTS THE DUTYCYCLE AT 32.767KhZ
GOTO LOOP