I have my LCD working on my TM4C123 and I have printf working.
I'm trying to make the input from the left column look like the output from the right column.
What I tried was to extract each digit from the integer and then print each one out and then put the decimal somewhere in there. When I do extract the digits, the sign seems to stick with the digit and I just can't seem to get it formatted right.
outTestCaseType1 outTests1[13]={
{ 0, " = 0.000?\r" }, // 0/1000 = 0.000
{ 4, " = 0.004?\r" }, // 4/1000 = 0.004
{ -5, " = -0.005?\r" }, // -5/1000 = -0.005
{ 78, " = 0.078?\r" }, // 78/1000 = 0.078
{ -254, " = -0.254?\r" }, // -254/1000 = -0.254
{ 999, " = 0.999?\r" }, // 999/1000 = 0.999
{ -1000, " = -1.000?\r" }, // -1000/1000 = -1.000
{ 1234, " = 1.234?\r" }, // 1234/1000 = 1.234
{ -5678, " = -5.678?\r" }, // -5678/1000 = -5.678
{ -9999, " = -9.999?\r" }, // -9999/1000 = -9.999
{ 9999, " = 9.999?\r" }, // 9999/1000 = 9.999
{ 10000, " = *.***?\r" }, // error
{-10000, " = *.***?\r" } // error
};
My code is as follows:
void ST7735_sDecOut3(int32_t n) {
int32_t msd,nsd, lsd;
int32_t value;
value=n;
if (value > 999)
{
value = 999;
}
msd = value / 100;
value -= msd * 100;
nsd = value / 10;
value -= nsd * 10;
lsd = value;
printf("%d%d%d", msd, nsd, lsd);
}
The output is as follows on the LCD:
I'm picking at this as much as I can and this is as close as I got so far. There could be an easier way that I don't see because of my inexperience but I'll keep trying because that's all I can do.
Any hints or help will be appreciated.
This doesn't use your data structure or types, but can be readily adapted to match.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int values[] = { 0, 4, -5, 78, -254, 999, -1000, 1234, -5678, -9999, +9999, 10000, -10000 };
enum { NUM_VALUES = sizeof(values) / sizeof(values[0]) };
for (int i = 0; i < NUM_VALUES; i++)
{
int32_t value = values[i];
char integer[2];
char fraction[4];
int f_part = abs(value) % 1000;
int i_part = abs(value) / 1000;
char sign = ' ';
if (abs(value) > 9999)
{
strcpy(integer, "*");
strcpy(fraction, "***");
}
else
{
if (value < 0)
sign = '-';
sprintf(integer, "%d", i_part);
sprintf(fraction, "%.3d", f_part);
}
printf("%6d: = %c%s.%s?\\r\n", value, sign, integer, fraction);
}
return 0;
}
Example output:
0: = 0.000?\r
4: = 0.004?\r
-5: = -0.005?\r
78: = 0.078?\r
-254: = -0.254?\r
999: = 0.999?\r
-1000: = -1.000?\r
1234: = 1.234?\r
-5678: = -5.678?\r
-9999: = -9.999?\r
9999: = 9.999?\r
10000: = *.***?\r
-10000: = *.***?\r
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.