• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

simple C question

Colt45

Lifer
I can't seem to think of how to do this, but there must be a simple way.

I've got an ADC coming in, dumping the value into a char.. so 0...255.

I need to take the value and split each decimal digit into its own char.

eg.
adc in = 123

char A = 1
char B = 2
char C = 3

I can't seem to think of an easy way to do it though? 🙁
 
I came up with this abortion.. seems to work.. not exactly pretty though

char_A = ADC_IN;
char_B = ADC_IN;
char_C = ADC_IN;
char_A /= 100;
char_B -= char_A * 100;
char_B /= 10;
char_C -= char_A * 100;
char_C -= char_B * 10;
char_C /= 1;
 
Well as long as it works..
I actually don't use C, only been using C++ so I don't know.

Here's a idea.

char str[80] = "235";
(str) or (str + 0) points to the '2'
(str + 1) points to 1st '3'
(str + 2) points to 2nd '5'

Then you could throw it into a variable.

This is how I'd do it in C++, I think it would be similar in C..

 
Okay, hopefully I didn't ready the post wrong.

here ya go:

#include <stdio.h>

int main(){

int num = 123, i, index = 3;
int num_chop[index];

for (i = (index-1); num != 0; i--){
num_chop = num%10;
num /= 10;
}

for (i = 0; i < index; i++){
printf ("\nnum_chop[%d]= %d", i, num_chop );
}

return 0;
}
 
num is the value to seperate into digits.
so num = 123;

printout looks like this

num_chop[0] = 1
num_chop[1] = 2
num_chop[2] = 3
 
here's how I'd do it. the whole approach really boils down to a sprintf(...) call. the rest of the code just shows you a way to dynamically find string length for any integral value.
 
Colt45 what is the program for (ie intent or purpose).
there might be an even simpler approach.
 
I'm not sure how exactly you're getting the input, but if it's from user input, you could just use:

scanf("%c%c%c", &a, &b, &c);

where a, b and c are defined as characters.

If you get it as an integer already entered, just do:

my_int = 123;
a = (my_int / 100) + 48;
b = ((my_int / 10) % 10) + 48;
c = (my_int % 10) + 48;

You mentioned something similar to this above, but it shouldn't work correctly, because a single-digit integer's value is not the same as its character representation. They're 48 off, which is why I added 48 to the characters in the value above. You can also simply add '0' (the character zero).
 
Its for a PIC microcontroller.

the compiler has a built in "adc_get" sort of routine. I pull the data from that,
then I split the value into the 3 pieces, read them off an array to change them to a 7-segment value instead of binary, and send them to 3 multiplexed 7 segment displays.

I'm using a pressure sensor from one of my car ECUs that blew up. So it will be a sort of atmospheric pressure / boost / vacuum gauge.


I guess I should have made it more clear originally. The code i've got now seems to function, but I managed to blow up the micro I originally wrote it for, so now i need to rebuild the circuit to use one with a different pinout :|
I knew using brown as +5v was going to come back to haunt me. hehe.
 
I would stick with the code you already have. The only change I would make would be to remove the divide by 1 at the end. It doesn't do anything other than waste CPU cycles.
 
ah, reminds me of a electrical engineering class I had were we programed a 7 segment display on a fpga board. it was creating a stop watch.
 
Back
Top