RGB_LED是一种有特色的器件,它将红绿蓝三种色彩集成在同一器件中,依赖这三基色可以组合出多种多样的色彩,那如何才能展示这种效果呢? 借助L432的多通道PWM输出功能可以轻松地实现这一功能,它只需一个RGB_LED、3个2K的限流电阻及少量导线即可。其显示效果如下图所示,程序只需将main()函数中的while(1){} 循环改为如下的程序就行了。 由于器件中的3个发光点并不在一点,所以色彩的混合效果不均匀,因此最好在该器件前附上一层纸来辅助混光。 色彩环变换效果图 - 色彩环变换效果程序:
- while (1)
- {
- for(n=0;n<3;n++) // 以三基色组合色彩
- {
- for(i=0;i<100;i++) // 调节色彩的亮度
- {
- if(n==0)
- {
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*(100-i)/100); // R
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2);
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*i/100); // G
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3);
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*0); // B
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
- }
- if(n==1)
- {
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*0); // R
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2);
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*(100-i)/100); // G
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3);
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*i/100); // B
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
- }
- if(n==2)
- {
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*i/100); // R
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2);
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*0); // G
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3);
- sConfig.Pulse = (uint32_t) (PERIOD_VALUE*(100-i)/100); // B
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
- HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
- }
- // 延时处理
- for(j=0;j<2000;j++)
- {
- for(k=0;k<2000;k++);
- }
- }
- }
复制代码
相关推荐:
NUCLEO-L432KC官方技术文档
STM32 L4系列超低功耗微控制器官方介绍
STM32L4单片机参考设计
STM32单片机官方开发工具一览
|