原生 c 中的 arduino `shiftOut()` 函数
arduino `shiftOut()` function in native c
我正在尝试在我的单片机上用本机 c 创建与 arduino shiftOut()
函数等效的函数。
我想通过带有 MSBFIRST
的 shiftOut()
类型函数发送命令 int gTempCmd = 0b00000011;
。
这个伪代码是什么样的,这样我就可以尝试将它映射到我的 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);
....
}
考虑以下 'psuedo-c++'
代码的工作原理如下:
- 通过与除 MSB 或 LSB 之外的全零进行与运算来获取字中的最高位或最低位,具体取决于 MSBFIRST 标志
- 写入输出pin
- 将命令向右移动一步
- 脉冲时钟引脚
- 针对命令中的每一位重复 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)
}
}
我正在尝试在我的单片机上用本机 c 创建与 arduino shiftOut()
函数等效的函数。
我想通过带有 MSBFIRST
的 shiftOut()
类型函数发送命令 int gTempCmd = 0b00000011;
。
这个伪代码是什么样的,这样我就可以尝试将它映射到我的 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);
....
}
考虑以下 'psuedo-c++'
代码的工作原理如下:
- 通过与除 MSB 或 LSB 之外的全零进行与运算来获取字中的最高位或最低位,具体取决于 MSBFIRST 标志
- 写入输出pin
- 将命令向右移动一步
- 脉冲时钟引脚
- 针对命令中的每一位重复 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)
}
}