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

decimal to binary

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hisham.i



Joined: 22 Aug 2010
Posts: 43

View user's profile Send private message

decimal to binary
PostPosted: Thu Sep 01, 2011 12:06 am     Reply with quote

I am reading the timer value and store it into an integer, but I want to put the first 4 bits of the integer value on port b, then after 1 sec I want to put the last 4 bits in portb.

How can I do this?

Thanks in advance.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 6:23 am     Reply with quote

Hi,

You can use 'bit masking' on the integer value to alternately look at the lower 4 bits, followed by the upper 4 bits. You would AND the original value with 0Fh to get the lower bits, and F0h to get the upper bits.

Bit shifting: http://en.wikipedia.org/wiki/Mask_%28computing%29

You don't say which PIC you are using, and whether all 8 bits of port b are available? If so, you can output the new values directly. If not, and say only 4 bits were available, the lower bits would be output directly, and the upper bits would be shifted right 4 places before being output.

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 7:18 am     Reply with quote

and of course on the PIC (available via CCS), you have the 'swap' function, which swaps the nibbles in a byte. So:
Code:

int8 timer_val;

timer_val = what_you_want;
output_b(timer_val & 0xF);
delay_ms(1000);
swap(timer_val);
output_b(timer_val & 0xF);


Best Wishes
hisham.i



Joined: 22 Aug 2010
Posts: 43

View user's profile Send private message

PostPosted: Sat Sep 03, 2011 12:48 am     Reply with quote

Thank you

I tried your code Ttelmah and it works correctly:)

Thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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