本文整理汇总了C++中TIM_OC1PreloadConfig函数的典型用法代码示例。如果您正苦于以下问题:C++ TIM_OC1PreloadConfig函数的具体用法?C++ TIM_OC1PreloadConfig怎么用?C++ TIM_OC1PreloadConfig使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TIM_OC1PreloadConfig函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: timer2_init
//此为PWM的输出定时器端口
void timer2_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE); /* 打开 TIM2 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* 设置 GPIOA 上的 TIM2 1,2通道对应引脚 PA.0,PA.1为第二功能推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 设置timer2
* 计数重载值为1000
* 预分频值为3
* 时钟分割0
* 向上计数模式
* 则产生的PWM信号频率为24KHz,占空比为CCRx_Val/1000
*/
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 2;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2 , &TIM_TimeBaseStructure);
/* 设置timer2的 OC1,OC2通道
* 工作模式为 PWM 输出模式
* 使能比较匹配输出极性
* 时钟分割0
* 向上计数模式
* 设置各匹配值分别为 CCR1_Val, CCR2_Val
*/
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 = CCR1_Val;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/* 使能预装载寄存器 */
TIM_OC1PreloadConfig(TIM2 , TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2 , TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* 启动 TIM 计数 */
TIM_Cmd(TIM2 , ENABLE);
}
示例2: PIOS_Brushless_Init
/**
* Initialise Servos
*/
int32_t PIOS_Brushless_Init(const struct pios_brushless_cfg * cfg)
{
uintptr_t tim_id;
if (PIOS_TIM_InitChannels(&tim_id, cfg->channels, cfg->num_channels, NULL, 0)) {
return -1;
}
/* Store away the requested configuration */
brushless_cfg = cfg;
/* Configure the channels to be in output compare mode */
for (uint8_t i = 0; i < cfg->num_channels; i++) {
const struct pios_tim_channel * chan = &cfg->channels[i];
/* Set up for output compare function */
switch(chan->timer_chan) {
case TIM_Channel_1:
TIM_OC1Init(chan->timer, (TIM_OCInitTypeDef*)&cfg->tim_oc_init);
TIM_OC1PreloadConfig(chan->timer, TIM_OCPreload_Enable);
break;
case TIM_Channel_2:
TIM_OC2Init(chan->timer, (TIM_OCInitTypeDef*)&cfg->tim_oc_init);
TIM_OC2PreloadConfig(chan->timer, TIM_OCPreload_Enable);
break;
case TIM_Channel_3:
TIM_OC3Init(chan->timer, (TIM_OCInitTypeDef*)&cfg->tim_oc_init);
TIM_OC3PreloadConfig(chan->timer, TIM_OCPreload_Enable);
break;
case TIM_Channel_4:
TIM_OC4Init(chan->timer, (TIM_OCInitTypeDef*)&cfg->tim_oc_init);
TIM_OC4PreloadConfig(chan->timer, TIM_OCPreload_Enable);
break;
}
TIM_ARRPreloadConfig(chan->timer, ENABLE);
TIM_CtrlPWMOutputs(chan->timer, ENABLE);
TIM_Cmd(chan->timer, ENABLE);
}
for (uint8_t i = 0 ; i < NUM_BGC_CHANNELS; i++) {
// Enable the available enable lines
if (cfg->enables[i].gpio) {
GPIO_Init(cfg->enables[i].gpio, (GPIO_InitTypeDef *) &cfg->enables[i].init);
GPIO_SetBits(cfg->enables[i].gpio, cfg->enables[i].init.GPIO_Pin);
}
}
// Start main task
taskHandle = PIOS_Thread_Create(
PIOS_BRUSHLESS_Task, "pios_brushless", STACK_SIZE_BYTES, NULL, TASK_PRIORITY);
return 0;
}
示例3: RGB_Init
void RGB_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//TIM2 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 1000; //1ms
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM_OCPolarity_High
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
示例4: TIM_LED_Config
/**
* @brief Configures the TIM Peripheral for Led toggling.
* @param None
* @retval None
*/
static void TIM_LED_Config(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
uint16_t prescalervalue = 0;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the TIM3 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Initialize Leds mounted on STM324F4-EVAL board */
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDInit(LED6);
/* Compute the prescaler value */
prescalervalue = (uint16_t) ((SystemCoreClock ) / 550000) - 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(TIM4, &TIM_TimeBaseStructure);
/* Enable TIM4 Preload register on ARR */
TIM_ARRPreloadConfig(TIM4, ENABLE);
/* TIM PWM1 Mode configuration: Channel */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel2 */
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable);
/* TIM Interrupts enable */
TIM_ITConfig(TIM4, TIM_IT_CC1 , ENABLE);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
示例5: buzzer_Configuration
void buzzer_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOB Configuration: TIM3 CH1 (PB4) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // alternative function mode
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//do we need this line? why?
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // push pull mode
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;//can this be flow?
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM3 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
//TIM3 clock = APB1 clock * 2 = 84MHz
//TIM3 clock = (period+1) * (prescaler+1) * PWM_frequency
// ==> 84MHz = (period+1) * (prescaler+1) * PWM_frequency(Hz)
// ==> PWM_frequency(Hz) = 84000000Hz / (period+1) / (prescaler+1)
// usually, we only change the period(the corresponding register is TIM3->AAR, so we change the value of TIM3->AAR) in order to change
// the frequency of the frequency of the buzzer beeping. you can either write a function or have
// a macro like this: #define setBuzzerFrequency(f) TIM3->ARR=84000000/(f)/140-1
//PWM frequency = 4KHz as default because the buzzer has a 4KHz+-500Hz max resonant range.
//any thing beyond the max resonant range will still work
//you can also change the value for prescaler in order to change the frequency
//the period and prescaler are 150 and 140, since the registers start to count from 0,
//so the actual value should +1 when you are calculate the frequency and anything else
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 149;//150 TIM3->AAR
TIM_TimeBaseStructure.TIM_Prescaler = 139;//140
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//TIM3 clock(84MHz here) divide by 1(0+1) to be the clock to be the base clock of TIM3 PWM
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM mode 1, most commond mode
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//if polarity is high, when you set PWM at 25% duty cycle, the 25% will be high and 75% will be low
//when the polarity is LOW, 75% will be high and 25% will be low
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//enable the preload for PWM channel 1
TIM_ARRPreloadConfig(TIM3, ENABLE); //enable the proload for TIM3-ARR register
TIM_Cmd(TIM3, ENABLE); // enable TIM3
}
示例6: PWM_Init_Output
void PWM_Init_Output(PWM_Output* output)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(output->RCC_APB2Periph, ENABLE);
RCC_APB1PeriphClockCmd(output->RCC_APB1Periph, ENABLE);
/* GPIOB Configuration:TIM4 Channel4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = output->GPIO_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(output->GPIO_PORT, &GPIO_InitStructure);
/* Time base configuration */
output->TIM_TimeBaseStructure.TIM_Period = output->TIM_Period;
output->TIM_TimeBaseStructure.TIM_Prescaler = 2;//Ô¤·ÖƵ2,ƵÂÊ36M
output->TIM_TimeBaseStructure.TIM_ClockDivision = 0;
output->TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(output->GPIO_TIM, &output->TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
output->TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
output->TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* PWM1 Mode configuration: ChannelX */
output->TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
output->TIM_OCInitStructure.TIM_Pulse = (output->TIM_Period/100)*50;
switch( output->Channel )
{
case 1:
TIM_OC1Init(output->GPIO_TIM, &output->TIM_OCInitStructure);
TIM_OC1PreloadConfig(output->GPIO_TIM, TIM_OCPreload_Enable);
break;
case 2:
TIM_OC2Init(output->GPIO_TIM, &output->TIM_OCInitStructure);
TIM_OC2PreloadConfig(output->GPIO_TIM, TIM_OCPreload_Enable);
break;
case 3:
TIM_OC3Init(output->GPIO_TIM, &output->TIM_OCInitStructure);
TIM_OC3PreloadConfig(output->GPIO_TIM, TIM_OCPreload_Enable);
break;
case 4:
TIM_OC4Init(output->GPIO_TIM, &output->TIM_OCInitStructure);
TIM_OC4PreloadConfig(output->GPIO_TIM, TIM_OCPreload_Enable);
break;
}
TIM_ARRPreloadConfig(output->GPIO_TIM, ENABLE);
/* output->GPIO_TIM enable counter */
TIM_Cmd(output->GPIO_TIM, ENABLE);
}
示例7: pwmout_write
void pwmout_write(pwmout_t* obj, float value) {
TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm);
TIM_OCInitTypeDef TIM_OCInitStructure;
if (value < 0.0) {
value = 0.0;
} else if (value > 1.0) {
value = 1.0;
}
obj->pulse = (uint32_t)((float)obj->period * value);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = obj->pulse;
// Configure channel 1
if (obj->pin == PA_7) {
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
TIM_OC1Init(tim, &TIM_OCInitStructure);
}
// Configure channel 1N
if (obj->pin == PB_6) {
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
TIM_OC1Init(tim, &TIM_OCInitStructure);
}
// Configure channel 2
if (obj->pin == PC_7) {
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable);
TIM_OC2Init(tim, &TIM_OCInitStructure);
}
}
示例8: timer_init
void timer_init(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
NVIC_EnableIRQ(TIM4_IRQn);
//25Hz merení 200-1 50Hz 100-1 100Hz 50-1
TIM_TimeBaseStructure.TIM_Period = 2048 - 1; //336
TIM_TimeBaseStructure.TIM_Prescaler = 1 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 512;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 1024;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 1536;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
TIM4->DIER = TIM_DIER_UIE; // povolení prerušení
TIM_Cmd(TIM4, ENABLE);
}
示例9: Timer3_PWM_Output_Config
void Timer3_PWM_Output_Config(uint32_t PeriodValue, uint16_t PrescalerValue)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = PeriodValue - 1;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PulseValue1;
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 = PulseValue2;
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 = PulseValue3;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PulseValue4;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
}
示例10: PWM_vInit
/**
* @brief Initialisiert die Ports an dennen der Motor und der Servo angeschlossen ist.
* Ausserdem werden PWM Cycle eingestellt und der neutrale DutyCyle von
* 1.5ms (Millisekunden gesetzt).
* @param none
* @retval none
**/
void PWM_vInit(void)
{
TIM_TimeBaseInitTypeDef myTIM;
GPIO_InitTypeDef myGPIO_TIM;
TIM_OCInitTypeDef myTIM_OC;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
//**********GPIO's konfigurieren
myGPIO_TIM.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7;
myGPIO_TIM.GPIO_Mode=GPIO_Mode_AF;
myGPIO_TIM.GPIO_Speed=GPIO_Speed_100MHz;
myGPIO_TIM.GPIO_OType=GPIO_OType_PP;
myGPIO_TIM.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOA,&myGPIO_TIM);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3);
//TIMER KONFIGURIEREN
//Prescaler = ((SystemCoreClock /2) / TIM3 counter clock) - 1
myTIM.TIM_Prescaler=(uint16_t)(((168000000/2)/PWM_CK_CNT)-1);
// ARR = (TIM3 counter clock / TIM3 output clock) - 1
PWM_u16ARR=(PWM_CK_CNT/PWM_TIM_OUTPUT_CLK)-1;
myTIM.TIM_Period=(uint32_t)PWM_u16ARR;
myTIM.TIM_ClockDivision=TIM_CKD_DIV1;
myTIM.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&myTIM);
//1 KANAL KONFIGURIEREN
//DutyCycle = (TIM3_CCR1/ (TIM3_ARR+1))* 100
PWM_u16CCR=(uint16_t)((PWM_fDCycle/100.0f)*(PWM_u16ARR+1));
myTIM_OC.TIM_OCMode=TIM_OCMode_PWM1;
myTIM_OC.TIM_OutputState=TIM_OutputState_Enable;
myTIM_OC.TIM_Pulse=PWM_u16CCR;
myTIM_OC.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM3,&myTIM_OC);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
//2 KANAL KONFIGURIEREN
myTIM_OC.TIM_OutputState=TIM_OutputState_Enable;
myTIM_OC.TIM_Pulse=PWM_u16CCR;
TIM_OC2Init(TIM3,&myTIM_OC);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
示例11: led_timer_init
void led_timer_init()
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Clock for GPIOD */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Alternating functions for pins */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
/* Set pins */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStruct);
TIM_TimeBaseInitTypeDef TIM_BaseStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); // enable clock
TIM_BaseStruct.TIM_Prescaler = 0;
TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseStruct.TIM_Period = 8399;
TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_BaseStruct);
TIM_Cmd(TIM4, ENABLE);
TIM_OCInitTypeDef TIM_OCStruct;
TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCStruct.TIM_Pulse = 0;
TIM_OC1Init(TIM4, &TIM_OCStruct);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCStruct.TIM_Pulse = 0;
TIM_OC2Init(TIM4, &TIM_OCStruct);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCStruct.TIM_Pulse = 0;
TIM_OC3Init(TIM4, &TIM_OCStruct);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCStruct.TIM_Pulse = 0;
TIM_OC4Init(TIM4, &TIM_OCStruct);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
}
示例12: TIM3_PWM_Init
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟使能
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Timer3全映射 GPIOC-> 6,7,8,9 //用于TIM3的CH2输出的PWM通过该LED显示
//设置该引脚为复用输出功能,输出TIM3 CH1 CH2 CH3 CH4 的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; //初始化GPIO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR1上的预装载寄存器
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR2上的预装载寄存器
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR3上的预装载寄存器
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR4上的预装载寄存器
TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
示例13: PWM_Init
void PWM_Init()
{
// Initialization struct
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
// Step 1: Initialize TIM2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Create 1kHz PWM
// TIM2 is connected to APB1 bus that have default clock 72MHz
// So, the frequency of TIM2 is 72MHz
// We use prescaler 10 here
// So, the frequency of TIM2 now is 72MHz
TIM_TimeBaseInitStruct.TIM_Prescaler = 10;
// TIM_Period determine the PWM frequency by this equation:
// PWM_frequency = timer_clock / (TIM_Period + 1)
// If we want 1kHz PWM we can calculate:
// TIM_Period = (timer_clock / PWM_frequency) - 1
// TIM_Period = (7.2MHz / 1kHz) - 1 = 7199
TIM_TimeBaseInitStruct.TIM_Period = 7199;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
// Start TIM2
TIM_Cmd(TIM2, ENABLE);
// Step 2: Initialize PWM
// Common PWM settings
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
// Duty cycle calculation equation:
// TIM_Pulse = (((TIM_Period + 1) * duty_cycle) / 100) - 1
// Ex. 25% duty cycle:
// TIM_Pulse = (((7199 + 1) * 25) / 100) - 1 = 1799
// TIM_Pulse = (((7199 + 1) * 75) / 100) - 1 = 5399
// We initialize PWM value with duty cycle of 0%
TIM_OCInitStruct.TIM_Pulse = 0;
TIM_OC1Init(TIM2, &TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
// Step 3: Initialize GPIOA (PA0)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Initialize PA0 as push-pull alternate function (PWM output) for LED
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
示例14: 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
*/
/* GPIO Configuration */
GPIO_Configuration();
/* -----------------------------------------------------------------------
TIM10 Configuration: generate 1 PWM signal.
The TIM10CLK frequency is set to SystemCoreClock (72 MHz), to get TIM10 counter
clock at 24 MHz the Prescaler is computed as following:
- Prescaler = (TIM10CLK / TIM10 counter clock) - 1
The TIM10 is running at 36 KHz: TIM10 Frequency = TIM10 counter clock/(ARR + 1)
= 24 MHz / 666 = 36 KHz
TIM10 Channel1 duty cycle = (TIM10_CCR1/ TIM10_ARR)* 100 = 37.5%
----------------------------------------------------------------------- */
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 665;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM10, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM10, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM10, ENABLE);
/* TIM10 enable counter */
TIM_Cmd(TIM10, ENABLE);
while (1)
{}
}
示例15: motor_init
/********************************************************************
º¯Êý¹¦ÄÜ£ºµç»ú³õʼ»¯
Èë¿Ú²ÎÊý£ºÎÞ¡£
·µ »Ø£ºÎÞ¡£
±¸ ×¢£º
********************************************************************/
void motor_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t PrescalerValue = 0; //¿ØÖƵç»úPWMƵÂÊ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //´ò¿ªÍâÉèAµÄʱÖӺ͸´ÓÃʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 ,ENABLE); //´ò¿ª¶¨Ê±Æ÷2ʱÖÓ
// ÉèÖÃGPIO¹¦ÄÜ¡£
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// ¸´Î»¶¨Ê±Æ÷¡£
TIM_DeInit(TIM2);
// ÅäÖüÆʱÆ÷¡£
PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
TIM_TimeBaseStructure.TIM_Period = 999; //¼ÆÊýÉÏÏß
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; //pwmʱÖÓ·ÖƵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
// ÅäÖÃTIM2ΪPWMÊä³öģʽ
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //0
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_OC2Init(TIM2,&TIM_OCInitStructure);
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_OC4Init(TIM2,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
// Æô¶¯¼ÆʱÆ÷¡£
TIM_Cmd(TIM2,ENABLE);
//printf("-> motor enable...\r\n");
}