wenyangzeng 发表于 2013-12-11 22:41:03

【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)

本帖最后由 wenyangzeng 于 2017-6-12 22:06 编辑



久闻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);
}

LITIN 发表于 2015-11-20 16:27:51

你好!我用你的代码来驱动STM32F030F4P6 的PA13(IR)不知为何见不到波形。前辈能否指点。
uint16_t TimerPeriod16 = 0,Channel1Pulse16 =0,TimerPeriod17 = 0,Channel1Pulse17 =0;


        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
        TIM_OCInitTypeDef TIM_OCInitStructure;


        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ;
        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);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource13, GPIO_AF_0);




        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);

youyouyouoy 发表于 2016-12-5 17:02:59

wenyangzeng 发表于 2015-11-20 17:28
参考数据手册第29页:
PA13映像AF0是到SWDAT,你应该映像到AF1(IR_OUT)才对。
需要注意的是,PA13映像到A ...

“”需要注意的是,PA13映像到AF1后,下载一次代码并运行后,再次下载可能就下载不了了。“”
那如果是这样,这款芯片设计这个引脚功能有什么意义呢?有什么办法可以既能多次下载,又能使用这个引脚作为IR_OUT?在要下载的时候按下复位按键的同时,点击keil下载,可以吗?

wenyangzeng 发表于 2016-12-24 15:14:19

youyouyouoy 发表于 2016-12-5 17:02
“”需要注意的是,PA13映像到AF1后,下载一次代码并运行后,再次下载可能就下载不了了。“”
那如果是这 ...

不管你使用STM32F0在何处,你要是把PA13-SWD下载引脚挪作它用,你都会遇到这个问题,你都要用下载工具删除FLASH代码区,才能重新编程芯片。否则上电后,初始化代码将该引脚SWD功能废了,你当然无法再对其编程了。

沐紫 发表于 2014-1-7 10:14:45

回复:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)

多谢!
 
已添加到社区网友原创汇总:
STM32F030探索套件开发日志,中文教程,使用问答70篇

solo 发表于 2014-1-20 10:38:18

RE:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)

请问楼主,如何控制TIM16的波形按照NEC标准发送引导码,还有0和1呢?求教,万分感激!

tangshui198 发表于 2014-3-28 11:57:39

RE:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)

请问楼主能够提供完整的代码,本人初学:'(

xiongqian2011 发表于 2014-6-25 16:19:17

回复:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)

楼主你好,看了你的代码,学习了,现在我想用你的代码这里调整成输出包络时间可调,比如9ms高电平 4.5ms低电平 560us高电平等。
这种代码应该怎么写呢?
这个应该要在it.c中田间中断号来做!

dgdzes 发表于 2015-4-23 21:03:35

这是pwm了吧。

wenyangzeng 发表于 2015-4-24 07:26:51

dgdzes 发表于 2015-4-23 21:03
这是pwm了吧。

是SPI的时钟和数据时序

wenyangzeng 发表于 2015-11-20 17:28:29

本帖最后由 wenyangzeng 于 2015-11-21 07:33 编辑



参考数据手册第29页:
PA13映像AF0是到SWDAT,你应该映像到AF1(IR_OUT)才对。
需要注意的是,PA13映像到AF1后,下载一次代码并运行后,再次下载可能就下载不了了。

硕漓 发表于 2016-11-12 08:15:13

不错不错不错不错不错

peter001 发表于 2016-11-14 00:59:55

不错不错,这几天正想测试一下ir

whq 发表于 2016-11-19 19:09:13

不错不错,谢谢分享

wdshuang09 发表于 2016-11-19 20:04:17

还不错                        

zbber 发表于 2016-11-19 21:36:11

请问楼主能够提供完整的代码,本人初学

wenyangzeng 发表于 2016-11-20 07:18:26

zbber 发表于 2016-11-19 21:36
请问楼主能够提供完整的代码,本人初学

楼上:最关键的代码都贴上了,可否偷一回不懒?;P
页: [1] 2
查看完整版本: 【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)