Created
January 15, 2020 05:26
-
-
Save Tamakichi/69219d403b049c4d87b18074bf59f327 to your computer and use it in GitHub Desktop.
NeoPixel の制御 (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の制御 by たま吉さん 2018/05/20 | |
// | |
//*************** | |
// 定数 | |
//*************** | |
#define PIXCELNUM 16 // Neopixel ピクセル数(LED数) | |
#define PIN 2 // Neopixel 制御用ピン番号 | |
//*************** | |
// グローバル変数 | |
//*************** | |
uint8_t buf[PIXCELNUM*3]; // Nexpixel用ピクセル色データ(ピクセル数 x 24ビット) | |
volatile uint8_t * NeoOutReg; // Neopixcel出力レジスタ | |
uint8_t NeoBitOut; // Neopixcelセットビット | |
uint8_t NeoBitMask; // Neopixcelクリア用マスク | |
//*************** | |
// 関数 | |
//*************** | |
// Neopixel初期化 | |
void NeoInit() { | |
memset(buf, 0, PIXCELNUM*3); // バッファの初期化 | |
// 出力ピンの初期化 | |
pinMode(PIN, OUTPUT); | |
digitalWrite(PIN, LOW); | |
NeoOutReg = portOutputRegister(digitalPinToPort(PIN)); // Neopixcel出力レジスタ | |
NeoBitOut = digitalPinToBitMask(PIN); // Neopixcelセットビット | |
NeoBitMask = ~NeoBitOut; // Neopixcelクリア用マスク | |
} | |
// Neopixelへ1を出力 | |
inline void NeoOut_1() { | |
*NeoOutReg |= NeoBitOut; // HIGHの出力 | |
asm volatile( | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
); | |
*NeoOutReg &= NeoBitMask; // LOWの出力 | |
} | |
// Neopixelへ0を出力 | |
inline void NeoOut_0() { | |
*NeoOutReg |= NeoBitOut; // HIGHの出力 | |
*NeoOutReg &= NeoBitMask; // LOWの出力 | |
asm volatile( | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
"nop" "\n\t" | |
); | |
} | |
// Neopixelへのデータ送信 | |
void NeoUpdate() { | |
uint8_t testbit = 0b10000000; | |
*NeoOutReg &= NeoBitMask; // LOWの出力 | |
delayMicroseconds(50); | |
cli(); | |
for (uint8_t i = 0; i < PIXCELNUM*3; i++) { | |
if (buf[i] & 128) NeoOut_1(); else NeoOut_0(); | |
if (buf[i] & 64) NeoOut_1(); else NeoOut_0(); | |
if (buf[i] & 32) NeoOut_1(); else NeoOut_0(); | |
if (buf[i] & 16) NeoOut_1(); else NeoOut_0(); | |
if (buf[i] & 8) NeoOut_1(); else NeoOut_0(); | |
if (buf[i] & 4) NeoOut_1(); else NeoOut_0(); | |
if (buf[i] & 2) NeoOut_1(); else NeoOut_0(); | |
if (buf[i] & 1) NeoOut_1(); else NeoOut_0(); | |
} | |
sei(); | |
} | |
// 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