AnsweredAssumed Answered

measuring the pulse with Pingi

Question asked by t.a on Jun 11, 2014
Hi,
I am measuring the distance with ultrasonicsensor that has both one pin for the echo and trigger signal. For this task I'am using 2 timers: TIM2 AND TIM3
TIM2 is responsible for the periodic trigger signal, every 60 ms a 5us pulse is present on PA6, and TIM3 measures the input pulse.
I am just switching the alterntive function of a pin. Is this the best way to do it or there are any other alternative? I have 750us time to switch the pin charecteristics. Although it has been working good when was sending the 5us pulse in the IRQHandler and toggling the led in the main. I haven't try the code yet, cause I have to implement the UART communication through USB.

main code:
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Strucutres ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
 
/* My functions and variables-----------------------------------------------*/
unsigned char TIM2_flag=0;
void Init_TIM2(void);
void Init_TIM3(void);
void Delay(__IO uint32_t nTime);
 
 
int main(void)
{
 
//STM_EVAL_LEDInit(LED4);
//STM_EVAL_LEDInit(LED5);
 
/*initialize timers*/
Init_TIM2();
Init_TIM3();
 
while(1)
{
if (TIM2_flag == 1)    // pulse_flag
{
 
//STM_EVAL_LEDToggle(LED4);
//Delay(80000);
//TIM2_flag = 0;
 
 
// Configure PA6 in input mode
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
 
TIM_Cmd(TIM3, ENABLE);
}
}
}

TIM2 function and interrupt handler:
void Init_TIM2(void)
    {
     
    // Enable APB1 peripheral clock for TIM2
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
         
    // TIM iniciatization
    TIM_TimeBaseInitStruct.TIM_Prescaler = 71;  // 1M Hz                                                                                            
    TIM_TimeBaseInitStruct.TIM_Period = 59999;  // 60ms
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;  
    TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
        
      
        TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);                                                                        // Enable the TIM2 interrupt
        NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;                                                           // Configure the TIM2 interrupts
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;                                     // Set the priority group of the TIM2 interrupts
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;                                                    // Set the subpriority inside the group
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                                           // Globally enable the TIM2 interrupts
        NVIC_Init(&NVIC_InitStructure);                                                                                           // Init NVIC structure
      
    //Enable TIM counter
    TIM_Cmd(TIM2, ENABLE);
}
     
void TIM2_IRQHandler(void)
{
      uint16_t i;  
        
            if (TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)        // Check interrupt status
            {
       RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);      //enable the AHB bus to use GPIOA
     
       /* Configure PA6 in output pushpull mode */
             GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
             GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;                                 
             GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                            
             GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                         
             GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;                          
             GPIO_Init(GPIOA, &GPIO_InitStructure);                    
            
       /* a 5us  pulse on PA6 */
             GPIO_SetBits(GPIOA, GPIO_Pin_6);                                      
                for(i=0;i<=35;i++)
          {};               //kill time
        GPIO_ResetBits(GPIOA, GPIO_Pin_6);              //PA6 = 0              
              TIM2_flag=1; // pulse has been sent
            }
             
     if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
     TIM_ClearITPendingBit(TIM2, TIM_IT_Update);    // Counter overflow, reset interrupt
    }
}  

TIM3 function and interrupt handler:
void Init_TIM3(void)
{
     
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
    
    /*input chanel configuration*/
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8;
  TIM_ICInitStructure.TIM_ICFilter = 0x2;
  
  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
 
    TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
     
     
  TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
  
  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
  //TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
}
 
void TIM3_IRQHandler(void)
  {
      /*just an example*/  
         
      // aurt_buuffer= TIM_GetCapture1(TIM3); 
    // sprintf(buf, "\n%2X", aurt_buuffer);                                                             //print a newline and integer value (in HEX) to the buf string
        // VCP_PutStr(buf);                                                                                     //write a string variable to the VCP
    // Delay(125);     
  }

Tnx fot the help

Outcomes