I am using MSP430F427 MCU, I have this code below to extract, ones digit (d5), tens digit (d4), and hundreds digit (d3). I am expecting a 4-digit value, like 1000+, but have to display only 3-digit (hundreds, tens, ones). i.e count = 1783, I have to display only the 783. The code below works already.
However, during compilation there is a warning to get rid of division and modulo operation when doing this as this is power intensive. (Description Resource Path Location (ULP 5.1) Detected modulo operation(s). Recommend moving them to RAM during run time or not using as these are processing/power intensive)
void updateDisplay(signed long counter_value) {
if (count<0)
{
NEG_ON;
}
else
{
NEG_OFF;
}
D3_NULL;
D4_NULL;
D5_NULL;
// Ensure that counter_value is positive or zero for further processing
counter_value = abs(counter_value);
// Display the appropriate digit on the LCD based on the count value
uint8_t d3= (counter_value/100)%10;
uint8_t d4= (counter_value/10)%10;
uint8_t d5= counter_value%10;
if (d3 != 0)
{
switch (d3)
{
case 1: DIGIT3_DISP_1; break;
case 2: DIGIT3_DISP_2; break;
case 3: DIGIT3_DISP_3; break;
case 4: DIGIT3_DISP_4; break;
case 5: DIGIT3_DISP_5; break;
case 6: DIGIT3_DISP_6; break;
case 7: DIGIT3_DISP_7; break;
case 8: DIGIT3_DISP_8; break;
case 9: DIGIT3_DISP_9; break;
}
}
else
{
DIGIT3_OFF;
}
if (d4 != 0 || d3 != 0)
{
switch (d4)
{
case 0: DIGIT4_DISP_0; break;
case 1: DIGIT4_DISP_1; break;
case 2: DIGIT4_DISP_2; break;
case 3: DIGIT4_DISP_3; break;
case 4: DIGIT4_DISP_4; break;
case 5: DIGIT4_DISP_5; break;
case 6: DIGIT4_DISP_6; break;
case 7: DIGIT4_DISP_7; break;
case 8: DIGIT4_DISP_8; break;
case 9: DIGIT4_DISP_9; break;
}
}
else if (d3 != 0)
{
DIGIT4_DISP_0;
}
switch (d5)
{
case 0: DIGIT5_DISP_0; break;
case 1: DIGIT5_DISP_1; break;
case 2: DIGIT5_DISP_2; break;
case 3: DIGIT5_DISP_3; break;
case 4: DIGIT5_DISP_4; break;
case 5: DIGIT5_DISP_5; break;
case 6: DIGIT5_DISP_6; break;
case 7: DIGIT5_DISP_7; break;
case 8: DIGIT5_DISP_8; break;
case 9: DIGIT5_DISP_9; break;
}
}
I want to learn how to save a single digit to a variable. i.e count 674 so d3 = 6, d4 = 7, and d5 = 4. This is what I think I should do. However, I am a bit sluggish in implementing bit shifting and bitwise operation to change the code. Any help would be appreciated.
Given:
(From Divide by 10 using bit shifts?)
And noting that MSP430 also lacks a hardware multiplier so also:
then:
You can get the least significant three digits on a value in BCD as follows:
Then (if you really must):
But that really has no particular benefit over simply using the
bcddigits directly e.g.:Whether that is truly more power efficient that using
%and/operators would require testing. The operators are generalised whereas these functions are specialised for multiply/divide by 10, so likely to generate less code even without resorting to assembly. I would expect a reasonable compiler to generate near optimal code for such simple operations.