如题,有16个字节的数据,一个数据对应一个IO口,从IO口输出相应的高低电平,总共有16个IO口,并且需要并行从STM32的一个16位的口比如PA口输出,所以首先需要做串行数据转化为并行数据,把这16个8位的数据转为8个16位数据。
我现在的程序如下:
unsigned char in_data[16];
unsigned int out_data[8];
out_data[0] |= (0x80 & in_data[15])<<8;
out_data[0] |= (0x80 & in_data[14])<<7;
out_data[0] |= (0x80 & in_data[13])<<6;
out_data[0] |= (0x80 & in_data[12])<<5;
out_data[0] |= (0x80 & in_data[11])<<4;
out_data[0] |= (0x80 & in_data[10])<<3;
out_data[0] |= (0x80 & in_data[9])<<2;
out_data[0] |= (0x80 & in_data[8])<<1;
out_data[0] |= (0x80 & in_data[7]);
out_data[0] |= (0x80 & in_data[6])>>1;
out_data[0] |= (0x80 & in_data[5])>>2;
out_data[0] |= (0x80 & in_data[4])>>3;
out_data[0] |= (0x80 & in_data[3])>>4;
out_data[0] |= (0x80 & in_data[2])>>5;
out_data[0] |= (0x80 & in_data[1])>>6;
out_data[0] |= (0x80 & in_data[0])>>7;
out_data[1] |= (0x40 & in_data[15])<<9;
out_data[1] |= (0x40 & in_data[14])<<8;
out_data[1] |= (0x40 & in_data[13])<<7;
out_data[1] |= (0x40 & in_data[12])<<6;
out_data[1] |= (0x40 & in_data[11])<<5;
out_data[1] |= (0x40 & in_data[10])<<4;
out_data[1] |= (0x40 & in_data[9])<<3;
out_data[1] |= (0x40 & in_data[8])<<2;
out_data[1] |= (0x40 & in_data[7])<<1;
out_data[1] |= (0x40 & in_data[6]);
out_data[1] |= (0x40 & in_data[5])>>1;
out_data[1] |= (0x40 & in_data[4])>>2;
out_data[1] |= (0x40 & in_data[3])>>3;
out_data[1] |= (0x40 & in_data[2])>>4;
out_data[1] |= (0x40 & in_data[1])>>5;
out_data[1] |= (0x40 & in_data[0])>>6;
out_data[2] |= (0x20 & in_data[15])<<10;
out_data[2] |= (0x20 & in_data[14])<<9;
out_data[2] |= (0x20 & in_data[13])<<8;
out_data[2] |= (0x20 & in_data[12])<<7;
out_data[2] |= (0x20 & in_data[11])<<6;
out_data[2] |= (0x20 & in_data[10])<<5;
out_data[2] |= (0x20 & in_data[9])<<4;
out_data[2] |= (0x20 & in_data[8])<<3;
out_data[2] |= (0x20 & in_data[7])<<2;
out_data[2] |= (0x20 & in_data[6])<<1;
out_data[2] |= (0x20 & in_data[5]);
out_data[2] |= (0x20 & in_data[4])>>1;
out_data[2] |= (0x20 & in_data[3])>>2 ;
out_data[2] |= (0x20 & in_data[2])>>3 ;
out_data[2] |= (0x20 & in_data[1])>>4 ;
out_data[2] |= (0x20 & in_data[0])>>5 ;
out_data[3] |= (0x10 & in_data[15])<<11;
out_data[3] |= (0x10 & in_data[14])<<10;
out_data[3] |= (0x10 & in_data[13])<<9;
out_data[3] |= (0x10 & in_data[12])<<8;
out_data[3] |= (0x10 & in_data[11])<<7;
out_data[3] |= (0x10 & in_data[10])<<6;
out_data[3] |= (0x10 & in_data[9])<<5;
out_data[3] |= (0x10 & in_data[8])<<4;
out_data[3] |= (0x10 & in_data[7])<<3;
out_data[3] |= (0x10 & in_data[6])<<2;
out_data[3] |= (0x10 & in_data[5])<<1;
out_data[3] |= (0x10 & in_data[4]);
out_data[3] |= (0x10 & in_data[3])>>1;
out_data[3] |= (0x10 & in_data[2])>>2;
out_data[3] |= (0x10 & in_data[1])>>3;
out_data[3] |= (0x10 & in_data[0])>>4;
out_data[4] |= (0x08 & in_data[15])<<12;
out_data[4] |= (0x08 & in_data[14])<<11;
out_data[4] |= (0x08 & in_data[13])<<10;
out_data[4] |= (0x08 & in_data[12])<<9;
out_data[4] |= (0x08 & in_data[11])<<8;
out_data[4] |= (0x08 & in_data[10])<<7;
out_data[4] |= (0x08 & in_data[9])<<6;
out_data[4] |= (0x08 & in_data[8])<<5;
out_data[4] |= (0x08 & in_data[7])<<4;
out_data[4] |= (0x08 & in_data[6])<<3;
out_data[4] |= (0x08 & in_data[5])<<2;
out_data[4] |= (0x08 & in_data[4])<<1;
out_data[4] |= (0x08 & in_data[3]);
out_data[4] |= (0x08 & in_data[2])>>1;
out_data[4] |= (0x08 & in_data[1])>>2;
out_data[4] |= (0x08 & in_data[0])>>3;
out_data[5] |= (0x04 & in_data[15])<<13;
out_data[5] |= (0x04 & in_data[14])<<12;
out_data[5] |= (0x04 & in_data[13])<<11;
out_data[5] |= (0x04 & in_data[12])<<10;
out_data[5] |= (0x04 & in_data[11])<<9;
out_data[5] |= (0x04 & in_data[10])<<8;
out_data[5] |= (0x04 & in_data[9])<<7;
out_data[5] |= (0x04 & in_data[8])<<6;
out_data[5] |= (0x04 & in_data[7])<<5;
out_data[5] |= (0x04 & in_data[6])<<4;
out_data[5] |= (0x04 & in_data[5])<<3;
out_data[5] |= (0x04 & in_data[4])<<2;
out_data[5] |= (0x04 & in_data[3])<<1;
out_data[5] |= (0x04 & in_data[2]);
out_data[5] |= (0x04 & in_data[1])>>1;
out_data[5] |= (0x04 & in_data[0])>>2;
out_data[6] |= (0x02 & in_data[15])<<14;
out_data[6] |= (0x02 & in_data[14])<<13;
out_data[6] |= (0x02 & in_data[13])<<12;
out_data[6] |= (0x02 & in_data[12])<<11;
out_data[6] |= (0x02 & in_data[11])<<10;
out_data[6] |= (0x02 & in_data[10])<<9;
out_data[6] |= (0x02 & in_data[9])<<8;
out_data[6] |= (0x02 & in_data[8])<<7;
out_data[6] |= (0x02 & in_data[7])<<6;
out_data[6] |= (0x02 & in_data[6])<<5;
out_data[6] |= (0x02 & in_data[5])<<4;
out_data[6] |= (0x02 & in_data[4])<<3;
out_data[6] |= (0x02 & in_data[3])<<2;
out_data[6] |= (0x02 & in_data[2])<<1;
out_data[6] |= (0x02 & in_data[1]);
out_data[6] |= (0x02 & in_data[0])>>1;
out_data[7] |= (0x01 & in_data[15])<<15;
out_data[7] |= (0x01 & in_data[14])<<14;
out_data[7] |= (0x01 & in_data[13])<<13;
out_data[7] |= (0x01 & in_data[12])<<12;
out_data[7] |= (0x01 & in_data[11])<<11;
out_data[7] |= (0x01 & in_data[10])<<10;
out_data[7] |= (0x01 & in_data[9])<<9;
out_data[7] |= (0x01 & in_data[8])<<8;
out_data[7] |= (0x01 & in_data[7])<<7;
out_data[7] |= (0x01 & in_data[6])<<6;
out_data[7] |= (0x01 & in_data[5])<<5;
out_data[7] |= (0x01 & in_data[4])<<4;
out_data[7] |= (0x01 & in_data[3])<<3;
out_data[7] |= (0x01 & in_data[2])<<2;
out_data[7] |= (0x01 & in_data[1])<<1;
out_data[7] |= (0x01 & in_data[0]);
由于有多组这样的数据,转化时占用CPU时间比较多,看一下大家有没什么更好的算法?能压缩这个转化的时间。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
- #include "stdio.h"
- #include "stdint.h"
- #include "stdlib.h"
- void bit_swap(uint8_t * in, uint16_t * out)
- {
- uint32_t * dat_grp = (uint32_t *)in;
- uint32_t val;
- /* Load new Group */
- val = *(dat_grp++);
- out[0] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28; val >>= 1;
- out[1] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28; val >>= 1;
- out[2] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28; val >>= 1;
- out[3] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28; val >>= 1;
- out[4] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28; val >>= 1;
- out[5] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28; val >>= 1;
- out[6] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28; val >>= 1;
- out[7] = (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 28;
-
- /* Load new Group */
- val = *(dat_grp++);
- out[0] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24; val >>= 1;
- out[1] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24; val >>= 1;
- out[2] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24; val >>= 1;
- out[3] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24; val >>= 1;
- out[4] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24; val >>= 1;
- out[5] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24; val >>= 1;
- out[6] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24; val >>= 1;
- out[7] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 24;
-
- /* Load new Group */
- val = *(dat_grp++);
- out[0] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20; val >>= 1;
- out[1] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20; val >>= 1;
- out[2] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20; val >>= 1;
- out[3] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20; val >>= 1;
- out[4] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20; val >>= 1;
- out[5] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20; val >>= 1;
- out[6] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20; val >>= 1;
- out[7] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 20;
-
- /* Load new Group */
- val = *(dat_grp++);
- out[0] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16; val >>= 1;
- out[1] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16; val >>= 1;
- out[2] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16; val >>= 1;
- out[3] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16; val >>= 1;
- out[4] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16; val >>= 1;
- out[5] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16; val >>= 1;
- out[6] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16; val >>= 1;
- out[7] |= (((val & 0x01010101) * 0x10204080) & 0xF0000000) >> 16;
- }
- int main(void)
- {
- int i;
- uint8_t input[16] =
- {
- 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
- 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42
- };
- uint16_t output[8];
- bit_swap(input, output);
- /* Dump */
- printf("Input:
");
- for(i = 0; i < 16; i++)
- {
- printf("0x%02X", input[i]);
- }
- printf("
");
- //
- printf("Output:
");
- for(i = 0; i < 8; i++)
- {
- printf("0x%04X
", output[i]);
- }
- }
复制代码print.png (15.83 KB, 下载次数: 0)
下载附件
2018-6-14 19:43 上传
一周热门 更多>