|
|
View previous topic :: View next topic |
Author |
Message |
KM
Joined: 14 Mar 2004 Posts: 3
|
rotate_right() vs shift_right() and output_bit() |
Posted: Mon Nov 29, 2004 3:43 am |
|
|
Can sombody tell me the difference between: rotate_right() and shift_left()?
My problem is that I have a 16bit variable that I like to shift-out on my
DATA_PIN (3 wire bus). Ex: int16 x = 0b0101010101010101; My function looks like
this:
void write(int16 x)
{
int i=0;
output_high(RW_PIN);
for(i=0; i<SIZE; i++)
{
output_bit(DATA_PIN, shift_right(&x,2,0)); //somethings wrong here!
output_high(CLOCK_PIN);
delay_ms(TIME);
output_low(CLOCK_PIN);
printf("value: %D\n ", &x); //Only for Debugging! And here!!
}
output_low(RW_PIN);
}
I need the printf() function to print the value 1 or 0 in the debugger-
monitor so i know that the right bitpattern is shifted out, but it only outputs: Dec 68,68,68....etc.
Please help! |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Mon Nov 29, 2004 4:52 am |
|
|
What if you do this:
Code: |
int16 y;
y=x;
...
...
...
output_bit(DATA_PIN, shift_right(&y,2,0));
|
|
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 29, 2004 5:00 am |
|
|
'&x', is the _address_ of 'x'. The shift function needs this, but printf, wants the 'value' of x. At present, it is just printing the same address each time. Change the printf line to:
Code: |
printf("value: %D\n ", x); //Only for Debugging! And here!!
| .
Shift, and rotate, differ , in what happens at the 'end' of the value. If (for instance), you rotate the binary value:
11110000
once left, you get:
11100001
with the bit from the left end, moves 'round' to the right end.
If instead you shift the value, you get:
11100000
with the top bit being dropped off the end. This bit is returned as the value from the call. Left, and right, determine which direction the data is moved.
Does your 'target', want LSB first (this is what 'shift_right', will send)?. Most standards for things like I2C etc., send MSB first, and so would need a shift_left to send the data required. Also make sure that as part of your initialisation, you take the clock and data lines high, then send the required 'start' condition, before the data.
Best Wishes |
|
|
me Guest
|
get the following error messages.. |
Posted: Mon Nov 29, 2004 6:33 am |
|
|
iŽll get the following error messages:
printf("value is: %D\n", x);
PRINTFORMAT TYPE IS INVALID.
reg KM |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Nov 29, 2004 9:50 am |
|
|
Quote: | The format takes the generic form %wt where w is optional and may be 1-9 to specify how many characters are to be outputted, or 01-09 to indicate leading zeros or 1.1 to 9.9 for floating point. t is the type and may be one of the following:
C
Character
S
String or character
U
Unsigned int
x
Hex int (lower case output)
X
Hex int (upper case output)
D
Signed int
e
Float in exp format
f
Float
Lx
Hex long int (lower case)
LX
Hex long int (upper case)
lu
unsigned decimal long
ld
signed decimal long
%
Just a %
|
Code: | printf("value is: %lu\n", x);
|
or in hex
Code: | printf("value is: %Lx\n", x);
|
Note that you are not going to get it in binary form. |
|
|
|
|
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
|