AnsweredAssumed Answered

STM32 Discovery, RC Receiver, PWM Input

Question asked by chia.benjamin on Feb 7, 2014
Latest reply on Feb 28, 2015 by roofie01
Hi,

I want to acquire a PWM input from a Futaba RC Receiver : http://www.futaba-rc.com/systems/futk6000.html Using this input I would like to test it out by varying the brightness of the onboard LED.

I have tried using the Peripheral Library - PWM input but to no avail. I am getting confused with the various timers, and their respective channels. If anybody could point me in the right direction it would be much appreciated. My code is below. Thanks again!!!

Ben


/*
 * futaba.c
 *
 *  Created on: 3 Feb 2014
 *      Author: Benjamin
 */
 
/*
RC PulseIn Joystick
 By: Nick Poole
 SparkFun Electronics
 Date: 5
 License: CC-BY SA 3.0 - Creative commons share-alike 3.0
 use this code however you'd like, just keep this license and
 attribute. L et me know if you make hugely, awesome, great changes.
 */
#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"
 
 
 
uint32_t Current;
int ch1; // Here's where we'll keep our channel values
//int ch2;
 
void Delay1(__IO uint32_t nCount) {
    while(nCount--) {
    }
}
 
void TIM_Config3(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 channel2 configuration : PB.07 */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
  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_NOPULL ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
 
  /* Connect TIM pin to AF2 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
 
  /*TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
 
   TIM_TIxExternalClockConfig(TIM4, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);*/
 
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz, from 48 MHz
  TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Maximal, TIM2 is 32-bit counter
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  /* 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);
 
 
}
 
void initLeds10() {
 
    /* GPIOG Peripheral clock enable */
       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
 
       GPIO_InitTypeDef  GPIO_InitStructure;
       /* Configure PG6 and PG8 in output pushpull mode */
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
       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(GPIOD, &GPIO_InitStructure);
 
       GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
}
 
int initfutaba(void){
    TIM_Config3();
    initLeds10();
 
    return 0;
}
 
int futaba(void) {
 
    TIM_ICInitTypeDef  TIM_ICInitStructure;
      /* ---------------------------------------------------------------------------
        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_Channel = TIM_Channel_2;
      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_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_CC2, ENABLE);
 
       //ch1 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6);   // Read the pulse width of
       //ch1 = TIM_GetCapture2(TIM4);
 
       TIM_SetCompare4(TIM4, Current);
       //ch2 = pulseIn(6, HIGH, 25000); // each channel
 
  /* I found that Ch1 was my left switch and that it
  floats around 900 in the off position and jumps to
  around 1100 in the on position */
       Delay1(100);
 
return 0;
}
 
volatile uint32_t Freq[4];
 
void TIM4_IRQHandler(void)
{
 
        uint32_t Delta;
        static uint32_t Last[4];
 
    if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)
        {
            /* Clear TIM2_CH1 Capture compare interrupt pending bit */
            TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
 
            Current = TIM_GetCapture2(TIM4);
          //  Delta = Current - Last[0];
          //  Last[0] = Current;
         //   if (Delta)
           //     Freq[0] = 1000000 / Delta; // 1MHz clock
 
            // ..
        }
}

Outcomes