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


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

目的
GPIOのテスト

NUCLEO-F303K8

説明
WS2812Bの接続ピンは、A6(PA7)です。

参考


#include "mbed.h"
#include <stdlib.h>

//Serial pc(USBTX, USBRX); // tx, rx
//Serial pc(SERIAL_TX, SERIAL_RX); //767 303
//RawSerial pc(PA_2, PA_3); //010
//Serial pc(PA_9, PA_10); // tx, rx 103

DigitalOut myled(PA_7);
                  //     65432109876543210
int on1  = GPIOA->ODR | 0b0000000010000000;
int off1 = GPIOA->ODR & 0b1111111101111111;

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

    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); //103

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

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

        }//for 8bit

    }//for max30byt

    //__enable_irq(); // enable interrupt

    return(0);
}//ws_led

//メイン
int main() {

    //pc.printf("\r\n103\r\n"); //103
    GPIOA->ODR = off1;
    wait_ms(1);

    //クロックカウンターの初期化
    CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
    DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

    int ii; //ループカウンター

    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(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

        DWT->CYCCNT = 0;

        GPIOA->ODR = on1;

        while (DWT->CYCCNT < 17L){ 
        } ;

        GPIOA->ODR = off1;

        while (DWT->CYCCNT < 70L){ 
        } ;

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

        DWT->CYCCNT = 0;

        GPIOA->ODR = on1;

        while (DWT->CYCCNT < 47L){ 
        } ;

        GPIOA->ODR = off1;

        while (DWT->CYCCNT < 70L){ 
        } ;

}//bit_on1