# Led Module Driving

## Recommended Posts

Hi all,

I am driving led modules from portb on my pic.

I need to take a value (either arbitory or from the adc) and split the number into ones, tens and hundreds.

I realise that you have to use a bit mask, and this is the problem.

Getting the ones value is straight forward, with, say, number = number&0x0f;.

However, getting the tens is more difficult, say, number = number&0xf0;.

The returned value is followed by a 0, what I would like to know is if anyone has a way of swapping the bit values, hence getting rid of the 0?

Hope it makes sense.

Regards,

Daz.

##### Share on other sites
I need to take a value (either arbitory or from the adc) and split the number into ones, tens and hundreds.

I realise that you have to use a bit mask, and this is the problem.

Getting the ones value is straight forward, with, say, number = number&0x0f;.

As you are using hexa decimal and four bit masks Am I correct in assuming you mean split into ones, sixteens, and 256s?

However, getting the tens is more difficult, say, number = number&0xf0;.

The returned value is followed by a 0, what I would like to know is if anyone has a way of swapping the bit values, hence getting rid of the 0?

number = (number&0xf0) >> 4;.

##### Share on other sites

I need to take a value (either arbitory or from the adc) and split the number into ones, tens and hundreds.

I realise that you have to use a bit mask, and this is the problem.

Getting the ones value is straight forward, with, say, number = number&0x0f;.

As you are using hexa decimal and four bit masks Am I correct in assuming you mean split into ones, sixteens, and 256s?

However, getting the tens is more difficult, say, number = number&0xf0;.

The returned value is followed by a 0, what I would like to know is if anyone has a way of swapping the bit values, hence getting rid of the 0?

number = (number&0xf0) >> 4;.

Yes you're correct.

In that case if the number value is decimal how would that work out?

##### Share on other sites
Yes you're correct.

In that case if the number value is decimal how would that work out?

You can use C's MOD (%)and DIV(/) operators

ones = number % 10;

tens = (number / 10) % 10;

hundreds = number % 100;

##### Share on other sites

Thanks Picxie,

It's a great help.

##### Share on other sites
Thanks Picxie,

It's a great help.

You can use C's MOD (%)and DIV(/) operators

ones = number % 10;

tens = (number / 10) % 10;

hundreds = number % 100;

Hi Picxie,

I think its,

hundreds=number/100;

tens=(number%100)/10;

units=(number%100)%10;

Regards

Raghunathan

##### Share on other sites
Hi Picxie,

I think its,

hundreds=number/100;

tens=(number%100)/10;

units=(number%100)%10;

I think you might find its actually:

```    units=(number%10);
tens=(number%100)/10;
hundreds=number/100;```

Regards

Dave

##### Share on other sites
Hi Picxie,

I think its,

hundreds=number/100;

tens=(number%100)/10;

units=(number%100)%10;

I think you might find its actually:

```    units=(number%10);
tens=(number%100)/10;
hundreds=number/100;```

Regards

Dave

Actually both are right.

Lets say the number is 987.

hundreds = 987/100 = 9

tens = (987%100)/10 = 87/10 = 8

units= (87)%10 = 7

Raghunathan

Edited by ra68gi
##### Share on other sites

Actually, all of my formulae work just as well except the one for generating hundreds which I err.. a test, yes, that'll do! ... which was a test to see if any one was paying attention.

Hi Picxie,

I think its,

hundreds=number/100;

tens=(number%100)/10;

units=(number%100)%10;

I think you might find its actually:

```    units=(number%10);
tens=(number%100)/10;
hundreds=number/100;```

Regards

Dave

## Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.