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 Key Operated Gate Locking System Circuit

This simple key-operated gate locking system allows only those persons who know the preset code to open the gate. The code is to be entered from the keypad within the preset time to operate the motor fitted in the gate. If anyone trying to open the gate presses a wrong key in the keypad, the system is disabled and, at the same time, sounds an alarm to alert you of an unauthorized entry. Figs 1 and 2 show the block and circuit diagrams of the key-operated code locking system, respectively. Connect points A, B, C, D, E, F and ground of the circuit to the respective points of the keypad. Keys S7, S16, S14 and S3 are used here for code entry, and the remaining keys are used for disabling the system. It is very important to press the keys in that order to form the code. To start the motor of the gate, press switches S7, S16, S14 and S3 sequentially. If the keys are pressed in a different order from the preset order, the system will lock automatically and the motor will not start. Fig. 1: Bl...

A basic Arduino Solar PV Monitor

I have just recently had solar pv installed, mainly to future proof my energy costs, I do not expect it to be like drilling for oil in my back garden, however the return looks to be encouraging. The install gives you another single unit meter, from this you will see the total amount the panels produce, but that is about it. I wanted to know how much the production was as it was happening, I discovered the light blinks on the front of the meter will flash 1000 times for each kWh of electricity which passes through. The rate of the flashing of the LED tells you how much power is currently passing through the meter. [ ]

Apple releases TV spot for new iPods

Apple has just released a fun commercial to showcase its new line of iPod players and the various colours they come in. The TV spot titled ‘Bounce’, has a bunch of colourful iPod touch, iPod nano and iPod shuffles er…bouncing to music. With all that colour and dancing and bouncing, you may forget that Apple’s latest gen line of iPods has some other awesome features. For instance, the fifth gen iPod touch comes with Siri, 4-inch retina display and an A5 chipset. Maybe the next ad will showcase some of these features with less bouncing.link