EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 80
PIC Peripherals in Practice: ADC and PWM
Configuring modules through registers.
Each peripheral on a PIC has control registers: one to turn it on, one to choose the clock, and others for settings. The pattern is always select the pin function, configure, enable, then start.
For ADC pick the input channel, reference and acquisition time, start a conversion and wait for the done flag. For PWM set the period, then the duty value. Read the part's reference manual for exact register names.
EXAMPLE
/* generic steps, not real register names */
adc_select(channel); adc_enable();
adc_start(); while (!adc_done()) { }
uint16_t v = adc_result();
pwm_set_period(255); pwm_set_duty(v >> 2);