I'm trying to create the equivalent of the arduino shiftOut()
function in native c on my mcu.
I want to send the command int gTempCmd = 0b00000011;
via a shiftOut()
type function with MSBFIRST
.
What would the pseudo code for this look like so I can try map it my mcu's gpio functions?
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++'
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
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With