|
|
View previous topic :: View next topic |
Author |
Message |
stma
Joined: 16 Feb 2004 Posts: 26
|
shiftin byte question |
Posted: Wed Mar 09, 2005 9:52 am |
|
|
Hi,
Having solved a problem of getting a port to shift_in to a byte I am now using the code below
int n;
byte buffer[4];
for(n=0;n<=31;n++)
{
shift_right(buffer,4,input(pin_b3));
}
to try to get a 32 bit number into variable buffer.
Havent had much sucess so far.
How can i display buffer as a variable i.e. in printf?
Thanks
Steve |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Mar 09, 2005 10:45 am |
|
|
It looks like that will, very rapidly, shift the state of b3 into all 32 bits of buffer. I doubt that is what you really want to do. Do you want to clock this shifting to synchronize with some serial bit stream? Where are these bits coming from? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Guest
|
|
Posted: Thu Mar 10, 2005 2:01 am |
|
|
Hi,
There is a little bit of extra code around the shiftin. I just took it out for clarity.
The bits are coming from a CPLD. Each time in the loop I select a line on a multiplexer via portA then read in that bit from portb.3 then move to next line in the multiplexer next time so I effectively read out a 32 bit number bit by bit.
I have written code in PicBasic Pro to do this and it works fine but my C efforts haven't worked so far.
After reading the variable in to byte (which is 32bit right?) I want to display the number but I just get jibberish with printf.
I sure im doing something basic wrong. I just dont know what.
thanks for the help
Steve |
|
|
Ttelmah Guest
|
|
Posted: Thu Mar 10, 2005 3:29 am |
|
|
Byte, is 8bits. Buffer is four bytes, a 32bit 'area'.
For 'clarity', I'd have posted the code as:
Code: |
byte buffer[4];
for(n=0;n<=31;n++)
{
//code selects input bit from multiplexor
select_bit(n);
shift_right(buffer,4,input(pin_b3));
}
|
This would then make it clear what is being done.
What you describe, should work. The obvious problem may be that the data is not stored in the order you think it is.
To output the data, something like:
Code: |
for(n=0;n<4;n++)
printf("%02x",buffer[n]);
printf("\n\r");
|
Will output the 8 hex digits corresponding to the buffer.
Remember also though, that the compiler has a 32bit data type (int32). So you could use:
Code: |
int32 buffer;
for(n=0;n<=31;n++)
{
//code selects input bit from multiplexor
select_bit(n);
shift_right(&buffer,4,input(pin_b3));
}
|
and
Code: |
printf("%08Lx\n\r",buffer);
|
Which avoids the need four four seperate outputs for the bytes. If you want to access the 'byte' contents, then consider a union, which would allow you to access the same four bytes of memory, as both a single int32, or as four seperate bytes.
Best Wishes |
|
|
Guest
|
|
Posted: Thu Mar 10, 2005 3:52 am |
|
|
Most appreciated.
Many Thanks
Steve |
|
|
|
|
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
|