I solved the problem, but I donβt know how to publish it in a good manner, so I will edit this entry and put it at the end.
Need help with the following in C, trying to shift the bytes in the reverse order.
I want Step1[] = {1,0,0,1,0,0,0,0}; became {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)
I know that bridcontrol is absolutely wrong, here I may need help! Yours faithfully
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]; }
Decision:
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 need the reverse order of the bits in the array, just change tempvalue[8-i] to tempvalue[i-1] .