Tuesday, March 13, 2018

Analog to Digital Converter with PIC Microcontroller (PIC16F877A)

Why need ADC? !!!!













ADC pins of PIC16F877A














Features of PIC16F877A ADC:

  • 10-bit 8 channels analogue input ADC
  • ±2 LSB absolute accuracy
  • Up to 15 KSPS at Maximum Resolution
  • 0-Vcc ADC input votage range 
  • Reference Voltage can be configured as Avcc, AREF or 2.56 Volts
  • Sleep Mode Noise Canceller. 

What is Resolution?

  • The smallest change that can be detected in the measuring voltage.
  • 10 Bit resolution provide 210 =1024 steps.
  • Vcc = 5Volt
  • Step Size(mV) = 5/1024 = 4.88

Conversion Time:

  • Conversion time is defined as the time an ADC takes to convert the analogue input into the digital (binary) output.
  • The conversion time depends on -MCU clock source.

ADC Timings:


  • Startup time: should be considered when the ADC has been enabled either for the first time or after a wake up from some of the sleep modes.
  • Sample and hold time: This time must be considered carefully especially when multiple channels are used during conversion.

The A/D converter module has the following features:

      The converter generates a 10-bit binary result using the method of successive approximation and stores the conversion results into the ADC registers (ADRESL and ADRESH). There are 14 separate analog inputs; The A/D converter converts an analog input signal into a 10- bit  binary number. The minimum resolution or quality of conversion may be adjusted to various needs by selecting voltage referances Vref- and Vref+.












ADC programming in PIC16F877A:

          The A/D module has four registers. These registers are:
  • A/D Result High Register (ADRESH)
  • A/D Result Low Register (ADRESL)
  • A/D Control Register 0 (ADCON0)
  • A/D Control Register 1 (ADCON1)













ADRESH and ADRESL Registers:


The result obtained after converting an analog value into digital is a10-bit number that is to be stored in the ADRESH and ADRESL registers. There are two ways of handling it – left and right justification which simplifies its use to a great extent. The format of conversion result depends on the ADFM bit of the ADCON1 register. In the event that the A/D converter is not used, these registers may be used as general-purpose registers.













ADCON0 Register:







ADCS1, ADCS0 – A/D Conversion Clock Select bits select clock frequency used for internal synchronization of A/D converter. It also affects duration of conversion.
CHS3-CHS0 – Analog Channel Select bits select a pin or an analog channel for A/D conversion, i.e. voltage measurement:
GO/DONE – A/D Conversion Status bit determines current status of conversion:
1 – A/D conversion is in progress.
0 – A/D conversion is complete. This bit is automatically cleared by hardware when the A/D conversion is complete.

ADON – A/D On bit enables A/D converter.
1 – A/D converter is enabled.

0 – A/D converter is disabled.

ADCON1 Register:

















To do an A/D Conversion, follow these steps:

Step 1 --Configure the A/D module:

  • Configure analog pins/voltage reference and digital I/O(ADCON1)
  • Select A/D input channel (ADCON0)
  • Select A/D Conversion clock (ADCON0)
  • Turn on A/D module (ADCON0)
  • Select one of input channels CH0-CH13 of the ADCON0 register
  • Select data format using the ADFM bit of the ADCON1 register.
  •  Enable A/D converter by setting the ADON bit of the ADCON0 register.
Step 2 – ADC interrupt configuration (optionally):
                Clear the ADIF bit.
                Set the ADIE, PEIE and GIE bits.
Step 3 – Wait for the required acquisition time to pass (approximately 20uS).
Step 4 – Start conversion by setting the GO/DONE bit of the ADCON0 register.
Step 5 – Wait for ADC conversion to complete.
                It is necessary to check in the program loop whether the GO/DONE pin is cleared or wait                      for an A/D interrupt (must be previously enabled).
Step 6 – Read ADC results:
                Read the ADRESH and ADRESL registers.



Circuit Diagram:







REMEMBER !!!!

  • The AVCC and VREF should be constant. If they vary, the accuracy of ADC conversion will be affected.
  • Input must not exceed VCC
  • Signal components higher than the Nyquist frequency (fADC/2) should not be present for either kind of channels, to avoid distortion from unpredictable signal convolution.

Basic code:

Define CONF_WORD = 0x3f72
Define CLOCK_FREQUENCY = 12
AllDigital
ADCON1 = 0x0e
Define LCD_BITS = 8
Define LCD_DREG = PORTD
Define LCD_DBIT = 0
Define LCD_RSREG = PORTE
Define LCD_RSBIT = 0
Define LCD_RWREG = PORTE
Define LCD_RWBIT = 1
Define LCD_EREG = PORTE
Define LCD_EBIT = 2
Define LCD_READ_BUSY_FLAG = 1
Lcdinit
Dim an0 As Word
loop:
  Adcin 0, an0
  Lcdcmdout LcdClear
  Lcdout "Analog input AN0"
  Lcdcmdout LcdLine2Home
  Lcdout "Value: ", #an0
  WaitMs 250
Goto loop

C Code


// LCD module connections
 sbit LCD_RS at RD0_bit;
 sbit LCD_EN at RD1_bit;
 sbit LCD_D4 at RD4_bit;
 sbit LCD_D5 at RD5_bit;
 sbit LCD_D6 at RD6_bit;
 sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
 // End LCD module connections

int adc;
char adc_con[7];

void main()
{
ADCON0 = 0x41; //ADC Module Turned ON and Clock is selected
ADCON1 = 0xC0; //All pins as Analog Input
TRISA.RA0=1;      // Configure RA0 pin as input
ADC_Init();         // Initialize ADC
Lcd_Init();          // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);    // Clear display
lcd_cmd(_LCD_CURSOR_OFF);
lcd_out(1,1,"ADC Reading");
delay_ms(1000);             // 1 Second delay
Lcd_Cmd(_LCD_CLEAR);  // Clear display

while(1)
{
adc = ADC_Read(0);          // Get 10-bit results of AD conversion
IntToStr(adc, adc_con);               // Convert temperature to string
lcd_out(1,1,"ADC Valu=");
Lcd_Out(1, 10, adc_con);                   // Display Temperature
 Delay_ms(500);                     // 1 Second delay
}
}

Circuit Diagram:



No comments:

Post a Comment