void timer0_isr (void) interrupt
here is what I wrote for an almost identical purpose. it takes an input signal, adc it and use that to control the intensity of an led.
it is done in picc/hi-tide for pic but the basic idea is the same.
===========code===========
#include <htc.h>
// hardware configuration
#define Vin_a GPIO0 //analyg in on GPio0;
#define LED GPIO5 //led out on gpio5;
// end hardware configuration
#define GETBIT(var,bit) (((var)>>(bit))&1) /* read bit from var */
#define SETBIT(var,bit) ((var)|=(1<<(bit))) /* set bit from var to 1 */
#define CLRBIT(var,bit) ((var)&=(~(1<<(bit)))) /* clear bit from var (set it to 0) */
#define Vin_d_th ((long) 2500*1023/5000) //input voltage threshold to turn on the LED. from 0-1023 (10bit adc)
#define wait2 NOP();NOP()
#define wait4 wait2;wait2
#define wait8 wait4;wait4
#define adc_bits 8 //how many bits in pwm stepping
#define Length (1<<adc_bits) //total length of pwm, should be consistent with sampling bits - 10 here
__CONFIG(MCLRDIS & INTIO & WDTDIS & BORDIS);
void
main(void)
{
int Vin_d; //adc result for Vin_a
int i=0; //where we are in length?
CMCON=7; //turn off comparators;
TRISIO=0b1; //input on GPIO0, all others digital output;
ADCON0=0b10000001; //right adjusted, fosc/2, Vdd reference, AN0 analog input, adc starts right away and will always be on;
ANSEL=0b1010001; //1/16th Fosc, analog input on GPIO0;
LED=0;
while (1){
//TODO Auto-generated main function
GODONE=1; //start the adc process
while (GODONE) ; //wait for adc to complete
Vin_d=((ADRESH<<8)+(ADRESL))>>(10-adc_bits); //read both the msb and lsb.
for (i=0; i<Length; i++)
if (i<Vin_d)
LED=1;
else
LED=0;
// wait8; //wait to charge up Chold
}
}
=======end code===========
here is the schematic and output waveform.