#include "main.h"
/*************************************************/
/****************PWM输出配置**********************/
/*****TIM4:PB6(CH1),PB7(CH2),PB8(CH3),PB9(CH4)****/
/*************************************************/

/*
 * 函数名：TIM4_GPIO_Config
 * 描述  ：配置TIM4输出的PWM信号的IO口
 * 输入  ：无
 * 输出  ：无
 * 调用  ：内部调用
 */
static void TIM4_GPIO_Config(void) 
{
  GPIO_InitTypeDef GPIO_InitStructure;

	/* TIM3 clock enable */
	//PCLK1经过2倍频后作为TIM4的时钟源等于72MHz
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); 

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

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;		    
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  /*GPIOB Configuration: TIM4 channel 1,2 and 3,4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;

  GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/*
 * 函数名：TIM4_Mode_Config
 * 描述  ：配置TIM4输出的PWM信号的模式，如周期、极性、占空比
 * 输入  ：无
 * 输出  ：无
 * 调用  ：内部调用
 */
static void TIM4_Mode_Config(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	/* PWM信号电平跳变值 */
	u16 CCR1_Val = 0;	//499
	u16 CCR2_Val = 0;
	u16 CCR3_Val = 0;
	u16 CCR4_Val = 0;
/* -----------------------------------------------------------------------
    TIM4 Configuration:PWM signals with different duty cycles:
    TIM4CLK = 72 MHz, Prescaler = 1440-1, TIM4 counter clock = 50 KHz
    TIM4 ARR Register = 999 => TIM3 Frequency = TIM4 counter clock/(ARR + 1)
    TIM4 Frequency = 50 Hz.
    TIM4 Channel1 duty cycle = (TIM4_CCR1/ TIM4_ARR) * %100 = 50%
  ----------------------------------------------------------------------- */

  /* Time base configuration */		 
  TIM_TimeBaseStructure.TIM_Period = 999;       							//当定时器从0计数到999，即为1000次，为一个定时周期
  TIM_TimeBaseStructure.TIM_Prescaler = 1439;	    						//设置预分频：1440
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;		//设置时钟分频系数：不分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* PWM Mode configuration*/
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;	    			//配置为PWM模式1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;  	//当定时器计数值小于CCR1_Val时为低电平
  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;										//设置通道1的电平跳变值，输出另外一个占空比的PWM

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);	 									//使能通道1

  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

  /* PWM2 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;										//设置通道2的电平跳变值，输出另外一个占空比的PWM

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

  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);

  /* PWM3 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;										//设置通道3的电平跳变值，输出另外一个占空比的PWM

  TIM_OC3Init(TIM4, &TIM_OCInitStructure);										//使能通道3

  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
	
	/* PWM4 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;										//设置通道4的电平跳变值，输出另外一个占空比的PWM

  TIM_OC4Init(TIM4, &TIM_OCInitStructure);										//使能通道4

  TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);

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

  /* TIM4 enable counter */
  TIM_Cmd(TIM4, ENABLE);                   										//使能定时器4
}

/*
 * 函数名：TIM4_PWM_Init
 * 描述  ：TIM4 输出PWM信号初始化
 * 输入  ：无
 * 输出  ：无
 * 调用  ：外部调用
 */
void TIM4_PWM_Init(void)
{
	TIM4_GPIO_Config();
	TIM4_Mode_Config();	
}

/*
 * 函数名：TIM4ChannelxDuty
 * 描述  ：改变TIM4输出通道的PWM占空比
 * 输入  ：	Channelx:通道1，2，3，4
						Duty ：占空比值（0，999）
 * 输出  ：无
 * 调用  ：内部调用
 */
void TIM4ChannelxDuty(uint8_t Channelx, uint16_t Duty)
{	
	switch(Channelx)
	{
		case 1: TIM4->CCR1 = Duty; break;
		case 2: TIM4->CCR2 = Duty; break;
		case 3: TIM4->CCR3 = Duty; break;
		case 4: TIM4->CCR4 = Duty; break;
		default: break;		
	}
}

