Пример #1
0
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);
}
Пример #2
0
void TIM3_Init(void) {

	/* Time base configuration */
	TIM_TimeBaseStructure.TIM_Prescaler = TIM3_PRESCALER;
	TIM_TimeBaseStructure.TIM_Period = TIM3_PERIOD;

	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;
	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 = 0;
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);
	TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

	TIM_ARRPreloadConfig(TIM3, ENABLE);

	/* TIM3 enable counter */
	TIM_Cmd(TIM3, ENABLE);
}
Пример #3
0
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 );
}
Пример #4
0
void initPWM() {
    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_High;

    /* PWM1 Mode configuration: Channel1 (GPIOD Pin 12)*/
    TIM_OC1Init(TIM4, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

    /* PWM1 Mode configuration: Channel2 (GPIOD Pin 13)*/
    TIM_OC2Init(TIM4, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);

    /* PWM1 Mode configuration: Channel3 (GPIOD Pin 14)*/
    TIM_OC3Init(TIM4, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);

    /* PWM1 Mode configuration: Channel4 (GPIOD Pin 15)*/
    TIM_OC4Init(TIM4, &TIM_OCInitStructure);
    TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);

    TIM_Cmd(TIM4, ENABLE);
}
Пример #5
0
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);
	}
}
Пример #6
0
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);
}
Пример #7
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
void Timer3Init(void)
{
  RCC_Configuration();
  GPIO_Configuration();

  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1;
	
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 1000;
  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;
  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);	
	
  TIM_ARRPreloadConfig(TIM3, ENABLE);

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}
Пример #8
0
void SA_TIMER_PWM_Init(TIM_TypeDef* TIMx, uint8_t NUM)
{
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	 
	TIM_OCInitStructure.TIM_OCMode 				= TIM_OCMode_PWM2; 			//选择定时器模式:TIM脉冲宽度调制模式2
 	TIM_OCInitStructure.TIM_OutputState 		= TIM_OutputState_Enable; 	//比较输出使能
	TIM_OCInitStructure.TIM_OCPolarity 			= TIM_OCPolarity_Low; 		//输出极性:TIM输出比较极性高
											  //TIM_OCPolarity_High		 
	if (1 == NUM)
	{	
		TIM_OC1Init(TIMx, &TIM_OCInitStructure);  								
		TIM_OC1PreloadConfig(TIMx, TIM_OCPreload_Enable);  						
	}
	else if(2 == NUM)
	{
		TIM_OC2Init(TIMx, &TIM_OCInitStructure);  								//根据T指定的参数初始化外设TIM3 OC2
		TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);  						//使能TIMx在CCR2上的预装载寄存器
	}
	else if(3 == NUM)
	{
		TIM_OC3Init(TIMx, &TIM_OCInitStructure);  								//根据T指定的参数初始化外设TIM3 OC2
		TIM_OC3PreloadConfig(TIMx, TIM_OCPreload_Enable);  						//使能TIMx在CCR2上的预装载寄存器
	}
	else
	{
		if (4 == NUM)
		{
			TIM_OC4Init(TIMx, &TIM_OCInitStructure);  								//根据T指定的参数初始化外设TIM3 OC2
			TIM_OC4PreloadConfig(TIMx, TIM_OCPreload_Enable);  						//使能TIMx在CCR2上的预装载寄存器
		}
	}
}
Пример #9
0
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
}
Пример #10
0
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);
}
Пример #11
0
void pwm_mode(timer_dev *dev, uint8_t channel) {
    timer_disable_irq(dev, channel);
   // timer_oc_set_mode(dev, channel, TIMER_OC_MODE_PWM_1, TIMER_OC_PE);
    switch (channel)
    {
		case 1:
			TIM_SelectOCxM(dev->regs, TIM_Channel_1, TIM_OCMode_PWM1);
			TIM_OC1PreloadConfig(dev->regs, TIM_OCPreload_Enable);		
			break;
		case 2:
			TIM_SelectOCxM(dev->regs, TIM_Channel_2, TIM_OCMode_PWM1);
			TIM_OC2PreloadConfig(dev->regs, TIM_OCPreload_Enable);					
			break;
		case 3:
			TIM_SelectOCxM(dev->regs, TIM_Channel_3, TIM_OCMode_PWM1);
			TIM_OC3PreloadConfig(dev->regs, TIM_OCPreload_Enable);					
			break;
		case 4:
			TIM_SelectOCxM(dev->regs, TIM_Channel_4, TIM_OCMode_PWM1);
			TIM_OC4PreloadConfig(dev->regs, TIM_OCPreload_Enable);					
			break;
    }
    
    timer_cc_enable(dev, channel);
}
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);
}
Пример #13
0
//----------------------------------------------------------
//TIM PWMモード時のパルス幅変更
void Tim_pulse_set(TIM_TypeDef *tim,int ch,unsigned int pulse){
	TIM_OCInitTypeDef TIM_OCInitStructure;					//TIMを設定するための構造体

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

	if((ch&TIM_CH1) != 0){
		TIM_OCInitStructure.TIM_Pulse = pulse;
		TIM_OC1Init(tim,&TIM_OCInitStructure);
		TIM_OC1PreloadConfig(tim,TIM_OCPreload_Disable);
	}
	if((ch&TIM_CH2) != 0){
		TIM_OCInitStructure.TIM_Pulse = pulse;
		TIM_OC2Init(tim,&TIM_OCInitStructure);
		TIM_OC2PreloadConfig(tim,TIM_OCPreload_Disable);
	}
	if((ch&TIM_CH3) != 0){
		TIM_OCInitStructure.TIM_Pulse = pulse;
		TIM_OC3Init(tim,&TIM_OCInitStructure);
		TIM_OC3PreloadConfig(tim,TIM_OCPreload_Disable);
	}
	if((ch&TIM_CH4) != 0){
		TIM_OCInitStructure.TIM_Pulse = pulse;
		TIM_OC4Init(tim,&TIM_OCInitStructure);
		TIM_OC4PreloadConfig(tim,TIM_OCPreload_Disable);
	}
	TIM_Cmd(tim,ENABLE);
	TIM_ARRPreloadConfig(tim,ENABLE);
}
Пример #14
0
void klPwmChannel_t::Init(TIM_TypeDef* PTimer, uint16_t ATopValue, uint32_t AFreqHz, uint8_t ANumber, uint16_t APolarity) {
    klTimer_t::Init(PTimer, ATopValue, AFreqHz);
    INumber = ANumber;

    TIM_OCInitTypeDef  TIM_OCInitStructure;
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OCInitStructure.TIM_OCPolarity = APolarity;
    switch (ANumber) {
        case 1:
            TIM_OC1Init(PTimer, &TIM_OCInitStructure);
            TIM_OC1PreloadConfig(PTimer, TIM_OCPreload_Enable);
            break;
        case 2:
            TIM_OC2Init(PTimer, &TIM_OCInitStructure);
            TIM_OC2PreloadConfig(PTimer, TIM_OCPreload_Enable);
            break;
        case 3:
            TIM_OC3Init(PTimer, &TIM_OCInitStructure);
            TIM_OC3PreloadConfig(PTimer, TIM_OCPreload_Enable);
            break;
        case 4:
            TIM_OC4Init(PTimer, &TIM_OCInitStructure);
            TIM_OC4PreloadConfig(PTimer, TIM_OCPreload_Enable);
            break;
        default: break;
    }
    TIM_ARRPreloadConfig(PTimer, ENABLE);   // Enable autoreload of preload
    klTimer_t::Enable();
}
Пример #15
0
/**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);
}
void modbus_rtu_driver__delay_timer__init(void) {
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;

    TIM_TimeBaseStructure.TIM_Period = 65535;
    TIM_TimeBaseStructure.TIM_Prescaler = MODBUS_RTU_DRIVER__DELAY_TIMER__PRESCALER;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = MODBUS_RTU_DRIVER__DELAY_TIMER__T15_VALUE;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM2, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = MODBUS_RTU_DRIVER__DELAY_TIMER__T35_VALUE;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(TIM2, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);

    TIM_ARRPreloadConfig(TIM2, ENABLE);
    /* TIM IT enable */
    TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
    /* TIM2 enable counter */
    TIM_Cmd(TIM2, ENABLE);
}
void MyMotor_TIM2_PWM_Config(void)
{
	TIM_TimeBaseInitTypeDef TIM_IS;
	TIM_OCInitTypeDef TIM_OC_IS;
	TIM_TimeBaseStructInit(&TIM_IS);
	TIM_OCStructInit(&TIM_OC_IS);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

	TIM_IS.TIM_Period = 2000;
	TIM_IS.TIM_Prescaler = 720;//PWM周期20ms 50Hz
	TIM_IS.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_IS.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM2,&TIM_IS);

	TIM_OC_IS.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OC_IS.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OC_IS.TIM_OCPolarity = TIM_OCPolarity_High;
	
	TIM_OC_IS.TIM_Pulse = THROTTLE_BASE;
	TIM_OC1Init(TIM2,&TIM_OC_IS);
	TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
	
	TIM_OC_IS.TIM_Pulse = THROTTLE_BASE;
	TIM_OC2Init(TIM2,&TIM_OC_IS);
	TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
	
	TIM_OC_IS.TIM_Pulse = THROTTLE_BASE;
	TIM_OC3Init(TIM2,&TIM_OC_IS);
	TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
	
	TIM_OC_IS.TIM_Pulse = THROTTLE_BASE;
	TIM_OC4Init(TIM2,&TIM_OC_IS);
	TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
}
Пример #18
0
static void pwmOCConfig(TIM_TypeDef *tim, uint8_t channel, uint16_t value)
{
    TIM_OCInitTypeDef  TIM_OCInitStructure;

    TIM_OCStructInit(&TIM_OCInitStructure);
    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 = value;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

    switch (channel) {
        case TIM_Channel_1:
            TIM_OC1Init(tim, &TIM_OCInitStructure);
            TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
            break;
        case TIM_Channel_2:
            TIM_OC2Init(tim, &TIM_OCInitStructure);
            TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable);
            break;
        case TIM_Channel_3:
            TIM_OC3Init(tim, &TIM_OCInitStructure);
            TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
            break;
        case TIM_Channel_4:
            TIM_OC4Init(tim, &TIM_OCInitStructure);
            TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
            break;
    }
}
Пример #19
0
void pwm_config(uint32_t freq)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
	gpio_af_pp_up_init(GPIOE, GPIO_Pin_5|GPIO_Pin_6);

	GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_TIM9);
	GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_TIM9); 
	
	TIM_TimeBaseStructure.TIM_Period = 1000-1;
	TIM_TimeBaseStructure.TIM_Prescaler = 84000000/1000/freq-1;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);
	
	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 = 0;
	TIM_OC1Init(TIM9, &TIM_OCInitStructure);
	
	TIM_OCInitStructure.TIM_Pulse = 0;
	TIM_OC2Init(TIM9, &TIM_OCInitStructure);
	
	TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Enable);
	TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable);
	
	TIM_ARRPreloadConfig(TIM9, ENABLE);
	TIM_Cmd(TIM9, ENABLE);
}
Пример #20
0
static void TIMx_Config(TIM_TypeDef* TIMx, uint16_t period, uint16_t prescaler)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 

	/* Time base configuration */		 
	TIM_TimeBaseStructure.TIM_Period = period;
	TIM_TimeBaseStructure.TIM_Prescaler = prescaler;	        //设置预分频

	TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);

	TIM_OC1PreloadConfig(TIMx, TIM_OCPreload_Enable);

	TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);

	TIM_OC3PreloadConfig(TIMx, TIM_OCPreload_Enable);

	TIM_OC4PreloadConfig(TIMx, TIM_OCPreload_Enable);

	TIM_ARRPreloadConfig(TIMx, ENABLE);

	TIM_ITConfig(TIMx, TIM_IT_Update, DISABLE);

}
Пример #21
0
void motor_pwm_init(int period)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12, ENABLE);

    TIM_TimeBaseInitTypeDef timerInitStructure;
    timerInitStructure.TIM_Prescaler = 2;
    timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    timerInitStructure.TIM_Period = period;
    timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    timerInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM12, &timerInitStructure);
    TIM_Cmd(TIM12, ENABLE);

	TIM_OCInitTypeDef outputChannelInit = {0,};
	outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
	outputChannelInit.TIM_Pulse = 500;
	outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
	outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;

	TIM_OC1Init(TIM12, &outputChannelInit);
	TIM_OC1PreloadConfig(TIM12, TIM_OCPreload_Enable);

	outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
	outputChannelInit.TIM_Pulse = 500;
	outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
	outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;

	TIM_OC2Init(TIM12, &outputChannelInit);
	TIM_OC2PreloadConfig(TIM12, TIM_OCPreload_Enable);
			GPIO_PinAFConfig(MTR_L_PWM_GPIO_PORT, MTR_L_PWM_SOURCE, MTR_L_PWM_AF);
      		GPIO_PinAFConfig(MTR_R_PWM_GPIO_PORT, MTR_R_PWM_SOURCE, MTR_R_PWM_AF);

}
Пример #22
0
void pwm_out_mode_config()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;

	TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD;
	TIM_TimeBaseStructure.TIM_Prescaler = TIM_PRESCALER;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	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 = TIM_PULSE;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

	//output Channel1
	TIM_OC1Init(TIM4, &TIM_OCInitStructure);
	TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
	//output Channel2
	TIM_OC2Init(TIM4, &TIM_OCInitStructure);
	TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
	//output Channel3
	TIM_OC3Init(TIM4, &TIM_OCInitStructure);
	TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
	//output Channel4
	TIM_OC4Init(TIM4, &TIM_OCInitStructure);
	TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
	//enable TIM4
	TIM_ARRPreloadConfig(TIM4, ENABLE);
	TIM_Cmd(TIM4, ENABLE);
}
Пример #23
0
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);
}
Пример #24
0
void TIM4_PWM_Init(u16 arr, u16 psc)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    static TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    static TIM_OCInitTypeDef  TIM_OCInitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);   //使能定时器3时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟
 
   	GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_TIM4); //GPIOA0复用为定时器2
	  GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_TIM4); //GPIOA0复用为定时器2
   	GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4); //GPIOA0复用为定时器2
	  GPIO_PinAFConfig(GPIOD,GPIO_PinSource13,GPIO_AF_TIM4); //GPIOA0复用为定时器2
	
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽复用输出
	  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //上拉
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽复用输出
	  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //上拉
    GPIO_Init(GPIOD, &GPIO_InitStructure);
  
    //初始化TIM4
    TIM_TimeBaseStructure.TIM_Period = arr - 1;    //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
    TIM_TimeBaseStructure.TIM_Prescaler = psc - 1; //设置用来作为TIMx时钟频率除数的预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x00;//设置时钟分割:TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;          //循环计数次数值
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位

    //初始化TIM4  PWM模式
    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_Low;       //输出极性
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;     //打开空闲状态选择关闭

    TIM_CtrlPWMOutputs(TIM4, ENABLE); //使能定时器8的PWM输出
		
    TIM_OC1Init(TIM4, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
		
		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);

    TIM_Cmd(TIM4, ENABLE);  //使能TIM4

}
Пример #25
0
/*
 * 函数名:TIM4_Mode_Config
 * 描述  :配置TIM4输出的PWM信号的模式,如周期、极性、占空比
 * 输入  :无
 * 输出  :12us高电平,周期5.069ms
 * 调用  :内部调用
 */
