1
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?

STM32C0116-DKでカラーLED、Grove - Chainable RGB LEDで遊ぶ(P9813)

Last updated at Posted at 2025-08-05

参考

結果

IMG_4270.jpeg

IMG_4271.jpeg

IMG_4272.jpeg

Screenshot from 2025-08-06 06-17-15.png

プログラム



//Chainable_LED_c0116_1
//1個専用のカラーRGB LEDのライブラリ

#define rgb_led_clk PA_3
#define rgb_led_dat PA_2


//送信クロックの発生
void clk() {

  digitalWrite(rgb_led_clk, LOW);//clk
  delay(20);                       // wait for a second
  digitalWrite(rgb_led_clk, HIGH);//clk
  delay(20);                       // wait for a second

}//clk


//1バイトを送信する
void sendByte(int b) {

  for (int i = 0; i < 8; i++) {
    if ( (b & 0x80) == 0) {
      digitalWrite(rgb_led_dat, LOW);//data
    } else {
      digitalWrite(rgb_led_dat, HIGH);//data
    }//endif
    clk();
    b = b << 1;
  }//for i

}//sendByte


//1個のLED専用
void setColorRGB(int red, int green, int blue) {

  //開始
  sendByte(0);
  sendByte(0);
  sendByte(0);
  sendByte(0);

  //ここから
  int prefix = 0b11000000;
  if( (blue  & 0x80)==0 ) {prefix = prefix | 0b00100000;}
  if( (blue  & 0x40)==0 ) {prefix = prefix | 0b00010000;}

  if( (green & 0x80)==0 ) {prefix = prefix | 0b00001000;}
  if( (green & 0x40)==0 ) {prefix = prefix | 0b00000100;}

  if( (red   & 0x80)==0 ) {prefix = prefix | 0b00000010;}
  if( (red   & 0x40)==0 ) {prefix = prefix | 0b00000001;}
  sendByte(prefix);

  sendByte(blue);
  sendByte(green);
  sendByte(red);
  //ここまで

  //終了
  sendByte(0);
  sendByte(0);
  sendByte(0);
  sendByte(0);

}//setColorRGB


// the setup function runs once when you press reset or power the board
void setup() {
  //GPIOポートの初期化
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(rgb_led_dat, OUTPUT); //data
  pinMode(rgb_led_clk, OUTPUT); //clk
}//setup


// the loop function runs over and over again forever
void loop() {

  //赤
  setColorRGB(0x55, 0x00, 0x00);
  delay(500);                       // wait for a second

  //緑
  setColorRGB(0x00, 0x55, 0x00);
  delay(500);                       // wait for a second

  //青
  setColorRGB(0x00, 0x00, 0x55);
  delay(500);                       // wait for a second

}//loop


1
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
1
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?