AnsweredAssumed Answered

Timer 8 disable problem

Question asked by das.sanjib on Jun 5, 2014
Latest reply on Jun 6, 2014 by das.sanjib
Hi all My timer 8 is not getting disable what wrong I am doing . I am configuring timer 8 for 87 clocks with r3spect to sys clock (168 mhz) ie approx .5 us .......inside that switch 12 cases are there ......completing 12 cases means 1 cycle ....and that will continue for 160 cycle. which will take around 1 millisec (87 clocks *12*160) ....and after that timer 8 should get stop as I am disabling it .....but it is not getting disabled....can anybody say what is the problem


/**
  ******************************************************************************
  * @file    USART/main.c
  * @author  Andrew Markham
  * @version V1.0.0
  * @date    27-April-2012
  * @brief   Main program body
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
 #include "stm32f4xx_tim.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx.h"
#include <misc.h>
#include "stm32f4xx_adc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_tim.h"

#include "stm32f4xx_usart.h"



//#include <stdio.h>

/* Private typedef -----------------------------------------------------------*/
GPIO_InitTypeDef  GPIO_InitStructure;
//TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_TimeBaseInitTypeDef    TIM_InitStruct;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void INTTIM_Config(void);
void LED_Config(void);
/* Private functions ---------------------------------------------------------*/
//short int a[3] = {0X0050,0x0050,0x0010,0x0000,0x0080,0x00A0,0x00A0,0x0020,0x0000,0x0040,0x0050};
int index = 0;
 volatile int pulse_width = 160;
 int  TX_FLAG = 0;
 int i = 0;
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
    LED_Config();
    /*
     * Interrupt in and out dont do anything
     */
    INTTIM_Config();

    Timer3_config();
    EnableTimerInterrupt();
    Enable_Timer3_interrupt();

  while(1)
  {

  }
}

void TIM3_IRQHandler()

{

    if (TIM3->SR &=0x0001 )
        {
    
            TIM3->SR = ~0x0001;
            /*
             * Dont want to start the TX_FLAG while it is running
             */

#if 1
            if (TX_FLAG == 0)
            {
            /*
             * Enabling the TIMER_8_TX_IRQ
             */

                TIM_Cmd(TIM8, ENABLE);

                TX_FLAG = 1;



            }
#endif
        }
}

void  TIM8_UP_TIM13_IRQHandler()

{


#if 1
    if (TIM8->SR &=0x0001 )
    //if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
    {



        TIM8->SR = ~0x0001;
#if 0
if (TX_FLAG == 0)
{
    GPIOD->ODR |= 0x2000;
     TIM8->CR1 &= ~0x0001;

          return;
}
#endif

#endif
#if 0
    // unsigned long long int digit = 0;
    if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
    {
    TIM_ClearITPendingBit(TIM8, TIM_IT_Update);




#endif
#if 1

            switch(index)
               {
               case 0:
                  GPIOD->BSRRH =0x0F00;
                   GPIOD->BSRRL =0x0000;


                   break;

               case 1:

                GPIOD->BSRRH = 0x0F00;

               break;

               case 2 :


                   GPIOD->BSRRL =0x0A00;

               break;

               case 3:

                   GPIOD->BSRRL =0x0A00;

               break;

               case 4:

                   GPIOD->BSRRL =0x0A00;

               break;

               case 5:

                   GPIOD->BSRRL =0x0A00;

               break;
               case 6:

                   GPIOD->BSRRH =0x0F00;
                   break;
               case 7:
                   GPIOD->BSRRH = 0x0F00;

             break;
            case 8:
               GPIOD->BSRRL =0x0500;


                        break;
            case 9:
               GPIOD->BSRRL =0x0500;
                         break;
            case 10:

               GPIOD->BSRRL =0x0500;

                         break;
            case 11:

               GPIOD->BSRRL =0x0500;
                             index = -1;
                         pulse_width--;
                         break;

             }
          index++;
          /*
           * Pulse width the pulse of the ping here it is 160 cycles of (the pulse is 160 Khz)
           */

/*
 * checked whether it is met 160 or not
 */
if (pulse_width == 0)
{

/*
 * making hte pins set to zero
 */
     GPIOD->BSRRH =0x0500;
/*
 * Disabling the TIMER_8_TX_IRQ
 */
//    TIM_Cmd(TIM8, DISABLE);
     TIM8->CR1 &= ~0x0001;

     /*
      * Dont want to start the TX_FLAG while it is running
     */
     TX_FLAG = 0;
}
#endif
        }
}
void INTTIM_Config(void)
{

  /* TIM8 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
  /* Time base configuration */
short int prescalar_val = 1 - 1;
  TIM_InitStruct.TIM_Prescaler = prescalar_val;                // This will configure the clock to 2 kHz
  TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;     // Count-up timer mode
  TIM_InitStruct.TIM_Period =  87 -1;                    // 2 kHz down to 1 Hz = 1 second
  TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;        // Divide clock by 1
  TIM_InitStruct.TIM_RepetitionCounter = 0;                // Set to 0, not used
  TIM_TimeBaseInit(TIM8, &TIM_InitStruct);
//  TIM_PrescalerConfig(TIM8, prescalar_val , TIM_PSCReloadMode_Immediate);

  /* TIM8 enable counter */
     TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);


}
void EnableTimerInterrupt()
{
    NVIC_InitTypeDef nvicStructure;
    nvicStructure.NVIC_IRQChannel =  TIM8_UP_TIM13_IRQn  ;
    nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
    nvicStructure.NVIC_IRQChannelSubPriority = 0;
    nvicStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvicStructure);

}

