gpt4 book ai didi

c++ - 在arduino项目中使用ArduinoCore-avr库

转载 作者:行者123 更新时间:2023-12-02 10:58:42 25 4
gpt4 key购买 nike

我想在我的项目中使用ArduinoCore-avr。我不想使用arduino的IDE默认功能setup()和loop()。我也不想使用arduino的IDE来编译十六进制文件并将其刻录到我的设备中。 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.
  • 我知道我必须在Makefile中添加该库,但是我不知道该怎么做。
  • 将头文件包含在main.c中时,应该使用#include "ArduinoCore-avr/cores/arduino/Arduino.h"还是#include "Arduino.h"
  • 之所以要包含Arduino.h文件,是因为我想在pinMode中使用digitalWritemain.c函数。我不想重新发明轮子(从头开始编写驱动程序)。那可能吗?我尝试搜索示例代码,但是它们都使用arduino的默认函数loopsetup,这是我要避免的功能。

  • 谢谢。

    最佳答案

    是的,如果您可以设法以正确的方式调用编译器进行编译并将其链接到程序,则应该可以使用Arduino AVR核心代码。

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

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

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

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

    关于c++ - 在arduino项目中使用ArduinoCore-avr库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51308620/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com