stm32f407 Timer1(PWM) çalışmıyor.

Başlatan Gökhan BEKEN, 07 Haziran 2015, 11:04:49

muhittin_kaplan


Gökhan BEKEN

#16
Çok ilginç, şu an Timer1 için sadece GPIOE 14'den çıkış alabiliyorum:
#include "stm32f4xx_conf.h"
#include <math.h>

volatile short MyDelay;

/* For 50 Hz PWM signal Prescaler and Period Values */
uint16_t Prescaler = 84;
uint32_t Period = 20000;

/* Pulse Values for Channel 1,2,3,4 */
uint32_t PulseValue1 = 3000;     // Channel1 Duty Cycle %0  = (0/20000)*100
uint32_t PulseValue2 = 1000; // Channel2 Duty Cycle %25 = (5000/20000)*100
uint32_t PulseValue3 = 12000; // Channel1 Duty Cycle %50 = (10000/20000)*100
uint32_t PulseValue4 = 18000; // Channel1 Duty Cycle %75 = (15000/20000)*100



void Pin3_Config(void);
void Timer3_PWM_Output_Config(uint32_t PeriodValue, uint16_t PrescalerValue);

void Pin1_Config(void);
void Timer1_PWM_Output_Config(uint32_t PeriodValue, uint16_t PrescalerValue);

void DelayMs(unsigned int ms);

int main(void)
{
    SystemInit();
    SysTick_Config(SystemCoreClock / 1000); //1milisaniyelik system clock elde edelim

    Pin3_Config();
    Timer3_PWM_Output_Config(Period, Prescaler);

    Pin1_Config();
    Timer1_PWM_Output_Config(Period, Prescaler);


    TIM_SetCompare1(TIM3,5000);
    TIM_SetCompare2(TIM3,5000);
    TIM_SetCompare3(TIM3,5000);
    TIM_SetCompare4(TIM3,5000);

    TIM_SetCompare1(TIM1,5000);
    TIM_SetCompare2(TIM1,5000);
    TIM_SetCompare3(TIM1,5000);
    TIM_SetCompare4(TIM1,5000);
    while(1);
}

void Pin3_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;


    /* TIM3 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    /* GPIOC clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
    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(GPIOC, &GPIO_InitStructure);

    /* Connect TIM3 pins to AF2 */
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);


    /* Connect TIM3 pins to AF2 */
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3);

    /******************************************************************/
}

void Timer3_PWM_Output_Config(uint32_t PeriodValue, uint16_t PrescalerValue)
{

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;

    // Time base configuration:
    TIM_TimeBaseStructure.TIM_Period = PeriodValue - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    // PWM1 Mode configuration:
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;

    // PWM1 Mode configuration: Channel1:
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);


    // PWM1 Mode configuration: Channel2:
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);


    // PWM1 Mode configuration: Channel3:
    TIM_OC3Init(TIM3, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);


    // PWM1 Mode configuration: Channel4:
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);


    TIM_ARRPreloadConfig(TIM3, ENABLE);


    TIM_Cmd(TIM3, ENABLE); // TIM3 enable counter

}






void Pin1_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); // Enable clock for TIM1

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); // GPIOE clock enable


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14 ; // GPIOE Configuration: TIM1 CH1 (PE9), TIM1 CH2 (PE11) TIM1 CH4 (PE13), TIM1 CH4 (PE14)
    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(GPIOE, &GPIO_InitStructure);



    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1); // Connect TIM1 pins to AF1
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_TIM1);
}


void Timer1_PWM_Output_Config(uint32_t PeriodValue, uint16_t PrescalerValue)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;


    // Time base configuration :
    TIM_TimeBaseStructure.TIM_Period = PeriodValue - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);


    // PWM1 Mode configuration :
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;


    // PWM1 Mode configuration: Channel1 :
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = PulseValue1;

    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

    // PWM1 Mode configuration: Channel2 :
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = PulseValue2;

    TIM_OC2Init(TIM1, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);

    // PWM1 Mode configuration: Channel3 :
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = PulseValue3;

    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);

    // PWM1 Mode configuration: Channel4 :
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = PulseValue4;

    TIM_OC4Init(TIM1, &TIM_OCInitStructure);
    TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);

    TIM_ARRPreloadConfig(TIM1, ENABLE);
    // TIM1 enable counter :
    TIM_Cmd(TIM1, ENABLE);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);////////

}




