Created
January 15, 2020 05:19
-
-
Save Tamakichi/b529a5c5792c9e6bea5efb8fdf4b0df1 to your computer and use it in GitHub Desktop.
NeoPixelの制御 SPI利用バージョン (Arduino Uno)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Neopixelの制御 SPIバージョン by たま吉さん 2018/05/22 | |
// | |
#include <SPI.h> | |
//*************** | |
// 定数 | |
//*************** | |
#define PIXCELNUM 16 // Neopixel ピクセル数(LED数) | |
#define PIN 11 // Neopixel 制御用ピン番号 | |
#define NEOSPI_0 0b11100000 // 1ビット 値0 | |
#define NEOSPI_1 0b11111000 // 1ビット 値1 | |
#define NEOSPI_RST 0b00000000 // REST | |
//*************** | |
// グローバル変数 | |
//*************** | |
uint8_t buf[PIXCELNUM*3]; // Nexpixel用ピクセル色データ(ピクセル数 x 24ビット) | |
//*************** | |
// 関数 | |
//*************** | |
// Neopixel初期化 | |
void NeoInit() { | |
memset(buf, 0, PIXCELNUM*3); // バッファの初期化 | |
// SPIの初期化 | |
SPI.setBitOrder(MSBFIRST); // 最上位ビットから送信 | |
SPI.setClockDivider(SPI_CLOCK_DIV2); // クロック 8MHz | |
SPI.setDataMode(SPI_MODE1); // アイドル時 LOW、立上りエッジ時送信 | |
SPI.begin(); // 開始 | |
} | |
// Neopixelへのデータ送信 | |
void NeoUpdate() { | |
// RESET送信 | |
SPDR = NEOSPI_RST; // SPIデータ送信 | |
while(!(SPSR & (1 << SPIF))) ; // 送信完了待ち | |
delayMicroseconds(50); | |
// ピクセル数x24ビット送信 | |
for (uint8_t i = 0; i < PIXCELNUM*3; i++) { | |
for (uint8_t j = 0; j < 8; j++) { | |
SPDR = buf[i] & (0x80>>j) ? NEOSPI_1:NEOSPI_0; // SPIデータ送信 | |
while(!(SPSR & (1 << SPIF))) ; // 送信完了待ち | |
} | |
} | |
} | |
// Neopixelの表示クリア | |
void NeoCLS() { | |
memset(buf, 0, PIXCELNUM*3); // バッファの初期化 | |
NeoUpdate(); // 表示更新 | |
} | |
// 指定したピクセルの色を設定 | |
void NeoSetRGB(uint8_t no, uint8_t R, uint8_t G, uint8_t B, uint8_t flgUpdate=false) { | |
if (no < PIXCELNUM) { | |
buf[no*3+0] = G; | |
buf[no*3+1] = R; | |
buf[no*3+2] = B; | |
} | |
if (flgUpdate) | |
NeoUpdate(); | |
} | |
// ピクセルのシフト | |
void ShiftPixel() { | |
uint8_t tmpbuf[3]; | |
memmove(tmpbuf,buf,3); | |
memmove(buf, buf+3, (PIXCELNUM-1)*3); | |
memmove(buf+(PIXCELNUM-1)*3,tmpbuf,3); | |
NeoUpdate(); | |
} | |
void setup() { | |
NeoInit(); // Neopixcelの初期化 | |
NeoCLS(); // Neopixcelの表示クリア | |
NeoSetRGB(0, 128,0,0,true); // No.0のピクセルを赤 | |
NeoSetRGB(1, 0,128,0,true); // No.1のピクセルを緑 | |
NeoSetRGB(2, 0,0,128,true); // No.2のピクセルを青 | |
} | |
void loop() { | |
delay(80); | |
ShiftPixel(); // ピクセルをシフトして更新表示 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment