AnsweredAssumed Answered

long excution time at stm32f103ret6

Question asked by ghasemi.naser on May 19, 2016
Latest reply on May 19, 2016 by Clive One
hi
I am using this code in my stm32f103ret6. I need to have 10us excution time for while(1) codes but I have 120us!. How can I do to reduce this time?


#include "stm32f10x.h"
#include "stdio.h"
#include "math.h"
#define SYS_CLK 72   /* for delay header */
#include "delay.h"

#define pi  3.1415f

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)



/*******************************************************************/
PUTCHAR_PROTOTYPE
{
  USART_SendData(USART1, (u8) ch);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  return ch;
}
/*******************************************************************/

void usart1_init(void);
void LED_Test_Configuration(void);
void ADC_Configuration(void);
//u16 readADC1(u8 channel);
void NVIC_Configuration(void);
void TIM3_Conf(void);
//void ADC_EXT_TIM1(void);
void RCC_Configure(void);
void SetSysClockTo72(void);



RCC_ClocksTypeDef RCC_ClockFreq;
ErrorStatus HSEStartUpStatus;

u16 ADC1ConvertedValue = 0;
float V_adc;
//float Ts = 30e-6; // 30us
float v_adc,v_adc1;

float u_k = 0;
float u_k_1 = 0;
float u_k_2 = 0;
float y1_k = 0;
float y1_k_1 = 0;
float y1_k_2 = 0;
float y2_k = 0;
float y2_k_1 = 0;
float y2_k_2 = 0;

float valpha = 0;
float vbeta = 0;
float Vd = 0;
float Vq = 0;
float Vd_in_k = 0;
float Vd_in_k_1 = 0;
float Vd_out_k = 0;
float Vd_out_k_1 = 0;
float Vd_out_m_k = 0;
float Vd_out_m_k_1 = 0;


float teta_out_k = 0;
float teta_out_k_1 = 0;
float teta_mod = 0;

//u16 a = 1;


float sample1[1000];
float sample2[1000];
float sample3[1000];
float sample4[1000];

u32 count = 0;
u16 i=0;
//char allow = 0;
float d1=0, d2=0;
float sin_data = 0;
TIM_OCInitTypeDef  TIM_OCInitStructure;
u16 d=0,db = 0;


void TIM3_IRQHandler(void)
{
        TIM3->CCR1 = d;
        
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
    
}

int main()
{
    RCC_Configure();
  NVIC_Configuration();
    
  usart1_init();
  printf("Usart_Test\r\n");
  ADC_Configuration();
    LED_Test_Configuration(); 
    TIM3_Conf();

while (1)
{
        GPIOA->BSRR = 0x0001;
  ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
    V_adc = ADC1ConvertedValue;
    v_adc = (V_adc/4095)*3.3f;
    v_adc = v_adc-(1.65f);  //  v_adc = v_adc-(3.3/2);
    v_adc1 = v_adc/(1.125f);  //  v_adc = v_adc-(3.3/2);

    u_k = v_adc;
    y1_k = -0.9476f*y1_k_2 + 1.946f*y1_k_1 - 0.0262f*u_k_2 + 0.0262f*u_k;
    y2_k = -0.9476f*y2_k_2 + 1.946f*y2_k_1 + 0.0004814f*u_k_2 + 0.0009629f*u_k_1 + 0.0004814f*u_k;
    
    u_k_2 = u_k_1;
    u_k_1 = u_k;
    
    y1_k_2= y1_k_1;
    y1_k_1 = y1_k;
    
    y2_k_2= y2_k_1;
    y2_k_1 = y2_k;
    
    
    /* PLL ***********************************************************************/
    valpha = y1_k;
    vbeta  = y2_k;
    
    Vd = cosf(teta_out_k)*valpha + sinf(teta_out_k)*vbeta;
  Vq = -sinf(teta_out_k)*valpha + cosf(teta_out_k)*vbeta;
    
    Vd_in_k = 0-Vd;

    Vd_out_k = Vd_out_k_1 + 92.25f*Vd_in_k - 91.75f*Vd_in_k_1;
    
    Vd_in_k_1 = Vd_in_k;
    Vd_out_k_1 = Vd_out_k;

  Vd_out_m_k = Vd_out_k;//    Vd_out_m_k = Vd_out_k + (2*pi*50);

    teta_out_k = teta_out_k_1 + 5.85e-005f*Vd_out_m_k + 5.85e-005f*Vd_out_m_k_1;
    
    Vd_out_m_k_1 = Vd_out_m_k;
    teta_out_k_1 = teta_out_k;

//  teta_mod = fmod(teta_out_k,2*pi);


    
    /*      *******************************************************************/
//    sin_data = v_adc
    sin_data = -sinf(teta_out_k+pi/6);
    d1 =0.6f * sin_data*199;
    d = d1;
    d2 =0.6f * v_adc1*199;
    db = d2;
    
    if(d1>=0){
                GPIOA->BSRR = 0x0100;  // A.8
            GPIOA->BSRR = 0x0200;  // A.9
        }
        else
        {
                GPIOA->BRR = 0x0100;
            GPIOA->BRR = 0x0200;
            }

        GPIOA->BRR = 0x0001;
    //        delay(100000);


}


void LED_Test_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure123;
    
         /* GPIOA Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
   

  /* Configure PA11 in output pushpull mode */
  GPIO_InitStructure123.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure123.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure123.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure123);
}


