#include "main.h"

u16 CCR1_Val = 10000;
u16 CCR2_Val = 1000;
u16 CCR3_Val = 100;
u16 CCR4_Val = 1;              

u16 Time2us = 0;
void TIM2_Configuration(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  	//TIM2 RCC
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);

	TIM_DeInit(TIM2);//使用缺省值初始化TIM外设寄存器
	TIM_TimeBaseStructure.TIM_Period=1;//自动重装载寄存器值为1
	TIM_TimeBaseStructure.TIM_Prescaler=(36-1);//时钟预分频数为36000
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//采样分频倍数1,未明该语句作用。
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//上升模式
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);	

	TIM_ClearFlag(TIM2,TIM_FLAG_Update);//清除更新标志位
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//使能中断
	TIM_Cmd(TIM2,ENABLE);//使能TIM2定时器

}

void TIM2_NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

	#ifdef	VECT_TAB_RAM
		NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
	#else
		NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
	#endif

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

}

void Tim2_delay_us(u16 us)
{
	Time2us = 0;
	while(Time2us < ((us+1)/2));	
}



void LED_GPIO_Configuration(void)
{
  	GPIO_InitTypeDef GPIO_InitStructure;
	
  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);

  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  	GPIO_Init(GPIOA , &GPIO_InitStructure); 
}


void RCC_Configuration(void)
{
	/* 定义枚举类型变量 HSEStartUpStatus */
	ErrorStatus HSEStartUpStatus;

  	/* 复位系统时钟设置*/
  	RCC_DeInit();
  	/* 开启HSE*/
  	RCC_HSEConfig(RCC_HSE_ON);
  	/* 等待HSE起振并稳定*/
  	HSEStartUpStatus = RCC_WaitForHSEStartUp();
	/* 判断HSE起是否振成功，是则进入if()内部 */
  	if(HSEStartUpStatus == SUCCESS)
  	{
    	/* 选择HCLK（AHB）时钟源为SYSCLK 1分频 */
    	RCC_HCLKConfig(RCC_SYSCLK_Div1); 
    	/* 选择PCLK2时钟源为 HCLK（AHB） 1分频 */
    	RCC_PCLK2Config(RCC_HCLK_Div1); 
    	/* 选择PCLK1时钟源为 HCLK（AHB） 2分频 */
    	RCC_PCLK1Config(RCC_HCLK_Div2);
    	/* 设置FLASH延时周期数为2 */
    	FLASH_SetLatency(FLASH_Latency_2);
    	/* 使能FLASH预取缓存 */
    	FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    	/* 选择锁相环（PLL）时钟源为HSE 1分频，倍频数为9，则PLL输出频率为 8MHz * 9 = 72MHz */
    	RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    	/* 使能PLL */ 
    	RCC_PLLCmd(ENABLE);
    	/* 等待PLL输出稳定 */
    	while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    	/* 选择SYSCLK时钟源为PLL */
    	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    	/* 等待PLL成为SYSCLK时钟源 */
    	while(RCC_GetSYSCLKSource() != 0x08);
  	} 
	
}


/*
#include "stm32f10x_it.h"

extern vu16 CCR1_Val;
extern vu16 CCR2_Val;
extern vu16 CCR3_Val;
extern vu16 CCR4_Val;



void TIM2_IRQHandler(void)
{
	vu16 capture=0;
	if(TIM_GetITStatus(TIM2,TIM_IT_CC1) != RESET)
	{
		GPIO_WriteBit(GPIOA , GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4))); 		
		capture = TIM_GetCapture1(TIM2);
		TIM_SetCompare1(TIM2,capture + CCR1_Val);
		TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);
	}
	else if(TIM_GetITStatus(TIM2,TIM_IT_CC2) != RESET)
	{
		GPIO_WriteBit(GPIOA , GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_5))); 		
		capture = TIM_GetCapture2(TIM2);
		TIM_SetCompare2(TIM2,capture + CCR2_Val);
		TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);
	}
	else if(TIM_GetITStatus(TIM2,TIM_IT_CC3) != RESET)
	{
		GPIO_WriteBit(GPIOA , GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_6))); 		
		capture = TIM_GetCapture3(TIM2);
		TIM_SetCompare3(TIM2,capture + CCR3_Val);
		TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);
	}
	else
	{
		GPIO_WriteBit(GPIOA , GPIO_Pin_7,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_7))); 		
		capture = TIM_GetCapture4(TIM2);
		TIM_SetCompare4(TIM2,capture + CCR4_Val);
		TIM_ClearITPendingBit(TIM2,TIM_IT_CC4);
	}
}

*/

