BCD to decimal error

Thread Starter

AlbertHall

Joined Jun 4, 2014
12,619
My BCD to decimal function generates a warning in XC8:
warning: implicit conversion loses integer precision: 'uint16_t' (aka 'unsigned short') to 'uint8_t' (aka 'unsigned char') [-Wimplicit-int-conversion]

C:
uint8_t BCD2Decimal(uint8_t BCD)
{
    uint8_t dec = ((BCD & 0xF0) >> 4) * 10 + (BCD & 0x0F);
    return dec;
}
Can I safely ignore this warning or change the code to eliminate the warning?
I don't understand where the uint16_t comes in.
 

Papabravo

Joined Feb 24, 2006
22,058
Just a guess, but the multiply by 10 triggers the conversion to uint16_t because it fails to consider the effect of the mask and shift operation.
 

nsaspook

Joined Aug 27, 2009
16,251
My BCD to decimal function generates a warning in XC8:
warning: implicit conversion loses integer precision: 'uint16_t' (aka 'unsigned short') to 'uint8_t' (aka 'unsigned char') [-Wimplicit-int-conversion]

C:
uint8_t BCD2Decimal(uint8_t BCD)
{
    uint8_t dec = ((BCD & 0xF0) >> 4) * 10 + (BCD & 0x0F);
    return dec;
}
Can I safely ignore this warning or change the code to eliminate the warning?
I don't understand where the uint16_t comes in.
What version of XC8 are you using and at what warning level?
V3.00 gives no -Wimplicit-int-conversion errors with a short demo of your code.
C:
#pragma warning disable 520  // not used function warning suppress

#include "mcc_generated_files/mcc.h"

uint8_t BCD2Decimal(uint8_t);

/*
             Main application
*/
void main(void)
{
    uint8_t i = 0;
    // Initialize the device
    SYSTEM_Initialize();

    while (1) {
        // Add your application code
        IO_RF7_LAT = BCD2Decimal(i++);
    }
}

uint8_t BCD2Decimal(uint8_t BCD)
{
    uint8_t dec = ((BCD & 0xF0) >> 4) * 10 + (BCD & 0x0F);
    return dec;
}