void usart1_init()
{
  USART_InitTypeDef uart;
  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 , ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  uart.USART_BaudRate = 921600;
  uart.USART_WordLength = USART_WordLength_8b;
  uart.USART_StopBits = USART_StopBits_1;
  uart.USART_Parity = USART_Parity_No ;
  uart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  uart.USART_Mode = USART_Mode_Tx ;//| USART_Mode_Rx;
  USART_Init(USART1, &uart);
  USART_Cmd(USART1, ENABLE);     
}



void ADC_Configuration(void)
{
  ADC_InitTypeDef  ADC_InitStructure;
  /* PCLK2 is the APB2 clock */
  /* ADCCLK = PCLK2/4 = 72/4 = 18MHz*/
  RCC_ADCCLKConfig(RCC_PCLK2_Div2);

  /* Enable ADC1 clock so that we can talk to it */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  /* Put everything back to power-on defaults */
  ADC_DeInit(ADC1);

  /* ADC1 Configuration ------------------------------------------------------*/
  /* ADC1 and ADC2 operate independently */
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  /* Disable the scan conversion so we do one at a time */
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  /* Don't do contimuous conversions - do them on demand */
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  /* Start conversin by software, not an external trigger */
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  /* Conversions are 12 bit - put them in the lower 12 bits of the result */
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  /* Say how many channels would be used by the sequencer */
  ADC_InitStructure.ADC_NbrOfChannel = 1;

  /* Now do the setup */
  ADC_Init(ADC1, &ADC_InitStructure);
    
    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_1Cycles5);
 
      /* Enable ADC1 external trigger */
//  ADC_ExternalTrigConvCmd(ADC1, ENABLE);
    
    /* Enable EOC interrupt */
//  ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);

    
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibaration register */
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));
  /* Start ADC1 calibaration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));

  // Start the conversion
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

}


//u16 readADC1(u8 channel)
//{
 // ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_1Cycles5);
  // Start the conversion
 // ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  // Wait until conversion completion
 // while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  // Get the conversion value
//  return ADC_GetConversionValue(ADC1);
//}


/**
  * @brief  Configures Vector Table base location.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure and enable ADC interrupt */
/*  NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
*/    
    /* Enable the TIM3 global Interrupt */
  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);
    
}



void TIM3_Conf(void)
{
      GPIO_InitTypeDef GPIO_InitStructure;
      TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
//    uint16_t CCR1_Val = 333;
//      uint16_t PrescalerValue = 0;
    
      /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
    
    /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    
      /* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
    clock at 24 MHz the Prescaler is computed as following:
     - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
    and Connectivity line devices and to 24 MHz for Low-Density Value line and
    Medium-Density Value line devices

    The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
                                                  = 24 MHz / 666 = 36 KHz
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */
    
    /* 60KHz *** DC = 0.5   T = 16.7us*/
    
  /* Compute the prescaler value */
//  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 199; //665
  TIM_TimeBaseStructure.TIM_Prescaler = 5;//PrescalerValue
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;  //0
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 99;  //CCR1_Val
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM3, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

      /* TIM IT enable */
  TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);

    
    TIM_ARRPreloadConfig(TIM3, ENABLE);
    
    
    
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
    
}


void RCC_Configure(void)
{
    
//    GPIO_InitTypeDef GPIO_InitStructure;
     /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
   SetSysClockTo72();
 
      /* This function fills the RCC_ClockFreq structure with the current
     frequencies of different on chip clocks (for debug purpose) */
  RCC_GetClocksFreq(&RCC_ClockFreq);
    
      /* Output HSE clock on MCO pin ---------------------------------------------*/
/*  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  RCC_MCOConfig(RCC_MCO_HSE);*/
    }
    
    /**
  * @brief  Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  *         and PCLK1 prescalers.
  * @param  None
  * @retval None
  */
void SetSysClockTo72(void)
{
  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/   
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if (HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
 
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);


    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);


    /* Enable PLL */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
  else
  { /* If HSE fails to start-up, the application will have wrong clock configuration.
       User can add here some code to deal with this error */    

    /* Go to infinite loop */
    while (1)
    {
    }
  }
}








Outcomes