Help with PIC16F716 ADC code using MPLAB v8.88

Status
Not open for further replies.

richardlaishram

Member level 4
Joined
Jan 6, 2013
Messages
77
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Location
Planet Earth
Visit site
Activity points
1,804
I'm using the header file <htc.h> for my PIC16F716 ADC project using PIC16
Following are the codes after referring the datasheet and a sample code for PIC16F877A.
Code:
void InitADC(void)
{
	TRISA0 = 0x01;			// PortA Pin0 configured as input
	ADCON1 = 0x00;			// PortA configured as Analog input with Vref as Vdd
	ADCON0 = 0x41;			// ADC Enabled
}

unsigned int ADCValue0(void)
{
	ADCON0 &= 0x45;			// Start AD Conversion
	ADCON0 |= 0x40;			// Conversion Cycle in Progress
	__delay_ms(10);
	while(ADCON0 |= 0x40);	// Wait for AD conversion to Complete
	return ADRES;			// ADC result stored
}

when building the project, I'm getting the error undefined identifier "ADRES"

I've got a code with PIC16F877A for an ADC Project, I've tried it and it's working fine in Proteus.

Code:
void InitADC(void)
{
	ADCON1 = 0x80;
	TRISA = 0x2F;
	ADCON0 = 0x81;
}

unsigned int ADCValue(void)
{
	ADCON0 &= 0x87;
	ADCON0 |= 0x08;
        __delay_ms(10);
	GO_nDONE  = 1;
	while(GO_nDONE);
	return ((ADRESH<<8)+ADRESL);
}

Here's my query:
1. The ADC result registers for PIC16F877A (ADRESH and ADRESL - 10bit) is not showing any error whereas PIC16F716 (ADRES - 8bit) is not recognized by <htc.h>
2. Can I use GO_nDONE to check for the completion of AD Conversion in PIC16F716 also?

Thanks in Advance.
 

Try this..... It may help you.....
Code:
void InitADC(void)
{
	TRISA0 = 0x01;			
	ADCON1 = 0x00;			
	ADCON0 = 0x41;			
}

unsigned int ADCValue0(void)
{
	ADCON0 &= 0x45;			
	ADCON0 |= 0x40;
GO_nDONE  = 1;			//changes
	__delay_ms(10);
	while(ADCON0 |= 0x40);	

	return(ADRES);               //changes			
}
 

Even this is not working. I tried using the ADRESH register and it's working fine with Proteus.
Code:
unsigned int ADCValue(void)
{
	ADCON0 &= 0x45;
	ADCON0 |= 0x40;
        __delay_ms(10);
	GO_nDONE  = 1;
	while(GO_nDONE);
	return ADRESH;
}

I need to check it again with the actual hardware, though. I'm not able to find any register with the name ADRES in the Compiler Help pdf also, so I guessed and tried ADRESH, and luckily it worked in Proteus.
 

1. ADRES is not a reg of PIC16F877A. Instead you have ADRESH and ADRESL regs... It's just used for 10 bit resolution... But PIC16F716 has only 8 bit resolution....
2. GO_nDONE is similar for both...

- - - Updated - - -


ADRESL is Lower 8 bits and ADRESH contains higher 2 bits of your result. You need to use both the regs to get proper result...
 

Created a whole new PIC16F716 project in MPLAB v8.8 again and now the previous code is working fine with ADRES register. Thanks for your help. Maybe I have selected the wrong chip and parameters.
 

I think you used code of some other PIC with the PIC you are using. If that is right then ADCONx values change. Read the datasheet.


If 4 MHz clock then


Code C - [expand]
1
__CONFIG(FOSC_XT & WDTE_ON & PWRTE_ON & CP_OFF & BOREN_OFF);

 

what is happening in hardware can you post the schematic??

I don't have the schematics for now. For the ADC I/P I'm using a voltage divider from a 12V battery. For the 5V Power supply I'm using a 7805 voltage regulator . The problem is that the ADC O/P value is jumbling and not showing the accurate value.

Now I tried connecting the power supply to a 12V battery and seems like the ADC value is showing the correct value. Can the problem be because of the 12V AC-DC adapter used for powering the board?

- - - Updated - - -

I think you used code of some other PIC with the PIC you are using. If that is right then ADCONx values change. Read the datasheet.
I have gone through the datasheets, sir.
ADCON1 = 0x00; // All pins as ADC
ADCON0 = 0x41; // ADC ON and Fosc/8
ADCON0 &= 0x4D; // ADC ON, Conversion in Progress, AN1 Selected and Fosc/8
ADCON0 |= 0x08; // ANI Selection

Correct me if I'm wrong in this. Checking with the config now. Thanks
 

Now I tried connecting the power supply to a 12V battery and seems like the ADC value is showing the correct value. Can the problem be because of the 12V AC-DC adapter used for powering the board?
Exactly.. The Source and uc shd have a common ground and the value to be measured is shd be stable unless no use in software corrections.......
 

Exactly.. The Source and uc shd have a common ground and the value to be measured is shd be stable unless no use in software corrections.......

Maybe your AC-DC power adapter doesn't have good filtering.

Thanks for your replies. Sorry to bother you again. Now I'm trying to use 2 ADC Channels. I have gone through lots of example codes. Can you please help where have I gone wrong in this code. Thanks in advance.
 

Attachments

  • 2Channel_PIC16F716.rar
    114.1 KB · Views: 96


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
__delay_ms(250);
        adcval2 = ADC_Chan0();
        o = adcval2%10;
        n = (adcval2%100)/10;
        m = adcval2/100;
        __delay_ms(250);
        WriteCommandToLCD(0xC5);
        WriteDataToLCD(o+0x30);
        WriteDataToLCD(n+0x30);
        WriteDataToLCD(m+0x30);

 


Here I'm trying to display the exact 8-bit ADC value (0-255). Please tell me if I have made any mistake.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…