【问题标题】:What does this Arduino code do?这个 Arduino 代码有什么作用?
【发布时间】:2013-09-08 16:51:48
【问题描述】:

我了解其中的大部分内容,但是,

    if (digitalRead(miso)) {      d |= 1;    }

是一句我听不懂的台词。此代码来自用于 Arduino 板的 Adafruit MAX31855 库。我正在尝试将代码移植到 c 中的 C8051F020 MCU。该代码从热电偶读取,MAX31855 是 MCU 的数字接口。这是此文件的完整代码。我不熟悉digitalRead()。上面发布的 if 语句是我无法解释的地方。此 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;
}

【问题讨论】:

    标签: c++ arduino microcontroller


    【解决方案1】:

    DigitalRead

    从指定的数字引脚读取值,HIGH 或 LOW。


    MISO(Master In Slave Out):Master的移位寄存器的输入,Slave的移位寄存器的输出。

    SPI overview


    | 是按位或运算符。

    d |= 1 是的简写符号

    d = d | 1
    

    如果条件为真,此代码会将d 的最后一位设置为 1。


    所以你正在做的是读取从寄存器的输出,如果它是 1,它会将d 的最后一位设置为 1。就在该行之前,它将d 左移一位d &lt;&lt;= 1;。它会循环执行此操作:

    • d 左移 1 位
    • 读取味噌,如果是1,将d的最低有效位设置为1
    • 重复

    【讨论】:

    • 接受这个答案的彻底性和清晰性。谢谢。
    【解决方案2】:

    MISO 是 SPI 总线上的主输入/从输出引脚。在这段代码中,你扮演的是主人的角色,所以这是你从奴隶读取输入的引脚。 d |= 1 只是将 d 中的最后一位设置为 1,如果它从从站读取 1(并且所有其他位将不受影响)。每次迭代如果读取到 1,它将 d 的最后一位 (LSB) 设置为 1,然后在下一次迭代开始时将 d 向左移动。

    【讨论】:

    • 很好的解释。循环执行 32 次迭代,因此如果 miso 每次都打开,那么 d 中的所有位都将等于 1
    【解决方案3】:

    我对这个 API 一点经验都没有,但是...

    如果您查看digitalRead() documentation,该函数所做的只是读取您作为参数提供的引脚上的位。它返回HIGHLOW

    所以基本上,我所看到的:如果miso 位打开,则将d 中的第一个位打开。

    【讨论】:

    • 这对我来说最有意义。我阅读了相同的文档,但让我感到困惑的是 digitalRead(miso) 没有与 if 语句中的任何内容进行比较。谢谢
    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2014-07-30
    相关资源
    最近更新 更多