Skocz do zawartości

[KEIL][STM32]Problem z PWM


Armir

Pomocna odpowiedź

Mam problem z generowaniem pwm. Chcę uruchomić PWM na timerze 1 kanał 3. Po wgraniu kodu żaden sygnał nie jest generowany na PA10. W załączniku cały projekt w keilu.

// Includes ------------------------------------------------------------------
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#define S1	(1 << 2)	//D
#define S2	(1 << 12)	//C
#define S3	(1 << 11)	//C
// Private function prototypes -----------------------------------------------
void GPIO_Configuration(void);
void Delay(__IO uint32_t nCount);
void Clock_conf(void);
void Pwm_conf(void);
 GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  TIM_OCInitTypeDef TIM_OCInitStruct;

int main(void)
{

Clock_conf(); 
 	// GPIO Configuration
 	GPIO_Configuration();
   Pwm_conf();

     GPIO_SetBits(GPIOB, GPIO_Pin_12);
  GPIO_SetBits(GPIOA, GPIO_Pin_10);
 while (1)
 {
 TIM1->CCR3 = 999;
   // Turn on D2
if(GPIO_ReadInputDataBit(GPIOD, S1) == 0){          
//	GPIO_SetBits(GPIOA, GPIO_Pin_10);
   GPIO_SetBits(GPIOB, GPIO_Pin_12);
  	}else{
//	GPIO_ResetBits(GPIOA, GPIO_Pin_10);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}

 }
}


void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure all unused GPIO port pins in Analog Input mode (floating input
    trigger OFF), this will reduce the power consumption and increase the device
    immunity against EMI/EMC *************************************************/

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                        RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
                        RCC_APB2Periph_GPIOE | RCC_APB2ENR_TIM1EN , ENABLE);		    
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM1, ENABLE );
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 GPIO_Init(GPIOB, &GPIO_InitStructure);
 GPIO_Init(GPIOC, &GPIO_InitStructure);
 GPIO_Init(GPIOD, &GPIO_InitStructure);
 GPIO_Init(GPIOE, &GPIO_InitStructure);

 // Configuration of LED pins
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(GPIOB, &GPIO_InitStructure);

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_11;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_Init(GPIOC, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_Init(GPIOD, &GPIO_InitStructure);	

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
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_10 | GPIO_Pin_2;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
  */
}

void Clock_conf(void) 
{ 
  ErrorStatus HSEStartUpStatus; 

  // Reset ustawien RCC 
  RCC_DeInit(); 
  // Wlacz HSE 
  RCC_HSEConfig(RCC_HSE_ON); 
  // Czekaj za HSE bedzie gotowy 
  HSEStartUpStatus = RCC_WaitForHSEStartUp(); 
  if(HSEStartUpStatus == SUCCESS) 
  { 
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); 
        // zwloka dla pamieci Flash 
        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_PREDIV1, RCC_PLLMul_9); 
        // Wlacz PLL 
        RCC_PLLCmd(ENABLE); 
        // Czekaj az PLL poprawnie sie uruchomi 
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); 
        // PLL bedzie zrodlem sygnalu zegarowego 
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 
        // Czekaj az PLL bedzie sygnalem zegarowym systemu 
        while(RCC_GetSYSCLKSource() != 0x08); 
  }

}

void Pwm_conf(void){
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure; 
TIM_OCInitTypeDef  TIM_OCInitStructure; 

/* Time base configuration */ 
  TIM_TimeBaseStructure.TIM_Period = 999; 
  TIM_TimeBaseStructure.TIM_Prescaler = 0; 
   TIM_TimeBaseStructure.TIM_ClockDivision = 0; 
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 

   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 

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

  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 = 999; 
  TIM_OC3Init(TIM1, &TIM_OCInitStructure); 
  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); 

  TIM_ARRPreloadConfig(TIM1, ENABLE); 

  TIM_Cmd(TIM1, ENABLE); 
}

//******************************************************************************
// Simple delay function
//******************************************************************************
void Delay(__IO uint32_t nCount)
{
 for(; nCount != 0; nCount--);
}

[ Dodano: 29-08-2011, 10:38 ]

