原生c中的arduino`shiftOut()`函数

pel*_*can 5 c arduino bit

我正在尝试shiftOut()在我的 mcu 上的原生 c 中创建等效的 arduino函数。

我想int gTempCmd = 0b00000011;通过shiftOut()带有MSBFIRST.

这个伪代码看起来像什么,所以我可以尝试将它映射到我的 mcu 的 gpio 功能?

谢谢

float readTemperatureRaw()
{
  int val;

  // Command to send to the SHT1x to request Temperature
  int gTempCmd  = 0b00000011;

  sendCommandSHT(gTempCmd);
  ...
  return (val);
}


//Send Command to sensor
void sendCommandSHT(int command)
{
  int ack;

  shiftOut(dataPin, clockPin, MSBFIRST, command);
  ....
}
Run Code Online (Sandbox Code Playgroud)

Pel*_*lle 3

考虑以下“psuedo-c++”

代码的工作原理如下:

  • 通过与除 MSB 或 LSB 之外的全零进行 AND 运算,获取字中的最高位或最低位,具体取决于 MSBFIRST 标志
  • 将其写入输出引脚
  • 将命令向正确的方向移动一步
  • 脉冲时钟引脚
  • 对于命令中的每个位重复 8 次

通过添加重复次数参数将其扩展为任意位数(最多 32 位)是相当简单的

void shiftOut(GPIO dataPin, GPIO clockPin, bool MSBFIRST, uint8_t command)
{
   for (int i = 0; i < 8; i++)
   {
       bool output = false;
       if (MSBFIRST)
       {
           output = command & 0b10000000;
           command = command << 1;
       }
       else
       {
           output = command & 0b00000001;
           command = command >> 1;
       }
       writePin(dataPin, output);
       writePin(clockPin, true);
       sleep(1)
       writePin(clockPin, false);
       sleep(1)
    }
}
Run Code Online (Sandbox Code Playgroud)