本文整理汇总了C++中HAL_TIM_PWM_Init函数的典型用法代码示例。如果您正苦于以下问题:C++ HAL_TIM_PWM_Init函数的具体用法?C++ HAL_TIM_PWM_Init怎么用?C++ HAL_TIM_PWM_Init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HAL_TIM_PWM_Init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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);
/* Compute the prescaler value to have TIM1 counter clock equal to 18 MHz */
uwPrescalerValue = (uint32_t) (SystemCoreClock / 18000000) - 1;
/*##-1- Configure the TIM peripheral #######################################*/
/* Initialize TIM peripheral as follow:
+ Prescaler = SystemCoreClock/18000000
+ Period = 1799 (to have an output frequency equal to 10 KHz)
+ ClockDivision = 0
+ Counter direction = Up
*/
/* Select the Timer instance */
TimHandle.Instance = TIM1;
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 */
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 */
//.........这里部分代码省略.........
示例2: SC_TIM4_Init
/* TIM4 init function */
void SC_TIM4_Init(SERVO_CONTROLLER_Frequency frequency)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim4.Instance = TIM4;
htim4.Init.Prescaler = CORE_FCLK / TIM_FCLK - 1;;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = (uint16_t)(TIM_FCLK / frequency); //should not exceed 0xFFFF
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim4);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim4);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3);
HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4);
}
示例3: MX_TIM2_Init
/* TIM2 init function */
void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim2.Instance = TIM2;
htim2.Init.Prescaler = 24;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 200;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim2);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim2);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);
HAL_TIM_MspPostInit(&htim2);
}
示例4: MX_TIM4_Init
/* TIM4 init function */
void MX_TIM4_Init(void)
{
TIM_OC_InitTypeDef sConfigOC;
TIM_MasterConfigTypeDef sMasterConfig;
htim4.Instance = TIM4;
htim4.Init.Prescaler = 1800;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 10;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&htim4);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 5;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig);
}
示例5: pwmout_period_us
void pwmout_period_us(pwmout_t* obj, int us)
{
TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
float dc = pwmout_read(obj);
__HAL_TIM_DISABLE(&TimHandle);
SystemCoreClockUpdate();
TimHandle.Init.Period = us - 1;
TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&TimHandle);
// Set duty cycle again
pwmout_write(obj, dc);
// Save for future use
obj->period = us;
__HAL_TIM_ENABLE(&TimHandle);
}
示例6: Time3Enable
void Time3Enable(unsigned int timvalue)
{
uwPrescalerValue = ((SystemCoreClock ) / 16000000) - 1;
TimHandle.Instance = TIM3;
TimHandle.Init.Prescaler = uwPrescalerValue;
TimHandle.Init.Period = 600;
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 3 */
sConfig.Pulse = timvalue;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Start channel 3 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
}
示例7: main
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/* STM32F3xx HAL library initialization:
- Configure the Flash prefetch
- 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.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();
/* Configure LED3 */
BSP_LED_Init(LED3);
/* Configure the system clock to have a system clock = 72 Mhz */
SystemClock_Config();
/* Compute the value of ARR regiter to generate signal frequency at 17.57 Khz */
uhTimerPeriod = (uint32_t) ((SystemCoreClock / 17570 ) - 1);
/* Compute CCR1 value to generate a duty cycle at 75% */
aCCValue_Buffer[0] = (uint32_t)(((uint32_t) 75 * (uhTimerPeriod - 1)) / 100);
/* Compute CCR2 value to generate a duty cycle at 50% */
aCCValue_Buffer[1] = (uint32_t)(((uint32_t) 50 * (uhTimerPeriod - 1)) / 100);
/* Compute CCR3 value to generate a duty cycle at 25% */
aCCValue_Buffer[2] = (uint32_t)(((uint32_t) 25 * (uhTimerPeriod - 1)) / 100);
/*##-1- Configure the TIM peripheral #######################################*/
/* ---------------------------------------------------------------------------
TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2), since APB2
prescaler is 1.
TIM1CLK = PCLK2
PCLK2 = HCLK
=> TIM1CLK = HCLK = SystemCoreClock
TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
SystemCoreClock is set to 72 MHz for STM32F3xx devices.
The objective is to configure TIM1 channel 2 to generate complementary PWM
signal with a frequency equal to 17.57 KHz:
- TIM1_Period = (SystemCoreClock / 17570) - 1
and a variable duty cycle that is changed by the DMA after a specific number of
Update DMA request.
The number of this repetitive requests is defined by the TIM1 Repetion counter,
each 4 Update Requests, the TIM1 Channel 2 Duty Cycle changes to the next new
value defined by the aCCValue_Buffer.
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 TIM1 peripheral as follows:
+ Period = TimerPeriod (To have an output frequency equal to 17.570 KHz)
+ Repetition Counter = 3
+ Prescaler = 0
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Instance = TIMx;
TimHandle.Init.Period = uhTimerPeriod;
TimHandle.Init.RepetitionCounter = 3;
TimHandle.Init.Prescaler = 0;
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 2 ########################################*/
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.Pulse = aCCValue_Buffer[0];
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Start PWM signal generation in DMA mode ############################*/
if(HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_2, aCCValue_Buffer, 3) != HAL_OK)
{
/* Starting PWM generation Error */
//.........这里部分代码省略.........
示例8: ultrasound
void ultrasound (uint8_t bridge){ //ez a fv egyszerre csak egy hidat hajt meg!
uint16_t i,old_i,temp_i,f;
uint32_t CH1,CH2,CHx,CHy,tim,tim_now;
//bridge1=0,bridge2=1-> hogy melyik hidat hajtsa meg
if(bridge){ //bridge1
CH1=(uint32_t)TIM_CHANNEL_1;
CH2=(uint32_t)TIM_CHANNEL_2;
CHx=(uint32_t)TIM_CHANNEL_3;
CHy=(uint32_t)TIM_CHANNEL_4;
}else{ //bridge2
CH1=(uint32_t)TIM_CHANNEL_3;
CH2=(uint32_t)TIM_CHANNEL_4;
CHx=(uint32_t)TIM_CHANNEL_1;
CHy=(uint32_t)TIM_CHANNEL_2;
}
SystemClock_Config_48MHz();
HAL_Delay(20);
MX_TIM3_Init();
HAL_TIM_PWM_Init(&htim3);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel( &htim3 , &sConfigOC , CH1);
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
HAL_TIM_PWM_ConfigChannel( &htim3 , &sConfigOC , CH2);
HAL_TIM_PWM_ConfigChannel( &htim3 , &sConfigOC , CHx);
HAL_TIM_PWM_ConfigChannel( &htim3 , &sConfigOC , CHy);
HAL_TIM_PWM_Start(&htim3, CH1);
HAL_TIM_PWM_Start(&htim3, CH2);
HAL_TIM_PWM_Start(&htim3, CHx);
HAL_TIM_PWM_Start(&htim3, CHy);
//csattanás csökkentés, 19khz kiadás eloszor
HAL_GPIO_WritePin( DCDC_EN_GPIO_Port , DCDC_EN_Pin , 0 );
// HAL_GPIO_WritePin( DCDC_PWR_GPIO_Port , DCDC_PWR_Pin , 0 ); //open drain kimenet,pch fet,
//*****************************************
/*for( i=900 ; i >= 620 ; i-=3){
__HAL_TIM_SetAutoreload(&htim3,i*2);
__HAL_TIM_SetCompare(&htim3,CH1, i);
__HAL_TIM_SetCompare(&htim3,CH2, i);
HAL_Delay(1);
}*/
/***********************************
HAL_Delay(100);
__HAL_TIM_SetAutoreload(&htim3,i*2);
__HAL_TIM_SetCompare(&htim3,CH1, i);
__HAL_TIM_SetCompare(&htim3,CH2, i);
HAL_Delay(1000);*/
HAL_GPIO_WritePin( DCDC_EN_GPIO_Port , DCDC_EN_Pin , 1 );
HAL_Delay(100);
//19KHz-620 // 27KHz-440
old_i=i;
for(uint8_t x=0; x < soundNum; x++){
// a következo hanghoz kis lépésenként jutunk, ígty nincs csattanás
srand(HAL_GetTick());
i=(rand()%210)+440;
//i=210+440;
/*
if( old_i < i){
// ha az új hang kissebb frekvenciájó -> nagyobb i
for( temp_i=old_i ; i >= temp_i; temp_i++){
__HAL_TIM_SetAutoreload(&htim3,temp_i*2);
__HAL_TIM_SetCompare(&htim3,CH1, temp_i);
__HAL_TIM_SetCompare(&htim3,CH2, temp_i);
HAL_Delay(1);
}
}else{
//ha az uj hang nagypbb frekvenciáju-> kisebb i
for( temp_i=old_i ; i <= temp_i; temp_i--){
__HAL_TIM_SetAutoreload(&htim3,temp_i*2);
__HAL_TIM_SetCompare(&htim3,CH1, temp_i);
__HAL_TIM_SetCompare(&htim3,CH2, temp_i);
HAL_Delay(1);
}
}
*/
__HAL_TIM_SetAutoreload(&htim3,i*2);
__HAL_TIM_SetCompare(&htim3,CH1, i);
__HAL_TIM_SetCompare(&htim3,CH2, i);
//.........这里部分代码省略.........
示例9: uartTester
void uartTester(){
//UART fogadás, teszteléshez ---------------------------------------------------------
uint8_t pData;
PutString("S");
HAL_UART_Receive(&huart1,&pData,1,100);
if(pData=='O'){
HAL_GPIO_TogglePin(Kimenet_GPIO_Port,Kimenet_Pin);
PutString("R");
HAL_UART_Receive(&huart1,&pData,1,1000);
while(pData != 'A')
HAL_UART_Receive(&huart1,&pData,1,1000);
HAL_GPIO_WritePin( DCDC_EN_GPIO_Port , DCDC_EN_Pin , 1 );
PutString("R");
HAL_UART_Receive(&huart1,&pData,8,1000);
while(pData != 'B')
HAL_UART_Receive(&huart1,&pData,8,1000);
soundNum=1;
sounDelay=10;
SystemClock_Config_48MHz();
HAL_Delay(2);
MX_TIM3_Init();
HAL_TIM_PWM_Init(&htim3);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel( &htim3 , &sConfigOC , TIM_CHANNEL_1);
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
HAL_TIM_PWM_ConfigChannel( &htim3 , &sConfigOC , TIM_CHANNEL_2);
HAL_GPIO_TogglePin(Kimenet_GPIO_Port,Kimenet_Pin);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
uint16_t i=630;
__HAL_TIM_SetAutoreload(&htim3,i*2);
__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1, i);
__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_2, i);
HAL_Delay(500);
PutString("R");
HAL_UART_Receive(&huart1,&pData,8,1000);
while(pData != 'C')
HAL_UART_Receive(&huart1,&pData,8,1000);
HAL_GPIO_WritePin( DCDC_EN_GPIO_Port , DCDC_EN_Pin , 0);
HAL_TIM_PWM_Stop( &htim3 , TIM_CHANNEL_1 );
HAL_TIM_PWM_Stop( &htim3 , TIM_CHANNEL_2 );
HAL_TIM_PWM_DeInit( &htim3 );
SystemClock_Config_8MHz();
HAL_ADC_MspDeInit(&hadc);
HAL_UART_MspDeInit(&huart1);
HAL_TIM_Base_DeInit(&htim3);
while(1){
HAL_GPIO_TogglePin(Kimenet_GPIO_Port,Kimenet_Pin);
HAL_Delay(200);
}
}
}
示例10: tracks_init
extern void tracks_init(){
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OC_InitTypeDef PWMConfig;
uint32_t PrescalerValue;
__BRD_D4_GPIO_CLK();
__BRD_D5_GPIO_CLK();
__BRD_D6_GPIO_CLK();
__BRD_D7_GPIO_CLK();
//Track direction pins
GPIO_InitStructure.Pin = BRD_D4_PIN; //Pin
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; //Output Mode
GPIO_InitStructure.Pull = GPIO_PULLDOWN; //Enable Pull up, down or no pull resister
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM; //Pin latency
HAL_GPIO_Init(BRD_D4_GPIO_PORT, &GPIO_InitStructure); //Initialise Pin
GPIO_InitStructure.Pin = BRD_D7_PIN; //Pin
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; //Input Mode
GPIO_InitStructure.Pull = GPIO_PULLDOWN; //Enable Pull up, down or no pull resister
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM; //Pin latency
HAL_GPIO_Init(BRD_D7_GPIO_PORT, &GPIO_InitStructure); //Initialise Pin
//Track ENABLE pins
__TIM2_CLK_ENABLE();
__TIM3_CLK_ENABLE();
/* Compute the prescaler value. SystemCoreClock = 168000000 - set for 50Khz clock */
PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 1000000) - 1;
//Track speed pins (PWM)
GPIO_InitStructure.Pin = BRD_D5_PIN; //Pin
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; //Set mode to be output alternate
GPIO_InitStructure.Pull = GPIO_NOPULL; //Enable Pull up, down or no pull resister
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM; //Pin latency
GPIO_InitStructure.Alternate = GPIO_AF2_TIM3; //Set alternate function to be timer 3
HAL_GPIO_Init(BRD_D5_GPIO_PORT, &GPIO_InitStructure); //Initialise Pin
/* Configure Timer settings */
TIM_Init5.Instance = TIM3; //Enable Timer 3
TIM_Init5.Init.Period = 10000; //Set for 200ms (5Hz) period
TIM_Init5.Init.Prescaler = PrescalerValue; //Set presale value
TIM_Init5.Init.ClockDivision = 0; //Set clock division
TIM_Init5.Init.RepetitionCounter = 0; // Set Reload Value
TIM_Init5.Init.CounterMode = TIM_COUNTERMODE_UP; //Set timer to count up.
/* PWM Mode configuration for Channel 2 - set pulse width*/
PWMConfig.OCMode = TIM_OCMODE_PWM1; //Set PWM MODE (1 or 2 - NOT CHANNEL)
PWMConfig.Pulse = 7500; //1ms pulse width to 10ms
PWMConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
PWMConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
PWMConfig.OCFastMode = TIM_OCFAST_DISABLE;
PWMConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
PWMConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
/* Enable PWM for Timer 3, channel 2 */
HAL_TIM_PWM_Init(&TIM_Init5);
HAL_TIM_PWM_ConfigChannel(&TIM_Init5, &PWMConfig, TIM_CHANNEL_1);
/* Start PWM */
HAL_TIM_PWM_Start(&TIM_Init5, TIM_CHANNEL_1);
GPIO_InitStructure.Pin = BRD_D6_PIN; //Pin
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; //Set mode to be output alternate
GPIO_InitStructure.Pull = GPIO_NOPULL; //Enable Pull up, down or no pull resister
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM; //Pin latency
GPIO_InitStructure.Alternate = GPIO_AF1_TIM2; //Set alternate function to be timer 3
HAL_GPIO_Init(BRD_D6_GPIO_PORT, &GPIO_InitStructure); //Initialise Pin
/* Configure Timer settings */
TIM_Init6.Instance = TIM2; //Enable Timer 3
TIM_Init6.Init.Period = 10000; //Set for 200ms (5Hz) period
TIM_Init6.Init.Prescaler = PrescalerValue; //Set presale value
TIM_Init6.Init.ClockDivision = 0; //Set clock division
TIM_Init6.Init.RepetitionCounter = 0; // Set Reload Value
TIM_Init6.Init.CounterMode = TIM_COUNTERMODE_UP; //Set timer to count up.
/* PWM Mode configuration for Channel 2 - set pulse width*/
PWMConfig.OCMode = TIM_OCMODE_PWM1; //Set PWM MODE (1 or 2 - NOT CHANNEL)
PWMConfig.Pulse = 7500; //1ms pulse width to 10ms
PWMConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
PWMConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
PWMConfig.OCFastMode = TIM_OCFAST_DISABLE;
PWMConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
PWMConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
/* Enable PWM for Timer 3, channel 2 */
HAL_TIM_PWM_Init(&TIM_Init6);
HAL_TIM_PWM_ConfigChannel(&TIM_Init6, &PWMConfig, TIM_CHANNEL_3);
/* Start PWM */
HAL_TIM_PWM_Start(&TIM_Init6, TIM_CHANNEL_3);
}
示例11: 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 168 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 24 MHz, the prescaler is computed as follows:
Prescaler = (TIM1CLK / TIM1 counter clock) - 1
Prescaler = (SystemCoreClock /24 MHz) - 1
The TIM1 period is 5.8 KHz: TIM1 Frequency = TIM1 counter clock/(ARR + 1)
= 24 MHz / 4096 = 5.85 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 / 24000000) - 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)
{
}
}
示例12: main
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* STM32F2xx 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 120 MHz */
SystemClock_Config();
/* Configure LED3 */
BSP_LED_Init(LED3);
/* Timers Configuration */
/* ---------------------------------------------------------------------------
TIM1 and Timers(TIM3 and TIM4) synchronisation in parallel mode.
1/TIM1 is configured as Master Timer:
- PWM Mode is used
- The TIM1 Update event is used as Trigger Output
2/TIM3 and TIM4 are slaves for TIM1,
- PWM Mode is used
- The ITR0(TIM1) is used as input trigger for both slaves
- Gated mode is used, so starts and stops of slaves counters
are controlled by the Master trigger output signal(update event).
In this example 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 = HCLK = SystemCoreClock
The TIM1 counter clock is equal to SystemCoreClock = 120 MHz.
The Master Timer TIM1 is running at:
TIM1 frequency = TIM1 counter clock / (TIM1_Period + 1) = 469 KHz
TIM1_Period = (TIM1 counter clock / TIM1 frequency) - 1 = 255
and the duty cycle is equal to: TIM1_CCR1/(TIM1_ARR + 1) = 50%
The TIM3 is running at:
(TIM1 frequency)/ ((TIM3 period +1)* (Repetition_Counter+1)) = 31.25 KHz and
a duty cycle equal to TIM3_CCR1/(TIM3_ARR + 1) = 33.3%
The TIM4 is running at:
(TIM1 frequency)/ ((TIM4 period +1)* (Repetition_Counter+1)) = 46.9 KHz and
a duty cycle equal to TIM4_CCR1/(TIM4_ARR + 1) = 50%
Note:
SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f2xx.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
--------------------------------------------------------------------------- */
/* Set Timers instance */
TimMasterHandle.Instance = TIM1;
TimSlave1Handle.Instance = TIM3;
TimSlave2Handle.Instance = TIM4;
/*====================== Master configuration : TIM1 =======================*/
/* Initialize TIM1 peripheral in PWM mode*/
TimMasterHandle.Init.Period = 255;
TimMasterHandle.Init.Prescaler = 0;
TimMasterHandle.Init.ClockDivision = 0;
TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimMasterHandle.Init.RepetitionCounter = 4;
if(HAL_TIM_PWM_Init(&TimMasterHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* Configure the PWM_channel_1 */
sOCConfig.OCMode = TIM_OCMODE_PWM1;
sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sOCConfig.Pulse = 127;
if(HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Configure TIM1 as master & use the update event as Trigger Output (TRGO) */
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
if( HAL_TIMEx_MasterConfigSynchronization(&TimMasterHandle,&sMasterConfig) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
//.........这里部分代码省略.........
示例13: TIM_Config
/**
* @brief TIM3, TIM4, TIM8 configuration
* @param None
* @retval None
*/
static void TIM_Config(void) {
TIM_MasterConfigTypeDef sMasterConfig;
/*########## TIM3 peripheral - PING (1 Hz) ##########*/
TimHandle3.Instance = TIM3;
TimHandle3.Init.Period = 10000;
TimHandle3.Init.Prescaler = (uint32_t)(((SystemCoreClock / 2) / 10000) - 1); //10kHz
// T = 1/f = 1/10k = 0,0001 ; time = Period * T = 1s
TimHandle3.Init.ClockDivision = 0;
TimHandle3.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_OC_Init(&TimHandle3) != HAL_OK) {
Error_Handler();
}
// Configure the Output Compare channel:
sConfig.OCMode = TIM_OCMODE_TOGGLE;
sConfig.Pulse = 100;
sConfig.OCPolarity = TIM_OCPOLARITY_LOW;
if(HAL_TIM_OC_ConfigChannel(&TimHandle3, &sConfig, TIM_CHANNEL_1) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_OC_Start_IT(&TimHandle3, TIM_CHANNEL_1) != HAL_OK) {
Error_Handler();
}
/*########## TIM4 peripheral - UDP (10 Hz) ##########*/
TimHandle4.Instance = TIM4;
TimHandle4.Init.Period = 10000;
TimHandle4.Init.Prescaler = (uint32_t)(((SystemCoreClock / 2) / 100000) - 1); //100kHz
// T = 1/f = 1/100k = 0,00001 ; time = Period * T = 0,1s
TimHandle4.Init.ClockDivision = 0;
TimHandle4.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_OC_Init(&TimHandle4) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_OC_ConfigChannel(&TimHandle4, &sConfig, TIM_CHANNEL_2) != HAL_OK) {
Error_Handler();
}
if(HAL_TIM_OC_Start_IT(&TimHandle4, TIM_CHANNEL_2) != HAL_OK) {
Error_Handler();
}
/*########## TIM8 peripheral - ADC ##########*/
TimHandle8.Instance = TIM8;
TimHandle8.Init.Period = 0x3C;
TimHandle8.Init.Prescaler = 0;
TimHandle8.Init.ClockDivision = 0;
TimHandle8.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle8.Init.RepetitionCounter = 0x0;
if(HAL_TIM_Base_Init(&TimHandle8) != HAL_OK) {
Error_Handler();
}
// TIM8 TRGO selection:
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if(HAL_TIMEx_MasterConfigSynchronization(&TimHandle8, &sMasterConfig) != HAL_OK) {
Error_Handler(); //TIM8 TRGO selection Error
}
if(HAL_TIM_Base_Start(&TimHandle8) != HAL_OK) {
Error_Handler(); //Counter Enable Error
}
/*########## TIM1 peripheral - PWM ##########*/
TimHandle1.Instance = TIM1;
TimHandle1.Init.Period = uhTimerPeriod;
TimHandle1.Init.RepetitionCounter = 3;
TimHandle1.Init.Prescaler = 0;
TimHandle1.Init.ClockDivision = 0;
TimHandle1.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle1) != HAL_OK) {
Error_Handler();
}
// Configure the PWM channel 3:
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.Pulse = aCCValue_Buffer;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle1, &sConfig, TIM_CHANNEL_3) != HAL_OK) {
Error_Handler();
}
// Start PWM signal generation in DMA mode:
if(HAL_TIM_PWM_Start_DMA(&TimHandle1, TIM_CHANNEL_3, &aCCValue_Buffer, 1) != HAL_OK) {
Error_Handler();
}
}
示例14: ws2811LedStripHardwareInit
void ws2811LedStripHardwareInit(void)
{
TimHandle.Instance = WS2811_TIMER;
TimHandle.Init.Prescaler = 1;
TimHandle.Init.Period = 135; // 800kHz
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
return;
}
static DMA_HandleTypeDef hdma_tim;
ws2811IO = IOGetByTag(IO_TAG(WS2811_PIN));
/* GPIOA Configuration: TIM5 Channel 1 as alternate function push-pull */
IOInit(ws2811IO, OWNER_LED_STRIP, RESOURCE_OUTPUT, 0);
IOConfigGPIOAF(ws2811IO, IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLUP), WS2811_TIMER_GPIO_AF);
__DMA1_CLK_ENABLE();
/* Set the parameters to be configured */
hdma_tim.Init.Channel = WS2811_DMA_CHANNEL;
hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim.Init.MemInc = DMA_MINC_ENABLE;
hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD ;
hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_WORD ;
hdma_tim.Init.Mode = DMA_NORMAL;
hdma_tim.Init.Priority = DMA_PRIORITY_HIGH;
hdma_tim.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tim.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tim.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_tim.Init.PeriphBurst = DMA_PBURST_SINGLE;
/* Set hdma_tim instance */
hdma_tim.Instance = WS2811_DMA_STREAM;
uint32_t channelAddress = 0;
switch (WS2811_TIMER_CHANNEL) {
case TIM_CHANNEL_1:
timDMASource = TIM_DMA_ID_CC1;
channelAddress = (uint32_t)(&WS2811_TIMER->CCR1);
break;
case TIM_CHANNEL_2:
timDMASource = TIM_DMA_ID_CC2;
channelAddress = (uint32_t)(&WS2811_TIMER->CCR2);
break;
case TIM_CHANNEL_3:
timDMASource = TIM_DMA_ID_CC3;
channelAddress = (uint32_t)(&WS2811_TIMER->CCR3);
break;
case TIM_CHANNEL_4:
timDMASource = TIM_DMA_ID_CC4;
channelAddress = (uint32_t)(&WS2811_TIMER->CCR4);
break;
}
/* Link hdma_tim to hdma[x] (channelx) */
__HAL_LINKDMA(&TimHandle, hdma[timDMASource], hdma_tim);
dmaSetHandler(WS2811_DMA_HANDLER_IDENTIFER, WS2811_DMA_IRQHandler, NVIC_PRIO_WS2811_DMA, timDMASource);
/* Initialize TIMx DMA handle */
if(HAL_DMA_Init(TimHandle.hdma[timDMASource]) != HAL_OK)
{
/* Initialization Error */
return;
}
TIM_OC_InitTypeDef TIM_OCInitStructure;
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.OCMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.Pulse = 0;
TIM_OCInitStructure.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_RESET;
TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
TIM_OCInitStructure.OCFastMode = TIM_OCFAST_DISABLE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &TIM_OCInitStructure, WS2811_TIMER_CHANNEL) != HAL_OK)
{
/* Configuration Error */
return;
}
const hsvColor_t hsv_white = { 0, 255, 255};
ws2811Initialised = true;
setStripColor(&hsv_white);
}
示例15: 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);
/* Compute the Timer period to generate a signal frequency at 17.57 Khz */
uwPeriod = (SystemCoreClock / 17570 ) - 1;
/* Compute Pulse1 value to generate a duty cycle at 50% for channel 1 and 1N */
uwPulse1 = (5 * (uwPeriod - 1)) / 10;
/* Compute Pulse2 value to generate a duty cycle at 37.5% for channel 2 and 2N */
uwPulse2 = (375 * (uwPeriod - 1)) / 1000;
/* Compute Pulse3 value to generate a duty cycle at 25% for channel 3 and 3N */
uwPulse3 = (25 * (uwPeriod - 1)) / 100;
/* Compute Pulse4 value to generate a duty cycle at 12.5% for channel 4 */
uwPulse4 = (125 * (uwPeriod- 1)) / 1000;
/*##-1- Configure the TIM peripheral #######################################*/
/* Initialize TIMx peripheral as follow:
+ Prescaler = 0
+ Period = uwPeriod (to have an output frequency equal to 17.57 KHz)
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Instance = TIM1;
TimHandle.Init.Period = uwPeriod;
TimHandle.Init.Prescaler = 0;
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_PWM2;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCPolarity = TIM_OCPOLARITY_LOW;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCIdleState = TIM_OCIDLESTATE_SET;
sConfig.OCNIdleState= TIM_OCNIDLESTATE_RESET;
/* Set the pulse value for channel 1 */
sConfig.Pulse = uwPulse1;
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 = uwPulse2;
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 = uwPulse3;
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 = uwPulse4;
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)
{
/* Starting Error */
//.........这里部分代码省略.........