CLEAN SUCCESSFUL (total time: 7ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make -f nbproject/Makefile-default.mk dist/default/production/promo.X.production.hex
make[2]: Entering directory 'C:/Users/nsasp/New Folder/promo.X'
"C:\Program Files\Microchip\xc8\v3.00\bin\xc8-cc.exe" -mcpu=18F57Q84 -c -mdfp="C:/Users/nsasp/.mchp_packs/Microchip/PIC18F-Q_DFP/1.28.451/xc8" -memi=wordwrite -O2 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-download -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wimplicit-int-conversion -o build/default/production/mcc_generated_files/device_config.p1 mcc_generated_files/device_config.c
make[2]: Leaving directory 'C:/Users/nsasp/New Folder/promo.X'
make[2]: Entering directory 'C:/Users/nsasp/New Folder/promo.X'
"C:\Program Files\Microchip\xc8\v3.00\bin\xc8-cc.exe" -mcpu=18F57Q84 -c -mdfp="C:/Users/nsasp/.mchp_packs/Microchip/PIC18F-Q_DFP/1.28.451/xc8" -memi=wordwrite -O2 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-download -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wimplicit-int-conversion -o build/default/production/mcc_generated_files/pin_manager.p1 mcc_generated_files/pin_manager.c
make[2]: Leaving directory 'C:/Users/nsasp/New Folder/promo.X'
make[2]: Entering directory 'C:/Users/nsasp/New Folder/promo.X'
"C:\Program Files\Microchip\xc8\v3.00\bin\xc8-cc.exe" -mcpu=18F57Q84 -c -mdfp="C:/Users/nsasp/.mchp_packs/Microchip/PIC18F-Q_DFP/1.28.451/xc8" -memi=wordwrite -O2 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-download -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wimplicit-int-conversion -o build/default/production/mcc_generated_files/mcc.p1 mcc_generated_files/mcc.c
make[2]: Leaving directory 'C:/Users/nsasp/New Folder/promo.X'
make[2]: Entering directory 'C:/Users/nsasp/New Folder/promo.X'
"C:\Program Files\Microchip\xc8\v3.00\bin\xc8-cc.exe" -mcpu=18F57Q84 -c -mdfp="C:/Users/nsasp/.mchp_packs/Microchip/PIC18F-Q_DFP/1.28.451/xc8" -memi=wordwrite -O2 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-download -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wimplicit-int-conversion -o build/default/production/mcc_generated_files/interrupt_manager.p1 mcc_generated_files/interrupt_manager.c
make[2]: Leaving directory 'C:/Users/nsasp/New Folder/promo.X'
make[2]: Entering directory 'C:/Users/nsasp/New Folder/promo.X'
"C:\Program Files\Microchip\xc8\v3.00\bin\xc8-cc.exe" -mcpu=18F57Q84 -c -mdfp="C:/Users/nsasp/.mchp_packs/Microchip/PIC18F-Q_DFP/1.28.451/xc8" -memi=wordwrite -O2 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-download -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wimplicit-int-conversion -o build/default/production/main.p1 main.c
make[2]: Leaving directory 'C:/Users/nsasp/New Folder/promo.X'
make[2]: Entering directory 'C:/Users/nsasp/New Folder/promo.X'
"C:\Program Files\Microchip\xc8\v3.00\bin\xc8-cc.exe" -mcpu=18F57Q84 -Wl,-Map=dist/default/production/promo.X.production.map -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -mdfp="C:/Users/nsasp/.mchp_packs/Microchip/PIC18F-Q_DFP/1.28.451/xc8" -memi=wordwrite -O2 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-download -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wl,--memorysummary,dist/default/production/memoryfile.xml -o dist/default/production/promo.X.production.elf build/default/production/mcc_generated_files/pin_manager.p1 build/default/production/mcc_generated_files/mcc.p1 build/default/production/mcc_generated_files/device_config.p1 build/default/production/main.p1 build/default/production/mcc_generated_files/interrupt_manager.p1

18F57Q84 Memory Summary:
Program space used 234h ( 564) of 20000h bytes ( 0.4%)
Data space used 5h ( 5) of 3200h bytes ( 0.0%)
Configuration bits used Fh ( 15) of 12h words ( 83.3%)
EEPROM space used 0h ( 0) of 400h bytes ( 0.0%)
ID Location space used 0h ( 0) of 40h bytes ( 0.0%)

make[2]: Leaving directory 'C:/Users/nsasp/New Folder/promo.X'

BUILD SUCCESSFUL (total time: 2s)
* Load symbols when building or programming for production is enabled. Loading from: C:/Users/nsasp/New Folder/promo.X/dist/default/production/promo.X.production.elf...
Loading code from C:/Users/nsasp/New Folder/promo.X/dist/default/production/promo.X.production.hex...
Program loaded with pack,PIC18F-Q_DFP,1.28.451,Microchip
Loading completed
The xc8 does integer promotion to native integer size (16 bits here).
Don't just ignore it when shifting is done. Check (print the results for expected inputs) to make sure the right things is being done. The compiler is warning you that the result may not be what you expected. You can cast it to stop the warning or ignore it if the results are correct.

https://developerhelp.microchip.com...ogramming/operators/implicit-type-conversion/
1744116131901.png
 

Thread Starter

AlbertHall

Joined Jun 4, 2014
12,619
I have found the problem. The BCD value passed to this function is defined as uint16_t. Imade the parameter 'BCD & 0xFF' when the function is called and all seems well now.
Thanks everyone.
 

nsaspook

Joined Aug 27, 2009
16,251
Oh, yes. The prototype says 'uint8_t BCD2Decimal(uint8_t BCD);'
Just checking. Your warning was a classic C function parameter integer promotion warning. XC8 default integer size is 16-bits even with the hardware being 8-bit. You did exactly the right thing checking to see why it was happening.
https://barrgroup.com/blog/efficient-c-code-8-bit-microcontrollers

Integer promotion
The integer promotion rules of ANSI C are probably the most heinous crime committed against those of us who labor in the 8-bit world. I have no doubt that the standard is quite detailed in this area. However, the two most important rules in practice are the following:

Any expression involving integral types smaller than an int have all the variables automatically promoted to int
Any function call that passes an integral type smaller than an int automatically promotes the variable to an int, if the function is not prototyped.
 
Top