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?

STM32C0116-DKとWS2812B(3個)でランダムに光らせる。

Last updated at Posted at 2025-08-13

参考

  • ユーザマニュアル (1)

Screenshot from 2025-08-14 05-19-02.png

  • M5Stack用RGB LEDユニット(SK6812)

結果

Screenshot from 2025-08-29 06-39-12.png

プログラム

  • RGB LED out PA2 pin
  • (GPIOA->ODR) PA2


//GPIO_WS2812B_RAND_C0116_1


//インクルド
#include <Arduino.h>


//プロトタイプ宣言
void bit_on1();   //(1)ビット
void bit_off1();  //(0)ビット


//定義
//フルカラー(RGB)LEDの設定領域
int l[90];  //max30 led

int on1;   //ビットのオン
int off1;  //ビットのオフ


//フルカラー(RGB)LEDの点灯関数 引数は、バイト数
void ws_led(int num1) {

  int led_b;  //バイトの一時

  //オンオフの設定 PA(x)
  //                    1111110000000000
  //                    5432109876543210
  on1  = GPIOA->ODR | 0b0000000000000100;
  off1 = GPIOA->ODR & 0b1111111111111011;

  //バイト数分だけループする
  for (int ii = 0; ii < num1; ii++) {

    //8ビット分送る
    led_b = l[ii];     //次の1バイト
    int jj = 8;        //カウンター
    while (jj != 0) {  //8から0

      //ビット毎にオン、オフを送信
      if ((led_b & 0x80)) {
        bit_on1();  //ビットが1
      } else {
        bit_off1();  //ビットが0
      }  //endif

      led_b = led_b << 1;  //1ビットシフト
      jj--;                //1引く
    }  //while 8bit

  }  //for max30byt

}  //ws_led


//初期化
void setup() {

  randomSeed(micros());  //乱数の初期化

  //ピンの初期化
  pinMode(PA2, OUTPUT);

}  //setup


//メインループ
void loop() {

  //debug
  //l[3*0+0] = 32;
  //l[3*0+1] = 10;
  //l[3*0+2] = 0;
  //
  //l[3*1+0] = 0;
  //l[3*1+1] = 20;
  //l[3*1+2] = 32;
  //
  //l[3*2+0] = 32;
  //l[3*2+1] = 30;
  //l[3*2+2] = 32;

  //ランダムの値の設定
  for (int ii = 0; ii < 9; ii++) {
    l[ii] = random(32);
  }  //for

  //ws2812bへデータを送る
  ws_led(9);

  //1秒待つ
  delay(1000);

}  //loop


// 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3
void bit_off1() {
  //0.3us 800khz
  GPIOA->ODR = on1;

  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;


  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  GPIOA->ODR = off1;

  GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;

  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
}  //0.3us


// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
void bit_on1() {
  //1us 800khz
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;

  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;
  GPIOA->ODR = on1;

  GPIOA->ODR = on1;
  GPIOA->ODR = on1;


  GPIOA->ODR = off1;

  GPIOA->ODR = off1;
  GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
  //GPIOA->ODR = off1;
}  //1us



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?