本文整理汇总了C++中HAL_TIM_PWM_Start函数的典型用法代码示例。如果您正苦于以下问题:C++ HAL_TIM_PWM_Start函数的具体用法?C++ HAL_TIM_PWM_Start怎么用?C++ HAL_TIM_PWM_Start使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HAL_TIM_PWM_Start函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(void)
{
/* -1- Init System*/
HAL_Init();
SystemClock_Config();
/* -2- Calculate Prescaler */
//We want the Timer to run with 8MHz (the maximum, since we're running of the internal Clock, which runs at 8MHz, without PLL)
uhPrescalerValue = (uint32_t) (SystemCoreClock / 8000000) - 1;
/* -3- Enable Clocks*/
//GPIO
__GPIOA_CLK_ENABLE();
//TIMER
__TIM1_CLK_ENABLE();
/* -4- Configure GPIO Pin*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* -5- Configure Timer*/
TIM_HandleStruct.Instance = TIM1;
TIM_HandleStruct.Init.Prescaler = uhPrescalerValue;
TIM_HandleStruct.Init.Period = PERIOD_VALUE;
TIM_HandleStruct.Init.ClockDivision = 0;
TIM_HandleStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM_HandleStruct.Init.RepetitionCounter = 0;
HAL_TIM_PWM_Init(&TIM_HandleStruct);
/* -6- Configure PWM-Output*/
TIM_OC_InitStruct.OCMode = TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OC_InitStruct.OCFastMode = TIM_OCFAST_DISABLE;
TIM_OC_InitStruct.OCNPolarity = TIM_OCNPOLARITY_HIGH;
TIM_OC_InitStruct.OCIdleState = TIM_OCIDLESTATE_RESET;
TIM_OC_InitStruct.OCNIdleState = TIM_OCNIDLESTATE_RESET;
TIM_OC_InitStruct.Pulse = CMP_VAL;
HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct, &TIM_OC_InitStruct, TIM_CHANNEL_3);
/* -7- Enable Timer and PWM Output*/
HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_3);
while (1) {
/* -8- Illuminate LED*/
for (CMP_VAL = 0; CMP_VAL != (PERIOD_VALUE); CMP_VAL++) {
TIM_OC_InitStruct.Pulse = CMP_VAL;
HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct, &TIM_OC_InitStruct, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_3);
HAL_Delay(1);
}
/* -8- Wait a bit at full brightness*/
HAL_Delay(1000);
/* -10- Dim LED*/
for (CMP_VAL = (PERIOD_VALUE); CMP_VAL != 0; CMP_VAL--) {
TIM_OC_InitStruct.Pulse = CMP_VAL;
HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct, &TIM_OC_InitStruct, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_3);
HAL_Delay(1);
}
/* -11- Leave it dark*/
HAL_Delay(1000);
}
}
示例2: main
//.........这里部分代码省略.........
Timer clock prescalers selection activated (TIMPRE bit from RCC_DCKCFGR register is set).
TIM3CLK = 4 * PCLK1
PCLK1 = HCLK / 4
=> TIM3CLK = HCLK = SystemCoreClock
For TIM3CLK equal to SystemCoreClock and prescaler equal to (5 - 1), TIM3 counter clock
is computed as follows:
TIM3 counter clock = TIM3CLK / (Prescaler + 1)
= SystemCoreClock / (Prescaler + 1)
= 36MHz
For ARR equal to (1800 - 1), the TIM3 output clock is computed as follows:
TIM3 output clock = TIM3 counter clock / (ARR + 1)
= 20KHZ
The TIM3 CCR1 register value is equal to 900, so the TIM3 Channel 1 generates a
PWM signal with a frequency equal to 20 KHz and a duty cycle equal to 50%:
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR + 1)* 100 = 50%
Note:
SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
Each time the core clock (HCLK) changes, user had to update SystemCoreClock
variable value. Otherwise, any configuration based on this variable will be incorrect.
This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetSysClockFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
----------------------------------------------------------------------- */
/* Timer clock prescalers selection activation */
__HAL_RCC_TIMCLKPRESCALER(RCC_TIMPRES_ACTIVATED);
/* Initialize TIMx peripheral as follows:
+ Prescaler = (SystemCoreClock / 36000000) - 1
+ Period = (1800 - 1)
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Instance = TIMx;
TimHandle.Init.Prescaler = uhPrescalerValue;
TimHandle.Init.Period = PERIOD_VALUE;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
/* Set the pulse value for channel 1 */
sConfig.Pulse = PULSE1_VALUE;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Start PWM signals generation #######################################*/
/* Start channel 1 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 2 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 3 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
/* Start channel 4 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
while (1)
{
}
}
示例3: main
//.........这里部分代码省略.........
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
Note:
SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f3xx.c file.
Each time the core clock (HCLK) changes, user had to update SystemCoreClock
variable value. Otherwise, any configuration based on this variable will be incorrect.
This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetSysClockFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
----------------------------------------------------------------------- */
/* Initialize TIMx peripheral as follows:
+ Prescaler = (SystemCoreClock/24000000) - 1
+ Period = 665
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Instance = TIMx;
TimHandle.Init.Prescaler = uhPrescalerValue;
TimHandle.Init.Period = PERIOD_VALUE;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
/* Set the pulse value for channel 1 */
sConfig.Pulse = PULSE1_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 2 */
sConfig.Pulse = PULSE2_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 3 */
sConfig.Pulse = PULSE3_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 4 */
sConfig.Pulse = PULSE4_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Start PWM signals generation #######################################*/
/* Start channel 1 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 2 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 3 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 4 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
while (1)
{
}
}
示例4: HAL_SYSTICK_Callback
/**
* @brief SYSTICK callback.
* @param None
* @retval None
*/
void HAL_SYSTICK_Callback(void)
{
uint8_t *buf;
uint16_t Temp_X, Temp_Y = 0x00;
uint16_t NewARR_X, NewARR_Y = 0x00;
if (DemoEnterCondition != 0x00)
{
buf = USBD_HID_GetPos();
if((buf[1] != 0) ||(buf[2] != 0))
{
USBD_HID_SendReport (&hUSBDDevice,
buf,
4);
}
Counter ++;
if (Counter == 10)
{
/* Reset Buffer used to get accelerometer values */
Buffer[0] = 0;
Buffer[1] = 0;
/* Disable All TIM4 Capture Compare Channels */
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1);
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_2);
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
/* Read Acceleration*/
BSP_ACCELERO_GetXYZ(Buffer);
/* Set X and Y positions */
X_Offset = Buffer[0];
Y_Offset = Buffer[1];
/* Update New autoreload value in case of X or Y acceleration*/
/* Basic acceleration X_Offset and Y_Offset are divide by 40 to fir with ARR range */
NewARR_X = TIM_ARR - ABS(X_Offset/40);
NewARR_Y = TIM_ARR - ABS(Y_Offset/40);
/* Calculation of Max acceleration detected on X or Y axis */
Temp_X = ABS(X_Offset/40);
Temp_Y = ABS(Y_Offset/40);
MaxAcceleration = MAX_AB(Temp_X, Temp_Y);
if(MaxAcceleration != 0)
{
/* Reset CNT to a lowest value (equal to min CCRx of all Channels) */
__HAL_TIM_SET_COUNTER(&htim4,(TIM_ARR-MaxAcceleration)/2);
if (X_Offset < ThreadholdAcceleroLow)
{
/* Sets the TIM4 Capture Compare for Channel1 Register value */
/* Equal to NewARR_X/2 to have duty cycle equal to 50% */
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, NewARR_X/2);
/* Time base configuration */
__HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_X);
/* Enable TIM4 Capture Compare Channel1 */
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
}
else if (X_Offset > ThreadholdAcceleroHigh)
{
/* Sets the TIM4 Capture Compare for Channel3 Register value */
/* Equal to NewARR_X/2 to have duty cycle equal to 50% */
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, NewARR_X/2);
/* Time base configuration */
__HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_X);
/* Enable TIM4 Capture Compare Channel3 */
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
}
if (Y_Offset > ThreadholdAcceleroHigh)
{
/* Sets the TIM4 Capture Compare for Channel2 Register value */
/* Equal to NewARR_Y/2 to have duty cycle equal to 50% */
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,NewARR_Y/2);
/* Time base configuration */
__HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_Y);
/* Enable TIM4 Capture Compare Channel2 */
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);
}
else if (Y_Offset < ThreadholdAcceleroLow)
{
//.........这里部分代码省略.........
示例5: MOTOR_Init
/**
* @brief Configures IOs to control the two motors and one pump
* @param None
* @retval None
*/
void MOTOR_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct;
// Enable all timers
PUMP_PWM_TIMER_CLK_ENABLE();
MOTOR_PWM_TIMER_CLK_ENABLE();
MOTOR_HALL_ENC1_TIMER_CLK_ENABLE();
MOTOR_HALL_ENC2_TIMER_CLK_ENABLE();
MOTOR_HALL_SPEED_TIMER_CLK_ENABLE();
// Enable the clock for all IO pins
MOTOR_PWM_CLK_ENABLE();
MOTOR_CURR_CLK_ENABLE();
MOTOR_HALL_M1_ENC_CLK_ENABLE();
MOTOR_HALL_M2_ENC_CLK_ENABLE();
MOTOR_HALL_SPEED_CLK_ENABLE();
// Motor driver --------------------------------------------------------
// Configure the 4 motor pins of motor 1 and 2 as normal IO
GPIO_InitStruct.Pin = MOTOR_M2_IN1_PIN | MOTOR_M2_IN2_PIN
| MOTOR_M1_IN1_PIN| MOTOR_M1_IN2_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = MOTOR_PWM_TIMER_AF;
HAL_GPIO_Init(MOTOR_PWM_PORT, &GPIO_InitStruct);
// Configure the 2 motor pins of motor 3 as PWM output
GPIO_InitStruct.Pin = MOTOR_M3_IN1_PIN | MOTOR_M3_IN2_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = PUMP_PWM_TIMER_AF;
HAL_GPIO_Init(MOTOR_PWM_PORT, &GPIO_InitStruct);
// Configure the 10 motor current pins
GPIO_InitStruct.Pin = MOTOR_M2_I0_PIN | MOTOR_M2_I1_PIN | MOTOR_M2_I2_PIN
| MOTOR_M2_I3_PIN | MOTOR_M2_I4_PIN
| MOTOR_M1_I0_PIN | MOTOR_M1_I1_PIN | MOTOR_M1_I2_PIN
| MOTOR_M1_I3_PIN | MOTOR_M1_I4_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(MOTOR_CURR_PORT, &GPIO_InitStruct);
// Timer configuration for motor pwm
htimMotor.Instance = MOTOR_PWM_TIMER;
htimMotor.Init.Period = MOTOR_MAX - 1; // = 20kHz = 84MHz / 1 / 4200
htimMotor.Init.Prescaler = 1-1;
htimMotor.Init.ClockDivision = 1;
htimMotor.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&htimMotor);
// Timer configuration for pump
htimPump.Instance = PUMP_PWM_TIMER;
htimPump.Init.Period = MOTOR_MAX - 1; // = 20kHz = 168MHz / 2 / 4200
htimPump.Init.Prescaler = 2-1;
htimPump.Init.ClockDivision = 1;
htimPump.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&htimPump);
// Configure Timer 1 channel 1 and 2 as PWM output
sConfigTimMotor.OCMode = TIM_OCMODE_PWM1;
sConfigTimMotor.Pulse = 0;
sConfigTimMotor.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigTimMotor.OCFastMode = TIM_OCFAST_ENABLE;
sConfigTimMotor.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigTimMotor.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigTimMotor.OCNIdleState= TIM_OCNIDLESTATE_SET;
// Configure Timer 1 channel 1 as PWM output
sConfigTimPump.OCMode = TIM_OCMODE_PWM1;
sConfigTimPump.Pulse = 0;
sConfigTimPump.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigTimPump.OCFastMode = TIM_OCFAST_ENABLE;
sConfigTimPump.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigTimPump.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigTimPump.OCNIdleState= TIM_OCNIDLESTATE_SET;
// PWM Mode
HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_3);
HAL_TIM_PWM_ConfigChannel(&htimMotor, &sConfigTimMotor, TIM_CHANNEL_4);
HAL_TIM_PWM_ConfigChannel(&htimPump, &sConfigTimPump, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htimPump, &sConfigTimPump, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htimMotor, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htimPump, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htimPump, TIM_CHANNEL_2);
//.........这里部分代码省略.........
示例6: main
//.........这里部分代码省略.........
TimHandle.Init.RepetitionCounter = 0;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sPWMConfig.OCMode = TIM_OCMODE_PWM1;
sPWMConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sPWMConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sPWMConfig.OCIdleState = TIM_OCIDLESTATE_SET;
sPWMConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
/* Set the pulse value for channel 1 */
sPWMConfig.Pulse = PULSE1_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sPWMConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 2 */
sPWMConfig.Pulse = PULSE2_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sPWMConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 3 */
sPWMConfig.Pulse = PULSE3_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sPWMConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the Break feature & Dead time */
sBreakConfig.BreakState = TIM_BREAK_ENABLE;
sBreakConfig.DeadTime = 11;
sBreakConfig.OffStateRunMode = TIM_OSSR_ENABLE;
sBreakConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
sBreakConfig.LockLevel = TIM_LOCKLEVEL_1;
sBreakConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;
if(HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &sBreakConfig) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Start PWM signals generation #######################################*/
/* Start channel 1 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 1N */
if(HAL_TIMEx_PWMN_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 2 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 2N */
if(HAL_TIMEx_PWMN_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 3 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Start channel 3N */
if(HAL_TIMEx_PWMN_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* Infinite loop */
while (1)
{
}
}
示例7: pwmout_write
void pwmout_write(pwmout_t* obj, float value) {
TIM_OC_InitTypeDef sConfig;
int channel = 0;
int complementary_channel = 0;
TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
if (value < (float)0.0) {
value = 0.0;
} else if (value > (float)1.0) {
value = 1.0;
}
obj->pulse = (uint32_t)((float)obj->period * value);
// Configure channels
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.Pulse = obj->pulse;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
switch (obj->pin) {
// Channels 1
case PA_0:
case PA_5:
case PA_6:
case PA_8:
case PA_15:
case PB_4:
case PB_6:
case PC_6:
channel = TIM_CHANNEL_1;
break;
// Channels 1N
case PA_7:
case PB_13:
channel = TIM_CHANNEL_1;
complementary_channel = 1;
break;
// Channels 2
case PA_1:
case PA_9:
case PB_3:
case PB_5:
case PB_7:
case PC_7:
channel = TIM_CHANNEL_2;
break;
// Channels 2N
case PB_0:
case PB_14:
channel = TIM_CHANNEL_2;
complementary_channel = 1;
break;
// Channels 3
case PA_2:
case PA_10:
case PB_8:
case PB_10:
case PC_8:
channel = TIM_CHANNEL_3;
break;
// Channels 3N
case PB_1:
case PB_15:
channel = TIM_CHANNEL_3;
complementary_channel = 1;
break;
// Channels 4
case PA_3:
case PA_11:
case PB_9:
case PC_9:
channel = TIM_CHANNEL_4;
break;
default:
return;
}
HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
if (complementary_channel) {
HAL_TIMEx_PWMN_Start(&TimHandle, channel);
} else {
HAL_TIM_PWM_Start(&TimHandle, channel);
}
}
示例8: main
//.........这里部分代码省略.........
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR + 1)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR + 1)* 100 = 12.5%
Note:
SystemCoreClock variable holds HCLK frequency and is defined in SystemClock_Config().
Each time the core clock (HCLK) changes, user had to update SystemCoreClock
variable value. Otherwise, any configuration based on this variable will be incorrect.
This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetSysClockFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
----------------------------------------------------------------------- */
/* Initialize TIMx peripheral as follow:
+ Prescaler = (SystemCoreClock/2)/21000000
+ Period = 665
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Instance = TIMx;
TimHandle.Init.Prescaler = uhPrescalerValue;
TimHandle.Init.Period = PERIOD_VALUE;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
/* Set the pulse value for channel 1 */
sConfig.Pulse = PULSE1_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 2 */
sConfig.Pulse = PULSE2_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 3 */
sConfig.Pulse = PULSE3_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 4 */
sConfig.Pulse = PULSE4_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Start PWM signals generation #######################################*/
/* Start channel 1 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 2 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 3 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
/* Start channel 4 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
while (1)
{
}
}
示例9: MX_TIM_Init
void MX_TIM_Init(void) {
__HAL_RCC_TIM1_CLK_ENABLE();
__HAL_RCC_TIM8_CLK_ENABLE();
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
TIM_SlaveConfigTypeDef sTimConfig;
htim_right.Instance = RIGHT_TIM;
htim_right.Init.Prescaler = 0;
htim_right.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
htim_right.Init.Period = 64000000 / 2 / PWM_FREQ;
htim_right.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim_right.Init.RepetitionCounter = 0;
htim_right.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_PWM_Init(&htim_right);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim_right, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET;
HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_3);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = DEAD_TIME;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
HAL_TIMEx_ConfigBreakDeadTime(&htim_right, &sBreakDeadTimeConfig);
htim_left.Instance = LEFT_TIM;
htim_left.Init.Prescaler = 0;
htim_left.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
htim_left.Init.Period = 64000000 / 2 / PWM_FREQ;
htim_left.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim_left.Init.RepetitionCounter = 0;
htim_left.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_PWM_Init(&htim_left);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim_left, &sMasterConfig);
sTimConfig.InputTrigger = TIM_TS_ITR0;
sTimConfig.SlaveMode = TIM_SLAVEMODE_GATED;
HAL_TIM_SlaveConfigSynchronization(&htim_left, &sTimConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET;
HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_3);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = DEAD_TIME;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
HAL_TIMEx_ConfigBreakDeadTime(&htim_left, &sBreakDeadTimeConfig);
LEFT_TIM->BDTR &= ~TIM_BDTR_MOE;
RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE;
HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_3);
HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_2);
HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_3);
HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_2);
HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_3);
htim_left.Instance->RCR = 1;
__HAL_TIM_ENABLE(&htim_right);
}
示例10: main
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 4
- Global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure the system clock to 180 MHz */
SystemClock_Config();
/* Configure LED3 */
BSP_LED_Init(LED3);
/*##-1- Configure the TIM peripheral #######################################*/
/* -----------------------------------------------------------------------
TIM1 Configuration: generate 1 PWM signal using the DMA burst mode:
TIM1 input clock (TIM1CLK) is set to 2 * APB2 clock (PCLK2),
since APB2 prescaler is different from 1.
TIM1CLK = 2 * PCLK2
PCLK2 = HCLK / 2
=> TIM1CLK = 2 * (HCLK / 2) = HCLK = SystemCoreClock
To get TIM1 counter clock at 20 MHz, the prescaler is computed as follows:
Prescaler = (TIM1CLK / TIM1 counter clock) - 1
Prescaler = (SystemCoreClock /20 MHz) - 1
The TIM1 Frequency = TIM1 counter clock/(ARR + 1)
= 20 MHz / 4096 = 4.88 KHz
TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR)* 100 = 33.33%
Note:
SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
Each time the core clock (HCLK) changes, user had to update SystemCoreClock
variable value. Otherwise, any configuration based on this variable will be incorrect.
This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetSysClockFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
----------------------------------------------------------------------- */
TimHandle.Instance = TIMx;
TimHandle.Init.Period = 0xFFFF;
TimHandle.Init.RepetitionCounter = 0;
TimHandle.Init.Prescaler = (uint16_t) ((SystemCoreClock / 20000000) - 1);
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channel 3 ########################################*/
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.Pulse = 0xFFF;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Start PWM signal generation in DMA mode ############################*/
if( HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* Starting PWM generation Error */
Error_Handler();
}
/*##-4- Start DMA Burst transfer ###########################################*/
HAL_TIM_DMABurst_WriteStart(&TimHandle, TIM_DMABASE_ARR, TIM_DMA_UPDATE,
(uint32_t*)aSRC_Buffer, TIM_DMABURSTLENGTH_3TRANSFERS);
/* Infinite loop */
while (1)
{
}
}
示例11: xMotorStart
Status_t xMotorStart(TIM_HandleTypeDef* pxTIMHandle, MotorChannel_t xChannel)
{
/*##- Start PWM signals generation #######################################*/
switch(xChannel)
{
case MOTOR_CHANNEL_1:
/* Start channel 1 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
break;
case MOTOR_CHANNEL_2:
/* Start channel 2 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
break;
case MOTOR_CHANNEL_3:
/* Start channel 3 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
break;
case MOTOR_CHANNEL_4:
/* Start channel 4 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_4) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
break;
case MOTOR_CHANNEL_ALL:
/* Start channel 1 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
/* Start channel 2 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
/* Start channel 3 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
/* Start channel 4 */
if(HAL_TIM_PWM_Start(pxTIMHandle, TIM_CHANNEL_4) != HAL_OK)
{
/* PWM Generation Error */
return STATUS_ERROR;
}
break;
default:
return STATUS_ERROR;
}
/* Return OK */
return STATUS_OK;
}
示例12: main
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* This sample code shows how to use STM32L0xx TIM HAL API to generate 4 PWM
signals */
/* STM32L0xx HAL library initialization:
- Configure the Flash prefetch, Flash preread and Buffer caches
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Low Level Initialization
*/
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Compute the prescaler value to have TIM2 counter clock equal to 16 MHz */
uwPrescalerValue = (SystemCoreClock / 16000000) - 1;
/*##-1- Configure the TIM peripheral #######################################*/
/* Initialize TIMx peripheral as follow:
+ Prescaler = (SystemCoreClock)/16000000
+ Period = 1600 (to have an output frequency equal to 10 KHz)
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Instance = TIM2;
TimHandle.Init.Prescaler = uwPrescalerValue;
TimHandle.Init.Period = PERIOD_VALUE;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
ErrorHandler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
/* Set the pulse value for channel 1 */
sConfig.Pulse = PULSE1_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
ErrorHandler();
}
/* Set the pulse value for channel 2 */
sConfig.Pulse = PULSE2_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
ErrorHandler();
}
/* Set the pulse value for channel 3 */
sConfig.Pulse = PULSE3_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
ErrorHandler();
}
/* Set the pulse value for channel 4 */
sConfig.Pulse = PULSE4_VALUE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
{
/* Configuration Error */
ErrorHandler();
}
/*##-3- Start PWM signals generation #######################################*/
/* Start channel 1 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* Starting Error */
ErrorHandler();
}
/* Start channel 2 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* Starting Error */
ErrorHandler();
}
/* Start channel 3 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
//.........这里部分代码省略.........
示例13: BSP_Init
void BSP_Init(void) {
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__PWR_CLK_ENABLE()
;
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
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;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | 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;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
__GPIOB_CLK_ENABLE()
;
GPIO_InitTypeDef GPIO_Init;
GPIO_Init.Mode = GPIO_MODE_AF_PP;
GPIO_Init.Pull = GPIO_NOPULL;
GPIO_Init.Speed = GPIO_SPEED_FAST;
GPIO_Init.Alternate = GPIO_AF2_TIM3;
GPIO_Init.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4;
HAL_GPIO_Init(LEDS_PORT, &GPIO_Init);
__TIM3_CLK_ENABLE()
;
TIM_MasterConfigTypeDef TIM_MasterConfig;
TIM_OC_InitTypeDef TIM_OC_Init;
TIM3_Handle.Instance = TIM3;
TIM3_Handle.Init.Prescaler = 84 - 1;
TIM3_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM3_Handle.Init.Period = 1500;
TIM3_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
HAL_TIM_PWM_Init(&TIM3_Handle);
TIM_MasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
TIM_MasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&TIM3_Handle, &TIM_MasterConfig);
TIM_OC_Init.OCMode = TIM_OCMODE_PWM2;
TIM_OC_Init.Pulse = 0;
TIM_OC_Init.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OC_Init.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&TIM3_Handle, &TIM_OC_Init, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&TIM3_Handle, &TIM_OC_Init, TIM_CHANNEL_3);
HAL_TIM_PWM_ConfigChannel(&TIM3_Handle, &TIM_OC_Init, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&TIM3_Handle, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&TIM3_Handle, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&TIM3_Handle, TIM_CHANNEL_4);
BSP_ADC_Init();
}
示例14: main
//.........这里部分代码省略.........
/* Compute the value to be set in ARR regiter to generate signal frequency at 8.78 Khz */
TimerPeriod = (SystemCoreClock / 17570 ) - 1;
/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
/* Compute CCR2 value to generate a duty cycle at 37.5% for channel 2 */
Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
/* Compute CCR3 value to generate a duty cycle at 25% for channel 3 */
Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
/* Compute CCR5 value to generate a duty cycle at 6.22% for channel 5 (in PWM2)*/
Channel5Pulse = (uint16_t) (((uint32_t) 622 * (TimerPeriod - 1)) / 10000);
/* Initialize Timer TIM1 */
TimHandle.Instance = TIM1;
TimHandle.Init.Prescaler = 0;
TimHandle.Init.Period = TimerPeriod;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
TimHandle.Init.RepetitionCounter = 0;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Channels 1 configuration on TIM1 */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.Pulse = Channel1Pulse;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Channels 2 configuration on TIM1 */
sConfig.Pulse = Channel2Pulse;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Channels 3 configuration on TIM1 */
sConfig.Pulse = Channel3Pulse;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Channels 5 configuration on TIM1 */
sConfig.OCMode = TIM_OCMODE_PWM2;
sConfig.Pulse = Channel5Pulse;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_5) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Group channel 5 and channels 1, 2 and 3 ############################*/
if(HAL_TIMEx_GroupChannel5(&TimHandle, (TIM_GROUPCH5_OC1REFC |\
TIM_GROUPCH5_OC2REFC |\
TIM_GROUPCH5_OC3REFC)) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-4- Start PWM signals generation #######################################*/
/* Start TIM1 channel 1 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start TIM1 channel 2 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start TIM1 channel 3 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start TIM1 channel 5 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_5) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
while (1)
{
}
}
示例15: Motors_init
/**********************************************************
* @brief Motors_init : motor initialisation
* @param
* @retval None
**********************************************************/
void Motors_init(void)
{
GPIO_InitTypeDef GPIO_DIR_InitStruct;
GPIO_InitTypeDef GPIO_PWM_InitStruct;
/* GPIO Ports Clock Enable */
__GPIOC_CLK_ENABLE();
__GPIOD_CLK_ENABLE();
/* GPIOD Configuration: Direction */
GPIO_DIR_InitStruct.Pin = HBRIDGE_DIR1_PIN | HBRIDGE_DIR2_PIN; // Pin direction
GPIO_DIR_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_DIR_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_DIR_InitStruct);
/* GPIOE Configuration: sleep pin */
GPIO_DIR_InitStruct.Pin = HBRIDGE_SLEEP_PIN; // Pin direction
GPIO_DIR_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;// Alternative function
HAL_GPIO_Init(HBRIDGE_SLEEP_PORT, &GPIO_DIR_InitStruct);
/* GPIOC PWM Configuration: TIM3 CH3 (PC8) and TIM3 CH4 (PC9) */
GPIO_PWM_InitStruct.Pin = HBRIDGE_PWM1_PIN | HBRIDGE_PWM2_PIN ;
GPIO_PWM_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_PWM_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_PWM_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOC, &GPIO_PWM_InitStruct);
/* TIM3 Clock Enable */
__TIM3_CLK_ENABLE();
/* TIM3 is connected to APB1 bus : so 42MHz clock. But PLL double this frequency => 42*2 = 84 Mhz */
/* Timer_Frequency = (84Mhz) / (Prescaler +1 ) */
/* TIMER_Period = (Timer_Frequency) / (PWM_frequency) - 1 */
// Init TIMER3 for 20 Khz frequency (PWM motors)
TIMER_InitStruct.Instance = TIM3;
TIMER_InitStruct.Init.Prescaler = 0; // Timer_Frequency = 84 Mhz.
TIMER_InitStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
TIMER_InitStruct.Init.Period = 4199; // TIMER_Period = (84M)/(20k) - 1 = 4199
TIMER_InitStruct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&TIMER_InitStruct);
// Output Compare Configuration
TIMER_OC_InitStruct.OCMode = TIM_OCMODE_PWM1;
TIMER_OC_InitStruct.OCIdleState = TIM_OCIDLESTATE_SET;
TIMER_OC_InitStruct.Pulse = 0;// PWM 0 %
TIMER_OC_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH;
TIMER_OC_InitStruct.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&TIMER_InitStruct, &TIMER_OC_InitStruct, TIM_CHANNEL_3);
HAL_TIM_PWM_ConfigChannel(&TIMER_InitStruct, &TIMER_OC_InitStruct, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&TIMER_InitStruct,TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&TIMER_InitStruct,TIM_CHANNEL_4);
// Enable nsleep
HBRIDGE_SLEEP_PORT->BSRRL = HBRIDGE_SLEEP_PIN;
s_motorRight.speed = 0;
s_motorLeft.speed = 0;
s_motorRight.direction = MOTOR_FORWARD;
s_motorLeft.direction = MOTOR_FORWARD;
}