This is an example program of WS2812 library https://developer.mbed.org/users/devararendy/code/WS2812/ in main program, i also put some line to measure how many asm NOP do we need. I have test it on STM32F411RE (Nucleo 411RE). if you would like to use another microcontroller, you need to adjust the asm("nop") in the library.
Dependencies: USBDevice WS2812 mbed-os
Diff: main.cpp
- Revision:
- 2:0d3906d69a41
- Parent:
- 0:a27bf0451169
diff -r c18078160be3 -r 0d3906d69a41 main.cpp --- a/main.cpp Sat Dec 10 08:38:13 2016 +0000 +++ b/main.cpp Sat Dec 10 09:21:00 2016 +0000 @@ -4,11 +4,12 @@ #define LED_RGB PA_1 -WS2812 RGB(LED_RGB, 1); -DigitalOut test(PB_7); +WS2812 RGB(LED_RGB, 3); +uint32_t colors[3]={0xFF0000,0xFFfe00,0xFF00FF}; +//DigitalOut test(PB_7); -USBSerial serial; -unsigned int timerValue; +USBSerial serial; //Im using STM32F411RE. USB Serial USB Full Speed D+ = PA12, D- PA11. D+ Pulled Up w/ 1k5 resistor (1K ohm is working too) + static TIM_HandleTypeDef s_TimerInstance = { .Instance = TIM2 }; @@ -17,7 +18,7 @@ __TIM2_CLK_ENABLE(); s_TimerInstance.Init.Prescaler = 1; s_TimerInstance.Init.CounterMode = TIM_COUNTERMODE_UP; - s_TimerInstance.Init.Period = 45000000; //tim clock = 45MHz, 1Sec = 1 Period + s_TimerInstance.Init.Period = 45000000; //Timer clock = 45MHz s_TimerInstance.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; s_TimerInstance.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&s_TimerInstance); @@ -31,7 +32,9 @@ while(1) { /* - timerValue = __HAL_TIM_GET_COUNTER(&s_TimerInstance); + //to convert timer val to nanoSecond = val/45*1000 + //example timer val for one asm("nop") is 3. convert to nanoSecond: 3/45*1000 = 66.666nS + unsigned int timerValue = __HAL_TIM_GET_COUNTER(&s_TimerInstance); test = 1; asm("nop"); asm("nop"); @@ -223,6 +226,7 @@ wait(2); */ + /* RGB.send1Color(0xFF0000); Thread::wait(1000); RGB.send1Color(0xFF00); @@ -248,6 +252,9 @@ Thread::wait(1000); RGB.send1Color(0x009999); Thread::wait(1000); + */ + RGB.sendColors(colors); + wait(1); } } \ No newline at end of file