• AVR Freaks

Hot!Scope issues with ATTiny 412 and XC8 variable definition

Author
jventerprises
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2011/10/13 18:01:46
  • Location: 0
  • Status: offline
2019/11/08 06:53:25 (permalink)
0

Scope issues with ATTiny 412 and XC8 variable definition

Read the next item in this thread, forum would not let me post for some reason.
post edited by jventerprises - 2019/11/08 07:02:50
#1

11 Replies Related Threads

    jventerprises
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/10/13 18:01:46
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/08 06:55:25 (permalink)
    0
    Hi all,
     
    (had to post in chunks because the forum would not let me create this issue)
     
     
    I am having trouble understanding just what is going on with this issue, and was hoping someone could shed some light on it.
     
    • Windows 10 (latest updates)
    • MPLAB X 5.30
    • XC8 2.10
    • I used the MPLAB code configurator to setup the original project 3.85.1.
    • Compiler optimization is set to 0 (Behaves similarly at 1)
     
    Ok, to the problem.  Complete code is attached, the good bits are detailed below: 
     
    I have a simple program that reads a value from ADC0  and assigns to a variable defined in main as lightLevel as such:
     
    int main(void)
    {
        uint16_t lightLevel = 0;
        uint16_t lightLevelOld = 0;

        
        /* Initializes MCU, drivers and middleware */
        SYSTEM_Initialize();
        
        // select pin6 as the A/D input and start conversion
        ADC0_StartConversion(6);
        
        // seed
        while (!ADC0_IsConversionDone());
        lightLevel = ADC0_GetConversionResult();
     
        ...
    }

     
    and the support function code (in a separate source file):
     
    adc_result_t ADC0_GetConversionResult(void)
    {
     return (ADC0.RES);
    }

     
     
    Surprisingly, this does not behave as expected.  A watch of the variables shows the correct data in ADC0.RES, however lightLevel receives only the bits (16:31).  Bits (0:15) are always zero.  for example:
     
    RES     lightLevel in Main
    0x123  0x100
    0x256  0x200
     
    So it partially works.  There are two things I can do to fix this problem.  
     
    1. declare lightLevel outside of Main (global)
    2. add volatile to the declaration inside Main
     
    Once either option is implemented everything works as expected with the complete value being returned.  This looks like a scope problem to me with the XC8 compiler, or an optimization problem, or somehow related to the fact that the source is split into multiple files?  however I do not believe it should be.  lightLevel is only used inside Main set by the return of ADC0_GetConversionResult().
     
    ADC0.RES is a 16 bit word in the ATTiny412, and is handled with a system atomic read since the bus width of the part is only 8 bits.  Perhaps this has something to do with it?
     
    Anybody have any insight into why this would be?
     
     
     
    #2
    jventerprises
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/10/13 18:01:46
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/08 07:03:57 (permalink)
    0
    main.c
     

    /*
        (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.
    */
    #include "mcc_generated_files/mcc.h"
    // uint16_t lightLevel, lightLevelOld;
    /*
        Main application
    */
    int main(void)
    {
        volatile uint16_t lightLevel = 0;
        volatile uint16_t lightLevelOld = 0;
       
        /* Initializes MCU, drivers and middleware */
        SYSTEM_Initialize();
       
        // select pin6 as the A/D input and start conversion
        ADC0_StartConversion(6);
        TCB0_DisableInterrupt();
       
        // seed
        while (!ADC0_IsConversionDone());
        lightLevel = ADC0_GetConversionResult();
        lightLevelOld = lightLevel;
        // make sure all it quiet
        IO_PA1_SetLow();
        IO_PA3_SetLow();
        IO_PA7_SetLow();
       
        while (1)
        {
             while (!ADC0_IsConversionDone());
             lightLevel = ADC0_GetConversionResult();
            // lightLevel = ADC0_GetConversion(6);
            // lightLevel = ADC0.RES;
           
            if ((lightLevel < 300) && (lightLevelOld >= 300))
            {
                // dark, turn on the IR and switch out cut filter(off))
                IO_PA7_SetHigh();
                IO_PA1_SetHigh();
                IO_PA3_SetLow();
                ClearTimerB();
                TCB0_EnableInterrupt();
                while (GetTimerB() <= 200); // 2 seconds
                IO_PA1_SetLow();
                IO_PA3_SetLow();
                TCB0_DisableInterrupt();
            }
            else if ((lightLevel > 500) && (lightLevelOld <= 500))
            {
                // light, turn off the IR and switch in cut filter(on))
                IO_PA7_SetLow();
                IO_PA1_SetLow();
                IO_PA3_SetHigh();
                ClearTimerB();
                TCB0_EnableInterrupt();
                while (GetTimerB() <= 200); // 2 seconds
                IO_PA1_SetLow();
                IO_PA3_SetLow();
                TCB0_DisableInterrupt();
            }
            // update history
            asm("nop");
            lightLevelOld = lightLevel;
        }
    }
    /**
        End of File
    */
    #3
    jventerprises
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/10/13 18:01:46
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/08 09:55:25 (permalink)
    0
    also, i incorrectly identified the bits in lightLevel:
     
    lightLevel receives only the bits (16:31).  Bits (0:15) are always zero
     
    should be:
     
    lightLevel receives only the bits (8:15).  Bits (0:7) are always zero
     
    I cant edit the post, i just keep getting "access denied"
    #4
    NKurzman
    A Guy on the Net
    • Total Posts : 17916
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/08 11:12:42 (permalink)
    0
    So you believe the compiler is wrong, and the Debugger is correct?
    You need to Verify that what the Debugger says is correct.
     
    adc_result_t is defined as what?   uint16_t ?
     
    #5
    jventerprises
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/10/13 18:01:46
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/08 16:11:02 (permalink)
    0
    Hi,
     
    Yes, adc_result_t is a uint16_t.
     
    and yes, I believe the debugger.  I connected a power supply to the AD directly and watched as ADC0.RES always reported the correct value, but the value received in lightLevel was always ADC0.RES & (0xFF00).
     
    As soon as i add volatile to the declaration of lightLevel (or move it outside of Main) it also agrees with ADC0.RES, which agrees with the attached power supply.
     
    It does look suspicious to me, like a scope problem.  I just don't understand why.
    post edited by jventerprises - 2019/11/09 15:08:10
    #6
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/08 16:14:01 (permalink)
    +1 (1)
    jventerprises
    ...
    but the value received in lightLevel was always ADC0.RES & (0xF0).

    If post#8 is accurate, then I think you mean "lightLevel was always ADC0.RES & (0xFF00)."

    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!
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 17916
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/08 18:11:38 (permalink)
    +1 (1)
    "and yes, I believe the debugger"  Why? MPlabX has not been without Bugs.  Additionally there can be issue with the Elf File.  This could be a compiler issue.  But you need to be sure.  
    I am not sure if Australia is handling the Atmel XC8.  If not you will need to enter support Ticket to get that Bug Handled.  Other wise you can see if Jeff pops in with an opinion.
    #8
    jventerprises
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/10/13 18:01:46
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/09 15:08:58 (permalink)
    0
    Yes,m sorry.  I have updated the mask in #8 to be 0xFF00
    #9
    jventerprises
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/10/13 18:01:46
    • Location: 0
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/09 15:11:46 (permalink)
    0
    Hi,
     
    Yes, I agree that the problem could easily be other than the compiler.  I was just trying to get some validation here on the forum before pushing to microchip.
     
    Can you recommend any other testing to help isolate?  
    #10
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/10 00:36:42 (permalink)
    0
    Have you had a look at the assembly language LST file output by the compiler to see where it is losing the bits?
     

    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
    mad_c
    Super Member
    • Total Posts : 1202
    • Reward points : 0
    • Joined: 2010/12/12 17:48:27
    • Location: Brisbane, Australia
    • Status: offline
    Re: Scope issues with ATTiny 412 and XC8 variable definition 2019/11/10 13:45:10 (permalink)
    +3 (3)
    NKurzman
    I am not sure if Australia is handling the Atmel XC8.

    The PIC and AVR sides of XC8 are handled by one team, but that team is spread over both hemispheres of the planet.
    If not you will need to enter support Ticket to get that Bug Handled.

    It's always a good idea to submit a ticket yourself. If a ticket is submitted on someone's behalf, it will not be your issue details and sample project that are looked into.

    Other wise you can see if Jeff pops in with an opinion.

    I agree with Ric. A good place to start would be to compare the list file for the good and bad cases.
     
    Jeff.
     
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5