void TIM4_Trig_Config(void)
{
	GPIO_InitTypeDef 					 GPIO_InitStructure;	
	TIM_TimeBaseInitTypeDef  	 TIM_TimeBaseStructure;
	TIM_OCInitTypeDef 				 TIM_OCInitStructure;
	
	/* PWM信号电平跳变值 */
	u16 CCR2_Val = 12;    
	
	/* TIM3 clock enable */
	//PCLK1经过2倍频后作为TIM3的时钟源等于72MHz
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); 

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 

  /*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;		    // 复用推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);    

/* -----------------------------------------------------------------------
    TIM4 Configuration: generate 4 PWM signals with 4 different duty cycles:
    TIM4CLK = 72 MHz, Prescaler = 0x0, TIM3 counter clock = 72 MHz
    TIM4 ARR Register = 999 => TIM4 Frequency = TIM4 counter clock/(ARR + 1)
    TIM4 Frequency = 72 KHz.
    TIM4 Channel1 duty cycle = (TIM4_CCR1/ TIM4_ARR)* 100 = 50%
    TIM4 Channel2 duty cycle = (TIM4_CCR2/ TIM4_ARR)* 100 = 37.5%
    TIM4 Channel3 duty cycle = (TIM4_CCR3/ TIM4_ARR)* 100 = 25%
    TIM4 Channel4 duty cycle = (TIM4_CCR4/ TIM4_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */

  /* Time base configuration */		 
  TIM_TimeBaseStructure.TIM_Period = 5000-1;       //设置脉冲周期 = Prescaler刻度 * Period
  TIM_TimeBaseStructure.TIM_Prescaler = 72;	    //设置预分频:72分频,也就是计时刻度72Mhz/72
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;	//设置时钟分频系数:不分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;	    //配置为PWM模式1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;	   //设置跳变值,当计数器计数到这个值时,电平发生跳变
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //当定时器计数值小于CCR1_Val时为高电平

  TIM_OC2Init(TIM4, &TIM_OCInitStructure);	 //使能通道2

  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);


  TIM_ARRPreloadConfig(TIM4, ENABLE);			 // 使能TIM4重载寄存器ARR

  /* TIM4 enable counter */
  TIM_Cmd(TIM4, ENABLE);                   //使能定时器4	
}
/*====================================================================================================*/
static void ILI9341_LigConfig( void )
{
  GPIO_InitTypeDef GPIO_InitStruct;

#ifdef BLIGHT_PWM
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
  TIM_OCInitTypeDef TIM_OCInitStruct;

  /* PWM Clk Init *************************************************************/
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  RCC_APB2PeriphClockCmd(LCD_LIG_GPIO_CLK, ENABLE);

  /* LIG PA1  */
  GPIO_InitStruct.GPIO_Pin = LCD_LIG_PIN;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(LCD_LIG_GPIO_PORT, &GPIO_InitStruct);

  TIM_DeInit(TIM2);

  /************************** PWM Output **************************************/
  /* 設定 TIM2 Time Base */
  TIM_TimeBaseStruct.TIM_Period = (uint16_t)(256-1);          // Set Period
  TIM_TimeBaseStruct.TIM_Prescaler = (uint16_t)(0);           // Set Freq
  TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;    // Count Up
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStruct);

  /* 設定 TIM2 OC2 */
  TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;              // PWM1 Mode
  TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;  // Enable OC
  TIM_OCInitStruct.TIM_Pulse = 0;                             // Period Pulse
  TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;      // 當計數值小於 PWM_MOTOR_MIN 時為高電平
  TIM_OC2Init(TIM2, &TIM_OCInitStruct);                       // 初始化 TIM2 OC2
  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);           // Enable TIM2 OC2 Preload

  /* 啟動 */
  TIM_ARRPreloadConfig(TIM2, ENABLE);                         // Enable TIM2 ARR Preload
  TIM_Cmd(TIM2, ENABLE);                                      // Enable TIM2

  LCD_LIG = BLIGHT_MIN;

