Jump to content

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.

Link to post
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;.

Link to post
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?

Link to post
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

Link to post
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
Link to post
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

Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Loading...
×
×
  • Create New...