• AVR Freaks

Hot!ADC not working

Author
bodnandi
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2009/06/19 15:58:49
  • Location: 0
  • Status: offline
2020/05/31 14:07:38 (permalink)
4 (1)

ADC not working

Hi,
 
I have a couple of questions. I will start with the most burning one. Fairly new to MPLAB X IDE and the Simulator...
 
First: why can I see only the first page of the Simulator Forum when it supposed to have over 9000 posts?
 
Trying to simulate a project on PIC18F1320. I set the ADC up and do
ADCON0bits.GO = 1;
while (ADCON0bits.GO);
the simulator sticks to the while loop forever. I have previously simulated a project with PIC18F26J13 succesfully with the ADC working.
 
Any thoughts?
 
#1

10 Replies Related Threads

    bodnandi
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2009/06/19 15:58:49
    • Location: 0
    • Status: offline
    Re: ADC not working 2020/05/31 14:36:10 (permalink)
    +1 (1)
    I am using MPLAB X IDE v5.35. XC8 v2.10.
     
    Just noticed that at the previous project I had an option to set up the ADC peripheral in the Project options/Simulator and in the new project I do not have this.
    #2
    ric
    Super Member
    • Total Posts : 28365
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: ADC not working 2020/05/31 15:30:58 (permalink)
    +1 (1)
    bodnandi
    First: why can I see only the first page of the Simulator Forum when it supposed to have over 9000 posts?

    By default, it only shows you posts for the last 90 days.
    There is a dropdown in the bottom left corner to extend that to show everything.
     

    Trying to simulate a project on PIC18F1320. I set the ADC up and do
    ADCON0bits.GO = 1;
    while (ADCON0bits.GO);
    the simulator sticks to the while loop forever. I have previously simulated a project with PIC18F26J13 succesfully with the ADC working.

    Check the notes for the simulator on that device.
    Not all peripherals are implemented on all devices in the simulator.
    On such an old PIC, if it's not supported now, it never will be.
    There's not a lot of point trying to simulate analog peripherals in that simulator anyway, far better to move to real hardware and do your own debiugging out a serial port.
     

    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
    bodnandi
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2009/06/19 15:58:49
    • Location: 0
    • Status: offline
    Re: ADC not working 2020/06/01 00:33:28 (permalink)
    0
    Thanks ric,
     
    I agree for the hardware, however I am trying to push this code as far as I can from home and also, I dont have anymore pins free for serial and this is an existing product.
     
    I checked and the ADC should be supported.
     
    However, there are other strange things. I was trying to implement SCL stimulus without any success even on Digital input pins. I have checked and it works fine with the PIC18F26J13.
     
    Further on, I seem to have lost the icons next to the titles of the tabs like the Stopwatch, Stimulus etc.
    #4
    GeorgePauley
    Moderator
    • Total Posts : 1268
    • Reward points : 0
    • Joined: 2009/12/01 13:59:30
    • Location: Chandler AZ
    • Status: offline
    Re: ADC not working 2020/06/01 05:59:49 (permalink)
    0 (2)
    There could be a problem in the application code.  For example, does it actually turn on the ADC?  Can you post your code?
    #5
    bodnandi
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2009/06/19 15:58:49
    • Location: 0
    • Status: offline
    Re: ADC not working 2020/06/01 11:12:18 (permalink)
    0
    // Include files

    #include <xc.h>  //#NB
    #include <math.h>
    #include "dmpr.h"

    // Configuration Bits
    #pragma config OSC = INTIO2                     // Internal RC, OSC1 as RA7, OSC2 as RA6

    //    Fail-Safe Clock Monitor:
    #pragma config FSCM = OFF                       // Fail-Safe Clock Monitor disabled

    //    Internal External Switch Over mode:
    #pragma config IESO = OFF                       // Internal External Switch Over mode disabled

    //    Power-up Timer:
    #pragma config PWRT = ON                        // Enabled

    //    Brown-out Reset:
    #pragma config BOR = OFF                        // Disabled

    //    Brown-out Voltage:
    #pragma config BORV = 45                        // 4.5V

    //    Watchdog Timer:
    #pragma config WDT = OFF                        // Disabled

    //    Watchdog Postscaler:
    #pragma config WDTPS = 32768                    // 1:32768

    //    MCLR Enable:
    #pragma config MCLRE = ON                       // Enabled

    //    Stack Full/Overflow Reset:
    #pragma config STVR = OFF                       // Disabled

    //    Low Voltage ICSP:
    #pragma config LVP = OFF                        // Disabled

    //    Background Debugger Enable:
    //#pragma config DEBUG = OFF                     // Disabled

    //    Code Protection Block 0:
    #pragma config CP0 = OFF //#NB                            // Enabled

    //    Code Protection Block 1:
    #pragma config CP1 = OFF //#NB                        // Enabled

    //    Boot Block Code Protection:
    #pragma config CPB = OFF //#NB                            // Enabled

    //    Data EEPROM Code Protection:
    #pragma config CPD = OFF                        // Disabled

    //    Write Protection Block 0:
    #pragma config WRT0 = OFF                       // Disabled

    //    Write Protection Block 1:
    #pragma config WRT1 = OFF                       // Disabled

    //    Boot Block Write Protection:
    #pragma config WRTB = OFF                       // Disabled

    //    Configuration Register Write Protection:
    #pragma config WRTC = OFF                       // Disabled

    //    Data EEPROM Write Protection:
    #pragma config WRTD = OFF                       // Disabled

    //    Table Read Protection Block 0:
    #pragma config EBTR0 = OFF                      // Disabled

    //    Table Read Protection Block 1:
    #pragma config EBTR1 = OFF                      // Disabled

    //    Boot Block Table Read Protection:
    #pragma config EBTRB = OFF                      // Disabled






    unsigned char gucMode;
    unsigned char gucMasterPower = 0;
    unsigned char gucTrackingPower = 0;
    unsigned int guiPhaseAngleDelay;
    unsigned char gucSignal;
    unsigned char gucSoftStartCermet;
    unsigned char gucSoftStartCount;
    unsigned char gucSpanCermet;
    signed char gscZeroCermet;
    unsigned char gucDutyCycleHi = 0;
    unsigned char gucDutyCycleLo = 0;
    unsigned int guiCycleDuration = 0;
    unsigned char gucNewFeedback;
    unsigned char gucOldFeedback;
    unsigned char gucHeatsinkTemp = 0;
    unsigned char gucVoltFeedback = 127;
    unsigned char gucPowerFeedback = 0;



    // Main function
    void main(void)
    {
        // 8MHz oscillator
        OSCCON = 0b01110000;
    //#ifndef SIMULATION
        while (!OSCCONbits.IOFS);
    //#endif //SIMULATION

          
        // VREF+ = VDD, VREF- = VSS, channel 0 selected, no ADC in progress, ADC module on
        ADCON0 = 0b00000001;

        // RA0/AN0, RA1/AN1, RA2/AN2, RB1/AN5 and RB4/AN6 analog inputs, rest digital I/O
        ADCON1 = 0b00011000;
        
           // Right justification (10-bit), 20 TAD acquisition time, FOSC/16
        ADCON2 = 0b10111101;

        // Clear all port latches
        LATA = 0;
        LATB = 0;

        // All inputs except RA6 and RA7
        TRISA = 0b00101111;

        // Inputs on RB1, RB2, RB4, RB6, RB7 and outputs on RB0, RB3/CCP1 and RB5 outputs
        TRISB = 0b11010110;

       
        // TMR0 off, 16-bit timer, internal clock, prescaler assigned, 1:32 prescaler
        T0CON = 0b00000100;

        // TMR1 16-bit read/write mode, 1:8 prescale, oscillator off, internal clock, timer off
        T1CON = 0b10110000;

        // Configure TMR2 period register
        PR2 = 0x7F; //

        // TMR2 1:16 postscale, timer on, 1:1 prescale
        T2CON = 0b01111100;

        // Clear TMR3
        TMR3H = 0;
        TMR3L = 0;

        // TMR3 16-bit read/write mode, 1:8 prescale, internal clock, timer on
        T3CON = 0b10110001;
        
        
        // Select channel 4 (voltage feedback)
        ADCON0bits.CHS0 = 0;
        ADCON0bits.CHS1 = 0;
        ADCON0bits.CHS2 = 1;
        
        // Left justify the result for an 8-bit value
        ADCON2bits.ADFM = 0;

        // Start an ADC (Acquisition delay provided by ADC module)
        ADCON0bits.GO = 1;

        // Wait for conversion to complete
        while (ADCON0bits.GO);
    #6
    bodnandi
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2009/06/19 15:58:49
    • Location: 0
    • Status: offline
    Re: ADC not working 2020/06/01 11:14:42 (permalink)
    0
    First the simulator gets stuck at: while (!OSCCONbits.IOFS); Then I skip that part and then it gets stuck at while (ADCON0bits.GO);
     
    Thats the start of the code, the complete code is bigger, so I wouldn't like to post it.
     
    #7
    GeorgePauley
    Moderator
    • Total Posts : 1268
    • Reward points : 0
    • Joined: 2009/12/01 13:59:30
    • Location: Chandler AZ
    • Status: offline
    Re: ADC not working 2020/06/02 10:42:12 (permalink)
    +1 (1)
    This is working fine for me.  How do you "know" that you are stuck at "while (ADCON0bits.GO);"?

    Attached Image(s)

    #8
    bodnandi
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2009/06/19 15:58:49
    • Location: 0
    • Status: offline
    Re: ADC not working 2020/06/03 01:15:11 (permalink)
    0
    Well, I press the Debug Main Project button. It stops at Main loop. Next I press Continue button - green circle with white triangle. I wait and press the Pause button - orange circle and two vertical white rectangles (as you would expect from a Pause button) and the green line is sitting at "while (ADCON0bits.GO);" line. Check the Stopwach and there is a couple of seconds passed already.
     
    It is something very silly, I promise you.
     
    As you can see I have commented out the "while (!OSCCONbits.IOFS);" line as well as it gets stuck there as well.
    post edited by bodnandi - 2020/06/03 01:31:11
    #9
    bodnandi
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2009/06/19 15:58:49
    • Location: 0
    • Status: offline
    Re: ADC not working 2020/06/03 01:54:52 (permalink)
    +1 (1)
    When I start up MPLAB I get this message from the simulator:
     
    W0103-SIM: Peripheral Failed: ADC
     
    #10
    bodnandi
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2009/06/19 15:58:49
    • Location: 0
    • Status: offline
    Re: ADC not working 2020/06/03 02:58:56 (permalink)
    0
    Seem to have the cure ...
     
    After GeorgePauley post I have tried opening a new project and including the source files and it seems to work now. This project has a slight history that it was initially written in C18 and for PIC18F1220. So I migrated it. I think some remanants from the C18 have left in the makefile or somewhere in the system so it prevented the Simulation to flow properly.
     
    So as a conclusion, maybe a general advice for migrating old code: put it in a new project to start with.
     
    #11
    Jump to:
    © 2020 APG vNext Commercial Version 4.5