• AVR Freaks

PIC 16F676 ADC PROBLEM!

Author
buildlab
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/06/26 11:50:03
  • Location: 0
  • Status: offline
2019/07/15 22:55:58 (permalink)
0

PIC 16F676 ADC PROBLEM!

Hi, I am a beginner in programming pic controllers. i been trying to work with pic16f676 for a while,i am trying to get adc pins working. The problem i am facing is the code is working in Proteus simulator with xc8 compailer, But not working when code is flashed to the controller. 
I have kept MCLR pin high and pulled up from vcc pin directly.
  •  [PIC12 ADC] PC=0x03A3. ADC conversion started before 'wait' time has expired following previous conversion or channel change. [U1]
                     >> Above is the message i am getting while simulating in Proteus, Tried changing the TAD values (ADCON1 values) but not working. Any help would be appreciated. 
 
/*
 * File: adctest.c
 * Author: Welcome
 *
 * Created on 11 July, 2019, 3:49 PM
 */



// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include<pic.h>
#define _XTAL_FREQ 4000000
int voltage1,voltage2;

int readAdc(int);
void main(void) {
    ANSEL = 0X00;
    TRISA = 0X00;
    TRISC = 0X00;
    ANS6 = 1;
    ANS5 = 1;
    TRISC2 = 1;
    TRISC1 = 1;
    
    PORTA = 0X00;
    PORTC = 0X00;
    
    while(1){
        voltage1 = readAdc(6);
        voltage2 = readAdc(5);
        if(voltage1 > 500){
            RC3 = 1;
        }
        else{
            RC3 = 0;
        }
        if(voltage2 > 500){
            RC4 = 1;
        }
        else{
            RC4 = 0;
        }
    }
}
int readAdc(int channel){
    int vol;
        ADCON0 = 0x83 | (channel << 2);
        ADCON1 = 0b100;
        __delay_us(20);
        GO = 1;
        while(GO == 1);
        vol = (ADRESH << 8) + ADRESL;
        __delay_us(5);
        return vol;
}

 
#1

6 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC 16F676 ADC PROBLEM! 2019/07/15 23:18:59 (permalink)
    0
    Do you have a crystal connected to your PIC?
    This line says you do, regardless of what the comment says:
    #pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)

    change it to:
    #pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)

    if you really do want to run from the internal oscillator.
     
    This line should only be executed once, before your main function enters the main loop. Not in the read() function
           ADCON1 = 0b100;

    and change this line
            ADCON0 = 0x83 | (channel << 2);
    to
            ADCON0 = 0x81 | (channel << 2);
    to get rid of that warning. You should NOT be setting the GO bit in the same instruction that turns the ADC on.
     
    Even better, write 0x81 to ADCON0 once in your initialisation code, then when you want to select the channel, do this:
        ADCON0bits.CHS = channel;

    That way you are not touching the ON or GO bits at all.
     
    post edited by ric - 2019/07/15 23:37:38

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC 16F676 ADC PROBLEM! 2019/07/15 23:25:09 (permalink)
    0
    and change
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)


    to
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)


    Do not enable the WDT until you understand how it works, and how to stop it from regularly resetting your PIC.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    acharnley
    Super Member
    • Total Posts : 309
    • Reward points : 0
    • Joined: 2016/05/01 06:51:28
    • Location: 0
    • Status: offline
    Re: PIC 16F676 ADC PROBLEM! 2019/07/15 23:26:07 (permalink)
    0
    You're better off learning about interrupts and using the ADC's interrupt to run and toggle between two functions which do the work you require. This is how Microchip do it in MCC:


    void ADC_SetInterruptHandler(void (* InterruptHandler)(void)){
    ADC_InterruptHandler = InterruptHandler;
    }


    void ADC_ISR(void)
    {

    ADC_InterruptHandler();

    // Clear the ADC interrupt flag
    PIR1bits.ADIF = 0;
    }
     
    #4
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC 16F676 ADC PROBLEM! 2019/07/15 23:39:07 (permalink)
    0
    I don't think interrupts do anything useful at all in this instance.
    This user's problems are elsewhere.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    pcbbc
    Super Member
    • Total Posts : 1188
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC 16F676 ADC PROBLEM! 2019/07/16 01:10:59 (permalink)
    0
    ric
    I don't think interrupts do anything useful at all in this instance.This user's problems are elsewhere. 

    Agreed. If the OP cannot get it working using polling from main, precious little change of getting it working using interrupts.
    #6
    buildlab
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: PIC 16F676 ADC PROBLEM! 2019/07/16 02:30:06 (permalink)
    0
    Yes it worked!.
    Thank you so much Ric.
    also to change to internal oscillator it was 
    "#pragma config FOSC = INTRCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)"
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5