void Timer3_config(void)
{

  /* TIM8 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  /* Time base configuration */

  TIM_InitStruct.TIM_Prescaler = 800 - 1;                // This will configure the clock to 2 kHz
  TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;     // Count-up timer mode
  TIM_InitStruct.TIM_Period =  2100 - 1;                    // 2 kHz down to 1 Hz = 1 second
  TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;        // Divide clock by 1
  TIM_InitStruct.TIM_RepetitionCounter = 0;                // Set to 0, not used
  TIM_TimeBaseInit(TIM3, &TIM_InitStruct);


  /* TIM8 enable counter */

  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

}
void Enable_Timer3_interrupt()
{
    NVIC_InitTypeDef nvicStructure;
    nvicStructure.NVIC_IRQChannel =  TIM3_IRQn      ;
    nvicStructure.NVIC_IRQChannelPreemptionPriority = 1;
    nvicStructure.NVIC_IRQChannelSubPriority = 1;
    nvicStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvicStructure);
    TIM_Cmd(TIM3, ENABLE);
}
void LED_Config(void)
{
  /* GPIOD Periph clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11|GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

}
void adc_configure(){
 ADC_InitTypeDef ADC_init_structure; //Structure for adc confguration
 GPIO_InitTypeDef GPIO_initStructre; //Structure for analog input pin
 ADC_CommonInitTypeDef ADC_CommonInitStructure;
 //Clock configuration
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//The ADC1 is connected the APB2 peripheral bus thus we will use its clock source
 RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN,ENABLE);//Clock for the ADC port!! Do not forget about this one ;)
 //Analog pin configuration
//GPIO_initStructre.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//The channel 10 is connected to PC0
 GPIO_initStructre.GPIO_Pin = GPIO_Pin_2;
 GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN; //The PC0 pin is configured in analog mode
 GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL; //We don't need any pull up or pull down
 GPIO_Init(GPIOC,&GPIO_initStructre);//Affecting the port with the initialization structure configuration
 //ADC structure configuration

 ADC_DeInit();
// ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
 //  ADC_CommonInit(&ADC_CommonInitStructure);
 ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right
 ADC_init_structure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096
 ADC_init_structure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once
 ADC_init_structure.ADC_ExternalTrigConv =  ADC_ExternalTrigConvEdge_None;// conversion is synchronous with TIM1 and CC1 (actually I'm not sure about this one :/)
 ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion
 ADC_init_structure.ADC_NbrOfConversion = 1;//I think this one is clear :p
 ADC_init_structure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel
 ADC_Init(ADC1,&ADC_init_structure);//Initialize ADC with the previous configuration
 //Enable ADC conversion
 ADC_Cmd(ADC1,ENABLE);
 //Select the channel to be read from
 //ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_3Cycles);
 //ADC_RegularChannelConfig(ADC1,ADC_Channel_11,1,ADC_SampleTime_3Cycles);
 ADC_RegularChannelConfig(ADC1,ADC_Channel_12,1,ADC_SampleTime_3Cycles);
// NVIC_SetPriority(ADC_IRQn, 10);
//  NVIC_EnableIRQ(ADC_IRQn);
 //ADC_RegularChannelConfig(ADC1,ADC_Channel_13,1,ADC_SampleTime_3Cycles);
 }



#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 2011 STMicroelectronics *****END OF FILE****/

Outcomes