STM32L010でWS2812Bを光らせる。 HAL (Mbed) (約5Kバイト)


x Mbedのリビジョン143

目的
HAL
軽量コンパクト高速化
GPIOのテスト

構成
マイコン内蔵ＲＧＢＬＥＤ　ＷＳ２８１２Ｂ　（２個入）
[WS2812B(V5)]
通販コード　I-07915
発売日　2014/05/27
メーカーカテゴリ　Worldsemi Co.,Limited (ワールドセミ)


![h_con68.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/d8e8abd9-1b90-9967-51c3-1fce8ebdfee9.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, };

int ws_led(int num1)
{
    int on_off;

    //__disable_irq(); // disable interrupt
    //__enable_irq(); // enable interrupt

    //GPIO bkup
    //int g_bk  = GPIOA->ODR;


    for(int ii=0;ii<num1;ii++){
        //pc.printf("  -%d\t%d\r\n",ii,l[ii]); //303

        //8ビット分送る
        for(int jj=0;jj<8;jj++){
            //pc.printf("  -%d\r\n",jj); //303

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

            if( on_off == 0 ){
                //ビットが0
                //pc.printf("0"); //303
                bit_off1();
            } else {
                //ビットが1 
                //pc.printf("1"); //303
                bit_on1();
            }//endif

        }//for 8bit


    }//for max30byt

    //__disable_irq(); // disable interrupt
    //__enable_irq(); // enable interrupt

    //GPIO bkup
    //GPIOA->ODR = g_bk;

    return(0);
}

//メイン
int main() {

    //GPIOの初期化
    GPIO_INIT1();

    l[0] = 31;
    l[1] = 32;
    l[2] = 33;

    ws_led(3);

} //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




```