本文整理汇总了C++中TIM_OC2Init函数的典型用法代码示例。如果您正苦于以下问题:C++ TIM_OC2Init函数的具体用法?C++ TIM_OC2Init怎么用?C++ TIM_OC2Init使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TIM_OC2Init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: setStatusLED
void setStatusLED(Led_status status)
{
current_led_status = status;
switch(status){
case SYSTEM_LED_ALLWAYS_OFF:
TIM_Cmd(TIM3, DISABLE);
TIM_ForcedOC2Config(TIM3, TIM_ForcedAction_InActive);
break;
case SYSTEM_LED_ALLWAYS_ON:
TIM_Cmd(TIM3, DISABLE);
TIM_ForcedOC2Config(TIM3, TIM_ForcedAction_Active);
break;
case SYSTEM_LED_TWINKLE_EASYLINK:
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
break;
case SYSTEM_LED_TWINKLE_WPS:
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
break;
default:
TIM_Cmd(TIM3, DISABLE);
TIM_ForcedOC2Config(TIM3, TIM_ForcedAction_InActive);
break;
}
}
示例2: TIM1_Mode_Init
void TIM1_Mode_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
/*定时器初始化*/
Channel1Pulse = 0;//初始化,没有任何pwm输出
Channel2Pulse = 0;
TIM_TimeBaseStructure.TIM_Prescaler = 12; //设置用来作为TIM 时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器模式 /* 向上计数模式 */
TIM_TimeBaseStructure.TIM_Period = 999; //自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分割 不分频
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //设置 周期 计数值
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
/*TIM1 配置*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出比较状态
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //使能 互补 输出状态
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; //给1通道填入初始值
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; //给2通道填入初始值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出比较极性低
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//互补 输出极性低
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //MOE=0 设置 TIM1输出比较空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;//MOE=0 重置 TIM1输出比较空闲状态
// TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; //给2通道填入初始值
TIM_OC1Init(TIM1, &TIM_OCInitStructure);//设定1通道好的参数 初始化TIM
TIM_OC1Init(TIM8, &TIM_OCInitStructure);//设定1通道好的参数 初始化TIM
TIM_OC2Init(TIM1, &TIM_OCInitStructure);//初始化
TIM_OC2Init(TIM8, &TIM_OCInitStructure);//初始化
/*设置刹车特性 死区时间 锁电平 OSSI OSSR 状态 AOE(自动输出使能)*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //设置在运行模式下 非工作状态选项 OSSR
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //设置在运行模式下 非工作状态选项 OSSI
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //使用锁电平1
TIM_BDTRInitStructure.TIM_DeadTime = 80; //指定了输出和打开 关闭状态之间的延时 约 400ns
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//自动输出功能使能
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure); //初始化设定好的参数
TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure); //初始化设定好的参数
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM8, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
}
示例3: vLedSetDuty
void vLedSetDuty(LedState_t state, int32_t duty)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_Cmd(TIM2, DISABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 400;
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 24000000) - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty<<2;
if (TIM_OCInitStructure.TIM_Pulse>TIM_TimeBaseStructure.TIM_Period)
TIM_OCInitStructure.TIM_Pulse=TIM_TimeBaseStructure.TIM_Period-1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
if (state==LedState_White)
{
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
}
else if (state==LedState_Green)
{
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
}
else if (state==LedState_Red)
{
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
}
else if (state==LedState_Blue)
{
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
}
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
示例4: TIM_OC1Init
void pwm::setControlRegister(uint8_t registerNum, TIM_TypeDef* timer)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //sets the timer to output pwm signals
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 0;
if(registerNum == 1)
{
TIM_OC1Init(timer, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(timer, TIM_OCPreload_Enable);
}
else if(registerNum == 2)
{
TIM_OC2Init(timer, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(timer, TIM_OCPreload_Enable);
}
else if(registerNum == 3)
{
TIM_OC3Init(timer, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(timer, TIM_OCPreload_Enable);
}
else if(registerNum == 4)
{
TIM_OC4Init(timer, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(timer, TIM_OCPreload_Enable);
}
}
示例5: TIM9_Configuration
void TIM9_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Time base configuration - SystemCoreClock = 168000000 for 168 MHz board */
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (((SystemCoreClock / 1000000)) - 1); // Shooting for 1 MHz, (1us)
TIM_TimeBaseStructure.TIM_Period = 20000 - 1; // 1 MHz / 20000 = 50 Hz (20ms)
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);
/* Enable TIM4 Preload register on ARR */
TIM_ARRPreloadConfig(TIM9, ENABLE);
/* TIM PWM1 Mode configuration: Channel */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1500; // Servo Top-Center
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel1 PD.12 */
TIM_OC1Init(TIM9, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Enable);
/* Output Compare PWM1 Mode configuration: Channel2 PD.13 */
TIM_OC2Init(TIM9, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable);
/* TIM Interrupts enable */
TIM_ITConfig(TIM9, TIM_IT_Update, ENABLE);
/* TIM4 enable counter */
TIM_Cmd(TIM9, ENABLE);
}
示例6: PWM_Config
void PWM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// Update_event = TIM_CLK/((PSC + 1)*(ARR + 1)*(RCR + 1))
// 10e3=64e6/(2*3200*1)
//10kHz
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_Prescaler= 1; //32Mz
TIM_TimeBaseStructure.TIM_Period=3200-1; //10kHz
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4,&TIM_OCInitStructure);
TIM_OC2Init(TIM4,&TIM_OCInitStructure);
TIM_OC3Init(TIM4,&TIM_OCInitStructure);
TIM_OC4Init(TIM4,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
示例7: TIMER2_CH2_PWM_Init
void TIMER2_CH2_PWM_Init(int prescaler,int autoreload){
//USER LED / PB3 / TIM2_CH2 / AF1
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOBEN ,ENABLE);
GPIO_InitTypeDef myGPIO;
GPIO_StructInit(&myGPIO);
myGPIO.GPIO_Pin=GPIO_Pin_3;
myGPIO.GPIO_Mode=GPIO_Mode_AF;
myGPIO.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOB,&myGPIO);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_1);
//select the output mode by writing the CCS bits in the CCMRx register
//Timer time base configuration
RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM2EN,ENABLE);
TIM_TimeBaseInitTypeDef myTimeBase;
TIM_TimeBaseStructInit(&myTimeBase);
myTimeBase.TIM_CounterMode=TIM_CounterMode_Up;
myTimeBase.TIM_Period=autoreload;
myTimeBase.TIM_Prescaler=prescaler;
myTimeBase.TIM_ClockDivision= TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&myTimeBase);
//Timer capture compare configuration
TIM_OCInitTypeDef myTimerOC;
TIM_OCStructInit(&myTimerOC);
myTimerOC.TIM_OCMode=TIM_OCMode_PWM1;
myTimerOC.TIM_OCPolarity=TIM_OCPolarity_High;
myTimerOC.TIM_OutputState=TIM_OutputState_Enable;
myTimerOC.TIM_Pulse=autoreload;//0 Duty cycle at start
TIM_OC2Init(TIM2,&myTimerOC);
TIM_CCxCmd(TIM2,TIM_Channel_2,TIM_CCx_Enable);//enable CCP2
//start Timer
TIM_Cmd(TIM2,ENABLE);//Counter enabled
}
示例8: RGBLED_Update
void RGBLED_Update(uint8_t RED_Val, uint8_t GREEN_Val, uint8_t BLUE_Val)
{
//printf("r: %x g: %x b:%x\n", RED_Val, GREEN_Val, BLUE_Val);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = GREEN_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = BLUE_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = RED_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
}
示例9: TIM2_Configuration
/**TIM2选用没有重影像方式**/
void TIM2_Configuration(void)//TIMER INITIALIZATION
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// GPIO_PinRemapConfig(_,_);//没有重映像
// TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_TimeBaseStructure.TIM_Period = 100-1;
TIM_TimeBaseStructure.TIM_Prescaler =36-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//定时器中断
//PWM初始化
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能预装载寄存器
TIM_OC2Init(TIM2,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能预装载寄存器
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能预装载寄存器
TIM_OC4Init(TIM2,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能预装载寄存器
TIM_Cmd(TIM2,ENABLE);
}
示例10: TIM3_PWM_Init
void TIM3_PWM_Init( void )
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init( GPIOB, &GPIO_InitStructure );
/* ------------------------------------------------------------
* TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
* TIM3CLK = 72 MHz, Prescaler = 0x0, TIM3 counter clock = 72 MHz
* TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
* TIM3 Frequency = 72 KHz.
* TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
* TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
* TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
* TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
* ------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = PWM_ARR;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );
TIM_ARRPreloadConfig( TIM3, ENABLE );
TIM_Cmd( TIM3, ENABLE );
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init( TIM3, &TIM_OCInitStructure );
TIM_OC1PreloadConfig( TIM3, TIM_OCPreload_Enable );
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init( TIM3, &TIM_OCInitStructure );
TIM_OC2PreloadConfig( TIM3, TIM_OCPreload_Enable );
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC3Init( TIM3, &TIM_OCInitStructure );
TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Enable );
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC4Init( TIM3, &TIM_OCInitStructure );
TIM_OC4PreloadConfig( TIM3, TIM_OCPreload_Enable );
}
示例11: PWM_TIM
/**
* @brief Configure the TIM4 parameters
*/
void PWM_TIM(void){
/* Compute the prescaler value */
//SystemCoreClock max value is 72MHz for TIM4
PrescalerValue_t = 72-1;
Period = 20000-1;
/* Time base configuration for TIM4 */
TIM_TimeBaseStructure.TIM_Period = Period;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue_t;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Set the ouput compare structure mode to PWM1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* PWM1 Mode configuration: Channels 1 and 2 */
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
}
示例12: RCC_APB2PeriphClockCmd
void Rilma_t::Init() {
// ==== GPIO ====
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
// ==== Timer4 as PWM ====
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 255;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
// ==== PWM outputs ====
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
// Init channels
#ifdef CHANNEL1_ENABLE
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
#endif
#ifdef CHANNEL2_ENABLE
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
#endif
#ifdef CHANNEL3_ENABLE
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
#endif
TIM_ARRPreloadConfig(TIM4, ENABLE); // Enable autoreload of preload
}
示例13: configBeeperPWMTimer
static void configBeeperPWMTimer(const beeperDevConfig_t *config)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (1000000 / BEEPER_PWM_FREQUENCY) * 50 / 100; // 50% duty cycle
TIM_OCInitStructure.TIM_OCPolarity = config->isInverted ? TIM_OCPolarity_High : TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = config->isInverted ? TIM_OCIdleState_Reset : TIM_OCIdleState_Set;
configTimeBase(BEEPER_PWM_TIMER, 1000000 / BEEPER_PWM_FREQUENCY, PWM_TIMER_MHZ);
TIM_Cmd(BEEPER_PWM_TIMER, ENABLE);
switch (BEEPER_PWM_TIMER_CH) {
case TIM_Channel_1:
TIM_OC1Init(BEEPER_PWM_TIMER, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(BEEPER_PWM_TIMER, TIM_OCPreload_Enable);
break;
case TIM_Channel_2:
TIM_OC2Init(BEEPER_PWM_TIMER, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(BEEPER_PWM_TIMER, TIM_OCPreload_Enable);
break;
case TIM_Channel_3:
TIM_OC3Init(BEEPER_PWM_TIMER, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(BEEPER_PWM_TIMER, TIM_OCPreload_Enable);
break;
case TIM_Channel_4:
TIM_OC4Init(BEEPER_PWM_TIMER, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(BEEPER_PWM_TIMER, TIM_OCPreload_Enable);
break;
}
TIM_CtrlPWMOutputs(BEEPER_PWM_TIMER, DISABLE);
}
示例14: PWM_Config
void PWM_Config(void)
{
/* Configure PWM mode for TIM4 Channels 1->4*/
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
//3 other channels
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
示例15: Holder_PWM_Init
static void Holder_PWM_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB_HOLDER_TIM, ENABLE);
/* time base --> all pwm's period and prescaler */
TIM_TimeBaseStructure.TIM_Period = HOLDER_PERIOD;
TIM_TimeBaseStructure.TIM_Prescaler = HOLDER_PRESCALER - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(HOLDER_TIM, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(HOLDER_TIM,ENABLE);
/* output channel --> every pwm's pulse */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* channel 1 --> holder vertical */
TIM_OCInitStructure.TIM_Pulse = HOLDER_V_PULSE_INIT;
TIM_OC1Init(HOLDER_TIM, &TIM_OCInitStructure);
TIM_OC1PolarityConfig(HOLDER_TIM, TIM_OCPreload_Enable);
/* channel 2 --> holder horizontal */
TIM_OCInitStructure.TIM_Pulse = HOLDER_H_PULSE_INIT;
TIM_OC2Init(HOLDER_TIM, &TIM_OCInitStructure);
TIM_OC2PolarityConfig(HOLDER_TIM, TIM_OCPreload_Enable);
/* enable holder's pwm module */
TIM_Cmd(HOLDER_TIM, ENABLE);
}