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

3 Channels Audio Splitter Amplifier Circuit Diagram using TL084

This is the schematic diagram of 3 channels audio splitter amplifier circuit which built using op-amp IC TL084. The 3 channels amplifier output distribution applies a single TL084.   3 Channels Audio Splitter Amplifier Circuit Diagram The very first step is to capacitive coupling having a p. 1.0 ~ electrolytic capacitor. The entries are railways Vee Y2 or 4.5 V. This enables working with an individual 9V power source. A voltage gain of 10 (1 M?/100 Kohm) is obtained in the first stage, as well as the other three floors are connected as a unity gain voltage followers. Every single output stage drives independently through an amplifier output 50 pF capacitor towards the resistance of 5.1 k ohm load. The response range is flat from 10 Hz to 30 kHz.

Simple But Automatic Load Sensing Power Switch

This circuit will automatically switch on several mains-powered "slave" loads when a "master" load is turned on. For example, it will switch on the amplifier and CD player in a stereo system when the receiver is turned on. It works by sensing the current draw of the "master" device through a low value high wattage resistor using a comparator. The output of that comparator then switches on the "slave" relay. The circuit can be built into a power bar, extension cord or power center to provide a convenient set of "smart" outlets that switch on when the master appliance is powered (turn on the computer monitor and the computer, printer and other peripherals come on as well). Automatic Load Sensing Power Switch Circuit Diagram Parts List: Notes: This circuit is designed for 120V operation. For 240V operation, resistors R2 and R6 will need to be changed. A maximum of 5A can be used as the master unless the wattage of R1 is increased S1 provid...

RF amplifier protection

RF amplifier protection I have developed the protection circuit for the EB104 amplifier I am working on, after I finally had some time to design and test a few models. The main requirements have been: - protection in case of high temperature; - protection in case of high SWR; - protection in case of wrong output filter selection; - simple design (i’m a fan of the whole K.I.S.S. rule of thought), able to work in strong electromagnetic fields, reliable, inexpensive. Because i will be using the same directional coupler i have used in the SWR meter (the one made on PCB) wich is directly influenced by the signal frequency, and because i want full HF coverage, i cannot just measure the reflected signal and make a circuit cut the amplifier when it goes over a limit; on 28Mhz the coupler generates roughly 4 times more voltage that let’s say in 7Mhz. So a system that compares direct and reflected signal and triggers when the latter is percentually too high was needed, therefore an operational a...