c++ - 在没有 Arduino 库的情况下获取引脚输入

标签 c++ c arduino

我不允许为该程序使用 Arduino 库(或任何库)。我将如何检查引脚的输入?

我发现了两个不同的函数:

在 Arduino.h 中:

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)

跟随 digitalRead 回到 pgmspace.h:

#define __LPM_enhanced__(addr)  \
(__extension__({                \
    uint16_t __addr16 = (uint16_t)(addr); \
    uint8_t __result;           \
    __asm__ __volatile__        \
    (                           \
        "lpm %0, Z" "\n\t"      \
        : "=r" (__result)       \
        : "z" (__addr16)        \
    );                          \
    __result;                   \
}))

对于第一个,我不知道比特和值(value)从何而来,我根本不理解第二个。

最佳答案

这些实现就不用去了。如下所示非常简单。

当引脚 0 为高电平时,LED13 将点亮。我在 arduino 上测试了这段代码

#include <avr/io.h>                                      // Includes all the definition of register port etc  
#ifndef F_CPU

#define F_CPU 16000000UL      //Need to include it before <util/delay.h>

#endif                                       //Change 16000000 with your crystal freq. In my case its 16 MHz

#include <util/delay.h>    //includes delay functions delay_ms and delay_us

void setup() {
  // put your setup code here, to run once:
  DDRB |= 0xFF; //Configured Port B as OP
  DDRD &= 0x00; //Configured Port D as IP
}

void loop() {
  // put your main code here, to run repeatedly:
  
  if (PIND&(0x01)) //to check pin0 of portD (which is Pin 0 of arduino)
    PORTB |= 0xFF;
  else
    PORTB &= 0x00;

}

关于c++ - 在没有 Arduino 库的情况下获取引脚输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866771/

相关文章:

android - 当 Arduino 遇上 Android

c++ - 我应该使用什么类型的工具来用 C 或 C++ 制作一个简单的模拟器

c++ - 帮助使用 boost relaxed heap

c - 在 c 中的 main() 中使用全局变量

C - 如何检查 8 位是否在 32 位中?

c++ - 使用For循环生成随机视觉噪声

c++ - Arduino电路没有响应

c++ - 错误 C2440 : 'initializing' : cannot convert from 'const wchar_t [9]' to 'LPCSTR'

c++ - 如何在 CMake 文件中添加定义 QT_NO_DEBUG_OUTPUT?

c - 我的 "Guess the password"不起作用,如何修复?