STM32F0红外发射功能的应用
扫描二维码
随时随地手机看文章
久闻STM32F0系列内置IR_OUT红外发射功能,此次有幸得到社区赠送STM32F0308-DISCOVERY,终于有机会动手体会。
在许多应用场合需要红外发射管连续发送经过调制的38KHZ脉冲信号,经过接收头接收后送信号处理电路来判断其间有否物体经过。以往使用单片机来处理需要先从IO口产生38KHZ的PWM信号,再通过定时器中断来控制PWM信号的开启和关断。本例中定时器需要每500微秒中断一次来执行该任务,当CPU还有其他更重要的任务要处理日就显得有些紧张。
图1
STM32F0系列的一个特色是增加了IR_OUT功能,它在内部把TIM17-CCR1和TIM16-CCR1经过与非门从IR_OUT输出(图1),这样,我们只要配置好TIM16和TIM17的PWM参数,并把复用功能分配给IR_OUT,在这里是PB9-AF0,就可以完全不占用CPU时间而达到所需功能。此例中TIM17负责IR的载频,TIM16负责IR的调制,我们把TIM17的PWM配置成38KHZ占空比75%,TIM16的PWM配置成1KHZ占空比50%。由于红外发射管是在PWM低电平时导通,因此38K载频信号的占空比为25%。运行结果见图2和图3.。
图2 调制后的信号波形
图3波形展开
附代码:
#include "stm32f0xx.h"
uint16_t TimerPeriod16 = 0,Channel1Pulse16 =0,TimerPeriod17 = 0,Channel1Pulse17 =0;;
void GPIO_Config(void);
void TIM_Config(void);
//----------------------------------------------------------------------------
int main(void)
{ GPIO_Config();
TIM_Config();
while (1)
{}
}
//----------------------------------------------------------------------------
void GPIO_Config(void)
{ PIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
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(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_0);
}
//--------------------------------------------------------------------------
void TIM_Config(void)
{TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TimerPeriod16 = (SystemCoreClock /1000) - 1;
Channel1Pulse16 = (uint16_t) (((uint32_t) 5 * (TimerPeriod16 - 1)) / 10);
TimerPeriod17 = (SystemCoreClock /38000) - 1;
Channel1Pulse17 = (uint16_t) (((uint32_t) 75 * (TimerPeriod17 - 1)) / 100);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16|RCC_APB2Periph_TIM17, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod16;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
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_OCInitStructure.TIM_Pulse = Channel1Pulse16;
TIM_OC1Init(TIM16, &TIM_OCInitStructure);
TIM_Cmd(TIM16, ENABLE);
TIM_CtrlPWMOutputs(TIM16, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod17;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
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_OCInitStructure.TIM_Pulse = Channel1Pulse17;
TIM_OC1Init(TIM17, &TIM_OCInitStructure);
TIM_Cmd(TIM17, ENABLE);
TIM_CtrlPWMOutputs(TIM17, ENABLE);
}