اطلاعیه

Collapse
No announcement yet.

کار با تایمرها

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    کار با تایمرها

    آقا من یک موج pwm با فرکانس 2KHZ می خوام.میکرویی که باهاش کار میکنم stm32f407vg هسته....برنامه که نوشتم باید خروجی با فرکانس دو کیلو رو بده ولی متاسفانه نمیده(641HZ رو میده)...کسی میتونه راهنماییم کنه؟
    کد:
    /**
     ******************************************************************************
     * @file  TIM/TIM_CascadeSynchro/main.c 
     * @author MCD Application Team
     * @version V1.3.0
     * @date  13-November-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:
     *
     *    http://www.st.com/software_license_agreement_liberty_v2
     *
     * 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.c"
    #include "stm32f4xx_rcc.c"
    #include "stm32f4xx_tim.c"
    
    
    /** @addtogroup STM32F4xx_StdPeriph_Examples
     * @{
     */
    
    /** @addtogroup TIM_CascadeSynchro
     * @{
     */ 
    
    /* Private typedef -----------------------------------------------------------*/
    /* Private define ------------------------------------------------------------*/
    /* Private macro -------------------------------------------------------------*/
    /* Private variables ---------------------------------------------------------*/
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    
    /* Private function prototypes -----------------------------------------------*/
    void TIM_Config(void);
    
    /* Private functions ---------------------------------------------------------*/
    
    /**
     * @brief Main program
     * @param None
     * @retval None
     */
    int main(void)
    {
     /*!< 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
       */   
        
     /* TIM2/3/4 Configuration */
     TIM_Config();
    
    
    
     /* Time base configuration */
     TIM_TimeBaseStructure.TIM_Period = 41999;
     TIM_TimeBaseStructure.TIM_Prescaler = 0;
     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
     TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    
     TIM_TimeBaseStructure.TIM_Period = 41999;
     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    	
    
     TIM_TimeBaseStructure.TIM_Period = 41999;
     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
    
     /* Master Configuration in PWM1 Mode */
     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
     TIM_OCInitStructure.TIM_Pulse = 21000;
     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    
     TIM_OC1Init(TIM2, &TIM_OCInitStructure);
    
     /* Select the Master Slave Mode */
     TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
    
     /* Master Mode selection */
     TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
    
     /* Slaves Configuration: PWM1 Mode */
     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
     TIM_OCInitStructure.TIM_Pulse = 40000;
    
     TIM_OC1Init(TIM3, &TIM_OCInitStructure);
     
     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
     TIM_OCInitStructure.TIM_Pulse = 40000;
     
     TIM_OC1Init(TIM4, &TIM_OCInitStructure);
    
     /* Slave Mode selection: TIM3 */
     TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);
     TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1);
    
     /* Select the Master Slave Mode */
     TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
    
     /* Master Mode selection: TIM3 */
     TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
    
     /* Slave Mode selection: TIM4 */
     TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);
     TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);
     
     /* TIM enable counter */
    	
    
     TIM_Cmd(TIM2, ENABLE);
     TIM_Cmd(TIM3, ENABLE);
     TIM_Cmd(TIM4, ENABLE);
    
     while (1)
     {
     }
    }
    
    /**
     * @brief Configure the TIM2/3/4 Pins.
     * @param None
     * @retval None
     */
    void TIM_Config(void)
    {
     GPIO_InitTypeDef GPIO_InitStructure;
    
     /* TIM2, TIM3 and TIM4 clock enable */
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
                 RCC_APB1Periph_TIM4, ENABLE);
    
     /* GPIOA and GPIOB clocks enable */
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
    
     /* GPIOB Configuration: PB4(TIM3 CH1) as alternate function push-pull ------*/
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
     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 pins to AF2 */
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3);
    
     /* GPIOB Configuration: PB6(TIM4 CH1) as alternate function push-pull ------*/
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
     GPIO_Init(GPIOB, &GPIO_InitStructure);
     
     /* Connect TIM pins to AF2 */
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4); 
    
     /* GPIOA Configuration: PA0(TIM2 CH1) as alternate function push-pull ------*/
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
    
     /* Connect TIM pins to AF1 */
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
    
    }
    
    #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****/

    #2
    پاسخ : کار با تایمرها

    سلام
    خوب در جایی که برای تنظیم فرکانس هست درست ست نشده
    دو رجیستر PSC و ARR برای این کار هست ...
    فيلم آموزشي ميکروکنترلر LPC1768
    فيلم آموزشي تکميلي و پيشرفته ميکروکنترلر LPC1768
    فيلم آموزش کاربردی زبان سی در میکروکنترلر ARM
    فیلم آموزش مقدماتی میکروکنترلر LPC1788 به زودی ...

    دیدگاه

    لطفا صبر کنید...
    X