0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2021-09-28

x STM8S-Discovery ボード
x ws2812bがなぜ3個なのかと言うと2,3個で電流が超えるらしい
x 割り込みを止めないと誤作動する
x NOP (ノップ?)やループでタイミングを取らずにダイレクト
x 遅い

目的
GPIOのテスト

o_con27.jpg




# define L_NUM 3

char l[L_NUM*3]; //max3 led

# define PD_ODR *(volatile uint8_t *)0x500F

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

    PD_ODR = 0b00000001; //1 0.125
    PD_ODR = 0b00000000; //2 0.250
    PD_ODR = 0b00000000; //3 0.375
    PD_ODR = 0b00000000; //4
    PD_ODR = 0b00000000; //5

    PD_ODR = 0b00000000; //1
    PD_ODR = 0b00000000; //2
    PD_ODR = 0b00000000; //3
    PD_ODR = 0b00000000; //4
    //PD_ODR = 0b00000000; //5

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

    PD_ODR = 0b00000001; //1
    PD_ODR = 0b00000001; //2
    PD_ODR = 0b00000001; //3
    PD_ODR = 0b00000001; //4
    PD_ODR = 0b00000001; //5

    PD_ODR = 0b00000001; //1
    PD_ODR = 0b00000001; //2
    PD_ODR = 0b00000000; //3 0.375
    PD_ODR = 0b00000000; //4 0.250
    PD_ODR = 0b00000000; //5 0.125

}//bit_on1


//RGB_LEDにデータをn回分送る
void ws_led()
{
  disableInterrupts();
  //num1で指定された分だけループする
  for(char ii=0;ii<(L_NUM*3);ii++){

      if( l[ii] & 0b10000000 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

      if( l[ii] & 0b01000000 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

      if( l[ii] & 0b00100000 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

      if( l[ii] & 0b00010000 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

      if( l[ii] & 0b00001000 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

      if( l[ii] & 0b00000100 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

      if( l[ii] & 0b00000010 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

      if( l[ii] & 0b00000001 ){
        bit_on1(); //ビットが1 
      } else {
        bit_off1(); //ビットが0
      }//endif

  }//for
  enableInterrupts();
}//ws_led


//RGB_LEDに0データをn回分送る
void ws_led_cl()
{
  disableInterrupts();
  //num1で指定された分だけループする
  for(char ii=0;ii< (L_NUM*3)  ;ii++){
    bit_off1(); //ビットが0
    bit_off1(); //ビットが0
    bit_off1(); //ビットが0
    bit_off1(); //ビットが0

    bit_off1(); //ビットが0
    bit_off1(); //ビットが0
    bit_off1(); //ビットが0
    bit_off1(); //ビットが0
  }//for
  enableInterrupts();
}//ws_led

void setup() {

   randomSeed( micros() ); //乱数の初期化

  //GPIOの初期化
  pinMode(PD0, OUTPUT);

}//setup

void loop() {

    for(int ii=0;ii<9;ii++){
      l[ii] = random(32);
    }//for

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


  //0.1秒待つ
  delay(100);

}//loop




0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?