View previous topic :: View next topic |
Author |
Message |
hisham.i
Joined: 22 Aug 2010 Posts: 43
|
decimal to binary |
Posted: Thu Sep 01, 2011 12:06 am |
|
|
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
|
|
Posted: Thu Sep 01, 2011 6:23 am |
|
|
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
|
|
Posted: Thu Sep 01, 2011 7:18 am |
|
|
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
|
|
Posted: Sat Sep 03, 2011 12:48 am |
|
|
Thank you
I tried your code Ttelmah and it works correctly:)
Thanks |
|
|
|