program pwm_test_me2;
// program uses AN3 for Vref+ and AN2 for Vref- and AN0 for analog input
// and RC2 as pwm out on a 16F877A
// if you want to just connect AN3 to +5v and AN2 to ground it also will work
// but i think it is more usefull to have the option of using different references.
// because your maximum voltage that you may want to measure might only be +0.6V.
var temp_res : word;
result : byte;
begin
TRISA := $FF; // PORTA is input
TRISC := $FB; // Initialize PORTC
ADCON0 := $80;
ADCON1 := $8F; // Configure analog inputs and Vref
Pwm_Init(19000); // Initialize PWM module, freq = 19kHz.
Pwm_Start; // Start PWM
while true do
begin
temp_res := Adc_Read(0); // put 10bit adc output into 16 bit word
result := word(temp_res shr 2); // shift 10bits right and store the eight bits into result
Pwm_Change_Duty(result); // Change duty ratio
end;
end.