STM32F103C8でWS2812Bを光らせる。 (Mbed)


x Mbed NUCLEO-F103RB Mbed2 リビジョン172

目的
GPIOのテスト

BluePill F103C8

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

参考



#include "mbed.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
    //__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); //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

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

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

    return(0);
}

//メイン
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;

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

    ws_led(3);

    while(1){}

} //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){ 
         //a++;
        } ;

        GPIOA->ODR = off1;

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

}//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){ 
         //a++;
        } ;

        GPIOA->ODR = off1;

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

}//bit_on1