Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ADC p16F917

Status
Not open for further replies.

mircea1981

New Member
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 :
C:
#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;
        }
    }
 
}
 
Last edited by a moderator:
TAD timings will cause a warning.. Not an error..

The main issue here is... At the end of the ADC read you turn off the module.... Then never turn it back on..

ADCON0.ADON = 1; .. Then wait over 100nS... then ADCON0.GO = 1;
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top