Hi, Johan

For what you do at this time ... no great difference !!!

Note HPWM Always use 8 bits...

but why use 10 bits resolution to divide the result afterwards ... ( note here it's a division by 4 or two times right shifting !!! ).

Here, with 8 bits, ADC result is faster given, no need to add complementary formatting and no need to use an intermediate variable ( "pwmduty" can be replaced by "analogvalue" )
add to this your variables can be only BYTES ... so you spare lots of room !!!

Note also ADCON1.7 must be set to LEFT Justification ( "0" for BYTE result )

One habit I have is NOT to change HPWM if pwmduty doesn't change ... that saves also time for other stuff ... and response to pot change is faster.

Also always give initial values to variables ( CLEAR, if all = 0 )

so, gives at last ...


DEFINE OSC 4 ' Clock 4 Mhz
DEFINE ADC_BITS 8 ' ADC resolution 10 bit
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Sampling time 50ms

OSCCON = $60 ' Internal oscillator frequency 4 Mhz
ANSEL = %01111011 ' Analog channel 0,1,3-7 Digital channel 2,8
ADCON1 = %00000000 ' VDD & VSS as VREF

PORTA = %00000000
PORTB = %00000000
TRISA = %11111111 ' Set PORTA to all input
TRISB = %00000000 ' Set PORTB to all output



analogport VAR PORTA.0 ' Analog port pin RA.0

analogvalue VAR BYTE ' Value digital 0 - 255
oldanalogvalue VAR BYTE

CLEAR


main:
ADCIN analogport,analogvalue

IF analogvalue = oldanalogvalue THEN Jump

hpwm 1,analogvalue,7800

oldanalogvalue = analogvalue

Jump:
goto main



Hope I didn't forget anything ... LOL

Alain