// Includes ------------------------------------------------------------------
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_it.h"
#define S1	(1 << 2)	//D
#define S2	(1 << 12)	//C
#define S3	(1 << 11)	//C
// Private function prototypes -----------------------------------------------
void GPIO_Configuration(void);
void Delay(__IO uint32_t nCount);
void Clock_conf(void);
void Pwm_conf(void);
 GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  TIM_OCInitTypeDef TIM_OCInitStruct;

int main(void)
{

Clock_conf(); 
 	// GPIO Configuration
 	GPIO_Configuration();
   Pwm_conf();

     //GPIO_SetBits(GPIOB, GPIO_Pin_12);
  //GPIO_SetBits(GPIOA, GPIO_Pin_10);
 while (1)
 {
 	// TIM1->CCR3 = 32000;
 //TIM1->CCR3 = 999;
   // Turn on D2
while(GPIO_ReadInputDataBit(GPIOD, S1) == 0){          
//	GPIO_SetBits(GPIOA, GPIO_Pin_10);
   GPIO_SetBits(GPIOB, GPIO_Pin_12);
TIM1->CCR3 = 32000;}
 while(GPIO_ReadInputDataBit(GPIOD, S1) == 1){
//	GPIO_ResetBits(GPIOA, GPIO_Pin_10);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
TIM1->CCR3 = 0;
}

 }
}


void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure all unused GPIO port pins in Analog Input mode (floating input
    trigger OFF), this will reduce the power consumption and increase the device
    immunity against EMI/EMC *************************************************/

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                        RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
                        RCC_APB2Periph_GPIOE | RCC_APB2ENR_TIM1EN , ENABLE);		    
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM1, ENABLE );
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 GPIO_Init(GPIOB, &GPIO_InitStructure);
 GPIO_Init(GPIOC, &GPIO_InitStructure);
 GPIO_Init(GPIOD, &GPIO_InitStructure);
 GPIO_Init(GPIOE, &GPIO_InitStructure);

 // Configuration of LED pins
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(GPIOB, &GPIO_InitStructure);

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_11;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_Init(GPIOC, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_Init(GPIOD, &GPIO_InitStructure);	
/*
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
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_10 | GPIO_Pin_2;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
  */
}

void Clock_conf(void) 
{ 
  ErrorStatus HSEStartUpStatus; 

  // Reset ustawien RCC 
  RCC_DeInit(); 
  // Wlacz HSE 
  RCC_HSEConfig(RCC_HSE_ON); 
  // Czekaj za HSE bedzie gotowy 
  HSEStartUpStatus = RCC_WaitForHSEStartUp(); 
  if(HSEStartUpStatus == SUCCESS) 
  { 
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); 
        // zwloka dla pamieci Flash 
        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_PREDIV1, RCC_PLLMul_9); 
        // Wlacz PLL 
        RCC_PLLCmd(ENABLE); 
        // Czekaj az PLL poprawnie sie uruchomi 
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); 
        // PLL bedzie zrodlem sygnalu zegarowego 
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 
        // Czekaj az PLL bedzie sygnalem zegarowym systemu 
        while(RCC_GetSYSCLKSource() != 0x08); 
  }

}

void Pwm_conf(void){

TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef IO_InitStruct;
u16 PrescalerValue;

/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1, ENABLE);

/* Enable the TIM1 GPIO on PA10 */
IO_InitStruct.GPIO_Pin = GPIO_Pin_10;
IO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
IO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &IO_InitStruct);

/* Time base configuration */
PrescalerValue = 0;
TIM_TimeBaseStruct.TIM_Period = (uint16_t) (72000000 / (2048));
TIM_TimeBaseStruct.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);

/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_Pulse = 0;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;		

TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_OC3Init(TIM1, &TIM_OCInitStruct);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM1->CR2 = 0x00;

/* TIM1 enable counter and interrupts */
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, ENABLE);
   TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* Enable the TIM1 Interrupt */
//NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM16_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


//******************************************************************************
// Simple delay function
//******************************************************************************
void Delay(__IO uint32_t nCount)
{
 for(; nCount != 0; nCount--);
}

Problem rozwiązany. Poprawny kod u góry.

program.rar

Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.