一、LED灯
1. 必须是全彩RGB_LED灯
2. 通过不同的红绿蓝三色LED混合出256种颜色,同电脑画图里的调色RGB888,每个灯各用8位控制其亮度,其中全灭表示黑色。
二、
1. LED亮度等级表
/* LED亮度等级 PWM表,指数曲线 ,此表使用工程目录下的python脚本index_wave.py生成*/
const uint16_t indexWave[] = {
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
11, 12, 12, 13, 14, 15, 17, 18,
19, 20, 22, 23, 25, 27, 29, 31,
33, 36, 38, 41, 44, 47, 51, 54,
58, 63, 67, 72, 77, 83, 89, 95,
102, 110, 117, 126, 135, 145, 156,
167, 179, 192, 206, 221, 237, 254,
272, 292, 313, 336, 361, 387, 415,
445, 477, 512, 512, 477, 445, 415,
387, 361, 336, 313, 292, 272, 254,
237, 221, 206, 192, 179, 167, 156,
145, 135, 126, 117, 110, 102, 95,
89, 83, 77, 72, 67, 63, 58, 54, 51,
47, 44, 41, 38, 36, 33, 31, 29, 27,
25, 23, 22, 20, 19, 18, 17, 15, 14,
13, 12, 12, 11, 10, 9, 9, 8, 8, 7, 7,
6, 6, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3,
3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1
};
2. 各项参数宏定义
extern uint16_t POIT_NUM;
extern __IO uint16_t period_class;
/*电压幅值等级数*/
#define AMPLITUDE_CLASS 256
/*****************定时器通道**************************/
#define BREATH_TIMx TIM3
#define BREATH_TIM_APBxClock_FUN RCC_APB1PeriphClockCmd
#define BREATH_TIM_CLK RCC_APB1Periph_TIM3
/*****************红灯**************************/
#define BREATH_RED_LED_PORT GPIOB
#define BREATH_RED_LED_PIN GPIO_Pin_5
#define BREATH_RED_CCRx CCR2
#define RED_TIM_OCxInit TIM_OC2Init
#define RED_TIM_OCxPreloadConfig TIM_OC2PreloadConfig
/*****************绿灯**************************/
#define BREATH_GRE_LED_PORT GPIOB
#define BREATH_GRE_LED_PIN GPIO_Pin_0
#define BREATH_GRE_CCRx CCR3
#define GRE_TIM_OCxInit TIM_OC3Init
#define GRE_TIM_OCxPreloadConfig TIM_OC3PreloadConfig
/*****************蓝灯**************************/
#define BREATH_BLU_LED_PORT GPIOB
#define BREATH_BLU_LED_PIN GPIO_Pin_1
#define BREATH_BLU_CCRx CCR4
#define BLU_TIM_OCxInit TIM_OC4Init
#define BLU_TIM_OCxPreloadConfig TIM_OC4PreloadConfig
#define BREATH_LED_REMAP_FUN() GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 ,ENABLE);
//中断
#define BREATH_TIM_IRQHandler TIM3_IRQHandler
三、breathing_led.c 编写
1. GPIO配置函数,其中red灯用TIM3_CH2,其由PB5部分重映射而来,因此在配置时必须开启AFIO时钟,并重映射。
__IO uint16_t period_class = 1;//pwm表中的每个数据只使用一次
uint16_t POIT_NUM = sizeof(indexWave) / sizeof (indexWave[0]); //计算pwm表中一共有多少数据,数据表应放在此条语句之前
static void Breath_LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //重映射时必须开启AFIO时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
BREATH_LED_REMAP_FUN();
GPIO_InitStructure.GPIO_Pin = BREATH_RED_LED_PIN;
GPIO_Init(BREATH_RED_LED_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = BREATH_GRE_LED_PIN;
GPIO_Init(BREATH_GRE_LED_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = BREATH_BLU_LED_PIN;
GPIO_Init(BREATH_BLU_LED_PORT, &GPIO_InitStructure);
}
2. 中断向量表配置,只用到定时器3中断,因而中断优先级可任意配置。
/**
* @brief 中断参数配置
* @param 无
* @retval 无
*/
static void Breath_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStructure);
}
3. 定时器模式配置函数,其中低电平led量,因而PWM有效电平为low,占空比通过TIM_TimeBaseInitStructure.TIM_Period ,TIM_OCInitStructure.TIM_Pulse 来配置,初始化占空比为0;
static void Breath_TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//开时钟
BREATH_TIM_APBxClock_FUN(BREATH_TIM_CLK, ENABLE);
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = (512 - 1);
TIM_TimeBaseInitStructure.TIM_Prescaler =(10 - 1);
TIM_TimeBaseInit(BREATH_TIMx, &TIM_TimeBaseInitStructure);
//输出比较配置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; /********************配置为低的原因是 低电平点亮LED灯**********************/
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable ;
TIM_OCInitStructure.TIM_Pulse = 0;
RED_TIM_OCxInit(BREATH_TIMx, &TIM_OCInitStructure);
RED_TIM_OCxPreloadConfig (BREATH_TIMx, TIM_OCPreload_Enable ); //预装载使能
GRE_TIM_OCxInit(BREATH_TIMx, &TIM_OCInitStructure);
GRE_TIM_OCxPreloadConfig (BREATH_TIMx, TIM_OCPreload_Enable );
BLU_TIM_OCxInit(BREATH_TIMx, &TIM_OCInitStructure);
BLU_TIM_OCxPreloadConfig (BREATH_TIMx, TIM_OCPreload_Enable );
TIM_ARRPreloadConfig (BREATH_TIMx, ENABLE);//一定要使能自动重装载
TIM_Cmd (BREATH_TIMx, ENABLE);
//中断开启
TIM_ITConfig(BREATH_TIMx, TIM_IT_Update, ENABLE);
//中断优先级
Breath_NVIC_Config();
}
.4. 初始化
void Breath_LED_Init(void)
{
Breath_LED_Config();
Breath_TIM_Config();
}
四、中断函数,关键点为将每个亮度等级(一共有512个亮度等级,也就是TIM_Period的值)又分为256个等级,通过对比设定参数与实际参数控制LED亮灭来实现调色。
extern uint16_t indexWave[];
extern __IO uint32_t rgb_color;
void BREATH_TIM_IRQHandler(void)
{
static uint16_t period_cnt = 0;
static uint16_t pwm_index = 0;
static uint16_t amplitude_cnt = 0;//计算幅值等级
if(TIM_GetFlagStatus (BREATH_TIMx, TIM_FLAG_Update) == SET)
{
amplitude_cnt ++;
if(amplitude_cnt > AMPLITUDE_CLASS)
{
period_cnt ++;
if(period_cnt > period_class)
{
period_cnt = 0;
pwm_index ++;
if(pwm_index >= POIT_NUM)
{
pwm_index = 0;
}
}
amplitude_cnt = 0;
}
else
{
if(((rgb_color & 0xff0000) >> 16) >= amplitude_cnt)
BREATH_TIMx -> BREATH_RED_CCRx = indexWave[pwm_index];
else
BREATH_TIMx -> BREATH_RED_CCRx = 0;
if(((rgb_color & 0x00ff00) >> 8) >= amplitude_cnt)
BREATH_TIMx -> BREATH_GRE_CCRx = indexWave[pwm_index];
else
BREATH_TIMx -> BREATH_GRE_CCRx = 0;
if((rgb_color & 0xff) >=amplitude_cnt)
BREATH_TIMx -> BREATH_BLU_CCRx = indexWave[pwm_index];
else
BREATH_TIMx -> BREATH_BLU_CCRx = 0;
}
TIM_ClearITPendingBit (BREATH_TIMx, TIM_IT_Update);
}
五、主函数
#include "breathing_led.h"
#define SOFT_DELAY() Delay(0xFFFFFF); //通过设定此参数来实现指定颜色,其值可通过电脑-画图-编辑颜色算得
__IO uint32_t rgb_color = 0x9CE326; // 156 227 38,此参数用来设置混合的颜色
int main()
{
Breath_LED_Init();
while(1)
;
}
上一篇:STM32实战 2.矩阵键盘通过串口3输出
下一篇:STM32的PWM波的频率和占空比
推荐阅读最新更新时间:2024-03-16 16:27
设计资源 培训 开发板 精华推荐
- TI 工业周系列研讨会诚邀您预报名参加,惊喜奖不停
- 阅读并了解是德科技汽车电子、物联网(IOT) 精彩专题,答题赢好礼!
- Littelfuse 高效 • 可靠 • 精准的功率控制和电路保护方案在工业、交通、通讯、医疗及新能源中的应用
- 下载有礼|精华课件汇编:泰克半导体器件的表征及可靠性研究交流会暨吉时利测试测量年度研讨会
- TI 嵌入式处理器最新产品发布会 全程在线直播 4月16日精彩为您呈现!预报名、看直播、享好礼
- 帮忙他人,成就自我——EEWORLD问答榜(10期)
- 助力电赛,抢楼有礼啦~~
- 有奖直播:基于TI Jacinto™ 的深度学习平台介绍
- ADI有奖下载活动之9 电机控制解决方案—伺服控制