[英]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: 代码的工作原理如下:
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.