Go to Post So I thought to myself "Man, I wish I was a part of a large technically minded community... oh... right..." - BordomBeThyName [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 29-12-2007, 23:09
Robostang 548's Avatar
Robostang 548 Robostang 548 is offline
I can program the future...
AKA: Don
FRC #0548 (Robostangs)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Northville Mi
Posts: 69
Robostang 548 is on a distinguished road
Send a message via AIM to Robostang 548 Send a message via MSN to Robostang 548 Send a message via Yahoo to Robostang 548
Manipulating bits

Ok, I'm having a bit of trouble. I have been experimenting with using an off-board and so far everything has been going great. But now I'm having trouble fitting 10 gallons in an 8 gallon hat. My problem is that we have set up the serial link to send 8 bits at a time. Its perfect for sending joystick or pwm values back and forth but what happens when I want to send an integer that I have given an analog value? I know I can bit shift off the two right most bits and then replace them when they get to the coprocessor with zeros. That gives me the same value as when it was sent within 3. My question is, is there a way in c to take the two right-most bits of a value and put them into a byte and then fill up the rest of the byte with other right most bits of values so I don't wast space. I know how to cut this byte back up and distribute the bits to their corresponding values (my coprocessor runs on python) but I don't know how I would package and send something like this in C. Does anyone have a solution to this problem?

-Don
__________________

Team 548:
Attending National Championship, Genesee District, Detroit District 2, West Michigan District, Michigan Championship?


  #2   Spotlight this post!  
Unread 29-12-2007, 23:18
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: Manipulating bits

Read up on the bitwise operators:
~ (NOT)
& (AND)
^ (XOR)
| (OR)

You can use these to manipulate bits pretty easily.
__________________
Team 701
  #3   Spotlight this post!  
Unread 29-12-2007, 23:22
Robostang 548's Avatar
Robostang 548 Robostang 548 is offline
I can program the future...
AKA: Don
FRC #0548 (Robostangs)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Northville Mi
Posts: 69
Robostang 548 is on a distinguished road
Send a message via AIM to Robostang 548 Send a message via MSN to Robostang 548 Send a message via Yahoo to Robostang 548
Re: Manipulating bits

OK, got it. There was a great example on wikipedia.
Thanks!

-Don
__________________

Team 548:
Attending National Championship, Genesee District, Detroit District 2, West Michigan District, Michigan Championship?



Last edited by Robostang 548 : 29-12-2007 at 23:33. Reason: I figured it out.
  #4   Spotlight this post!  
Unread 30-12-2007, 00:27
EricVanWyk EricVanWyk is offline
Registered User
no team
 
Join Date: Jan 2007
Rookie Year: 2000
Location: Boston
Posts: 1,597
EricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond repute
Send a message via AIM to EricVanWyk
Re: Manipulating bits

It looks like you are trying to "serialize" or "marshal" your data. Googling these terms may help.

In C, one nifty trick is to recast your data as strings.

Code:
int n=4;
char * s = (char *)&n;
Then, write a function that can push strings across.
  #5   Spotlight this post!  
Unread 30-12-2007, 01:02
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is offline
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,647
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
Re: Manipulating bits

One word of warning about the PIC, though it probably doesn't apply in your case if you're just using shifts for bit manipulation. Right shifts are unsigned only. So if you have a signed int that you want to divide by 4, x >> 2 only works if the value is positive. A negative value will have 0 shifted into the high order bits and will end up looking like a positive number.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
  #6   Spotlight this post!  
Unread 04-01-2008, 12:23
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: Manipulating bits

Code:
144:                   var1 = var1 >> 2;
 04524    0101     MOVLB 0x1
 04526    90D8     BCF 0xfd8, 0, ACCESS                ; set Carry flag to '0'
 04528    33E7     RRCF 0xe7, F, BANKED                ; C -> var1.hi -> C
 0452A    33E6     RRCF 0xe6, F, BANKED                ; C -> var1.lo -> C
 0452C    90D8     BCF 0xfd8, 0, ACCESS                ; set C flag to '0'
 0452E    33E7     RRCF 0xe7, F, BANKED                ; shift again
 04530    33E6     RRCF 0xe6, F, BANKED
Its more accurate to say the compiler doesn't support signed shifts. The PIC is capable of doing signed shifts if asked to. All shifts are treated as unsigned by the compiler and have the carry in flag set to zero during the rotations. The PIC is capable of shifting in a '1' for negative values but the compile chooses not to -- 'cause all shifting operations are unsigned. Other compilers, such as HI-TECH's PIC C compiler chose to preserve the sign bit. This is a compiler specific implementation selection allowed by the ANSI C spec.

If you want to do signed shifts, you have to roll your own and change the instructions to:

Code:
    BTFSC 0xe7, 7, BANKED
    BRA     NEGVALUE_SHIFT
POSVALUE_SHIFT:
    BCF     0xfd8, 0, ACCESS                      ; Set CARRY (C) flag to '0'
    RRCF    0xe7, F, BANKED                       ; C -> var1.hi -> C
    RRCF    0xe6, F, BANKED                       ; C -> var1.lo -> C
    BCF     0xfd8, 0, ACCESS                      ; clear C flag for next shift op
    RRCF    0xe7, F, BANKED
    RRCF    0xe6, F, BANKED
    BRA     DONE_SHIFT
NEGVALUE_SHIFT:
    BSF     0xfd8, 0, ACCESS                      ; Set CARRY (C) flag to '1'
    RRCF    0xe7, F, BANKED                       ; C -> var1.hi -> C
    RRCF    0xe6, F, BANKED                       ; C -> var1.lo -> C
    BSF     0xfd8, 0, ACCESS                      ; set C flag for next shift op
    RRCF    0xe7, F, BANKED
    RRCF    0xe6, F, BANKED
    BRA     DONE_SHIFT
DONE_SHIFT:
alternatively, in C a little more convoluted... plus there are other methods.

Code:
    if (var1 > 0)
        var1 = (var1 >> 2);
    else
        var1 = (varl >> 2) | 0xC000;
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
pic: Sorting drill bits Andy Baker Extra Discussion 12 16-02-2005 21:46
Manipulating tetras from the ground. Petey Rules/Strategy 4 08-01-2005 23:19
Hanging vs. Manipulating 2x balls me9342 Rules/Strategy 41 13-01-2004 09:24
Extracting Individual bits in C Noah Programming 7 08-01-2004 15:53
Sweet Repeat Bits skrussel Off-Season Events 0 09-10-2003 21:13


All times are GMT -5. The time now is 07:13.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi