Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Not getting adc value correctly

Status
Not open for further replies.

cnandha19

Member level 3
Joined
Aug 28, 2015
Messages
59
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
604
Code:
#include<htc.h>
#include<pic.h>
//Config word
__CONFIG(FOSC_INTOSC & WDTE_OFF   & PWRTE_ON & MCLRE_OFF & BOREN_ON & CP_OFF & CPD_OFF);

//Internal clock oscillator & Watchdog timer off & Master clear reset & brown out protection on & CODE PROTECTION _OFF & DATA CODE PROTECTION BIT_OFF 
// Define LED pin

// Define CPU Frequency
// This must be defined, if __delay_ms() or 
// __delay_us() functions are used in the code
#define _XTAL_FREQ   8000000    


#define BUZZER   LATA0
#define REDLED   LATA1
#define GREENLED LATA5    //RA5 connected

//Sensor connected across RA2 

#define BAUDRATE 9600

unsigned int ADC_Value;

void InitADC(void)
{
	ADCON1  = 0x80;	     // Make PORTA and PORTE analog pins
						 
	TRISA   = 0x04;      // Make RA5, RA3, RA2, RA1, RA0 input
	
	ADCON0  = 0x81;		 // Turn on the A/D Converter
}



unsigned int GetADCValue(unsigned char Channel)
{
	ADCON0 = 0x00;         // Clear Channel selection bits
	ADCON0 |= (Channel<<3); // Select channel pin as ADC input
    
    __delay_ms(50);         // Time for Acqusition capacitor 
							// to charge up and show correct value
	GO_nDONE  = 1;		    // Enable Go/Done

	while(GO_nDONE);        // Wait for conversion completion

	return ((ADRESH<<8)+ADRESL);   // Return 10 bit ADC value
}



void InitUART(void)
{
	TRISA4 = 1;   					// TX Pin
	//TRISC7 = 1;   					// RX Pin
	
	SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
	BRGH  = 1;                   	// Fast baudrate
	SYNC  = 0;						// Asynchronous
	SPEN  = 1;						// Enable serial port pins
	CREN  = 1;						// Enable reception
	SREN  = 0;						// No effect
	TXIE  = 0;						// Disable tx interrupts
	RCIE  = 1;						// Enable rx interrupts
	TX9   = 0;						// 8-bit transmission
	RX9   = 0;						// 8-bit reception
	TXEN  = 0;						// Reset transmitter
	TXEN  = 1;						// Enable the transmitter
}



void SendByteSerially(unsigned char Byte)  // Writes a character to the serial port
{
	while(!TXIF);  // wait for previous transmission to finish
	TXREG = Byte;
}


void SendStringSerially(const unsigned char* st)
{
	while(*st)
		SendByteSerially(*st++);
}

// Main function
void main()
{	

	
    TRISA=0x04; //0x000000    Set PORTA as output PORT
    LATA=0x00;   
    ANSELA=0x04;
    OSCCON=0x69;
	while(1)
	{
    

    void InitADC(void);
    unsigned int GetADCValue(unsigned char);
    InitADC();
    InitUART;

    ADC_Value = GetADCValue(AN2);   
 
    BUZZER=1;
    __delay_ms(1000);
    REDLED=1;
    GREENLED=1;
    __delay_ms(1000);
  
     BUZZER=0;
     REDLED=0;
     GREENLED=0;
    }
    
}
 

Why did you keep initialization functions (InitADC(),InitUART() etc.) ? It should be called once.
The program should be

Code:
void main()
{	

	
    TRISA=0x04; //0x000000    Set PORTA as output PORT
    LATA=0x00;   
    ANSELA=0x04;
    OSCCON=0x69;
    InitADC();
    InitUART;

	while(1)
	{
 
   
     ADC_Value = GetADCValue(AN2);   
 
    BUZZER=1;
    __delay_ms(1000);
    REDLED=1;
    GREENLED=1;
    __delay_ms(1000);
  
     BUZZER=0;
     REDLED=0;
     GREENLED=0;
    }
    
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top