View previous topic :: View next topic |
Author |
Message |
rhaguiuda
Joined: 05 Sep 2007 Posts: 46 Location: Londrina - Brazil
|
Code to split a number in digits (for 7-seg displays) |
Posted: Fri Jul 04, 2008 12:09 pm |
|
|
This is a very simple code that I've used to split a number into single digits.
Code: |
#include <string.h>
char string[7],Data[7];
int32 Result;
sprintf(string, "%6Ld", Result);
Data[0] = string[5];
Data[1] = string[4];
Data[2] = string[3];
Data[3] = string[2];
Data[4] = string[1];
Data[5] = string[0];
|
In this code Result is the number to split and Data holds digits already splited.
When I used this code, I had six 7-segment display, that's why I got 6 variables Data, one for each digit. |
|
|
ratgod
Joined: 27 Jan 2006 Posts: 69 Location: Manchester, England
|
|
Posted: Wed Sep 10, 2008 11:01 am |
|
|
Here is another version done with purely numbers:
a little longer than the string version, but doesn't require any extra files to be included.
This has been written for 4 - digit screen, but can be easily expanded.
I used this in a 4-digit 7-segment display driver and works excellent.
Code: |
{
int16 temp;
if (inval<10)
{
screen_buffer[0]=0;
screen_buffer[1]=0;
screen_buffer[2]=0;
screen_buffer[3]=inval;
}
else if (inval<100)
{
screen_buffer[0]=0;
screen_buffer[1]=0;
screen_buffer[2]=inval/10;
screen_buffer[3]=inval % 10;
}
else if (inval<1000)
{
screen_buffer[0]=0;
screen_buffer[1]=inval/100;
temp=inval%100;
screen_buffer[2]=temp/10;
screen_buffer[3]=temp % 10;
}
else if (inval<9999)
{
screen_buffer[0]=inval/1000;
temp=inval%1000;
screen_buffer[1]=temp/100;
temp=temp%100;
screen_buffer[2]=temp/10;
screen_buffer[3]=temp % 10;
}
else
{
screen_buffer[0]=0;
screen_buffer[1]=0;
screen_buffer[2]=0;
screen_buffer[3]=0;
}
} |
|
|
|
horde_fuego
Joined: 31 Mar 2009 Posts: 11
|
|
Posted: Tue May 19, 2009 7:51 am |
|
|
Hello! Can you give a simple example using this technique? Thanks! |
|
|
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
|
Posted: Sat Jul 11, 2009 11:07 pm |
|
|
This is nice,
Code: |
sprintf(string, "%6Ld", Result);
Data[0] = string[5];
Data[1] = string[4];
Data[2] = string[3];
Data[3] = string[2];
Data[4] = string[1];
Data[5] = string[0]; |
but the Data[x] is not necessary, just use string[x] for the values needed. Unless there is a specific reason to transfer them to another var? |
|
|
rhaguiuda
Joined: 05 Sep 2007 Posts: 46 Location: Londrina - Brazil
|
|
Posted: Mon Jul 13, 2009 7:23 am |
|
|
MBradley
The reason I`v used Data[x] is just one of my systems need. Data[X] is not necessary for the code work! _________________ Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll feed him for a lifetime. |
|
|
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
|
Posted: Mon Jul 13, 2009 11:30 am |
|
|
Gotcha ;)
I was scratching my head, and starting to ask myself, hmm, is it needed? |
|
|
|