STM32L010とWS2812B(3個)でランダムに光らせる。 (Mbed) HAL


x Mbed2 リビジョン143
x ws2812bがなぜ3個なのかと言うと2,3個で電流が超えるらしい

目的
GPIOのテスト

STM32L010F4P6

説明
WS2812Bの接続ピンは、PA4(10ピン)です。

-69-

![h_con69.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/264f67ba-3efe-7d9f-5fc6-350c323096c9.jpeg)

-85-

![h_con85.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/62a79f09-ee72-c14e-b50d-270a00bc535b.jpeg)

-86-

![h_con86.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/63647cd2-f26c-2501-a732-d13d9c2156e8.jpeg)

-45-

![h_con45.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/22b9098e-1745-ffbc-1fa7-6f4acce623b3.jpeg)


-74-

![h_con74.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/237a414d-bbb4-764a-04d1-0a5ddbcfe99a.jpeg)

-73-

![h_con73.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/bbf200fd-bcf3-e398-306c-773b64512837.jpeg)

-72-

![h_con72.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/bab692fa-c80f-a371-cdf8-93f65289a112.jpeg)




```c++


#include "mbed.h"

//myled         1.827Mhz 010
//GPIOA->ODR    7,995Mhz 010

//              5432109876543210
#define on1   0b0000000000010000
#define off1  0b0000000000000000

//GPIOの初期化
void GPIO_INIT1() {

    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}//GPIO_INIT1


int l[90]; //max30 led

void bit_on1();
void bit_off1();

//             12345678   12345678   12345678   12345678
int b8[8] = {0b10000000,0b01000000,0b00100000,0b00010000,
             0b00001000,0b00000100,0b00000010,0b00000001, };

//ws2812bにデータを送る
int ws_led(int num1)
{
    int on_off;

    for(int ii=0;ii<num1;ii++){

        //8ビット分送る
        for(int jj=0;jj<8;jj++){

            on_off = l[ii] & b8[jj];

            if( on_off == 0 ){                
                bit_off1();//ビットが0
            } else {
                bit_on1();//ビットが1 
            }//endif

        }//for 8bit


    }//for max30byt

    return(0);//リターン
}

//メイン
int main() {

    //GPIOの初期化
    GPIO_INIT1();

    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 ii=0;ii<9;ii++){
            l[ii] = rand()%52;
        }//for

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

        //1秒待つ
        wait_ms(100);

    } //while

} //main

// 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 = 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;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;
        GPIOA->ODR = off1;


}//bit_off1


// 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 = 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;
}//bit_on1



```

-80-

![h_con80.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/cc7bdaed-dd17-48ce-b77a-d30f4dab2995.jpeg)

-81-

![h_con81.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/5f09eafa-8191-1652-ef1b-5d00a6d30eaf.jpeg)

-79-

![h_con79.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/5fe01398-a67d-bfac-7b62-127070dcd47d.jpeg)

-82-

![h_con82.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/1ec902ab-d3d6-41f4-4365-4da05395a120.jpeg)

-83-

![h_con83.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/b5a38e5e-741a-7738-9330-1a110804562d.jpeg)

-84-

![h_con84.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/828f89db-49e5-fe7a-010d-f6fee7599659.jpeg)