CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Code to split a number in digits (for 7-seg displays)

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
rhaguiuda



Joined: 05 Sep 2007
Posts: 46
Location: Londrina - Brazil

View user's profile Send private message

Code to split a number in digits (for 7-seg displays)
PostPosted: Fri Jul 04, 2008 12:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 10, 2008 11:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 19, 2009 7:51 am     Reply with quote

Hello! Can you give a simple example using this technique? Thanks!
mbradley



Joined: 11 Jul 2009
Posts: 118
Location: California, USA

View user's profile Send private message Visit poster's website

PostPosted: Sat Jul 11, 2009 11:07 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jul 13, 2009 7:23 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Mon Jul 13, 2009 11:30 am     Reply with quote

Gotcha ;)

I was scratching my head, and starting to ask myself, hmm, is it needed?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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