本文整理汇总了C++中TIM_OC3PreloadConfig函数的典型用法代码示例。如果您正苦于以下问题:C++ TIM_OC3PreloadConfig函数的具体用法?C++ TIM_OC3PreloadConfig怎么用?C++ TIM_OC3PreloadConfig使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TIM_OC3PreloadConfig函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/
/* TIM Configuration */
TIM_Config();
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
/* ---------------------------------------------------------------------------
TIM3 Configuration: Output Compare Active Mode:
In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1)
TIM3CLK = PCLK1
PCLK1 = HCLK
=> TIM3CLK = HCLK = SystemCoreClock
To get TIM3 counter clock at 1 KHz, the prescaler is computed as follows:
Prescaler = (TIM3CLK / TIM3 counter clock) - 1
Prescaler = (SystemCoreClock /1 KHz) - 1
Generate 4 signals with 4 different delays:
TIM3_CH1 delay = CCR1_Val/TIM3 counter clock = 1000 ms
TIM3_CH2 delay = CCR2_Val/TIM3 counter clock = 500 ms
TIM3_CH3 delay = CCR3_Val/TIM3 counter clock = 250 ms
TIM3_CH4 delay = CCR4_Val/TIM3 counter clock = 125 ms
Note:
SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
function to update SystemCoreClock variable value. Otherwise, any configuration
based on this variable will be incorrect.
--------------------------------------------------------------------------- */
/*Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 1000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Output Compare Active Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM3, DISABLE);
/* Output Compare Active Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
TIM_GenerateEvent(TIM3, TIM_EventSource_Update);
/* Turn on LED1 */
STM_EVAL_LEDOn(LED1);
/* Infinite loop */
while (1)
{
}
}
示例2: main
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* GPIO Configuration */
GPIO_Configuration();
/* ---------------------------------------------------------------
TIM2 Configuration: Output Compare Inactive Mode:
TIM2CLK = 36 MHz, Prescaler = 35999, TIM2 counter clock = 1 KHz
TIM2_CH1 delay = CCR1_Val/TIM2 counter clock = 1000 ms
TIM2_CH2 delay = CCR2_Val/TIM2 counter clock = 500 ms
TIM2_CH3 delay = CCR3_Val/TIM2 counter clock = 250 ms
TIM2_CH4 delay = CCR4_Val/TIM2 counter clock = 125 ms
--------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 35999;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Output Compare Active Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
/* Set PC.06, PC.07, PC.08 and PC.09 pins */
GPIO_SetBits(GPIOC, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
while (1)
{}
}
示例3: main
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* GPIO Configuration */
GPIO_Configuration();
/* ---------------------------------------------------------------------------
TIM3 Configuration: Output Compare Toggle Mode:
TIM3CLK = SystemCoreClock / 2,
The objective is to get TIM3 counter clock at 12 MHz:
- Prescaler = (TIM3CLK / TIM3 counter clock) - 1
CC1 update rate = TIM3 counter clock / CCR1_Val = 366.2 Hz
CC2 update rate = TIM3 counter clock / CCR2_Val = 732.4 Hz
CC3 update rate = TIM3 counter clock / CCR3_Val = 1464.8 Hz
CC4 update rate = TIM3 counter clock / CCR4_Val = 2929.6 Hz
----------------------------------------------------------------------------*/
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
while (1)
{}
}
示例4: main
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* GPIO Configuration */
GPIO_Configuration();
/* ---------------------------------------------------------------
TIM2 Configuration:
TIM2CLK = SystemCoreClock / 2,
The objective is to get TIM2 counter clock at 1 KHz:
- Prescaler = (TIM2CLK / TIM2 counter clock) - 1
And generate 4 signals with 4 different delays:
TIM2_CH1 delay = CCR1_Val/TIM2 counter clock = 1000 ms
TIM2_CH2 delay = CCR2_Val/TIM2 counter clock = 500 ms
TIM2_CH3 delay = CCR3_Val/TIM2 counter clock = 250 ms
TIM2_CH4 delay = CCR4_Val/TIM2 counter clock = 125 ms
* SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
and Connectivity line devices and to 24 MHz for Low-Density Value line and
Medium-Density Value line devices
--------------------------------------------------------------- */
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 2000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Output Compare Active Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* Output Compare Active Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
/* Set PC.06, PC.07, PC.08 and PC.09 pins */
GPIO_SetBits(GPIOC, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
while (1)
{}
}
示例5: Timer_OC_Config
static void Timer_OC_Config(TIM_TypeDef* TIMn,uint8_t Channel,uint32_t SET_PrescalerValue,uint32_t Period)
{
uint16_t CCR_Val = 0;
uint16_t PrescalerValue = 0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
if(TIMn == TIM2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
else if(TIMn == TIM3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
else if(TIMn == TIM4)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
else if(TIMn == TIM5)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
else if(TIMn == TIM12)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12,ENABLE);
else if(TIMn == TIM13)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE);
else
{
if(TIMn == TIM14)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
}
// TIM_DeInit(TIMn);
if(CCR_Val < Period)
{
/* Compute the prescaler value */
PrescalerValue = (uint16_t) ((60000000) / SET_PrescalerValue) - 1; //config the input frequency 20K
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period =Period;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIMn, &TIM_TimeBaseStructure);
//
// TIM_ClearFlag(TIMn,TIM_FLAG_Update);
// TIM_ITConfig(TIMn,TIM_IT_Update,ENABLE);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR_Val; //CCR的值,设置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
if(Channel == 1)
{
TIM_OC1Init(TIMn, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIMn, TIM_OCPreload_Enable);
}
else if(Channel == 2)
{
TIM_OC2Init(TIMn, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIMn, TIM_OCPreload_Enable);
}
else if(Channel == 3)
{
TIM_OC3Init(TIMn, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIMn, TIM_OCPreload_Enable);
}
else
{
if(Channel == 4)
{
TIM_OC4Init(TIMn, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIMn, TIM_OCPreload_Enable);
}
}
TIM_ARRPreloadConfig(TIMn, ENABLE); //自动重装载寄存器
TIM_Cmd(TIMn, ENABLE);
}
}
示例6: pwm_init
/**
* Initialize the three phase (6outputs) PWM peripheral and internal state.
*/
void pwm_init(void)
{
NVIC_InitTypeDef nvic;
GPIO_InitTypeDef gpio;
TIM_TimeBaseInitTypeDef tim_base;
TIM_OCInitTypeDef tim_oc;
TIM_BDTRInitTypeDef tim_bdtr;
(void)gpc_setup_reg(GPROT_PWM_OFFSET_REG_ADDR, &pwm_offset);
/* Enable clock for TIM1 subsystem */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |
RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB, ENABLE);
/* Enable TIM1 interrupt */
nvic.NVIC_IRQChannel = TIM1_TRG_COM_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
/* Enable TIM1 interrupt */
nvic.NVIC_IRQChannel = TIM1_CC_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
/* GPIOA: TIM1 channel 1, 2 and 3 as alternate function
push-pull */
gpio.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpio);
/* GPIOB: TIM1 channel 1N, 2N and 3N as alternate function
* push-pull
*/
gpio.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOB, &gpio);
/* Time base configuration */
tim_base.TIM_Period = PWM__BASE_CLOCK / PWM__FREQUENCY;
tim_base.TIM_Prescaler = 0;
tim_base.TIM_ClockDivision = 0;
tim_base.TIM_CounterMode = TIM_CounterMode_Up;
tim_base.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &tim_base);
/* TIM1 channel 1, 2 and 3 settings */
tim_oc.TIM_OCMode = TIM_OCMode_Timing;
tim_oc.TIM_OutputState = TIM_OutputState_Enable;
tim_oc.TIM_OutputNState = TIM_OutputNState_Enable;
tim_oc.TIM_Pulse = pwm_val;
tim_oc.TIM_OCPolarity = TIM_OCPolarity_High;
tim_oc.TIM_OCNPolarity = TIM_OCNPolarity_High;
tim_oc.TIM_OCIdleState = TIM_OCIdleState_Set;
tim_oc.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OC1Init(TIM1, &tim_oc);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2Init(TIM1, &tim_oc);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3Init(TIM1, &tim_oc);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM1 configure channel 4 as adc trigger source */
tim_oc.TIM_OCMode = TIM_OCMode_PWM2;
tim_oc.TIM_Pulse = pwm_offset;
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4Init(TIM1, &tim_oc);
/* Automatic Output enable, break, dead time and lock configuration */
tim_bdtr.TIM_OSSRState = TIM_OSSRState_Enable;
tim_bdtr.TIM_OSSIState = TIM_OSSIState_Enable;
tim_bdtr.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
tim_bdtr.TIM_DeadTime = 10;
tim_bdtr.TIM_Break = TIM_Break_Disable;
tim_bdtr.TIM_BreakPolarity = TIM_BreakPolarity_High;
tim_bdtr.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM1, &tim_bdtr);
TIM_CCPreloadControl(TIM1, ENABLE);
/* Enable COM and CC interrupt */
TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);
//TIM_ITConfig(TIM1, TIM_IT_COM | TIM_IT_CC4, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
/* Main output enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
//.........这里部分代码省略.........
示例7: platform_pwm_init
OSStatus platform_pwm_init( const platform_pwm_t* pwm, uint32_t frequency, float duty_cycle )
{
TIM_TimeBaseInitTypeDef tim_time_base_structure;
TIM_OCInitTypeDef tim_oc_init_structure;
RCC_ClocksTypeDef rcc_clock_frequencies;
uint16_t period = 0;
float adjusted_duty_cycle = ( ( duty_cycle > 100.0f ) ? 100.0f : duty_cycle );
OSStatus err = kNoErr;
require_action_quiet( pwm != NULL, exit, err = kParamErr);
platform_mcu_powersave_disable();
RCC_GetClocksFreq( &rcc_clock_frequencies );
if ( pwm->tim == TIM1 || pwm->tim == TIM8 || pwm->tim == TIM9 || pwm->tim == TIM10 || pwm->tim == TIM11 )
{
RCC_APB2PeriphClockCmd( pwm->tim_peripheral_clock, ENABLE );
if( rcc_clock_frequencies.PCLK2_Frequency == rcc_clock_frequencies.HCLK_Frequency )
period = (uint16_t)( rcc_clock_frequencies.PCLK2_Frequency / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */
else
period = (uint16_t)( rcc_clock_frequencies.PCLK2_Frequency * 2 / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */
}
else
{
RCC_APB1PeriphClockCmd( pwm->tim_peripheral_clock, ENABLE );
if( rcc_clock_frequencies.PCLK1_Frequency == rcc_clock_frequencies.HCLK_Frequency )
period = (uint16_t)( rcc_clock_frequencies.PCLK1_Frequency / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */
else
period = (uint16_t)( rcc_clock_frequencies.PCLK1_Frequency * 2 / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */
}
/* Set alternate function */
platform_gpio_set_alternate_function( pwm->pin->port, pwm->pin->pin_number, GPIO_OType_PP, GPIO_PuPd_UP, pwm->gpio_af );
/* Time base configuration */
tim_time_base_structure.TIM_Period = (uint32_t) period;
tim_time_base_structure.TIM_Prescaler = (uint16_t) 19; /* Divide clock by 19 + 1 to enable a count of high cycle + low cycle = 1 PWM cycle */
tim_time_base_structure.TIM_ClockDivision = 0;
tim_time_base_structure.TIM_CounterMode = TIM_CounterMode_Up;
tim_time_base_structure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit( pwm->tim, &tim_time_base_structure );
/* PWM1 Mode configuration */
tim_oc_init_structure.TIM_OCMode = TIM_OCMode_PWM1;
tim_oc_init_structure.TIM_OutputState = TIM_OutputState_Enable;
tim_oc_init_structure.TIM_OutputNState = TIM_OutputNState_Enable;
tim_oc_init_structure.TIM_Pulse = (uint16_t) ( adjusted_duty_cycle * (float) period / 100.0f );
tim_oc_init_structure.TIM_OCPolarity = TIM_OCPolarity_High;
tim_oc_init_structure.TIM_OCNPolarity = TIM_OCNPolarity_High;
tim_oc_init_structure.TIM_OCIdleState = TIM_OCIdleState_Reset;
tim_oc_init_structure.TIM_OCNIdleState = TIM_OCIdleState_Set;
switch ( pwm->channel )
{
case 1:
{
TIM_OC1Init( pwm->tim, &tim_oc_init_structure );
TIM_OC1PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
break;
}
case 2:
{
TIM_OC2Init( pwm->tim, &tim_oc_init_structure );
TIM_OC2PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
break;
}
case 3:
{
TIM_OC3Init( pwm->tim, &tim_oc_init_structure );
TIM_OC3PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
break;
}
case 4:
{
TIM_OC4Init( pwm->tim, &tim_oc_init_structure );
TIM_OC4PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
break;
}
default:
{
break;
}
}
exit:
platform_mcu_powersave_enable();
return err;
}
示例8: Init_PWM
/******************************************************************************
* タイトル : PWM初期設定
* 関数名 : Init_PWM
* 戻り値 : int型 0:設定できた 1:設定できない
* 引数1 : TIM_TypeDef *型 TIMx TIMx TIMのポインタ
* 引数2 : GPIO_TypeDef型 *GPIOx GPIOx GPIOのポインタ
* 引数3 : uint16_t型 pin GPIO_Pin_x PINの設定
* 引数4 : int型 frequency PWM周波数[Hz](整数)
* 作成者 : 永谷 智貴
* 作成日 : 2014/11/10
******************************************************************************/
int Init_PWM(TIM_TypeDef * TIMx,GPIO_TypeDef *GPIOx,uint16_t pin,int frequency)//エラーがあれば1、なければ0をreturnする
{
long TIM_clock=0;
int prescaler=0;
int period=0;
int calc_retry_flag=1;
//float error_ratio=0;
unsigned short i = 0;
Pin_t pin_state;//
//システムクロックをRCC_Clocksで取得
SystemCoreClockUpdate();
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
//TIMのクロックの取得
if((TIM2<=TIMx&&TIMx<=TIM7)||(TIM12<=TIMx&&TIMx<=TIM14)){
TIM_clock=RCC_Clocks.PCLK1_Frequency*((RCC_TIMPRE+1)*2); //PCLK1のTIMプリスケーラ倍したらTIM2-7,12-14のクロックが出てくる
}else{
TIM_clock=RCC_Clocks.PCLK2_Frequency*((RCC_TIMPRE+1)*2); //PCLK2のTIMプリスケーラ倍したら上のやつ以外のクロックが出てくる
}
#ifdef PRINTF_AVAILABLE
printf("Init_PWM() start.\nTIM_clock:%d,\n",TIM_clock);
#endif
//上下の設定可能な周波数の中に納まっているか確認
if(frequency<FREQUENCY_UNDER_LIMIT || frequency>TIM_clock/PRESCALER_UNDER_LIMIT/PERIOD_UNDER_LIMIT)
{
#ifdef PRINTF_AVAILABLE
printf("Error. Frequency value out of range. '%d' - '%d' Requested frequency '%d'\n",FREQUENCY_UNDER_LIMIT,TIM_clock/PRESCALER_UNDER_LIMIT/PERIOD_UNDER_LIMIT,frequency);
#endif
return 1; //おかしければエラー返して終了
}
//prescaler,periodを計算
while(calc_retry_flag) //periodが制限内の最大になるまでprescalerを上げているだけ。計算でも出せるけど、見た目だけはこっちのほうがきれい。
{
prescaler++;
period=TIM_clock/prescaler/frequency;
if(period<=PERIOD_LIMIT) calc_retry_flag=0;
if(prescaler>=PRESCALER_LIMIT){ //prescalerが上の制限を超しちゃったらエラーを履くけどそうはならない。
#ifdef PRINTF_AVAILABLE
printf("Error. Prescaler value out of range. '%d'-'%d' \n",PRESCALER_UNDER_LIMIT,PRESCALER_LIMIT);
#endif
return 1; //おかしければエラー返して終了
}
}
// error_ratio=fabs(((float)TIM_clock/prescaler/period-(float)frequency)/(float)frequency)*100;//周波数の誤差をパーセントで計算。1%以内には納まる。
frequency=TIM_clock/prescaler/period; //設定した数値から算出される周波数 大体同じ。
#ifdef PRINTF_AVAILABLE
printf("Result: \n period:%d,\n prescaler:%d,\n frequency:%d,\n\n",period,prescaler,frequency);
#endif
//ここから普通のPWM設定
//設定に使用する構造体の宣言
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //TIM設定用構造体宣言
TIM_OCInitTypeDef TIM_OCInitStructure; //OC設定用構造体宣言
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//クロック供給
RCC_PeriphClock_TIM(TIMx);//TIMクロック供給
//クロック供給とGPIO設定
Init_port(GPIO_Mode_AF,GPIOx,pin,GPIO_PuPd_NOPULL,GPIO_OType_PP);
//TIM設定
TIM_TimeBaseStructure.TIM_Period = period-1; //計算したperiod-1
TIM_TimeBaseStructure.TIM_Prescaler = prescaler-1; //計算したprescaler-1
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //なんかここ変えても周波数変わらなかったんだよね ナニコレ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //カウンターモードアップ設定
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //高機能タイマー用 基本0
TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure); //設定書き込み
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWMモード1
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //アクティブレベル時の極性をHighレベルにセット
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //タイマ出力を有効化
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OC1Init(TIMx,&TIM_OCInitStructure); //初期化
TIM_OC1PreloadConfig(TIMx,TIM_OCPreload_Disable); //プリロード不許可
TIM_OC2Init(TIMx,&TIM_OCInitStructure); //初期化
TIM_OC2PreloadConfig(TIMx,TIM_OCPreload_Disable); //プリロード不許可
TIM_OC3Init(TIMx,&TIM_OCInitStructure); //初期化
TIM_OC3PreloadConfig(TIMx,TIM_OCPreload_Disable); //プリロード不許可
TIM_OC4Init(TIMx,&TIM_OCInitStructure); //初期化
//.........这里部分代码省略.........
示例9: Motor_Configuration
void Motor_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//GPIO_WriteBit(GPIOB, GPIO_Pin_14, (BitAction)(0));
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM8);
TIM_TimeBaseStructure.TIM_Prescaler = 2 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 10000;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = 2500;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
/*TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC4Init(TIM8, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);*/
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC4Init(TIM8, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);
//TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_Cmd(TIM8, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM8, ENABLE);
}
示例10: pwmInit
void pwmInit(void) {
// config structs
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOC clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* GPIOC Configuration: TIM3 CH1 (PC6), TIM3 CH2 (PC7), TIM3 CH3 (PC8) and TIM3 CH4 (PC9) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect TIM3 pins to AF2 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3);
// prescale the TIM clock by 84 for a Timer_Freq of 1MHz
uint16_t PrescalerValue = (uint16_t) 84;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1999; //standard 20ms period for servos
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //inital duty cycle
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
}
示例11: RmBatholicTIM8_Config
void RmBatholicTIM8_Config(){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Compute the prescaler value */
uint16_t PrescalerValue = (uint16_t) (SystemCoreClock / 160000000) - 1;
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM4 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8);
RCC_TIMCLKPresConfig(RCC_TIMPrescActivated);
TIM_DeInit(TIM8);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 9999;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
}
示例12: main
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32l1xx_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32l1xx.c file
*/
/* --------------------------- System Clocks Configuration ---------------------*/
/* PCLK1 = HCLK/4 */
RCC_PCLK1Config(RCC_HCLK_Div4);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);
/*--------------------------------- GPIO Configuration -------------------------*/
/* GPIOA Configuration: Pin 6 and 7 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
/* GPIOB Configuration: Pin 0 and 1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM3);
/*--------------------------------- NVIC Configuration -------------------------*/
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* ---------------------------------------------------------------------------
TIM3 Configuration: Output Compare Toggle Mode:
TIM3CLK = SystemCoreClock / 2,
The objective is to get TIM3 counter clock at 16 MHz:
- Prescaler = (TIM3CLK / TIM3 counter clock) - 1
CC1 update rate = TIM3 counter clock / CCR1_Val = 488.281 Hz
CC2 update rate = TIM3 counter clock / CCR2_Val = 976.562 Hz
CC3 update rate = TIM3 counter clock / CCR3_Val = 1953.125 Hz
CC4 update rate = TIM3 counter clock / CCR4_Val = 3906.25 Hz
----------------------------------------------------------------------------*/
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 32000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* Output Compare Toggle Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//.........这里部分代码省略.........
示例13: ws2811_init
void ws2811_init(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
// Default LED values
int i, bit;
for (i = 0;i < LED_BUFFER_LEN;i++) {
RGBdata[i] = 0;
}
for (i = 0;i < LED_BUFFER_LEN;i++) {
uint32_t tmp_color = rgb_to_local(RGBdata[i]);
for (bit = 0;bit < 24;bit++) {
if(tmp_color & (1 << 23)) {
bitbuffer[bit + i * 24] = WS2811_ONE;
} else {
bitbuffer[bit + i * 24] = WS2811_ZERO;
}
tmp_color <<= 1;
}
}
// Fill the rest of the buffer with zeros to give the LEDs a chance to update
// after sending all bits
for (i = 0;i < BITBUFFER_PAD;i++) {
bitbuffer[BITBUFFER_LEN - BITBUFFER_PAD - 1 + i] = 0;
}
// GPIOB clock enable
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
// GPIOB Configuration: Channel 3 as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
// DMA clock enable
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);
DMA_DeInit(DMA1_Stream7);
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM4->CCR3;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)bitbuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = BITBUFFER_LEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream7, &DMA_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// Time Base configuration
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
// Channel 3 Configuration in PWM mode
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = bitbuffer[0];
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
// TIM4 counter enable
TIM_Cmd(TIM4, ENABLE);
// DMA enable
DMA_Cmd(DMA1_Stream7, ENABLE);
// TIM4 Update DMA Request enable
TIM_DMACmd(TIM4, TIM_DMA_CC3, ENABLE);
// Main Output Enable
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
示例14: PWM_Init
//arr:自动重装值
//psc:时钟预分频数
void PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//初始化TIMx的时间基数
TIM_OCInitTypeDef TIM_OCInitStructure;//初始化外设TIMx
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;//死区设置
/*************1、引脚io设置*************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);//使能GPIO外设时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//使能复用功能
//设置该引脚为复用输出功能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //端口频率可设为2,10,50
GPIO_Init(GPIOA, &GPIO_InitStructure);//启动A端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);//启动B端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
GPIO_Init(GPIOC, &GPIO_InitStructure);//启动C端口
/*****************2、通道设置*****************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_TIM8, ENABLE);//使能外设时钟TIMx,TIM8
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM脉冲宽度调制模式1:小于设定值则为高;2反之
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //选择互补输出比较状态
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; //TIM互补输出极性为高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //选择空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //重置互补输出的输出比较状态
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //TIMx通道1设置
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //预装载使能,为了让控制更精准,更改不是立马改变,在本次波形执行完之后
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //不用也可以
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); //
//死区设置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;//设置在运行模式下非工作状态选项
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;//设置空闲状态下的非工作状态
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;//设置锁电平参数,不锁定任何位
TIM_BDTRInitStructure.TIM_DeadTime = 100;//死区时间,0-0xff,设置了输出打开和关闭之间的延时
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;//刹车功能使能
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//自动输出使能
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure); //
TIM_BDTRConfig(TIM8,&TIM_BDTRInitStructure); //
TIM_Cmd(TIM1, ENABLE); //使能TIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
TIM_Cmd(TIM8, ENABLE); //使能TIM8
TIM_CtrlPWMOutputs(TIM8,ENABLE); //MOE 主输出使能
pwm_set_off(TIM8, TIM_Channel_1);
pwm_set_off(TIM8, TIM_Channel_2);
pwm_set_off(TIM8, TIM_Channel_3);
pwm_set_off(TIM1, TIM_Channel_1);
}
示例15: analogWrite
/*
* @brief Should take an integer 0-255 and create a PWM signal with a duty cycle from 0-100%.
* TIM_PWM_FREQ is set at 500 Hz
*/
void analogWrite(uint16_t pin, uint8_t value)
{
if (pin >= TOTAL_PINS || PIN_MAP[pin].timer_peripheral == NULL)
{
return;
}
// SPI safety check
if (SPI.isEnabled() == true && (pin == SCK || pin == MOSI || pin == MISO))
{
return;
}
// I2C safety check
if (Wire.isEnabled() == true && (pin == SCL || pin == SDA))
{
return;
}
// Serial1 safety check
if (Serial1.isEnabled() == true && (pin == RX || pin == TX))
{
return;
}
if(PIN_MAP[pin].pin_mode != OUTPUT && PIN_MAP[pin].pin_mode != AF_OUTPUT_PUSHPULL)
{
return;
}
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//PWM Frequency : 500 Hz
uint16_t TIM_Prescaler = (uint16_t)(SystemCoreClock / 24000000) - 1;//TIM Counter clock = 24MHz
uint16_t TIM_ARR = (uint16_t)(24000000 / TIM_PWM_FREQ) - 1;
// TIM Channel Duty Cycle(%) = (TIM_CCR / TIM_ARR + 1) * 100
uint16_t TIM_CCR = (uint16_t)(value * (TIM_ARR + 1) / 255);
// AFIO clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
pinMode(pin, AF_OUTPUT_PUSHPULL);
// TIM clock enable
if(PIN_MAP[pin].timer_peripheral == TIM2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
else if(PIN_MAP[pin].timer_peripheral == TIM3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
else if(PIN_MAP[pin].timer_peripheral == TIM4)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// Time base configuration
TIM_TimeBaseStructure.TIM_Period = TIM_ARR;
TIM_TimeBaseStructure.TIM_Prescaler = TIM_Prescaler;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(PIN_MAP[pin].timer_peripheral, &TIM_TimeBaseStructure);
// PWM1 Mode configuration
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = TIM_CCR;
if(PIN_MAP[pin].timer_ch == TIM_Channel_1)
{
// PWM1 Mode configuration: Channel1
TIM_OC1Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable);
}
else if(PIN_MAP[pin].timer_ch == TIM_Channel_2)
{
// PWM1 Mode configuration: Channel2
TIM_OC2Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable);
}
else if(PIN_MAP[pin].timer_ch == TIM_Channel_3)
{
// PWM1 Mode configuration: Channel3
TIM_OC3Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable);
}
else if(PIN_MAP[pin].timer_ch == TIM_Channel_4)
{
// PWM1 Mode configuration: Channel4
TIM_OC4Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable);
}
TIM_ARRPreloadConfig(PIN_MAP[pin].timer_peripheral, ENABLE);
// TIM enable counter
//.........这里部分代码省略.........