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(40個、未定?)を単一の値で光らせる。

Last updated at Posted at 2021-09-28

x STM8S-Discovery ボード

苦労した点

なぜか処理落ちしまくる
処理を8ビットに合わせて
高速化しまくり
32ビット機の場合は、逆に遅くなるので要注意

x arduinoUNOとくらべるとなんか遅い。

o_con25.jpg



# define G0  0
# define R0  12
# define B0  0

# define L_NUM 40

char l[L_NUM*3]; //max40 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() {

// disableInterrupts();

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

  //状態を1づつ進める
  for(char ii=0;ii<L_NUM;ii++){

    //1個のデータをセット  
    l[(ii*3)+0] = G0;
    l[(ii*3)+1] = R0;
    l[(ii*3)+2] = B0;

  }//for

}//setup

void loop() {

  if( /*digitalRead(PB7)*/ 1 == 0 ){
    //ws2812bへ0データを送る
    ws_led_cl();
  }else{
    //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?