(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:
uint16_t lightLevel = 0;
uint16_t lightLevelOld = 0;
/* Initializes MCU, drivers and middleware */
// select pin6 as the A/D input and start conversion
lightLevel = ADC0_GetConversionResult();
and the support function code (in a separate source file):
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
So it partially works. There are two things I can do to fix this problem.
- declare lightLevel outside of Main (global)
- 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?