> 文档中心 > pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

【树莓派Pico测评】- 开始编译程序2

[复制链接]

本帖最后由 fxyc87 于 2021-1-28 11:05 编辑

上次发了一次贴子 链接

讲如何编译程序,不过没成功,后来讲如何用MicroPython来玩

后来不死心,再试,终于算是成功了

以下分享出过程:

1.先下载SDK,GIT,VS2019 C++工具,GCC编译器,具体方法官方PDF里有,设置系统环境变量,最好为绝对地址,见图:

pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

20210128093919.png (15.67 KB, 下载次数: 0)

2021-1-28 10:57 上传

2.下载 pico-project-generator ,它是一个GUI CMAKE工具,下载后,在其主目录有这个文件 pico_project.py

不能直接执行,需要给个参数,建议在其目录创建一个批处理文件 gui.bat,内容如下:

python pico_project.py --gui

这样以后直接执行 gui.bat即可,界面如下:

pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

企业微信截图_20210128090019.png (81.68 KB, 下载次数: 0)

2021-1-28 10:57 上传

3.创建后项目位置后,修改你的程序,我的程序如下:

#include

#include "pico/stdlib.h"

#include "hardware/uart.h"

#include "hardware/gpio.h"

#include "hardware/divider.h"

// UART defines

// By default the stdout UART is `uart0`, so we will use the second one

#define UART_ID uart1

#define BAUD_RATE 9600

// Use pins 4 and 5 for UART1

// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments

#define UART_TX_PIN 4

#define UART_RX_PIN 5

// GPIO defines

// Example uses GPIO 2

#define LED 25 //led 引脚

int main()

{

stdio_init_all();

// Set up our UART

uart_init(UART_ID, BAUD_RATE);

// Set the TX and RX pins by using the function select on the GPIO

// Set datasheet for more information on function select

gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);

gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);

// GPIO initialisation.

// We will make this GPIO an input, and pull it up by default

gpio_init(LED);

gpio_set_dir(LED, GPIO_OUT);

gpio_put(LED,1); //打开LED

// Example of using the HW divider. The pico_divider library provides a more user friendly set of APIs

// over the divider (and support for 64 bit divides), and of course by default regular C language integer

// divisions are redirected thru that library, meaning you can just use C level `/` and `%` operators and

// gain the benefits of the fast hardware divider.

int32_t dividend = 123456;

int32_t divisor = -321;

// This is the recommended signed fast divider for general use.

divmod_result_t result = hw_divider_divmod_s32(dividend, divisor);

printf("%d/%d = %d remainder %d\n", dividend, divisor, to_quotient_s32(result), to_remainder_s32(result));

// This is the recommended unsigned fast divider for general use.

int32_t udividend = 123456;

int32_t udivisor = 321;

divmod_result_t uresult = hw_divider_divmod_u32(udividend, udivisor);

printf("%d/%d = %d remainder %d\n", udividend, udivisor, to_quotient_u32(uresult), to_remainder_u32(uresult));

puts("Hello, world!");

return 0;

}

里边大部分是自动生成的,我只点亮了LED灯,其它没动。

修改程序用VS CODE或记事本均可

LED指示灯是接地GP25上的,见图纸:

pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

企业微信截图_20210128101010.png (11.81 KB, 下载次数: 0)

2021-1-28 10:58 上传

4.编译

昨天贴子里经过无数次测试没成功,今天偶然试一个命令竟然可以编译了

官方命令是直接用nmake命令,可我的直接执行不了

然后我用绝对命令,C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\nmake.exe

仍然报一推错误,后来考虑到要用ARM版的,然后再试 C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29617\bin\Hostx86\x86\nmake.exe

还是不行,偶然在nmake目录看到了一个DLL, C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\2052\nmakeui.dll

然后我试试加了-ui命令,奇迹出现了,可以编译了,并且成功编译出了elf文件,hex文件,bin都有

说明:至于为什么加了-ui可以,我还没查,等会我找下这个namke的帮助看一下

注意,需要在项目录下下的build目录下执行 ,如 ProjectName\build ,生成好的文件如图:

pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

企业微信截图_20210128095749.png (27.97 KB, 下载次数: 0)

2021-1-28 10:58 上传

5.转换

这个目标板只支持uf2文件,准备想用c#自己写一个转换程序,想来还是算了,用官方的,

官方SDK的这个目录下有个工具pico-sdk\tools\elf2uf2,不过只有源代码,

我用VS2019创建了一个C++工程,编译了一次,成功,以下是编译好的EXE,大家可以直接使用

elf转换到uf2.exe ,使用方法 ,命令行输入elf转换到uf2.exe 原文件.elf 目标文件.uf2

pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

elf转换到uf2.zip

(9.23 KB, 下载次数: 6)

2021-1-28 10:59 上传

点击文件名下载附件

pico编译器c语言,【树莓派Pico测评】- 开始编译程序2

企业微信截图_20210128102334.png (14.73 KB, 下载次数: 1)

2021-1-28 10:58 上传

6.下载

这个就比较简单了,上电前按住按钮,再插入USB上电,直接将UF2文件拖入目标盘即可,然后你就会发现LED灯奇迹的变亮了

很神奇吧?

修改原因:补充图片