本帖最后由 nyszx 于 2016-3-31 12:50 编辑
上次说的打算搞一个触摸调光的程序测试一下(https://bbs.21ic.com/icview-1446572-1-1.html),现在抽时间弄好了发上来,给大家分享一下,玩具级,比较简陋,大神勿喷。仅供测试参考。这次的NUCLEO-F446ZE开发板不知道有没有那么幸运,F4+eth很强大。
所需材料:除开发板外,一个发光二极管就OK
就是这么简陋!
为了实现调光,当然首选PWM输出驱动LED,本人很懒,找了个白色发光二级管,连驱动电路都没焊接,直接把管脚用手掰一掰,卡在PA1和VDD插针上。
程序是从自带Demo程序中改的。只留了TSL部分。
PA1配置为TIM2 PWM HC2输出。很简单。
/* TIM2 chennel2 configuration : PA.01 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2);
接下来就是配置TIM2:
TimerPeriod = (SystemCoreClock / 10000 ) - 1;
Channel2Pulse = (uint16_t) (((uint32_t) 10 * (TimerPeriod - 1)) / 10);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Channel 2 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/* TIM2 counter enable */
TIM_Cmd(TIM2, ENABLE);
/* TIM2 Main Output Enable */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
接下来配置触摸检测部分:
亮度分了50级
if (TSL_user_Action() == TSL_STATUS_OK)
{
if (MyLinRots[0].p_Data->StateId == TSL_STATEID_DETECT && MyLinRots[0].p_Data->Position > 0)
{
T2=MyLinRots[0].p_Data->Position;
if (T2< T1)
{
if( Pulse<51)Pulse++;
}
if (T2> T1)
{
if( Pulse>0)Pulse--;
}
T1=MyLinRots[0].p_Data->Position;
}
}
Channel2Pulse = (uint16_t) (((uint32_t) Pulse * (TimerPeriod - 1)) / 50);
TIM_SetCompare2(TIM2, Channel2Pulse);
这里其实还可以优化一下,比如没有触摸时就不用设定占空比了。
其他的没用到的都删了。
编译下载跑起来。视频上传优酷还在审核中。稍后补上视频
忘记传代码了,现在补上
STM32F072B-Discovery_FW_V1.0.1(调光).rar
(2 MB)
|