• AVR Freaks

Hot!ADC routine generated with MCC have no response

Author
kit1995
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2020/05/15 03:18:45
  • Location: 0
  • Status: offline
2020/05/19 21:49:09 (permalink)
0

ADC routine generated with MCC have no response

Hi,
 
I'm new to MCU programming and also new to here.
Not sure if it is alright to post here. Please let me know if I should post somewhere else.
 
The purpose is to measure the voltage output from a flow sensor. I have generated the code of adc module with mcc, disable interrupt and not configuring the CCP module and not in (I dont need auto conversion, it is weird that MCC keep telling me to configure CCP module)
The variable is not recognized, but it seems that it works alright with ADC interrupt and autoconvert with timer ( but I dont need it )
I am not sure if there is a MCC setting issue or anything else. Would any one can point me to a right direction.
Thankyou.
 

Attached Image(s)

#1

14 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3675
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/20 07:15:10 (permalink)
    0
    Start enlightening us by telling:
    • MPLAB version
    • MCC version
    • microcontroller derivative (denomination of your chip)
    • compiler / version used
    • whatever might help us to guess what's going wrong (resp. what you're doing wrong).

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    kit1995
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/05/15 03:18:45
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/20 19:05:59 (permalink)
    0
    Hi,
     
    Thanks for replying.
    I am using MPLAB X v5.35,
    MCC v3.95 with core v4.85.0 and Library Pic10/12/16/18 MCUs v1.80.0.
    The MCU is PIC18F46K22 and
    complier is XC8 v2.10 with C99.
     
     
     while (1)
        {
            
     
            for(unsigned int i=0; i<500; i++){
                unsigned int tem;
                Pump_Forward(i); //this is a pump driven by PWM signals which works fine

                tem = ADC_GetConversion(channel_AN0);
                /*problem occurs here, seems sometimes "ADCON0bits.GO_nDONE" do not become zero, im not sure if it is related to the issue*/
                          
                __delay_ms(500);
               
                
                
            }

     
     
    #3
    ric
    Super Member
    • Total Posts : 27022
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/20 19:24:14 (permalink)
    0
    You cut off the top of the image in the first post, so I cannot see which C file you are editing.
    Does that file #include "adc.h" ?
     

    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!
    #4
    kit1995
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/05/15 03:18:45
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/20 19:50:37 (permalink)
    0
    Hi,
     
    Sorry, I was worrying it would be difficult to read.
    Yes, since it was generated with MCC and I have included "mcc.h" which included "adc.h" inside.
    I try to remove all the irrelevant part but remain all #include and variable definition.
    #include "mcc_generated_files/mcc.h"
    #include "I2C_LCD.h"
    #include "I2CDrv.h"
    #include <xc.h>
    #include "Si7021.h"
    #include "Keypad.h"
    #include "math.h"
    #include "stdlib.h"
    #include "pump.h"
    #include "valve.h"

    /*
                             Main application
     *
     *
     */



    void ReadFlowRate(void){
        unsigned int convertedValue;
        convertedValue = ADC_GetConversionResult();
        //return convertedValue;
    }


    void main(void)
    {
        
        unsigned char Data0[2]={};
        unsigned char Data[3]={};
        unsigned char Data2[3]={};
        unsigned long long Tconc;
        unsigned long long Sconc;
        unsigned int data_count0 = 0;
        unsigned int data_count = 0;
        unsigned int data_count2 = 0;
        double TimeSrc;
        double TimeAir;
        
        unsigned int BagSize;
        unsigned int FR_High = 2;
        unsigned int FR_Low = 1;
        unsigned int FR_ind = FR_High;
        unsigned int FR;
        
        
        // Initialize the device
        SYSTEM_Initialize();

        //ADC_SetInterruptHandler(ReadFlowRate);



        // Enable the Global Interrupts
        //INTERRUPT_GlobalInterruptEnable();

        // Disable the Global Interrupts
        //INTERRUPT_GlobalInterruptDisable();

        // Enable the Peripheral Interrupts
        //INTERRUPT_PeripheralInterruptEnable();

        // Disable the Peripheral Interrupts
        //INTERRUPT_PeripheralInterruptDisable();
        
     
       
        while (1)
        {
            
     
            for(unsigned int i=0; i<500; i++){

                unsigned int tem;
                Pump_Forward(i);

                tem = ADC_GetConversion(channel_AN0);
                
                __delay_ms(500);
                          
                
            }

     
    in mcc.h:
    /**
      @Generated PIC10 / PIC12 / PIC16 / PIC18 MCUs Header File

      @Company:
        Microchip Technology Inc.

      @File Name:
        mcc.h

      @Summary:
        This is the mcc.h file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

      @Description:
        This header file provides implementations for driver APIs for all modules selected in the GUI.
        Generation Information :
            Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.80.0
            Device : PIC18F46K22
            Driver Version : 2.00
        The generated drivers are tested against the following:
            Compiler : XC8 2.10 and above or later
            MPLAB : MPLAB X 5.30
    */

    /*
        (c) 2018 Microchip Technology Inc. and its subsidiaries.
        
        Subject to your compliance with these terms, you may use Microchip software and any
        derivatives exclusively with Microchip products. It is your responsibility to comply with third party
        license terms applicable to your use of third party software (including open source software) that
        may accompany Microchip software.
        
        THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
        EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
        IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
        FOR A PARTICULAR PURPOSE.
        
        IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
        INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
        WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
        HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
        THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
        CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
        OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
        SOFTWARE.
    */

    #ifndef MCC_H
    #define MCC_H
    #include <xc.h>
    #include "device_config.h"
    #include "pin_manager.h"
    #include <stdint.h>
    #include <stdbool.h>
    #include <conio.h>
    #include "interrupt_manager.h"
    #include "tmr2.h"
    #include "epwm2.h"
    #include "epwm1.h"
    #include "ccp5.h"
    #include "memory.h"
    #include "ext_int.h"
    #include "adc.h"



    /**
     * @Param
        none
     * @Returns
        none
     * @Description
        Initializes the device to the default states configured in the
     * MCC GUI
     * @Example
        SYSTEM_Initialize(void);
     */
    void SYSTEM_Initialize(void);

    /**
     * @Param
        none
     * @Returns
        none
     * @Description
        Initializes the oscillator to the default states configured in the
     * MCC GUI
     * @Example
        OSCILLATOR_Initialize(void);
     */
    void OSCILLATOR_Initialize(void);

    #endif /* MCC_H */
    /**
     End of File
    */

     
     

    Attached Image(s)

    #5
    ric
    Super Member
    • Total Posts : 27022
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/20 19:58:21 (permalink)
    0
    Have you tried to compile it?
    The MPLABX editor is notorious for not being able to find things that the compiler can.
     

    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!
    #6
    kit1995
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/05/15 03:18:45
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/20 20:07:28 (permalink)
    0
    Yes, I have build it and enter debug mode. Either way there is no error, but there is no ADC value returned.
    I am wonder if I set the ADC in MCC wrong?
    Or do I have to set the CCP5 and timer for auto conversion even if I dont need it.
    #7
    kit1995
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/05/15 03:18:45
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 00:47:50 (permalink)
    0

    It seems that the "GO_nDONE" bit does not get back to zero, not sure if it is related.

    Attached Image(s)

    #8
    kit1995
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/05/15 03:18:45
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 02:29:43 (permalink)
    0
    Hi,
     
    Sorry, I was worrying it would be difficult to read.
    Yes, since it was generated with MCC and I have included "mcc.h" which included "adc.h" inside.
    I try to removed all the irreverent part but remain all the #include and variables defined. 
     
     
    #include "mcc_generated_files/mcc.h"
    #include "I2C_LCD.h"
    #include "I2CDrv.h"
    #include <xc.h>
    #include "Si7021.h"
    #include "Keypad.h"
    #include "math.h"
    #include "stdlib.h"
    #include "pump.h"
    #include "valve.h"

    /*
                             Main application
     *
     *
     */

    void ReadFlowRate(void){
        unsigned int convertedValue;
        convertedValue = ADC_GetConversionResult();
        return convertedValue;
    }


    void main(void)
    {
        
        unsigned char Data0[2]={};
        unsigned char Data[3]={};
        unsigned char Data2[3]={};
        unsigned long long Tconc;
        unsigned long long Sconc;
        unsigned int data_count0 = 0;
        unsigned int data_count = 0;
        unsigned int data_count2 = 0;
        double TimeSrc;
        double TimeAir;
        

        
        
        // Initialize the device
        SYSTEM_Initialize();
     
        //ADC_SetInterruptHandler(ReadFlowRate);




        //INTERRUPT_GlobalInterruptEnable();


        //INTERRUPT_GlobalInterruptDisable();


        //INTERRUPT_PeripheralInterruptEnable();


        //INTERRUPT_PeripheralInterruptDisable();
        
     
     
       
        while (1)
        {
            
     
            for(unsigned int i=0; i<500; i++){
                
                unsigned int tem;
                Pump_Forward(i);
                
                tem = ADC_GetConversion(channel_AN0);
                                        
                __delay_ms(500);
                          
            }

     
    In mcc.h:
    /**
      @Generated PIC10 / PIC12 / PIC16 / PIC18 MCUs Header File

      @Company:
        Microchip Technology Inc.

      @File Name:
        mcc.h

      @Summary:
        This is the mcc.h file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

      @Description:
        This header file provides implementations for driver APIs for all modules selected in the GUI.
        Generation Information :
            Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.80.0
            Device : PIC18F46K22
            Driver Version : 2.00
        The generated drivers are tested against the following:
            Compiler : XC8 2.10 and above or later
            MPLAB : MPLAB X 5.30
    */

    /*
        (c) 2018 Microchip Technology Inc. and its subsidiaries.
        
        Subject to your compliance with these terms, you may use Microchip software and any
        derivatives exclusively with Microchip products. It is your responsibility to comply with third party
        license terms applicable to your use of third party software (including open source software) that
        may accompany Microchip software.
        
        THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
        EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
        IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
        FOR A PARTICULAR PURPOSE.
        
        IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
        INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
        WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
        HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
        THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
        CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
        OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
        SOFTWARE.
    */

    #ifndef MCC_H
    #define MCC_H
    #include <xc.h>
    #include "device_config.h"
    #include "pin_manager.h"
    #include <stdint.h>
    #include <stdbool.h>
    #include <conio.h>
    #include "interrupt_manager.h"
    #include "tmr2.h"
    #include "epwm2.h"
    #include "epwm1.h"
    #include "ccp5.h"
    #include "memory.h"
    #include "ext_int.h"
    #include "adc.h"



    /**
     * @Param
        none
     * @Returns
        none
     * @Description
        Initializes the device to the default states configured in the
     * MCC GUI
     * @Example
        SYSTEM_Initialize(void);
     */
    void SYSTEM_Initialize(void);

    /**
     * @Param
        none
     * @Returns
        none
     * @Description
        Initializes the oscillator to the default states configured in the
     * MCC GUI
     * @Example
        OSCILLATOR_Initialize(void);
     */
    void OSCILLATOR_Initialize(void);

    #endif /* MCC_H */
    /**
     End of File
    */

     
     
     

    Attached Image(s)

    #9
    kit1995
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/05/15 03:18:45
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 02:31:35 (permalink)
    0
    H
    I give up using manually trigger, now I config the CCP5 and Tmr1 for auto trigger source, without any interrupt.
    Finally the register reads the correct value, but there is another problem, the result cannot be return to the assigned variable in my main loop.
     
    #include "mcc_generated_files/mcc.h"
    #include "I2C_LCD.h"
    #include "I2CDrv.h"
    #include <xc.h>
    #include "Si7021.h"
    #include "Keypad.h"
    #include "math.h"
    #include <stdlib.h>
    #include <stdbool.h>
    #include "pump.h"
    #include "valve.h"
    #include "eeprom.h"

    /*
                             Main application
     *
     *
     */

    void main(void)
    {
        
        unsigned char Data0[2]={};
        unsigned char Data[3]={};
        unsigned char Data2[3]={};
        unsigned int Tconc;
        unsigned int Sconc;
        unsigned int data_count0 = 0;
        unsigned int data_count = 0;
        unsigned int data_count2 = 0;
        double TimeSrc;
        double TimeAir;
        
        unsigned int BagSize;
        unsigned int FR_High = 2;
        unsigned int FR_Low = 1;
        unsigned int FR_ind = FR_High;
        unsigned int FR;
        
        unsigned int ad = 10;
        
        
        // Initialize the device
        SYSTEM_Initialize();



        // Enable the Global Interrupts
        //INTERRUPT_GlobalInterruptEnable();

        // Disable the Global Interrupts
        //INTERRUPT_GlobalInterruptDisable();

        // Enable the Peripheral Interrupts
        //INTERRUPT_PeripheralInterruptEnable();

        // Disable the Peripheral Interrupts
        //INTERRUPT_PeripheralInterruptDisable();
         
       
        while (1)
        {
                ad = ADC_GetConversionResult();
                                       
                __delay_ms(500);
                           
                }

     
     

    Attached Image(s)

    #10
    ric
    Super Member
    • Total Posts : 27022
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 03:00:38 (permalink)
    0
    kit1995
    Finally the register reads the correct value, but there is another problem, the result cannot be return to the assigned variable in my main loop.

    How do you know?
    HOW you are testing is just as important as what you are testing.
    The code you show does not use the returned value in any way, so the C compiler is permitted to throw away any reference to it. You cannot rely on just hovering over the variable afterwards in the debugger to see a value in it.
    You have to either USE the variable for output,  or to affect something else, or add a "volatile" qualifier" to the definition of the variable to force the compiler to always write to it, even if it isn't used.
     
     

    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!
    #11
    ric
    Super Member
    • Total Posts : 27022
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 03:00:39 (permalink)
    0
    (deleted double post)
    post edited by ric - 2020/05/21 03:27:37

    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!
    #12
    du00000001
    Just Some Member
    • Total Posts : 3675
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 03:49:23 (permalink)
    3 (2)
    Green like the green, green grass...  sad
     
    Code from post #3
    void ReadFlowRate(void){
    unsigned int convertedValue;
    convertedValue = ADC_GetConversionResult();
    //return convertedValue;
    }

     
    Who would expect this routine to ever return a value?
    As the "return convertedValue;" seems to have thrown an error (to be expected), the code was "improved" by commenting out the return.
     
    And now? (post #10, code from within main():
    while (1)
    {
        ad = ADC_GetConversionResult();
        __delay_ms(500);
    }

     
    OK - this one is the classical pitfall: as ad is neither used nor attributed volatile, it's usually optimized away.
     
     No - I doubt there's anything wrong with MCC or the hardware. PEBKAC!
     
    @ kid1995
    You'd better re-take the C(+/+) 101 course. And this time you'd better attend!
     

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #13
    LdB_ECM
    Super Member
    • Total Posts : 359
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 04:10:30 (permalink)
    0
    We need to see SYSTEM_Initialize() because usually there is a call to something called like ADC_Init() that sets up the ADC.
     
    So in "adc.h" is there something called "ADC_Init" or "ADC_Setup" and does that get called in SYSTEM_Initialize() because at the moment we can't even be sure the ADC is turned on?
     
    It's worrying you claim it's from MCC but you seem to have edited stuff.
    #14
    kit1995
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/05/15 03:18:45
    • Location: 0
    • Status: offline
    Re: ADC routine generated with MCC have no response 2020/05/21 19:32:20 (permalink)
    4 (1)
    Hi All,
     
    Thank you very much for helping.
    ADC_Init() is called in SYSTEM_Initialize(). So I guess that part is alright.
     
    Adding "volatile" did solve the problem.
    I removed all the other code to test if ADC is working, and yes the variable "ad" never called and likely to be the reason f this issue.
     
    #15
    Jump to:
    © 2020 APG vNext Commercial Version 4.5