ADC p16F917

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
mircea1981
Posts: 1
Joined: Sun Feb 12, 2017 10:49 am

ADC p16F917

Post by mircea1981 » Sun Feb 12, 2017 10:54 am

Hi.

I am a beginer in pic microcontroller and i need some help. I have a development board from Microchip, PICDEM Mechatronics with a pic16f917 microcontroller.
I use MPLAB IDE v8.70 and HI-TECH ANSI C Compiler.
In code if the current provided from a potentiometer is less than a threshold (2.5 V) the diode D0 must be ON, else OFF.

When i run the code in debug mode i receive the next error :
ADC-W0001: Tad time is less than 1.60us
ADC-W0008: No stimulus file attached to ADRESL for A/D.

I do not understand why i receive this error since the Fosc is 8MHz, AD conversion clock is 16.
So, with this setings i computed the
Tad = 2 microsec.

The code :

Code: Select all

#include<pic.h>

__CONFIG(FCMEN_ON & IESO_OFF & CP_OFF & WDTE_OFF & BOREN_OFF & MCLRE_ON & PWRTE_OFF);

#define ZERO		0
#define ONE			1

unsigned int ADC_result;
unsigned char ADC_channel;

void Clock_Init(void)
{
	// Internal Oscillator Frequency Select bits - 8 MHz
	OSCCONbits.IRCF2 = ONE;
	OSCCONbits.IRCF1 = ONE;
	OSCCONbits.IRCF0 = ONE;
	// Set the ADC clock frequency Fosc/16 = 500 KHz
	ADCON1bits.ADCS2 = ONE;
	ADCON1bits.ADCS1 = ZERO;
	ADCON1bits.ADCS0 = ONE;
}

void Comparators_Init(void)
{
	// Comparators Off
	CMCON0bits.CM2 = ONE;
	CMCON0bits.CM1 = ONE;
	CMCON0bits.CM0 = ONE;
}

void Port_Init(void)
{
	// Pin RA3/AN3 is set input
	TRISAbits.TRISA3 = ONE;
	// Set RA3/AN3 to analog mode
	ANSELbits.ANS3 = ONE;

	// Set RD7 as an output
	TRISD = ZERO;
	PORTD = ZERO;
}

void ADC_Init(void)
{
    // A/D result left justified
	ADCON0bits.ADFM = ZERO;
	// +/- Vref.
	ADCON0bits.VCFG1 = ZERO;
	ADCON0bits.VCFG0 = ZERO;
    // Analog channel 3 selected
	ADCON0bits.CHS2 = ZERO;
	ADCON0bits.CHS1 = ONE;
	ADCON0bits.CHS0 = ONE;
	// Select ADC Channel
   	ADC_channel = ADCON0bits.CHS;
	// Switch on the adc module
   	ADCON0bits.ADON = ONE;
}

unsigned int ADCRead(void)
{
	// Start conversion
   	ADCON0bits.GO = ONE;
	// Wait for the conversion to finish
   	while(ADCON0bits.GO == ONE);
	ADC_result = (ADRESH<<8) + ADRESL;
	// Switch off adc
   	ADCON0bits.ADON = ZERO; 
	
   	return ADC_result;
}

void main (void)
{	
	Clock_Init();
	
	Comparators_Init();

	// Init port pins
	Port_Init();

   	// Init ADC
   	ADC_Init();

    // code will run in for loop forever
    for( ; ; )
    {
		// Read Channel 3
		ADC_result = ADCRead();
		if(ADC_result < 2.5)
		{
			PORTDbits.RD0 = ONE;
		}
		else
		{
			PORTDbits.RD0 = ZERO;
		}
    }
	
}

User avatar
phalanx
Non-SFE Guru
Posts: 1947
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: ADC p16F917

Post by phalanx » Thu Feb 16, 2017 8:21 pm

Hi Mircea1981

It sounds like you are debugging with the MPLAB simulator and most likely you haven't configured it with the correct clock frequency. Depending on the version of MPLAB you are running, you either go to the Debug->Settings->OSC/Trace for version 8 and older or for MPLAB X, open up your project properties, select "Simulator" from your current configuration, and select "Oscillator Options" in the option categories drop down menu.

Once there you can enter your configuration and the simulator will do its error checking using the updated value.

-Bill

Post Reply