简体   繁体   English

本机c中的arduino`shiftOut()`函数

[英]arduino `shiftOut()` function in native c

I'm trying to create the equivalent of the arduino shiftOut() function in native c on my mcu. 我正在尝试在我的mcu上创建本机c中的arduino shiftOut()函数的等价物。

I want to send the command int gTempCmd = 0b00000011; 我想发送命令int gTempCmd = 0b00000011; via a shiftOut() type function with MSBFIRST . 经由shiftOut()类型功能MSBFIRST

What would the pseudo code for this look like so I can try map it my mcu's gpio functions? 伪代码会是什么样子,所以我可以尝试将它映射到我的mcu的gpio函数中?

Thanks 谢谢

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);
  ....
}

Consider the following 'psuedo-c++' 考虑以下'psuedo-c ++'

The code works as follows: 代码的工作原理如下:

  • take the highest, or lowest bit in the word by AND-ing with all zero's except the MSB or LSB, depending on the MSBFIRST flag 取决于MSBFIRST标志,除了MSB或LSB之外,通过与所有零进行AND运算来获取字中的最高位或最低位。
  • write it to the output pin 将其写入输出引脚
  • shift the command one step in the right direction 将命令向右移动一步
  • pulse the clock pin 脉冲时钟引脚
  • repeat 8 times, for each bit in the command 对命令中的每个位重复8次

It is fairly trivial to expand this to an arbitrary number of bits upto 32 by adding a parameter for the number of repetitions 通过为重复次数添加参数,将其扩展到高达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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM