unsigned short duty1=127; // pwm duty cycle
unsigned long temp=0;
unsigned int adcval1=0; //ADC 10 bit value
char ADCTXT[7],TEMPTXT[11]; //debug string
char DUTYTXT[5]; //debug string
void init(){
CM1CON0=0x07;
ANSEL=0x0f;
TRISC=0x00;
TRISB=0xf0;
}
void main() {
init();
UART1_Init(19200);
ADC_Init();
PWM1_Init(20000);
PWM1_Start();
PWM1_Set_Duty(duty1);
while(1){
adcval1 = ADC_Read(0); // read ADC value
delay_ms(100);
IntToStrWithZeros(adcval1, ADCTXT);
UART1_Write_Text("ADC: ");
UART1_Write_Text(ADCTXT);
UART1_Write_Text("\r\n");
temp = (adcval1 * 254)/1024; // make adc value fit short to duty cycle
duty1= temp;
LongWordToStrWithZeros(temp,TEMPTXT);
UART1_Write_Text("TEMP ");
UART1_Write_Text(TEMPTXT);
UART1_Write_Text("\r\n");
ShortToStr(duty1,DUTYTXT);
UART1_Write_Text("duty:");
UART1_Write_Text(DUTYTXT);
UART1_Write_Text("\r\n");
PWM1_Set_Duty(duty1);
delay_ms(1000);
}
}