Skip to main content

PIC16F887 877 programming in C Tutorial 10 ADC



Analog to Digital Converter (ADC):
In this tutorial i will show how to convert analog values into digital, by using 887 (little bit differ with 877), then display these digital value on LCD.

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 ADC voltage reference is software selectable to be either internally generated or externally supplied.

ADC Configuration:
When configuring and using the ADC the following functions must be considered:
• Port configuration
• Channel selection
• ADC voltage reference selection
• ADC conversion clock source
• Interrupt control
• Results formatting

Port configuration:
The ADC can be used to convert both analog and digital signals. When converting analog signals, the I/O pin should be configured for analog by setting the associated TRIS and ANSEL bits.

Channel selection:
The CHS bits of the ADCON0 register determine which channel is connected to the sample and hold circuit. When changing channels, a delay is required before starting the next conversion.
ADC voltage reference selection:
The VCFG bits of the ADCON0 register provide independent control of the positive and negative voltage references. The positive voltage reference can be either VDD or an external voltage source. Likewise, the negative voltage reference can be either VSS or an external voltage source. 

ADC conversion clock source: 
The source of the conversion clock is software selectable via the ADCS bits of the ADCON0 register. There are four possible clock options:
• FOSC/2
• FOSC/8
• FOSC/32
• FRC (dedicated internal oscillator)
The time to complete one bit conversion is defined as TAD. One full 10-bit conversion requires 11 TAD periods as shown in Figure.


Interrupt control:
The ADC module allows for the ability to generate an interrupt upon completion of an Analog-to-Digital conversion. The ADC interrupt flag is the ADIF bit in the PIR1 register. The ADC interrupt enable is the ADIE bit in the PIE1 register. The ADIF bit must be cleared in
software. 

Results formatting: 
The 10-bit A/D conversion result can be supplied in two formats, left justified or right justified. The ADFM bit of the ADCON0 register controls the output format. 


ADC Operation:
STARTING A CONVERSION:To enable the ADC module, the ADON bit of the ADCON0 register must be set to a ‘1’. Setting the GO/DONE bit of the ADCON0 register to a ‘1’ will start the Analog-to-Digital conversion. 


COMPLETION OF A CONVERSION:When the conversion is complete, the ADC module will:
• Clear the GO/DONE bit
• Set the ADIF flag bit
• Update the ADRESH:ADRESL registers with new conversion result


A/D CONVERSION PROCEDURE:
This is an example procedure for using the ADC to perform an Analog-to-Digital conversion:
1. Configure Port:
• Disable pin output driver (See TRIS register)
• Configure pin as analog
2. Configure the ADC module:
• Select ADC conversion clock
• Configure voltage reference
• Select ADC input channel
• Select result format
• Turn on ADC module
3. Configure ADC interrupt (optional):
• Clear ADC interrupt flag
• Enable ADC interrupt
• Enable peripheral interrupt
• Enable global interrupt(1)
4. Wait the required acquisition time(2).
5. Start conversion by setting the GO/DONE bit.
6. Wait for ADC conversion to complete by one of the following:
• Polling the GO/DONE bit
• Waiting for the ADC interrupt (interrupts enabled)
7. Read ADC Result
8. Clear the ADC interrupt flag (required if interrupt is enabled).


ADC REGISTER DEFINITIONS:
The following registers are used to control the operation of the ADC.




Code:
Lets make it easy by using adc library of mikroc. The following code will convert the analog input, at pin RA2, into digital and display it on lcd. As the conversion is of 10-bit, so the range is from 0-1023.

unsigned int temp_res;

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char txt1[] = "ADC Example";
char txt[7];
void main() {
/////////// comment for 877/////////////////////////////////////
  ansel=4;                     // Configure AN2 pin as analog
  anselh=0;
  c1on_bit=0;
  c2on_bit=0;
 ////////////////////////////////////////////////////////////////
  TRISA  = 0xFF;              // PORTA is input
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,txt1);
  adc_init();
  do {

    temp_res = ADC_read(2);   // Get 10-bit results of AD conversion
    IntToStr(temp_res, txt);    //int to string conversion
    Lcd_Out(2,1,txt);
  } while(1);
}

adc_init(); This routine initializes PIC’s internal ADC module to work with RC clock. Clock determines the time period necessary for performing AD conversion.

ADC_read(2); 10 or 12-bit unsigned value read from the specified channel (MCU dependent).Parameter channel represents the channel from which the analog value is to be acquired.In this case it is 2.

IntToStr(temp_res, txt); Converts input signed integer number to a string. The output string has fixed width of 7 characters including null character at the end (string termination). The output string is right justified and the remaining positions on the left (if any) are filled with blanks.

Schematic:


Comments

Popular posts from this blog

Build a 3000W Stereo Power Amplifier Circuit Diagram

How to Build a 3000W Stereo Power Amplifier Circuit Diagram? Lets start first we define 3000wstereo power amplifier circuit diagram this circuit has a power output of up to 1500W RMS power amplifier circuit is often used to power sound systems spelunker for outdor. In the final image can be seen a series of power amplifiers using 10 sets of power transistors for the ending. This power amplifier circuit using a transistor amplifier from the front, signal splitter, driver and power amplifier. Current consumption required is quite large power amplifier that is 15-20 A 1500W power amplifier circuits for this. Supply voltage needed by the power of this amplifier is the optimal working order symmetrical 130VDC (130VDC-130VDC ground). 1500W amplifier circuit below is a picture series of mono, stereo if you want to make it necessary to make two copies of the circuit. For more details can be viewed directly image the following 1500W power amplifier circuit. The series of High Power Amplifier 15

Solid State Switch For Dc Operated Gadgets

This solid state DC switch can be assembled using just three transistors and some passive components. It can be used to switch on one gadget while switching off the second gadget with momentary operation of switch. To reverse the operation, you just have to momentarily depress another switch.  The circuit operates over 6V-15V DC supply voltage. It uses positive feedback from transistor T2 to transistor T1 to keep this transistor pair in latched state (on/ off), while the state of the third transistor stage is the complement of transistor T2’s conduction state.  Initially when switch S3 is closed, both transistors T1 and T2 are off, as no forward bias is available to these, while the base of transistor T3 is effectively grounded via resistors R8 and R6 (shunted by the load of the first gadget). As a result, transistor T3 is forward biased and gadget 2 gets the supply. This is indicated by glowing of LED2.  Circuit diagram : Solid-State Switch For Dc-Operated Gadgets Circuit Diagram Whe

Amplifier Circuit 2 X 10 watt with IC AN7145

This is Stereo Amplifier On this schematic amplifier circuit  use the ic AN7145L , but can use the following IC : AN7145M AN7145H AN7146M AN7146H You can use of all ic above. anthis circuit need suply voltage with dual voltage . Minimum voltage 6 Volt and maximum voltage 20 volt . Voltage just (-) and (+), but (-) can also ground. Maximum power output 2 X 20 watt with minimum impedance 4 ohm. Click image to view enlarge. You can operate this circuit with  plugging input to , PC or other media can give signal audio.