4

RP2040(树莓派Pico) First Examples

 3 years ago
source link: https://www.taterli.com/7504/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

RP2040(树莓派Pico) First Examples

学习一个新的片子,无外乎就是掌握他的外设,知道他的外设限制在哪里,功能在哪里,然后就能针对性的应用在项目上,我就按照官方例子都走一遍,结合手册来食用.

https://github.com/raspberrypi/pico-examples

第一个例子是Hello serial,顾名思义,就是打印东西呗,这个很简单,但是需要焊接排针,并且打印是从默认串口出来的.何为默认串口?看到深色的UART0嘛.

引脚图,注意UART0(default).

既然是printf,应该可以传递参数,所以我就改了改程序试试.

/**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#include "pico/stdlib.h"

int main() {
    uint8_t i = 0;

    stdio_init_all();
    while (true) {
        printf("Hello, world! %d\n", i++);
        sleep_ms(300);
    }
    return 0;
}

其中stdio_init_all主要是初始化UART和USB功能,实现printf重载,只要用了它后续就可以用stdio标准打印函数了.

打印测试

想把他改成USB输出也很简单,其中USB serial工程里只修改了cmake文件,注意查看新增内容,即默认输出是UART,可改为USB,但是我测试如果同时启用,将会编译警告,但是也可以用.

启用USB UART

可以看到通过USB模拟的串口.

Pico USB UART

不过,stdio究竟实现了多少,包含getch吗?这直接会影响到我们的一些交互操作.

支持scanf输入,这样说明getch等也是支持的.

在First Examples的最后一个例子是操作IO,代码相当简单,就不多说了.

/**
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "pico/stdlib.h"

int main() {
    const uint LED_PIN = 25;
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
    while (true) {
        gpio_put(LED_PIN, 1);
        sleep_ms(250);
        gpio_put(LED_PIN, 0);
        sleep_ms(250);
    }
}




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK