[英]What does this Arduino code do?
I understand what most of this is doing, however, the 但我知道大部分内容都是这样做的
if (digitalRead(miso)) { d |= 1; }
Is a line that I dont understand. 是一条我不明白的路线。 THis code is from the Adafruit MAX31855 library which is used for Arduino boards. 这些代码来自Adafruit MAX31855库,用于Arduino板。 I am trying to port the code to my C8051F020 MCU in c. 我想在c中将代码移植到我的C8051F020 MCU。 This code reads from a thermocouple and the MAX31855 is the digital interface to the MCU. 该代码从热电偶读取,MAX31855是MCU的数字接口。 here is the whole code for this file. 这是该文件的完整代码。 I am not familiar with digitalRead()
. 我不熟悉digitalRead()
。 The if statement posted above is where I am failing to interpret. 上面发布的if语句是我无法解释的地方。 This if statement is in the uint32_t Adafruit_MAX31855::spiread32(void)
function. 此if语句位于uint32_t Adafruit_MAX31855::spiread32(void)
函数中。
/***************************************************
This is a library for the Adafruit Thermocouple Sensor w/MAX31855K
Designed specifically to work with the Adafruit Thermocouple Sensor
----> https://www.adafruit.com/products/269
These displays use SPI to communicate, 3 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_MAX31855.h"
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>
Adafruit_MAX31855::Adafruit_MAX31855(int8_t SCLK, int8_t CS, int8_t MISO) {
sclk = SCLK;
cs = CS;
miso = MISO;
//define pin modes
pinMode(cs, OUTPUT);
pinMode(sclk, OUTPUT);
pinMode(miso, INPUT);
digitalWrite(cs, HIGH);
}
double Adafruit_MAX31855::readInternal(void) {
uint32_t v;
v = spiread32();
// ignore bottom 4 bits - they're just thermocouple data
v >>= 4;
// pull the bottom 11 bits off
float internal = v & 0x7FF;
internal *= 0.0625; // LSB = 0.0625 degrees
// check sign bit!
if (v & 0x800)
internal *= -1;
//Serial.print("\tInternal Temp: "); Serial.println(internal);
return internal;
}
double Adafruit_MAX31855::readCelsius(void) {
int32_t v;
v = spiread32();
//Serial.print("0x"); Serial.println(v, HEX);
/*
float internal = (v >> 4) & 0x7FF;
internal *= 0.0625;
if ((v >> 4) & 0x800)
internal *= -1;
Serial.print("\tInternal Temp: "); Serial.println(internal);
*/
if (v & 0x7) {
// uh oh, a serious problem!
return NAN;
}
// get rid of internal temp data, and any fault bits
v >>= 18;
//Serial.println(v, HEX);
// pull the bottom 13 bits off
int16_t temp = v & 0x3FFF;
// check sign bit
if (v & 0x2000)
temp |= 0xC000;
//Serial.println(temp);
double centigrade = v;
// LSB = 0.25 degrees C
centigrade *= 0.25;
return centigrade;
}
uint8_t Adafruit_MAX31855::readError() {
return spiread32() & 0x7;
}
double Adafruit_MAX31855::readFarenheit(void) {
float f = readCelsius();
f *= 9.0;
f /= 5.0;
f += 32;
return f;
}
uint32_t Adafruit_MAX31855::spiread32(void) {
int i;
uint32_t d = 0;
digitalWrite(sclk, LOW);
_delay_ms(1);
digitalWrite(cs, LOW);
_delay_ms(1);
for (i=31; i>=0; i--)
{
digitalWrite(sclk, LOW);
_delay_ms(1);
d <<= 1;
if (digitalRead(miso)) {
d |= 1;
}
digitalWrite(sclk, HIGH);
_delay_ms(1);
}
digitalWrite(cs, HIGH);
//Serial.println(d, HEX);
return d;
}
Reads the value from a specified digital pin, either HIGH or LOW. 从指定的数字引脚读取值,可以是HIGH或LOW。
MISO (Master In Slave Out): the input of the Master's shift register, and the output of the Slave's shift register. MISO(主机输入从机输出):主机移位寄存器的输入,以及从机移位寄存器的输出。
|
is the bitwise OR operator. 是按位OR运算符。
d |= 1
is the shorthand notation for d |= 1
是简写符号
d = d | 1
This code sets the last bit of d
to 1 if the condition is true. 如果条件为真,则此代码将d
的最后一位设置为1。
So what you're doing is reading the output of the slave register, and if it's 1, it's setting the last bit of d
to 1. Right before that line, it shifts d
left by one bit with d <<= 1;
所以你正在做的是读取从寄存器的输出,如果它是1,它将d
的最后一位设置为1.在该行之前,它将d
左移一位, d <<= 1;
. 。 And it does this in a loop: 它在一个循环中执行此操作:
d
left 1 bit Shift d
离开1位 1
, set the least significant bit of d
to 1
. 读取味噌,如果为1
,则将d
的最低有效位设置为1
。 MISO is the Master In/Slave Out pin on an SPI bus. MISO是SPI总线上的主输入/从输出引脚。 In this code you're playing the part of the Master, so that is the pin where you read input from the slave. 在此代码中,您正在播放Master的一部分,因此这是您从从站读取输入的引脚。 d |= 1 is just setting the last bit in d to 1 if it read a 1 from the slave (and all of the other bits will be unaffected). d | = 1只是将从d中的最后一位设置为1,如果它从从机读取1(并且所有其他位将不受影响)。 Every iteration it sets the last (LSB) bit of d to 1 if it read a 1, and then at the beginning of the next iteration it shifts d to the left. 每次迭代,如果读取1,则将d的最后一个(LSB)位设置为1,然后在下一次迭代开始时将d移到左侧。
I have no experience at all with this API, however... 我对这个API没有任何经验,但是......
If you view the digitalRead() documentation , all the function does is read the bit on the pin that you supply as an argument. 如果您查看digitalRead()文档 ,则所有函数都会读取您作为参数提供的引脚上的位。 It returns either HIGH
or LOW
. 它返回HIGH
或LOW
。
So basically, what I see: if miso
bit is on, make the first bit in d
on. 所以基本上,我看到了:如果miso
位打开,请在d
打开第一位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.