AnsweredAssumed Answered

STM32 Discovery, RC Receiver, PWM Input

Question asked by chia.benjamin on Feb 11, 2014
Hi Clive,

Thank you so much for the code. I added parts of it to the PWM input code and it works great! I am however only able to get a reading from one channel at the moment. I was hoping to use all 4 channels from TIM 4. Could you please advise me on this. Do you work for STM? You seem to reply to every message in this forum! Thanks for your help!! 

/*
 * futaba.c
 *
 *  Created on: 3 Feb 2014
 *      Author: Benjamin
 */
 
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_exti.h"
#include "misc.h"
 
 
/**
  ******************************************************************************
  * @file    TIM/TIM_PWMInput/main.c
  * @author  MCD Application Team
  * @version V1.2.0
  * @date    19-September-2013
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_exti.h"
#include "misc.h"
 
/** @addtogroup STM32F4xx_StdPeriph_Examples
  * @{
  */
 
/** @addtogroup TIM_PWMInput
  * @{
  */
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_ICInitTypeDef  TIM_ICInitStructure;
 
/* Private function prototypes -----------------------------------------------*/
void TIM_Config(void);
int ch1, ch2, ch3, ch4;
/* Private functions ---------------------------------------------------------*/
 
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int initfutaba(void)
{
    TIM_Config();
 
      /*!< At this stage the microcontroller clock setting is already configured,
           this is done through SystemInit() function which is called from startup
           files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
           before to branch to application main.
           To reconfigure the default setting of SystemInit() function, refer to
           system_stm32f4xx.c file
         */
 
      /* TIM Configuration */
      //TIM_Config();
 
 
      /* ---------------------------------------------------------------------------
        TIM4 configuration: PWM Input mode
 
        In this example TIM4 input clock (TIM4CLK) is set to 2 * APB1 clock (PCLK1),
        since APB1 prescaler is different from 1.
          TIM4CLK = 2 * PCLK1
          PCLK1 = HCLK / 4
          => TIM4CLK = HCLK / 2 = SystemCoreClock /2
 
        External Signal Frequency = TIM4 counter clock / TIM4_CCR2 in Hz.
 
        External Signal DutyCycle = (TIM4_CCR1*100)/(TIM4_CCR2) in %.
 
      --------------------------------------------------------------------------- */
 
 
      TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
      TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
      TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
      TIM_ICInitStructure.TIM_ICFilter = 0x0;
 
      TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
      TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
      TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
      TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
      TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
      TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
      TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
      TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
 
      /* Select the TIM4 Input Trigger: TI2FP2 */
      TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
 
      /* Select the slave Mode: Reset Mode */
      TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
      TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
 
      /* TIM enable counter */
      TIM_Cmd(TIM4, ENABLE);
 
      /* Enable the CC2 Interrupt Request */
      TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
      TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
      TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
      TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE);
 
     //  ch1 = TIM_GetCapture1(TIM4);
     // while (1);
    return 0;
}
 
int futaba(void)
{
 
  return 0;
}
 
/**
  * @brief  Configure the TIM4 Pins.
  * @param  None
  * @retval None
  */
void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 
  /* TIM4 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
 
  /* GPIOB clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
 
  /* TIM4 chennel2 configuration : PB.07 */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9 ;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
 
  /* Connect TIM pin to AF2 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_TIM4);
 
  /* Enable the TIM4 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  /* Time base configuration - SystemCoreClock = 168000000 for 168 MHz board ABP1 @ 42 MHz (DIV4) */
  TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (((SystemCoreClock / 1000000) / 2) - 1); // Shooting for 1 MHz, (1us)
  TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // Maximal
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
 
}
 
void TIM4_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
 
    ch1 = TIM_GetCapture1(TIM4);
 
  }
  if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)
    {
      TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
 
      ch2 = TIM_GetCapture2(TIM4);
    }
  if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)
    {
      TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);
 
      ch3 = TIM_GetCapture3(TIM4); // Period (us) ~20000
 
    }
  if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET)
    {
      TIM_ClearITPendingBit(TIM4, TIM_IT_CC4);
 
      ch4 = TIM_GetCapture4(TIM4); // Period (us) ~20000
 
    }
}
 
#ifdef  USE_FULL_ASSERT
 
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  while (1)
  {}
}
#endif
 
/**
  * @}
  */
 
/**
  * @}
  */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Outcomes