在arduino项目中使用ArduinoCore avr库

Using ArduinoCore-avr library in arduino project

本文关键字:ArduinoCore avr arduino 项目      更新时间:2023-10-16

我想在我的项目中使用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);
}
}

Makefile:

# 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 AVR核心代码。

我不会给你一个完整的教程来解决你一路上可能遇到的所有问题。我认为只要指出如何解决您目前遇到的错误就足够了,即pins_arduino.h没有找到。

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

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

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