![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
vtrx
Joined: 11 Oct 2017 Posts: 142
|
sprintf buffer |
Posted: Fri May 06, 2022 6:14 pm |
|
|
I'm trying to use 'sprintf' to load a buffer with decimal numbers to show in the 7 segment module (TM1637) but I don't understand how the buffer is being loaded.
The function that displays the digits searches the table for the numbers that correspond to their decimal value.
It's 4 digits.
Code: |
const char segmentMap[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, // 0-7
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, // 8-9, A-F
0x00
};
char digitos[8];
int sec, h, m, s;
...
void Mostra_digitos(void)
{
h = (sec/3600);
m = (sec -(3600*h))/60;
s = (sec -(3600*h)-(m*60));
sprintf(digitos,"%02d%02d%02d",h,m,s);
start();
writeByte(0x40);
ask();
Stop();
Start();
writeByte(0xc0);
ask();
writeByte(segmentMap[digitos[0]]);//m
ask();
writeByte(segmentMap[digitos[1]]);//m
ask();
writeByte(segmentMap[digitos[2]]);//s
ask();
writeByte(segmentMap[digitos[3]]);//s
ask();
Stop();
}
//--- |
Code: |
...
sec = 360;
Mostra_digitos(); |
This code snippet converts seconds into hours, and returns the correct value if using a terminal or simulator.
Code: | h = (sec/3600);
m = (sec -(3600*h))/60;
s = (sec -(3600*h)-(m*60));
sprintf(digitos,"%02d%02d%02d",h,m,s);
... |
sec=360 returns 000600
I tried using 'sprintf' to load the buffer with digits[0]=0,digits[1]=0,digits[2]=0,digits[3]=6, digits[4]=0 and digits[5]=0 but 'sprintf' didn't load the buffer this way.
How could I do? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: sprintf buffer |
Posted: Fri May 06, 2022 8:19 pm |
|
|
vtrx wrote: |
int sec, h, m, s;
sec = 360;
|
From a previous topic, you are using the 18F4550.
In the PCH compiler, an 'int' is an unsigned 8-bit number.
It can only go up to 255. You are trying to put 360 into an int.
This will not work. |
|
![](templates/subSilver/images/spacer.gif) |
PrinceNai
Joined: 31 Oct 2016 Posts: 463 Location: Montenegro
|
|
Posted: Fri May 06, 2022 8:26 pm |
|
|
Was just writing this. Your declarations are not ok.
Quote: |
int sec, h, m, s;
h = (sec/3600);
m = (sec -(3600*h))/60;
s = (sec -(3600*h)-(m*60));
|
This can't work, since you declared at least sec as a wrong type (a number between 0 and 255), than divided that by 3600 and expected to get an integer bigger than 0 from that division. Try it in Excel with the result cell formatted as a number without decimals. Always 0. sec should be int16 |
|
![](templates/subSilver/images/spacer.gif) |
vtrx
Joined: 11 Oct 2017 Posts: 142
|
|
Posted: Fri May 06, 2022 8:57 pm |
|
|
You are really correct!
I tested the conversion code using this page.
https://www.programiz.com/c-programming/online-compiler/
Code: | int sec, h, m, s;
printf("Input seconds: ");
scanf("%d", &sec);
h = (sec/3600);
m = (sec -(3600*h))/60;
s = (sec -(3600*h)-(m*60));
printf("H:M:S - %02d:%02d:%02d\n",h,m,s);
return 0; |
Is it necessary for h,m and s to be int16 too?
I just changed sec to int16 and it worked.
thank you all. |
|
![](templates/subSilver/images/spacer.gif) |
PrinceNai
Joined: 31 Oct 2016 Posts: 463 Location: Montenegro
|
|
Posted: Fri May 06, 2022 9:23 pm |
|
|
If you know the size of the result, it isn't or shouldn't be necessary. If not, you lose 3 bytes of RAM and some extra processor time by declaring them all as int16. From personal experience it is better to work with the same size of the variables. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|