App*_*per 5 c ubuntu avr-gcc arduino-uno
如何在 Ubuntu 上用 C 编写我的 Arduino。我听说过 avr-gcc,但所有在线教程似乎都非常乏味,并且没有带有 Arduino 引导加载程序的 AVR 芯片的选项。谁能用更简单的方法帮助我在 Ubuntu 上安装 avr-gcc 并开始用 C 语言为 Arduino 编程?
The*_*ant 10
我建议使用以下命令行选项集进行编译:
avr-gcc -c
-std=gnu99
-Os
-Wall
-ffunction-sections -fdata-sections
-mmcu=m328p
-DF_CPU=16000000
Run Code Online (Sandbox Code Playgroud)
并用于链接:
avr-gcc -Os
-mmcu=m328p
-ffunction-sections -fdata-sections
-Wl,--gc-sections
Run Code Online (Sandbox Code Playgroud)
在哪里…
-c 表示“仅编译为目标文件,不链接”-std=gnu99 意思是“我的代码符合 C99 并且我使用 GNU 扩展”-Os 意味着“针对可执行文件大小而不是代码速度进行优化”-Wall 意思是“打开(几乎)所有警告”-ffunction-sections -fdata-sections是-Wl,--gc-sections优化所必需的-mmcu=m328p 意思是“MCU 部件号是 ATmega 328P”-DF_CPU=16000000 表示“时钟频率为 16 MHz”(根据您的实际时钟频率进行调整)-Wl,--gc-sections 表示“告诉链接器删除未使用的函数和数据段”(这有助于减少代码大小)。为了实际编译您的代码,您将首先发出avr-gcc带有“仅编译标志”的命令,如下所示:
avr-gcc -c -std=gnu99 <etc.> MyProgram.c -o MyProgram.o
Run Code Online (Sandbox Code Playgroud)
然后,您将对所有源文件重复此操作。最后,您可以通过在链接模式下调用 AVR-GCC 将生成的目标文件链接在一起:
avr-gcc -Os <etc.> MyProgram.o SomeUtility.o -o TheExecutable.elf
Run Code Online (Sandbox Code Playgroud)
这会生成一个 ELF 文件,该文件不能由您的 MCU 直接执行。因此,您需要以 Intel Hex 格式从中提取有用的部分(原始机器代码):
avr-objcopy -O ihex -R .eeprom TheExecutable.elf TheExecutable.ihex
Run Code Online (Sandbox Code Playgroud)
最后,您将需要 AVRdude 将 hex 文件的内容上传到 MCU:
avrdude -C /path/to/avrdude.conf
-p m328p
-c PROGRAMMER_NAME
-b 19600
-P PORT_NAME
-U flash:w:TheExecutable.ihex:i
Run Code Online (Sandbox Code Playgroud)
在哪里…
-C /path/to/avrdude.conf 意思是“使用这个文件作为配置文件”-c PROGRAMMER_NAME 表示“我正在使用 PROGRAMMER_NAME 类型的程序员”(您需要根据您使用的程序员类型自行填写)。-b 19600 是波特率(您可能需要根据您设置的波特率或已预编程到引导加载程序中的波特率进行调整)-P PORT_NAME表示“编程器连接到端口 PORT_NAME”。在 Linux 上,它通常类似于/dev/ttyusbN,其中 N 是某个数字。-U flash:w:TheExecutable.ihex:i 表示“将 Intel Hex 格式的 TheExecutable.ihex 的内容写入闪存”。小智 6
如果您只想在已安装引导加载程序的 Arduino 上使用 C 代码。您可以在 Arduino IDE 中用 C 语言编写代码并照常编译。Sketch 实际上是一堆头文件和宏。
这是用 C 语言编写的眨眼草图:
#include <avr/io.h> //defines pins, ports etc
#include<util/delay.h> //functions for wasting time
int main (void) {
//init
DDRB |= (1<<PB5); //Data Direction Register B:
//writing a 1 to the Pin B5 bit enables output
//Event loop
while (1) {
PORTB = 0b00100000; //turn on 5th LED bit/pin in PORT B (Pin13 in Arduino)
_delay_ms (1000); //wait
PORTB = 0b00000000; //turn off all bits/pins on PB
_delay_ms (1000); //wait
} //end loop
return(0); //end program. This never happens.
}
Run Code Online (Sandbox Code Playgroud)
将其粘贴到 IDE 中并亲自尝试一下。
如果您想从 Arduino 转向在没有引导加载程序的情况下对 AVR 进行编程,我可以推荐 Elliot Williams 的精彩网络广播作为介绍。- https://www.youtube.com/watch?v=ERY7d7W-6nA
祝好运并玩得开心点 :)
| 归档时间: |
|
| 查看次数: |
3740 次 |
| 最近记录: |