【问题标题】:Using ArduinoCore-avr library in arduino project在 arduino 项目中使用 ArduinoCore-avr 库
【发布时间】:2018-07-12 14:46:19
【问题描述】:

我想在我的项目中使用 ArduinoCore-avr。我不想使用 arduino 的 IDE 默认函数 setup() 和 loop()。我也不想使用 arduino 的 IDE 将 hex 文件编译并刻录到我的设备中。 ArduinoCore-avr库从ArduinoCore下载。
硬件:arduino uno (atmega328p)
目录结构:
./ArduinoCore-avr/cores/arduino/Arduino.h
./main.c
./Makefile

main.c:

#ifndef F_CPU
#define F_CPU 16000000UL // or whatever may be your frequency
#endif

#include <avr/io.h>
#include <util/delay.h>                // for _delay_ms()

#include "ArduinoCore-avr/cores/arduino/Arduino.h" // This line gave me error.

#define LED_PIN 13

int main(void)
{
    DDRB=0b11100000;//pin 13 is in output mode
    while (1) {
        PORTB=0b11100000; //make pin 13 high and power on the led
        _delay_ms(1000);
        PORTB=0b11000000; //make pin 13 low and power off the led
    _delay_ms(1000);
    }
}

生成文件:

# Name: Makefile
# Author: <insert your name here>
# Copyright: <insert your copyright message here>
# License: <insert your license reference here>

# DEVICE ....... The AVR device you compile for
# CLOCK ........ Target AVR clock rate in Hertz
# OBJECTS ...... The object files created from your source files. This list is
#                usually the same as the list of source files with suffix ".o".
# PROGRAMMER ... Options to avrdude which define the hardware you use for
#                uploading to the AVR and the interface where this hardware
#                is connected.
# FUSES ........ Parameters for avrdude to flash the fuses appropriately.

DEVICE     = atmega328p
AVRDUDE_DEVICE = m328p
CLOCK      = 16000000
PROGRAMMER = -c arduino -P /dev/tty.usbmodem1411
OBJECTS    = main.o
FUSES      = -U lfuse:w:0x64:m -U hfuse:w:0xdd:m -U efuse:w:0xff:m
HEADER     = ArduinoCore-avr/cores/arduino/Arduino.h

######################################################################
######################################################################

# Tune the lines below only if you know what you are doing:

AVRDUDE = avrdude $(PROGRAMMER) -p $(AVRDUDE_DEVICE)
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)

# symbolic targets:
all:    main.hex

.c.o:
    $(COMPILE) -c $< -o $@

.S.o:
    $(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.

.c.s:
    $(COMPILE) -S $< -o $@

flash:  all
    $(AVRDUDE) -U flash:w:main.hex:i

fuse:
    $(AVRDUDE) $(FUSES)

install: flash fuse

# if you use a bootloader, change the command below appropriately:
load: all
    bootloadHID main.hex

clean:
    rm -f main.hex main.elf $(OBJECTS)

# file targets:
main.elf: $(OBJECTS)
    $(COMPILE) -o main.elf $(OBJECTS)

main.hex: main.elf
    rm -f main.hex
    avr-objcopy -j .text -j .data -O ihex main.elf main.hex
# If you have an EEPROM section, you must also create a hex file for the
# EEPROM and add it to the "flash" target.

# Targets for code debugging and analysis:
disasm: main.elf
    avr-objdump -d main.elf

cpp:
    $(COMPILE) -E main.c

使用以下命令编译时。
make
它显示以下错误。

In file included from main.c:7:
ArduinoCore-avr/cores/arduino/Arduino.h:257:10: fatal error: pins_arduino.h: No such file or directory
 #include "pins_arduino.h"
          ^~~~~~~~~~~~~~~~
compilation terminated.
  1. 我知道我必须在我的Makefile 中添加库,但我不知道该怎么做。
  2. main.c 中包含头文件时,我应该使用#include "ArduinoCore-avr/cores/arduino/Arduino.h" 还是#include "Arduino.h"
  3. 我想包含Arduino.h文件的原因是我想在我的main.c中使用pinModedigitalWrite函数。我不想重新发明轮子(从头开始编写驱动程序)。那可能吗?我试图搜索示例代码,但它们都使用 arduino 的默认函数 loopsetup,这是我想要避免的。

谢谢。

【问题讨论】:

  • 您可以使用此程序从命令行编译您的 Arduino 代码,而不是自己构建 Makefile:platformio.org
  • @DavidGrayson:我想了解编译链是如何工作的,所以我避免使用该工具。谢谢
  • @DavidGrayson:我检查了 arduino-mk github,看起来比我想象的要困难。看来我必须弄清楚arduino库的整个包含结构,这样我才能使用简单的digitalWrite()函数。我只需要包含Arduino.h 并不是那么简单。我说的对吗?
  • 我的印象是您必须在包含路径中添加几个目录,并且您还必须将 Arduino 核心代码中的一堆 .c.cpp 文件编译到静态库中,并将静态库链接到您的程序中。我建议暂时放弃 Makefile,因为您仍在学习 GCC 的工作原理;只需编写一个运行您需要的所有命令的 shell 脚本。

标签: c++ compiler-errors arduino avr avr-gcc


【解决方案1】:

是的,如果您能够设法以正确的方式调用编译器来编译它并将其链接到您的程序中,那么应该可以使用 Arduino AVR 核心代码。

我不会为您提供完整的教程来解决您在此过程中可能会发现的所有问题。我认为只需指出如何解决您当前遇到的错误就足够了,即找不到pins_arduino.h

GCC 有一个目录列表,称为包含搜索路径。当您#include 一个文件时,GCC 在包含搜索路径中的目录中搜索该名称的文件。为了确保 GCC 可以找到 pins_arduino.h,您需要找到具有正确版本的 pins_arduino.h 的目录,并使用 GCC 的 -I 选项将其添加到包含路径中。例如:

avr-gcc -Ipath/to/folder/ ...

路径可以是相对路径或绝对路径,但相对路径更易于维护和与他人共享。

【讨论】:

  • 我发现该文件在 ArduinoCore-avr/variants/mega/pins_arduino.h 中,所以我在我的 Makefile 中添加了-Ipath./ArduinoCore-avr/variants/mega/pins_arduino.h,但它仍然输出相同的错误。
  • 我找到了github.com/sudar/Arduino-Makefile,我相信这是我需要的。但是对于我的简单程序来说还是太复杂了。
  • 您在论证中写的path. 看起来无效。在我上面写的答案中,您必须将 path/to/folder 替换为该文件夹的实际相对路径。记得删除path这个词。
猜你喜欢
  • 1970-01-01
  • 2012-10-26
  • 2015-12-01
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多