//// This program will show how to use the built in PIC PWM. ////
//// The program takes an analog input and uses the digital ////
//// value to set the duty cycle. The frequency is set by ////
//// the user over the RS-232. ////
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=12000000)
#use rs232(baud=9600, xmit=PIN_B0) // Jumpers: 8 to 11, 7 to 12
void main() {
char selection;
byte value;
selection = '1';
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
switch(selection) {
case '1' : setup_timer_2(T2_DIV_BY_1, 127, 1);
break;
case '2' : setup_timer_2(T2_DIV_BY_4, 127, 1);
break;
case '3' : setup_timer_2(T2_DIV_BY_16, 127, 1);
break;
}
setup_port_a(ALL_ANALOG);
setup_adc(adc_clock_internal);
set_adc_channel( 0 );
delay_ms(500);
while( TRUE ) {
value=read_adc();
printf("%d ",value);
set_pwm1_duty(value);
}
}