void SysTick_Handler(void)
{
    if (MyDelay)
    {
        MyDelay--;
    }
}

void DelayMs(unsigned int ms)
{
    MyDelay=ms;
    while(MyDelay);
}

#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) */

    /* Infinite loop */
    while (1)
    {
    }
}
#endif


Muhittin hocam bahsettiğin koda baktım da GPIOE'nin pwm olmayan pinlerini bile eklemiş, daha tam incelemedim, deneyeceğim.

mesaj birleştirme:: 09 Haziran 2015, 15:44:04

Bu daha da ilginç geldi, Muhittin hocam, senin verdiğin linkteki kodu denedim, e8,e9,e10,e11 den pwm çıkışı aldım. Nasıl olur böyle birşey? Normalde e9 e11 e13 ve e14 den çıkış alınabiliyor.

Kod bu:
void TIM1_GPIO_Config(void)
{
  //The output of the PE 891011121314
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
| GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOE,&GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource12,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_TIM1);
}
//TIM1 do PWM output
void Tim1_Config(void)
{
  unsigned int TimerPeriod =  (SystemCoreClock / 20000 ) - 1;
  unsigned int ccr1 = TimerPeriod / 2;  //1 duty cycle/2 = 50%
  unsigned int ccr2 = TimerPeriod / 3;  //1 duty cycle/3 = 33%
  unsigned int ccr3 = TimerPeriod / 4;  //1 duty cycle/4 = 25%
  unsigned int ccr4 = TimerPeriod / 5;  //1 duty cycle/5 = 20%

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

  //Time initialization
  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Control with dead zone.
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;  //Counter direction
  TIM_TimeBaseInitStructure.TIM_Prescaler = 0;   //Timer clock = sysclock /(TIM_Prescaler+1) = 168M
  TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInitStructure.TIM_Period = TimerPeriod - 1;
//Period = (TIM counter clock / TIM output clock) - 1 = 20K
  TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);


TIM_OCInitTypeDef TIM_OCInitStructure;

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_Pulse = ccr1;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

  TIM_OC1Init(TIM1,&TIM_OCInitStructure);
  TIM_OCInitStructure.TIM_Pulse = ccr2;
  TIM_OC2Init(TIM1,&TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = ccr3;
  TIM_OC3Init(TIM1,&TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = ccr4;
  TIM_OC4Init(TIM1,&TIM_OCInitStructure);

  TIM_Cmd(TIM1,ENABLE);
  TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
Özel mesaj okumuyorum, lütfen göndermeyin.

justice_for_all

normalde PE8 ve PE9 birbirininin tersi pwm üretir ama kodda ikiside aynı polaritede olduğu için aynı dutyde pwm çıkarır. PE10 ve PE11 de aynı şekilde.
Deneyip de başaramayanları değil, yalnızca denemeye bile kalkışmayanları yargıla.   Gökhan Arslanbay

skara1214

Aynı zamanda aynı sıkıntıyı yaşamışız herhalde hal libraryleri için;
   HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
   HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
   HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
   HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
eklemek sorunu çözüyor
Herkes ölür ama herkes gerçekten yaşamaz

RaMu

CubeMx eksik kod üretebiliyormuş,
sd card kullanımını araştırıyorum şuan,
sd card da benzer bir durumla karşılaşan birinin yazısını okudum,
cubemx in yeni versiyonlarında düzeltmeleri uyguluyorlar.
Son vrsiyon cubemx kullanmaya özen göstermek lazım.
Şuan cubemx 4.8.0 var son versiyon olarak,
Cubemx de help kısmından check for updates ile kontrol edebilirsiniz.
Sorularınıza hızlı cevap alın: http://www.picproje.org/index.php/topic,57135.0.html