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
main.cpp
- Committer:
- devararendy
- Date:
- 2016-12-10
- Revision:
- 2:0d3906d69a41
- Parent:
- 0:a27bf0451169
File content as of revision 2:0d3906d69a41:
#include "mbed.h" #include "USBSerial.h" #include "WS2812.h" #define LED_RGB PA_1 WS2812 RGB(LED_RGB, 3); uint32_t colors[3]={0xFF0000,0xFFfe00,0xFF00FF}; //DigitalOut test(PB_7); 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 }; void InitializeTimer() { __TIM2_CLK_ENABLE(); s_TimerInstance.Init.Prescaler = 1; s_TimerInstance.Init.CounterMode = TIM_COUNTERMODE_UP; 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); HAL_TIM_Base_Start(&s_TimerInstance); } int main(void) { wait(3); InitializeTimer(); serial.printf("Welcome to STM32FC RGB LED Tester\r\n"); while(1) { /* //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"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); timerValue = __HAL_TIM_GET_COUNTER(&s_TimerInstance) - timerValue; unsigned int timerValue2 = __HAL_TIM_GET_COUNTER(&s_TimerInstance); test = 0; asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); timerValue2 = __HAL_TIM_GET_COUNTER(&s_TimerInstance) - timerValue2; serial.printf("Send 0 val %u, val2 = %u \r\n", timerValue, timerValue2); ////////////////////////////////////////////////////////////////////////// timerValue = __HAL_TIM_GET_COUNTER(&s_TimerInstance); test = 1; asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); timerValue = __HAL_TIM_GET_COUNTER(&s_TimerInstance) - timerValue; timerValue2 = __HAL_TIM_GET_COUNTER(&s_TimerInstance); test = 0; asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); timerValue2 = __HAL_TIM_GET_COUNTER(&s_TimerInstance) - timerValue2; serial.printf("Send 1 val %u, val2 = %u \r\n", timerValue, timerValue2); */ /* RGB.writeColor(0xffffff); RGB.sendReset(); serial.printf("R = 0x%X, G = 0x%X ,B = 0x%X\r\n", RGB.Red, RGB.Green, RGB.Blue); wait(2); RGB.writeColor(0xff00ff); RGB.sendReset(); serial.printf("R = 0x%X, G = 0x%X ,B = 0x%X\r\n", RGB.Red, RGB.Green, RGB.Blue); wait(2); RGB.writeColor(0xffff00); RGB.sendReset(); serial.printf("R = 0x%X, G = 0x%X ,B = 0x%X\r\n", RGB.Red, RGB.Green, RGB.Blue); wait(2); */ /* RGB.send1Color(0xFF0000); Thread::wait(1000); RGB.send1Color(0xFF00); Thread::wait(1000); RGB.send1Color(0xFF); Thread::wait(1000); RGB.send1Color(0xFFFF00); Thread::wait(1000); RGB.send1Color(0xFFFF); Thread::wait(1000); RGB.send1Color(0xFF00FF); Thread::wait(1000); RGB.send1Color(0xff8000); Thread::wait(1000); RGB.send1Color(0x9933ff); Thread::wait(1000); RGB.send1Color(0xff3300); Thread::wait(1000); RGB.send1Color(0x99cc00); Thread::wait(1000); RGB.send1Color(0xff99ff); Thread::wait(1000); RGB.send1Color(0x009999); Thread::wait(1000); */ RGB.sendColors(colors); wait(1); } }