LEDs ws2812b is a very interesting thing. I want to tell now about the implementation of the protocol of their work. As in the previous article , the code was written in the IAR environment under the ATmega32 microcontroller with 16 MHz quartz. I want to clarify right away that less than 16 MHz quartz is most likely not enough, this protocol is designed for very tight timings. A zero is set at a time interval of 0.4 µs, a unit of 0.8 µs.
#define ClearOutBit PORTC &= ~(1<<1) //0 #define SetOutBit PORTC |= 1<<1 //1 void Set0( void ) // ~0.4 { SetOutBit; asm("nop");asm("nop");asm("nop");asm("nop");asm("nop"); ClearOutBit; // , , } void Set1( void ) // ~0.85 { SetOutBit; asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop"); ClearOutBit; // , , } 
unsigned long int mas[30]; //32 30 void setMas( void ) // { unsigned long int a; unsigned int j,i; for (j=0; j<30; j++) { // 30 a = 0x1000000; // G (Hi->Low), R B for (i=0;i<24;i++){ // G,R,B a=a>>1; if ((mas[j]&a)==0x00000000) { Set0(); // } else { Set1(); // } } } } 

Source: https://habr.com/ru/post/257057/
All Articles