0
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?

RISC-Vマイコン CH32V003でLチカボード 導入について。

Posted at

NT東京、お疲れさまでした。

さて、こちらは Lチカボード導入に関するメモになります。

  • CH32V003をArduinoIDE用にボード情報を追加。

基本設定を開いて、追加します。

image.png

  • 追加するボードの情報

https://github.com/openwch/board_manager_files/raw/main/package_ch32v_index.json

ボード情報を追加するとボードマネージャでCH32と検索できるようになります。
キーワード入力するとCH32 MCU EVT Boardsが出てくるので、インストールします。

image.png

インストールしたらボードが選択できるようになるので
CH32VXXXを選択して、接続されたポートを選択します。

image.png

  • 秋月電子さんで売られているWCH-LinkEエミュレーターを入手

WCH-LinkEエミュレーターとLチカボードは下記のように接続します。
CR2032電池が3Vなので、マイコンへの電圧印加は3Vにします。

image.png

参考ですが、ドングルとLチカマイコンボードを接続する配線は、こちらを切り取って使っています。

  • 電子工作キット:はんだ付けした後の動作チェック用コード

このコードを使うことで、プッシュスイッチ(タクトスイッチ)の動作確認と、LEDの接続確認ができます。
ボタンを押すと、0から9まで数字がカウントアップします。
次にLEDは個別に1個ずつ点灯させ、全点灯します。
点灯しない箇所があればマイコンのはんだ付けを再確認してください。
一見はんだ付けされているように見えても、基板のランドとマイコンのリードフレームが意外と接触していない場合があります。その場合は、ピンを上から軽く押さえつけるようにして再度はんだ付けすると確実です。

#include <Arduino.h>

// 行ピン (上端 PA1 → 下端 PD0)
const uint8_t rawPins[8] = { PA1, PD7, PD6, PD5, PD4, PD3, PD2, PD0 };

// 列ピン (左端 PC1 → 右端 PA2)
const uint8_t colPins[8] = { PC1, PC2, PC3, PC4, PC5, PC6, PC7, PA2 };

// フォント定義(0~9)
const uint8_t font[][8] = {
  {0x7E,0x81,0x89,0x91,0xA1,0xC1,0x81,0x7E}, // 0
  {0x18,0x38,0x18,0x18,0x18,0x18,0x18,0x7E}, // 1
  {0x7E,0x81,0x01,0x02,0x0C,0x30,0x40,0xFF}, // 2
  {0x7E,0x81,0x01,0x1E,0x01,0x01,0x81,0x7E}, // 3
  {0x02,0x06,0x0A,0x12,0x22,0xFF,0x02,0x02}, // 4
  {0xFF,0x80,0x80,0xFE,0x01,0x01,0x81,0x7E}, // 5
  {0x3E,0x40,0x80,0xFE,0x81,0x81,0x81,0x7E}, // 6
  {0xFF,0x01,0x02,0x04,0x08,0x10,0x20,0x20}, // 7
  {0x7E,0x81,0x81,0x7E,0x81,0x81,0x81,0x7E}, // 8
  {0x7E,0x81,0x81,0x81,0x7F,0x01,0x02,0x7C}, // 9
};

int currentNum = 0;
uint8_t displayData[8];
int lastPC0 = HIGH;  // 前回のPC0の状態

// 90°左回転して表示用に変換
void rotateLeft90(const uint8_t* src, uint8_t* dst) {
  for (int y = 0; y < 8; y++) {
    dst[y] = 0;
    for (int x = 0; x < 8; x++) {
      if (src[x] & (1 << (7 - y))) {
        dst[y] |= (1 << x);
      }
    }
  }
}

// 数字を表示
void showDigit(int num) {
  uint8_t rotated[8];
  rotateLeft90(font[num], rotated);
  memcpy(displayData, rotated, 8);
}

// マトリクスを描画(1フレーム)
void drawMatrix() {
  for (int row = 0; row < 8; row++) {
    digitalWrite(rawPins[row], LOW);
    for (int col = 0; col < 8; col++) {
      bool on = (displayData[col] >> row) & 0x01;
      digitalWrite(colPins[col], on ? HIGH : LOW);
    }
    delayMicroseconds(1000);
    digitalWrite(rawPins[row], HIGH);
    for (int col = 0; col < 8; col++) digitalWrite(colPins[col], LOW);
  }
}

// アニメーション(10達成時)
void playAnimation() {
  // 右→左 スキャン
  for (int row = 0; row < 8; row++) {
    for (int col = 7; col >= 0; col--) {
      for (int r = 0; r < 8; r++) displayData[r] = 0;
      displayData[col] = (1 << row);
      unsigned long start = millis();
      while (millis() - start < 80) drawMatrix();
    }
  }

  // 全点灯
  for (int c = 0; c < 8; c++) displayData[c] = 0xFF;
  unsigned long t1 = millis();
  while (millis() - t1 < 500) drawMatrix();

  // 全消灯
  for (int c = 0; c < 8; c++) displayData[c] = 0x00;
  unsigned long t2 = millis();
  while (millis() - t2 < 500) drawMatrix();

  // カウントを0にリセット
  currentNum = 0;
  showDigit(currentNum);
}

void setup() {
  for (int i = 0; i < 8; i++) {
    pinMode(rawPins[i], OUTPUT); digitalWrite(rawPins[i], HIGH);
    pinMode(colPins[i], OUTPUT); digitalWrite(colPins[i], LOW);
  }

  pinMode(PC0, INPUT);  // 外部プルアップを使用

  showDigit(currentNum);  // 初期 0 表示
}

void loop() {
  // PC0の状態を読む
  int nowPC0 = digitalRead(PC0);

  // ネガティブエッジ検出(HIGH→LOW)
  if (lastPC0 == HIGH && nowPC0 == LOW) {
    delay(10);  // チャタリング防止
    if (digitalRead(PC0) == LOW) {
      currentNum++;
      if (currentNum >= 10) {
        playAnimation();
      } else {
        showDigit(currentNum);
      }
    }
  }

  lastPC0 = nowPC0;  // 状態を更新
  drawMatrix();      // 表示更新
}
0
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
0
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?