shift bits from a byte into an (array)
I solved the problem but dont know how to post it in a good manner, so I edit this post and put the solution in the end of it.
Need help with following in C, trying to shift a bytes bits to reverse order.
I want Step1[] = {1,0,0,1,0,0,0,0};
to become {0,0,0,0,1,0,0,1}
.
void Stepper(void)
{
static uint8_t Step1[] = {1,0,0,1,0,0,0,0};
BridgeControl(Step1);
}
void BridgeControl(unsigned char *value)
{
uint8_t tempValue;
uint8_t bit = 8;
uint8_t rev = 1;
if (rev) // CW otherwise CCW
{
tempValue = *value;
do{
if(tempValue) // Right-shift one
tempValue = 1 >> 1;
else
tempValue = 0 >> 1;
}while(--bit, bit);
*value 开发者_StackOverflow= tempValue;
}
I know the bridcontrol is totally wrong, here i could need help! Kind Regards
New code:
void BridgeControl(uint8_t *value)
{
// For example, initial value could be 1001000 then I
// would like the outcome to be 00001001
uint8_t tempValue;
uint8_t bit = 3;
uint8_t rev = 1;
if (rev) // CW otherwise CCW
{
tempValue = *value; //so... its 0b10010000
do{
tempValue >>=1; //1st this produce 01001000
tempValue = 0 >> 1; //1st this produce 0010 0100
//2nd time produce 0001 0010
//3d time produce 0000 1001
}while(--bit, bit);
*value = tempValue;
}
M1BHI = value[7];
M1BLI = value[6];
M1AHI = value[5];
M1ALI = value[4];
M2BHI = value[3];
M2BLI = value[2];
M2AHI = value[1];
M2ALI = value[0];
}
Solution:
void BridgeControl(uint8_t value)
{
uint8_t tempvalue[8];
uint8_t i = 8;
uint8_t cont;
cont = value;
do{
value = value >> i-1;
value = value & 1;
tempvalue[8-i] = value;
value = cont;
}while(--i,i);
M1BHI = tempvalue[7];
M1BLI = tempvalue[6];
M1AHI = tempvalue[5];
M1ALI = tempvalue[4];
M2BHI = tempvalue[3];
M2BLI = tempvalue[2];
M2AHI = tempvalue[1];
M2ALI = tempvalue[0];
}
If I want the reversed order of the bits in the array, just change tempvalue[8-i]
to tempvalue[i-1]
.
Your variable names sounds like you are trying to work with hardware. So i guess you really want to shift bits in one byte variable and not in an int array.
This statment reverses the bits in a byte:
byte reversedVal = (byte) (val & 1 << 7
+ val & 2 << 5
+ val & 4 << 3
+ val & 8 << 1
+ val & 16 >> 1
+ val & 32 >> 3
+ val & 64 >> 5
+ val & 128 >> 7);
If you really want to reverse a int array you can use LINQs Reverse
method as suggested by scottm but thats probably not the fastest option.
Easy-cheesy with Array.Reverse():
byte[] step1 = new {0,1,0,1};
var reversed = Array.Reverse(step1);
If you actually need to swap endianess, you can look at the answer here.
void BridgeControl(unsigned char values[], int size){
unsigned char *head, *end, wk;
for(head = values, end = values + size - 1; head < end; ++head, --end){
wk = *head;
*head = *end;
*end = wk;
}
}
void Stepper(void){
static unsigned char Step1[] = {1,0,0,1,0,0,0,0};
BridgeControl(Step1, sizeof(Step1));
}
// changed the parameter to suit the type of members of array
void BridgeControl(uint8_t *value)
{
uint8_t tempvalue;
// only need to loop till midway; consider the two sides as lateral images of each other from the center
for (int i=0; i < 8/2; i++)
{
// preserve the current value temporarily
tempvalue = *[value + i];
// set the current value with the value in the mirrored location
// (8th position is the mirror of 1st; 7th position is the mirror of 2nd and so on)
*[value + i] = *[value + (7 - i)];
// change the value in the mirrored location with the value stored temporarily
*[value + (7 - i)] = tempvalue;
}
}
// you may want to use sizeof(uint8_t) * (7 - i) instead of (7 - i) above
//Exempel of input going to function: int value = 0b10010000;
void BridgeControl(uint8_t value uint8_t dir)
{
uint8_t tempvalue[8];
uint8_t i = 8;
uint8_t cont;
cont = value;
if (dir){
do{
value = value >> i-1;
value = value & 1;
tempvalue[8-i] = value;
value = cont;
}while(--i,i);
}
else{
do{
value = value >> i-1;
value = value & 1;
tempvalue[i-1] = value;
value = cont;
}while(--i,i);
}
M1BHI = tempvalue[7];
M1BLI = tempvalue[6];
M1AHI = tempvalue[5];
M1ALI = tempvalue[4];
M2BHI = tempvalue[3];
M2BLI = tempvalue[2];
M2AHI = tempvalue[1];
M2ALI = tempvalue[0];
}
精彩评论