2015-12-12 02:04 AM
Hi all,
i use KIT stm32f407 to dev PWM for motor. I use timer1 with channel 1 (Port A Pin 8 ) to output PWM. I use HAL library with Keil C 5. My code build success (no error). And i load to hardware OK. But i can know my code work OK or NOT. I use Keil C debug logic analyzer to view PWM but nothing show. Any body help me? Here is my code./* Includes ------------------------------------------------------------------*/
#include ''main.h''
GPIO_InitTypeDef GPIO_InitStruct;
TIM_Base_InitTypeDef TIM_BaseStruct;
TIM_HandleTypeDef TIM_HandleStruct;
TIM_OC_InitTypeDef TIM_OCStruct;
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
void GPIO_Init(void);
void TIM_Init(void);
void PWM_Init(void);
int main(void)
{
HAL_Init();
/* Configure the system clock to 168 MHz */
SystemClock_Config();
__TIM1_CLK_ENABLE(); //cap xung clock TIM1
__GPIOA_CLK_ENABLE();//cap xung clock Port A
GPIO_Init();
TIM_Init();
PWM_Init();
HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_4);
while (1)
{
}
}
void GPIO_Init()
{
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); //init port A
//----------------------------------------------------------------------------
}
void TIM_Init()
{
TIM_BaseStruct.Prescaler = 0;
TIM_BaseStruct.CounterMode = TIM_COUNTERMODE_UP;
TIM_BaseStruct.Period = 10000-1;
TIM_BaseStruct.ClockDivision = 0;
TIM_BaseStruct.RepetitionCounter = 0;
TIM_HandleStruct.Instance = TIM1;
TIM_HandleStruct.Init = TIM_BaseStruct;
//TIM_HandleStruct.Channel = HAL_TIM_ACTIVE_CHANNEL_1;
//HAL_TIM_PWM_Init(&TIM_HandleStruct);
/*if(HAL_TIM_PWM_Init(&TIM_HandleStruct) != HAL_OK)
{
printf(''HAL_TIM_PWM_Init ERROR'');
}*/
// Initialize the timer hardware in output compare mode
HAL_TIM_OC_Init(&TIM_HandleStruct);
}
void PWM_Init()
{
TIM_OCStruct.Pulse = 2000;
TIM_OCStruct.OCMode = TIM_OCMODE_PWM1;
//TIM_OCStruct.OCMode = TIM_OCMODE_PWM2;
TIM_OCStruct.OCPolarity =TIM_OCPOLARITY_HIGH; //Highe là dau chu ki la muc cao
TIM_OCStruct.OCIdleState = TIM_OCIDLESTATE_SET;// cho phép hoac k cho phép chan PWM hoat ****
TIM_OCStruct.OCNPolarity =TIM_OCPOLARITY_HIGH;
TIM_OCStruct.OCNIdleState = TIM_OCIDLESTATE_SET;
//TIM_OCStruct.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct,&TIM_OCStruct,TIM_CHANNEL_1 );
TIM_OCStruct.Pulse = 4000;
TIM_OCStruct.OCMode = TIM_OCMODE_PWM1;
//TIM_OCStruct.OCMode = TIM_OCMODE_PWM2;
TIM_OCStruct.OCPolarity =TIM_OCPOLARITY_HIGH; //Highe là dau chu ki la muc cao
TIM_OCStruct.OCIdleState = TIM_OCIDLESTATE_SET;// cho phép hoac k cho phép chan PWM hoat ****
TIM_OCStruct.OCNPolarity =TIM_OCPOLARITY_HIGH;
TIM_OCStruct.OCNIdleState = TIM_OCIDLESTATE_SET;
//TIM_OCStruct.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct,&TIM_OCStruct,TIM_CHANNEL_2 );
TIM_OCStruct.Pulse = 6000;
TIM_OCStruct.OCMode = TIM_OCMODE_PWM1;
//TIM_OCStruct.OCMode = TIM_OCMODE_PWM2;
TIM_OCStruct.OCPolarity =TIM_OCPOLARITY_HIGH; //Highe là dau chu ki la muc cao
TIM_OCStruct.OCIdleState = TIM_OCIDLESTATE_SET;// cho phép hoac k cho phép chan PWM hoat ****
TIM_OCStruct.OCNPolarity =TIM_OCPOLARITY_HIGH;
TIM_OCStruct.OCNIdleState = TIM_OCIDLESTATE_SET;
//TIM_OCStruct.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct,&TIM_OCStruct,TIM_CHANNEL_3 );
TIM_OCStruct.Pulse = 8000;
TIM_OCStruct.OCMode = TIM_OCMODE_PWM1;
//TIM_OCStruct.OCMode = TIM_OCMODE_PWM2;
TIM_OCStruct.OCPolarity =TIM_OCPOLARITY_HIGH; //Highe là dau chu ki la muc cao
TIM_OCStruct.OCIdleState = TIM_OCIDLESTATE_SET;// cho phép hoac k cho phép chan PWM hoat ****
TIM_OCStruct.OCNPolarity =TIM_OCPOLARITY_HIGH;
TIM_OCStruct.OCNIdleState = TIM_OCIDLESTATE_SET;
//TIM_OCStruct.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct,&TIM_OCStruct,TIM_CHANNEL_4 );
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 168000000
* HCLK(Hz) = 168000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 8000000
* PLL_M = 8
* PLL_N = 336
* PLL_P = 2
* PLL_Q = 7
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 5
* @param None
* @retval None
*/
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
if (HAL_GetREVID() == 0x1001)
{
/* Enable the Flash prefetch */
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
}
}
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
static void Error_Handler(void)
{
/* User may add here some code to deal with this error */
while(1)
{
}
}
#ifdef USE_FULL_ASSERT
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
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#timer-pwm #debug-pwm #timer #pwm #debug
2015-12-28 11:21 PM
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
....2016-01-12 02:56 AM
Hi nguyen.khoi.001,
It's normal behavior since you debug and the counter of timer still counting.
To debug correctly TIM1 and TIM4 processing you should set the corresponding debug bit in the debug MCU freeze registers:
DBG_TIM1_STOP (
DBGMCU_APB2_FZ register);
-Hannibal-