Tuesday, November 17, 2015

PIC12F615 A/D Converter and PWM Modules

The Analog-to-Digital Converter (ADC) allows conversion of an analog input signal to a 10-bit binary representation of that signal. This device uses analog inputs, which are multiplexed into a single sample and hold circuit. The output of the sample and hold is connected to the input of the converter. The converter generates a 10-bit binary result via successive approximation and stores the conversion result into the ADC result registers (ADRESL and ADRESH).
The PWM mode generates a Pulse-Width Modulated signal on the CCP1 pin.
This example shows how to use both A/C and PWM modules.
The following circuit schematic shows an Led connected to GP2 which is the PWM output of the PIC12F615 and a potentiometer connected to GP0 (AN0).
First the microcontroller reads the analog value on ANO then convert it to digital with a resolution of 10-bit, this 10-bit resolution needs to be converted into 8-bit in order to set duty cycle of the PWM signal, this conversion achieved by shifting right two times this 10-bit number.
pic adc pwm mikroc

 MikroC Code:

 unsigned int adc_value;
 void main() {
  ANSEL = 1;        // Pin AN0 is configured as analog
  GPIO = 0;         // Initial state of GPIO port
  TRISIO = 1;    // Configure GP0 as input
  PWM1_Init(5000);  // PWM module initialization (5KHz)

  adc_value = ADC_Read(0);    // Read analog value on AN0
  PWM1_Set_Duty((adc_value >> 2));