本帖最后由 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); } |
【MCU实战经验】STM32F030芯片极限超频测试
【STM32F0开发日志】+STM32F030 修改官方提供的例程
SPC5Studio入门手册1----SPC5STudio5.0安装应用向导
图形化玩转Nucleo-STM32F413手机APP远程控制应用开发
【STM32F030开发日志/评测/笔记】+(2)STM32F0处理器I2C实例(...
STM32F030F4核心板自己整理的几个程序可以当模板参考(Keil MDK...
【HAL库每天一例】第066例:以太网-基于W5500的11个应用例程
STM32超低功耗平台上的RTOS应用--PDF课件
CubeMX之STM32F0 UART DMA接收的bug
【STM32F0开发日志】STM32F030 TIM3 4路PWM 输出
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);
“”需要注意的是,PA13映像到AF1后,下载一次代码并运行后,再次下载可能就下载不了了。“”
那如果是这样,这款芯片设计这个引脚功能有什么意义呢?有什么办法可以既能多次下载,又能使用这个引脚作为IR_OUT?在要下载的时候按下复位按键的同时,点击keil下载,可以吗?
不管你使用STM32F0在何处,你要是把PA13-SWD下载引脚挪作它用,你都会遇到这个问题,你都要用下载工具删除FLASH代码区,才能重新编程芯片。否则上电后,初始化代码将该引脚SWD功能废了,你当然无法再对其编程了。
回复:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)
已添加到社区网友原创汇总:
STM32F030探索套件开发日志,中文教程,使用问答70篇
RE:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)
RE:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)
回复:【STM32F030开发日志】STM32F0红外发射功能的应用(附代码)
这种代码应该怎么写呢?
这个应该要在it.c中田间中断号来做!
是SPI的时钟和数据时序
参考数据手册第29页:
PA13映像AF0是到SWDAT,你应该映像到AF1(IR_OUT)才对。
需要注意的是,PA13映像到AF1后,下载一次代码并运行后,再次下载可能就下载不了了。
楼上:最关键的代码都贴上了,可否偷一回不懒?