加入交换机的输出

时间:2012-08-25 12:47:14

标签: c

我想知道是否有办法将交换机的输出加入一个变量,即 说我有开关1,开关2,开关3和开关4,每个开关都是单独声明的 开关上的输出是 开关1 -0 开关2 -1 开关3 -1 开关4 -0

我希望将这些输出加入一个变量,例如switch_output = 0110 然后我想把它改成一个自然数。

这可能吗?

谢谢

PS:这只是一个例子,我实际上是用18个开关来做这个,我正在使用的程序,只允许我单独声明每个开关

这是如何宣布

static  const gpio_pin_t SW0 = { .port = 2, .pin = 0};
static  const gpio_pin_t SW1 = { .port = 2, .pin = 1};
static  const gpio_pin_t SW2 = { .port = 2, .pin = 2};
static  const gpio_pin_t SW3 = { .port = 2, .pin = 3};
static  const gpio_pin_t SW4 = { .port = 2, .pin = 4};
static  const gpio_pin_t SW5 = { .port = 2, .pin = 5};
static  const gpio_pin_t SW6 = { .port = 2, .pin = 6};
static  const gpio_pin_t SW7 = { .port = 2, .pin = 7};
static  const gpio_pin_t SW8 = { .port = 2, .pin = 8};
static  const gpio_pin_t SW9 = { .port = 2, .pin = 9};
static  const gpio_pin_t SW10 = { .port = 2, .pin = 10};
static  const gpio_pin_t SW11 = { .port = 2, .pin = 11};
static  const gpio_pin_t SW12 = { .port = 2, .pin = 12};
static  const gpio_pin_t SW13 = { .port = 2, .pin = 13};
static  const gpio_pin_t SW14 = { .port = 2, .pin = 14};
static  const gpio_pin_t SW15 = { .port = 2, .pin = 15};
static  const gpio_pin_t SW16 = { .port = 2, .pin = 16};
static  const gpio_pin_t SW17 = { .port = 2, .pin = 17};
Mahmoud Fayez 我尝试了你的解决方案,它在某一点上工作,我得到的是一个自然数而不是二进制数 这是输出print screen

的打印屏幕

这是代码,我不得不修改它以使其工作

     for (i = 0; i < SwitchesCount; i++)
 {
     temp2 = GPIO_Get(Switches[i]);
     iResult = (iResult << 1) + temp2;
     printf ("%lu, ",temp2);
 }
printf ("\n iResult =  %lu \n",iResult);

static uint32_t iResult = 0;

uint32_t是无符号长

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

const int SwitchesCount = 18;
int iResult = 0;
int i = 0;

static  const gpio_pin_t switches[SwitchesCount] = {{ .port = 2, .pin = 0}, { .port = 2, .pin = 1},{ .port = 2, .pin = 2}, { .port = 2, .pin = 3}, { .port = 2, .pin = 4}, { .port = 2, .pin = 5}, { .port = 2, .pin = 6}, { .port = 2, .pin = 7}, { .port = 2, .pin = 8}, { .port = 2, .pin = 9},{ .port = 2, .pin = 10}, { .port = 2, .pin = 11}, { .port = 2, .pin = 12}, { .port = 2, .pin = 13}, { .port = 2, .pin = 14}, { .port = 2, .pin = 15}, { .port = 2, .pin = 16}, { .port = 2, .pin = 17}};

for (i = 0; i < SwitchesCount; i++)
{
    iResult = iResult << 1 + switches[i]; 
}
// you now have iResult with the value you are looking for.