STM32C011とWS2812B(8個)でテーブルの値の通り光らせる。(STM32C011J4M7)



STM32C011とWS2812B(8個)でテーブルの値の通り光らせる。(STM32C011J4M7)



Arduino gpio WS2812B STM32C011 ラインLED



x 微調整は、各自おこなうように!!
x 高速化も、各自おこなうように!!(動いているけど規格外)

x STM32C011J4M7

目的
GPIOのテスト
WS2812のテスト

説明
WS2812Bの接続ピンは、PA8です。



![o_cop912.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/e5e62651-3e79-04b0-d739-7bf184e14955.jpeg)



```c++



//GPIO_WS2812B_TABLE_C011_1


//プロトタイプ宣言
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;  //バイトの一時

  //オンオフの設定
  //                    1111110000000000
  //                    5432109876543210
  on1  = GPIOA->ODR | 0b0000000100000000;
  off1 = GPIOA->ODR & 0b1111111011111111;

  //バイト数分だけループする
  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


const char train_x[16][3*8] PROGMEM = 
{//G0,R0,B0  G1,R1,B1  G2,R2,B2  G3,R3,B3  G4,R4,B4  G5,R5,B5  G6,R6,B6  G7,R7,B7
  { 2,32,32,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //0
  { 0, 0, 0, 32, 2,32,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //1
  { 0, 0, 0,  0, 0, 0, 32,32, 2,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //2
  { 0, 0, 0,  0, 0, 0,  0, 0, 0, 32,32, 2,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //3
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0, 32,32,32,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //4
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  2,32,32,  0, 0, 0,  0, 0, 0}, //5
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0, 32, 2,32,  0, 0, 0}, //6
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0, 32,32, 2}, //7
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0, 32, 2,32,  0, 0, 0}, //8 
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  2,32,32,  0, 0, 0,  0, 0, 0}, //9
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0, 32,32,32,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //10
  { 0, 0, 0,  0, 0, 0,  0, 0, 0, 32,32, 2,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //11
  { 0, 0, 0,  0, 0, 0, 32,32, 2,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //12
  { 0, 0, 0, 32, 2,32,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //13
  { 2,32,32,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}, //14
  { 0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0,  0, 0, 0}  //15
};


//初期化
void setup() {

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

  //無限ループ
  while(1){

    //l[3*0+0] = 31;
    //l[3*0+1] = 32;
    //l[3*0+2] = 33;
    //
    //l[3*1+0] = 30;
    //l[3*1+1] = 10;
    //l[3*1+2] = 32;
    //
    //l[3*2+0] = 10;
    //l[3*2+1] = 10;
    //l[3*2+2] = 32;

    for(int jj=0;jj<16;jj++){
      for(int ii=0;ii<(3*8);ii++){
        l[ii] = pgm_read_byte(&(train_x[jj][ii]));
      }//for ii

      //ws2812bへデータを送る
      ws_led(3*8);

      //0.1秒待つ
      delay(200);
    }//for jj
  } //while

}//setup


//メインループ
void loop() {
}//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



```