1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RaspberryPiPicoでhello, world

Last updated at Posted at 2022-07-17

やりたいこと

RaspberryPiPicoのUART0を使ってhello, worldする。

手順

main.c
#include "pico/stdlib.h"

int main() {
    // stdioの初期化
    stdio_init_all();
    // GPIOの初期化
    gpio_init(PICO_DEFAULT_LED_PIN);
    // 入出力の設定
    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
    while (true) {
        // HIGHを出力
        gpio_put(PICO_DEFAULT_LED_PIN, 1);
        sleep_ms(300);
        // LOWを出力
        gpio_put(PICO_DEFAULT_LED_PIN, 0);
        sleep_ms(300);
        // uartでhello, worldを出力
        printf("hello, world.\n");
        sleep_ms(300);
    }
}
CMakeLists.txt
# CMakeのバージョン
cmake_minimum_required(VERSION 3.12)

# SDKパス/pico_sdk_init()の定義のパス
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)

# PICO SDKのCMakeを読み込み
include(${PICO_SDK_INIT_CMAKE_FILE})

# プロジェクト名
project(sample_blink CXX C ASM)

# C Compilerのバージョン
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# PICO SDK CMakeの初期化
pico_sdk_init()

# コンパイルターゲット
add_executable(main
        main.c
        )

# リンクターゲット
target_link_libraries(main pico_stdlib)

# USBシリアルの設定
pico_enable_stdio_usb(main 0)
pico_enable_stdio_uart(main 1)

# 出力ファイル
pico_add_extra_outputs(main)

ポイントは、pico_enable_stdio_usbとpico_enable_stdio_uartでprintfの向き先を制御すること。
両方ONにすることもできて、その場合は両方に出力される。

これをデプロイして、AE-TTL-232Rを使ってMacとシリアルで通信する。
接続は以下の通り。

  • PICO GND - AE-TTL-232R GND
  • PICO URART0 TXD - AE-TTL-232R RXD
  • PICO URART0 RXD - AE-TTL-232R TXT

3.3Vで通信する必要があるので、AE-TTL-232RのSW2はOFFに設定しておく。

テスト

Macから以下のコマンドでシリアルポートに接続すると、hello, worldが出力されているはず。

screen /dev/cu.usbserial-A1032LOB 115200,,cs8,-parenb,-cstopb
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?