EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

ADC p16F917

Started by mircea1981 7 years ago1 replylatest reply 7 years ago294 views

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 :

#include<pic.h>

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

#define ZERO0

#define ONE1

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;

}

    }

}

[ - ]
Reply by waltxFebruary 13, 2017

1. You are not setting up the clock selection bits in the configuration register. The default is for an external RC oscillator. see page 185 of the datasheet. 

2. The error messages imply you are using a simulator , not the debugger.


The 2024 Embedded Online Conference