【STM32】拨码开关

拨码开关长啥样嘞?

拨码开关不像按键那样,按下检测高低电平就可以书写代码,而是需要一定的编码规则,由于我选用的是4位拨码开关。因此有16种表示方式,2^4 = 16种。

uint16 DIP_Switch()
{
  uint16 status;
  if(gpio_get (A27)==1)   status |= 0x00;
  else                    status |= 0x01;//1
  if(gpio_get (A26)==1)   status |= 0x00;
  else                    status |= 0x02;//2
  if(gpio_get (A25)==1)   status |= 0x00;
  else                    status |= 0x04;//4
  if(gpio_get (A24)==1)   status |= 0x00;
  else                    status |= 0x08;//8
  return status;
}

利用8421即可计算出最终得到的数。

发布了49 篇原创文章 · 获赞 14 · 访问量 2647

猜你喜欢

转载自blog.csdn.net/qq_42108414/article/details/88820538