- #include "mcc_generated_files/mcc.h"
- typedef struct COLOR
- {
- uint8_t redChannel;
- uint8_t greenChannel;
- uint8_t blueChannel;
- } color_t;
- void ws2812_send(unsigned char x)
- {
- for(int i=0;i<8;i++)
- {
- x<<=i;
- if(x&0x80)
- {
- SPI1_WriteByte(0xF8);
- }
- else
- {
- SPI1_WriteByte(0xC0);
- }
- }
- }
- void WriteNeopixel(color_t color)
- {
- ws2812_send(color.greenChannel);
- ws2812_send(color.redChannel);
- ws2812_send(color.blueChannel);
- }
- color_t neopixelsBuffer[20] = {
- { 64, 100, 64 }, { 64, 64, 0 }, { 0, 64, 64 }, { 0, 64, 0 },
- { 64, 0, 64 }, { 64, 0, 0 }, { 0, 0, 64 }, { 30, 10, 2 },
- { 96, 96, 96 }, { 48, 48, 48 }, { 24, 24, 24 }, { 12, 24, 12 },
- { 36, 36, 6 }, { 3, 33, 33 }, { 16, 16, 1 }, { 50, 0, 0 },
- { 6, 60, 6 }, { 3, 3, 30 }, { 10, 1, 10 }, { 0, 10, 0 }
- };
- void main(void)
- {
- // Initialize the device
- SYSTEM_Initialize();
- // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
- // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
- // Use the following macros to:
- // Enable the Global Interrupts
- //INTERRUPT_GlobalInterruptEnable();
- // Disable the Global Interrupts
- //INTERRUPT_GlobalInterruptDisable();
- SPI1_Open(SPI1_DEFAULT);
- __delay_ms(1000);
- for(int i=0;i<20;i++) WriteNeopixel(neopixelsBuffer[i]);
-
- while (1)
- {
-
- }
- }
- /**
- End of File
- */
引脚的配置如下:
0
有图为证