電路圖:
採用範例程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <WS2812.h> #define NUM_LEDS 8 #define COLOR_PER_LEDS 3 #define NUM_BYTES (NUM_LEDS*COLOR_PER_LEDS) #if NUM_BYTES > 255 #error "NUM_BYTES can not be larger than 255." #endif __xdata uint8_t ledData[NUM_BYTES]; void setup() { pinMode(15, OUTPUT); //Possible to use other pins. } void loop() { for (uint8_t i = 0; i < NUM_LEDS; i++) { set_pixel_for_GRB_LED(ledData, i, 1, 0, 0); //Choose the color order depending on the LED you use. neopixel_show_P1_5(ledData, NUM_BYTES); //Possible to use other pins. delay(100); } for (uint8_t i = 0; i < NUM_LEDS; i++) { set_pixel_for_GRB_LED(ledData, i, 0, 1, 0); neopixel_show_P1_5(ledData, NUM_BYTES); delay(100); } for (uint8_t i = 0; i < NUM_LEDS; i++) { set_pixel_for_GRB_LED(ledData, i, 0, 0, 1); neopixel_show_P1_5(ledData, NUM_BYTES); delay(100); } } |
WS2812.h程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | /* * Copyright (c) 2020 by Deqing Sun <ds@thinkcreate.us> (c version for CH552 * port) Touch key library for arduino CH552. * * This file is free software; you can redistribute it and/or modify * it under the terms of either the GNU General Public License version 2 * or the GNU Lesser General Public License version 2.1, both as * published by the Free Software Foundation. */ #ifndef _WS2812_H_INCLUDED #define _WS2812_H_INCLUDED // clang-format off #include <Arduino.h> #include "template/WS2812_pins_header.h" // clang-format on #define set_pixel_for_RGB_LED(ADDR, INDEX, R, G, B) \ { \ __xdata uint8_t *ptr = (ADDR) + ((INDEX)*3); \ ptr[0] = (R); \ ptr[1] = (G); \ ptr[2] = (B); \ }; #define set_pixel_for_GRB_LED(ADDR, INDEX, R, G, B) \ { \ __xdata uint8_t *ptr = (ADDR) + ((INDEX)*3); \ ptr[0] = (G); \ ptr[1] = (R); \ ptr[2] = (B); \ }; #define set_pixel_for_RBG_LED(ADDR, INDEX, R, G, B) \ { \ __xdata uint8_t *ptr = (ADDR) + ((INDEX)*3); \ ptr[0] = (R); \ ptr[1] = (B); \ ptr[2] = (G); \ }; #define set_pixel_for_GBR_LED(ADDR, INDEX, R, G, B) \ { \ __xdata uint8_t *ptr = (ADDR) + ((INDEX)*3); \ ptr[0] = (G); \ ptr[1] = (B); \ ptr[2] = (R); \ }; #define set_pixel_for_BRG_LED(ADDR, INDEX, R, G, B) \ { \ __xdata uint8_t *ptr = (ADDR) + ((INDEX)*3); \ ptr[0] = (B); \ ptr[1] = (R); \ ptr[2] = (G); \ }; #define set_pixel_for_BGR_LED(ADDR, INDEX, R, G, B) \ { \ __xdata uint8_t *ptr = (ADDR) + ((INDEX)*3); \ ptr[0] = (B); \ ptr[1] = (G); \ ptr[2] = (R); \ }; #endif |
WS2812_pins_header.h的程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | // This file is generated by a script. #ifndef _WS2812_PIND_HEADER_H_INCLUDED #define _WS2812_PIND_HEADER_H_INCLUDED void neopixel_show_long_P1_0(uint32_t dataAndLen); #define neopixel_show_P1_0(ADDR, LEN) \ neopixel_show_long_P1_0((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P1_1(uint32_t dataAndLen); #define neopixel_show_P1_1(ADDR, LEN) \ neopixel_show_long_P1_1((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P1_2(uint32_t dataAndLen); #define neopixel_show_P1_2(ADDR, LEN) \ neopixel_show_long_P1_2((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P1_3(uint32_t dataAndLen); #define neopixel_show_P1_3(ADDR, LEN) \ neopixel_show_long_P1_3((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P1_4(uint32_t dataAndLen); #define neopixel_show_P1_4(ADDR, LEN) \ neopixel_show_long_P1_4((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P1_5(uint32_t dataAndLen); #define neopixel_show_P1_5(ADDR, LEN) \ neopixel_show_long_P1_5((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P1_6(uint32_t dataAndLen); #define neopixel_show_P1_6(ADDR, LEN) \ neopixel_show_long_P1_6((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P1_7(uint32_t dataAndLen); #define neopixel_show_P1_7(ADDR, LEN) \ neopixel_show_long_P1_7((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_0(uint32_t dataAndLen); #define neopixel_show_P3_0(ADDR, LEN) \ neopixel_show_long_P3_0((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_1(uint32_t dataAndLen); #define neopixel_show_P3_1(ADDR, LEN) \ neopixel_show_long_P3_1((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_2(uint32_t dataAndLen); #define neopixel_show_P3_2(ADDR, LEN) \ neopixel_show_long_P3_2((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_3(uint32_t dataAndLen); #define neopixel_show_P3_3(ADDR, LEN) \ neopixel_show_long_P3_3((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_4(uint32_t dataAndLen); #define neopixel_show_P3_4(ADDR, LEN) \ neopixel_show_long_P3_4((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_5(uint32_t dataAndLen); #define neopixel_show_P3_5(ADDR, LEN) \ neopixel_show_long_P3_5((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_6(uint32_t dataAndLen); #define neopixel_show_P3_6(ADDR, LEN) \ neopixel_show_long_P3_6((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); void neopixel_show_long_P3_7(uint32_t dataAndLen); #define neopixel_show_P3_7(ADDR, LEN) \ neopixel_show_long_P3_7((((uint16_t)(ADDR)) & 0xFFFF) | \ (((uint32_t)(LEN)&0xFF) << 16)); #endif |
沒有留言:
張貼留言