【发布时间】: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.
- 我知道我必须在我的
Makefile中添加库,但我不知道该怎么做。 - 在
main.c中包含头文件时,我应该使用#include "ArduinoCore-avr/cores/arduino/Arduino.h"还是#include "Arduino.h"。 - 我想包含
Arduino.h文件的原因是我想在我的main.c中使用pinMode和digitalWrite函数。我不想重新发明轮子(从头开始编写驱动程序)。那可能吗?我试图搜索示例代码,但它们都使用 arduino 的默认函数loop和setup,这是我想要避免的。
谢谢。
【问题讨论】:
-
您可以使用此程序从命令行编译您的 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