Example #1
0
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;
	}
}
Example #2
0
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);

}
Example #3
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);
}
Example #4
0
File: pwm.c Project: sedulity11/pdb
void pwmOCInit(const TIM_TypeDef *tim, uint16_t channel, uint32_t inititalValue) {
    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_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
    TIM_OCInitStructure.TIM_Pulse = inititalValue;

    switch (channel) {
	case TIM_Channel_1:
	    TIM_OC1Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);
	    break;
	case TIM_Channel_2:
	    TIM_OC2Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);
	    break;
	case TIM_Channel_3:
	    TIM_OC3Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);
	    break;
	case TIM_Channel_4:
	    TIM_OC4Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);
	    break;
    }
}
Example #5
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;
    }
}
Example #6
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);

}
Example #7
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);
}
Example #8
0
File: pwm.c Project: jasongwq/F4Fly
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

}
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);
}
Example #10
0
/****************************************************************************************
** 函数名称: LEDInit
** 功能描述: LED 指示灯管脚初始化函数
** 参           数: none
** 返   回  值: none
** 作      者: Sevent
** 日      期: 2013年08月13日
**---------------------------------------------------------------------------------------
** 修 改 人: 
** 日   期: 
**--------------------------------------------------------------------------------------
****************************************************************************************/
void LEDInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	uint16_t PrescalerValue;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	
	PrescalerValue = (uint16_t) (SystemCoreClock /2/10000) - 1;

	/*	配置PB7 为指示的LED	*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	/*	配置PC7 为指示的LED, 由TIMER8驱动	*/
	
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
  /* Configure green led as output */
  GPIO_InitStructure.GPIO_Pin = 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);
		
	/* Enable the TIM8 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);

  PrescalerValue = (uint16_t) (SystemCoreClock /2/10000) - 1;

  /* Time base configuration */
	TIM_DeInit(TIM3);
  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: Channel2 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 100;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);

    /* TIM enable counter */
  //TIM_Cmd(TIM3, ENABLE);

  /* TIM IT enable */
  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}
Example #11
0
/**
	* @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);
}
Example #12
0
File: rilma.cpp Project: Kreyl/nute
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
}
Example #13
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);
}
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);
}
Example #15
0
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);
}
Example #16
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);
}
Example #17
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 );
}
Example #18
0
/**
  * @brief  Initialize the timer channel linked to the servo in PWM mode.
  * @param  servo: pointer to a ausbeeServo structure that contains the
  *         configuration information for the specified servo.
  * @retval None
  */
void ausbeeInitServo(ausbeeServo* servo)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_TimeBaseStructure.TIM_Period = 1999;
  TIM_TimeBaseStructure.TIM_Prescaler = 812;
#ifdef SLOW_MCU
  TIM_TimeBaseStructure.TIM_Prescaler /= 5;
#endif
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(servo->TIMx, &TIM_TimeBaseStructure);


  TIM_OCInitTypeDef OCInit_PWM; // Create a OCInitTypeDef for PWM

  TIM_OCStructInit(&OCInit_PWM);
  OCInit_PWM.TIM_OCMode = TIM_OCMode_PWM1;  // Set PWM Mode
  OCInit_PWM.TIM_Pulse = 1500;                // Duty cycle to 5%
  OCInit_PWM.TIM_OutputState = TIM_OutputState_Enable;


  if(servo->CHANx == TIM_Channel_1)
    TIM_OC1Init(servo->TIMx, &OCInit_PWM); // Initialize OSC1
  else if(servo->CHANx == TIM_Channel_2)
    TIM_OC2Init(servo->TIMx, &OCInit_PWM); // Initialize OSC2
  else if(servo->CHANx == TIM_Channel_3)
    TIM_OC3Init(servo->TIMx, &OCInit_PWM); // Initialize OSC3
  else if(servo->CHANx == TIM_Channel_4)
    TIM_OC4Init(servo->TIMx, &OCInit_PWM); // Initialize OSC4
}
Example #19
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);
}
Example #20
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上的预装载寄存器
		}
	}
}
Example #21
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);
}
Example #22
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);
}
Example #23
0
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
}
Example #24
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);
}
Example #25
0
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);





}
Example #26
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);
	}
}
Example #27
0
/**
  * @brief  configure the TIM4 for PWM mode
  * @param  None
  * @retval None
  */
void TIM_Configuration(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_OCInitTypeDef TIM_OCInitStruct;

    // Let PWM frequency equal 100Hz.
    // Let period equal 1000. Therefore, timer runs from zero to 1000. Gives 0.1Hz resolution.
    // Solving for prescaler gives 240.
    TIM_TimeBaseStructInit( &TIM_TimeBaseInitStruct );
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4;
    TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 72 - 1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStruct );

    TIM_OCStructInit( &TIM_OCInitStruct );
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;

    // Initial duty cycle equals 0%. Value can range from zero to 65535.
    //TIM_Pulse = TIM4_CCR1 register (16 bits)
    TIM_OCInitStruct.TIM_Pulse = 100 - 1; //(0=Always Off, 65535=Always On)

    TIM_OC1Init( TIM3, &TIM_OCInitStruct ); // Channel 1  LED
    TIM_OC2Init( TIM3, &TIM_OCInitStruct ); // Channel 2  LED
    TIM_OC3Init( TIM3, &TIM_OCInitStruct ); // Channel 3  LED
    TIM_OC4Init( TIM3, &TIM_OCInitStruct ); // Channel 4  LED

    TIM_Cmd( TIM3, ENABLE );
}
void Initialise_TimerPWM()
{
	/*!----------------------------- Initialise Base Structures -----------------------------!*/

	TIM_TimeBaseInitTypeDef TIM4_INIT;
	TIM_OCInitTypeDef TIM4_OC;

	TIM4_INIT.TIM_Prescaler = 25;
	TIM4_INIT.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM4_INIT.TIM_CounterMode = TIM_CounterMode_Up;
	TIM4_INIT.TIM_Period = 255; //8 bit equivalent

	TIM_TimeBaseInit(TIM4,&TIM4_INIT);

	/*!----------------------------- Initialise Base Structures -----------------------------!*/

	/*!----------------------------- OC Mode -----------------------------!*/

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

	TIM_OC1Init(TIM4,&TIM4_OC);
	TIM_OC2Init(TIM4,&TIM4_OC);
	TIM_OC3Init(TIM4,&TIM4_OC);
	TIM_OC4Init(TIM4,&TIM4_OC);

	/*!----------------------------- OC Mode -----------------------------!*/

	/*!----------------------------- Enable Timer -----------------------------!*/

	TIM_Cmd(TIM4, ENABLE);

	/*!----------------------------- Enable Timer -----------------------------!*/
}
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);
}
Example #30
0
/**
 * @param pos Servo position.
 *         0   = Middle
 *        -127 = Max left
 *        +127 = max right
 */
void servo_set(int num, int8_t pos) {
    int wpulse;
    uint16_t pulse;

    TIM_OCInitTypeDef oc_init;

    oc_init = TIM_OC_CONFIG;

    wpulse = ((int)pos)*500/127 +PULSE_1_5_MS;
    pulse = MIN(PULSE_2_0_MS,wpulse);
    pulse = MAX(PULSE_1_0_MS,wpulse);
    oc_init.TIM_Pulse = pulse;

    switch( num ) {
    case 2:
        TIM_OC2Init(TIMER, &oc_init);
        break;
    case 1:
        TIM_OC3Init(TIMER, &oc_init);
        break;
    case 0:
        TIM_OC4Init(TIMER, &oc_init);
        break;
    default:
        assert_param(0);
    }
}