Sign In

ADC IN PIC16F877A

ANAND S anandsreebhadram@gmail.com India

Hardware: PIC16F877A . Software: HITECH C COMPILER WITH MPLAB IDE.

Hi sir,


I am a new bee in pic micro controller.I used arduino for projects .But while studying pic it feels very difficult and hard to understand.Sir i am using mplab x ide 1.41 with Hitech c compiler.I tried small programs like blinking led etcBut i faced a problem while reading an adc value from a potentiometer which is attached to ra0 pin of controller and want to display values in lcd.After writing the code and uploaded,LCD seems to showing like this 0000,0094,06321,6589..etc.Values are not constant and always jumping.When i turn potentiometer to full the value will be constant like 7823.Simlarly in min value it shows 0000.But while googling i understand that the value will ends at 1023.Because of 10 bit adc.
Kindly replay as soon as possible.


ANAND S
KERALA


Images

Program

#include <htc.h>
#include "delay.h"
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & CP_OFF);
#include <pic16f877a.h>
#include "lcd.h";
#define _XTAL_FREQ 8000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

unsigned short C;
void ADC_Init()
{
  ADCON0 = 0b01000001;              //Turn ON ADC and Clock Selection
  ADCON1 = 0x00;               //All pins as Analog Input and setting Reference Voltages
}

unsigned int ADC_Read(unsigned char channel)
{
               //Channel range is 0 ~ 7
   

  ADCON0=0b01000101;           //Clearing channel selection bits
  ADCON1=0b10000000;       //Setting channel selection bits
  TenMS_Delay(2);               //Acquisition time to charge hold capacitor
  GO_nDONE = 1;                //Initializes A/D conversion
  while(GO_nDONE);
  GO_nDONE = 0;
  C=ADRESH;//Return result
}

void main()
{
    TRISD = 0x00;
    TRISA = 0xFF;
    Lcd_Init();
    
    Lcd_Clear();


 char a=0;
  TRISA = 0xFF;                 
  ADC_Init();                   

  while(1)
  {

     Lcd_Set_Cursor(1, 1);
    a = ADC_Read(0);
   Lcd_Write_Char(C % 10 + 48);
   a = a / 10;

 
  


  }
  
}

Replies

R Avinash
2016-10-02 07:24:34
Please attach the schematic of LCD connection with MCU

Post a reply to ANAND S

Think you can help ANAND S ? Then post your thoughts that might help ANAND S. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.