#else
  /* GPIO Clk ******************************************************************/
  LCD_LIG_GPIO_CLK_ENABLE();

  /* SPI Pin ******************************************************************/
  GPIO_InitStruct.Mode      = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pin       = LCD_LIG_PIN;
  HAL_GPIO_Init(LCD_LIG_GPIO_PORT, &GPIO_InitStruct);

  LCD_LIG_L;

#endif
}
Пример #27
0
/**
 * @brief   Configure TIM4
 * @param   None
 * @retval  None
 */
void TIM_Configuration(void)
{
	/* -----------------------------------------------------------------------
	TIM4 Configuration: generate 4 PWM signals with 4 different duty cycles:
	TIM4CLK = 72 MHz, Prescaler = 18, TIM4 counter clock = 4 MHz
	TIM4 ARR Register = 3999 => TIM4 Frequency = TIM4 counter clock/(ARR + 1)
	TIM4 Frequency = 1 KHz.
	----------------------------------------------------------------------- */

	/* Time base configuration */
	TIM_TimeBaseStructure.TIM_Period = 3999;
	TIM_TimeBaseStructure.TIM_Prescaler = 18;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

	/* PWM1 Mode configuration: Channel1 */
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 4000;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

	TIM_OC1Init(TIM4, &TIM_OCInitStructure);

	TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

	/* PWM1 Mode configuration: Channel2 */
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 4000;

	TIM_OC2Init(TIM4, &TIM_OCInitStructure);

	TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);

	/* PWM1 Mode configuration: Channel3 */
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 4000;

	TIM_OC3Init(TIM4, &TIM_OCInitStructure);

	TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);

	/* PWM1 Mode configuration: Channel4 */
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 4000;

	TIM_OC4Init(TIM4, &TIM_OCInitStructure);

	TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);

	TIM_ARRPreloadConfig(TIM4, ENABLE);

	/* TIM4 enable counter */
	TIM_Cmd(TIM4, ENABLE);
}
Пример #28
0
RCOUT::RCOUT()
{
	memset(channel_datas, 0, sizeof(channel_datas));
	GPIO_InitTypeDef GPIO_InitStructure = {0};
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	
	// open B0-1 as output (TIM3 channel 0~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_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	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_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	// open C6-7 as output (TIM3 channel 3~4)
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
	
	// Time base configuration
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	TIM_TimeBaseStructure.TIM_Period = 2000*OC-1;
	TIM_TimeBaseStructure.TIM_Prescaler = 84/OC-1;
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

	TIM_Cmd(TIM3, ENABLE);

	TIM_ARRPreloadConfig(TIM3, ENABLE);

	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);


	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 = 0;
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);
	TIM_OC3Init(TIM3, &TIM_OCInitStructure);
	TIM_OC4Init(TIM3, &TIM_OCInitStructure);
}
Пример #29
0
void MotorInit_1(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_APB2Periph_TIM1 ,ENABLE);   //打开定时器1时钟
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //打开外设A的时钟和复用时钟
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 ,ENABLE);   //打开定时器4时钟  
    
    
    // 设置GPIO功能。
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    
    // 复位定时器。
    TIM_DeInit(TIM4);
    
    // 配置计时器。
    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(TIM1,&TIM_TimeBaseStructure);
    
    
    // 配置TIM1为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(TIM1,&TIM_OCInitStructure);
    TIM_OC2Init(TIM4,&TIM_OCInitStructure);
    TIM_OC3Init(TIM4,&TIM_OCInitStructure);
    TIM_OC4Init(TIM4,&TIM_OCInitStructure);
    
    //TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
    TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
    TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
    
    // 启动计时器。
    TIM_Cmd(TIM4,ENABLE);
    printf("TIM4 enable done...\r\n");
}
Пример #30
0
void Tim8_Configuration(void)
{

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
	u16 CCR1_Val = 75;      //前面的电机
 	u16 CCR2_Val = 75;	  	 //后面的电机
 	u16 CCR3_Val = 75;		 //左面的电机
 	u16 CCR4_Val = 75;		 //右面的电机
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);

     TIM_DeInit(TIM8); //重设为缺省值

	/*TIM1时钟配置*/
	TIM_TimeBaseStructure.TIM_Prescaler = 1439;      //预分频(时钟分频)72M/4000=18K
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseStructure.TIM_Period = 999;       //装载值 18k/144=125hz 就是说向上加的144便满了
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;   //设置了时钟分割 不懂得不管
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;    //周期计数器值 不懂得不管
	TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure);    //初始化TIMx的时间基数单位
	
	/* Channel 1 Configuration in PWM mode 通道一的PWM */
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;      //PWM模式2
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效 PA8 
	//TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //反向通道也有效 PB13
	TIM_OCInitStructure.TIM_Pulse = CCR1_Val;         //占空时间 144 中有40的时间为高,互补的输出正好相反
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性
	//TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;    //互补端的极性 
	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //空闲状态下的非工作状态 不管
	//TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //先不管
	
	TIM_OC1Init(TIM8,&TIM_OCInitStructure);       //数初始化外设TIMx通道1这里2.0库为TIM_OCInit
	
	  /* PWM1 Mode configuration: Channel2 */
	  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;	  //设置通道2的电平跳变值,输出另外一个占空比的PWM
	  TIM_OC2Init(TIM8, &TIM_OCInitStructure);	  //使能通道2
	  TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
	
	  /* PWM1 Mode configuration: Channel3 */
	  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;	//设置通道3的电平跳变值,输出另外一个占空比的PWM
	  TIM_OC3Init(TIM8, &TIM_OCInitStructure);	 //使能通道3
	  TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
	
	  /* PWM1 Mode configuration: Channel4 */
	  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;	//设置通道4的电平跳变值,输出另外一个占空比的PWM
	  TIM_OC4Init(TIM8, &TIM_OCInitStructure);	//使能通道4
	  TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);
	/* TIM1 counter enable开定时器 */
	TIM_Cmd(TIM8,ENABLE);
	
	/* TIM1 Main Output Enable 使能TIM1外设的主输出*/
	TIM_CtrlPWMOutputs(TIM8,ENABLE);
}