#include "Timer.h"
#include "led.h" 
#include "key.h"
#include "adc.h"
#include "Cyton_SysApi.h"



//高级 TIM1 TIM8
//通用 TIM2 TIM3 TIM4 TIM5
//基本 TIM6 TIM7

//C8T6 只有 1-4 4个定时器
u32 Tim1_flag;
void TIM1_Init(u16 arr,u16 psc)
{

		TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
		NVIC_InitTypeDef NVIC_InitStructure;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
	
		TIM_TimeBaseStructure.TIM_Period=arr-1; // 自动重装载寄存器的值
		TIM_TimeBaseStructure.TIM_Prescaler=psc-1; // 时钟预分频数
		TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // 采样分频
		TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数
		TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复寄存器，用于自动更新pwm占空比
		TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
	
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //设置中断分组
		NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;  //TIM中断
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //从优先级
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
		NVIC_Init(&NVIC_InitStructure); 
		
		TIM_ClearITPendingBit(TIM1, TIM_IT_Update|TIM_IT_Trigger); //清除更新中断请求位
		TIM_ITConfig(TIM1, TIM_IT_Update|TIM_IT_Trigger, ENABLE );//允许更新										
		TIM_Cmd(TIM1, DISABLE); 
}

void TIM1_Start()
{
		TIM_Cmd(TIM1, ENABLE); 
}

void TIM1_Stop()
{
		TIM_Cmd(TIM1, DISABLE); 
}

//定时器中断服务程序	 
void TIM1_UP_IRQHandler(void)
{ 
		if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)  //更新中断 
		{ 			
				TIM_ClearITPendingBit(TIM1, TIM_IT_Update);//清中断标志
				Tim1_flag++;
				CytonSys_CollectorCB();
		}		
}


//定时器初始化
void TIM2_Init(u16 arr,u16 psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
		NVIC_InitTypeDef NVIC_InitStructure;

		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //TIM5时钟使能 
	 
		TIM_TimeBaseStructure.TIM_Period = arr-1;; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 
		TIM_TimeBaseStructure.TIM_Prescaler =psc-1; //设置用来作为TIMx时钟频率除数的预分频值 
		TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
		TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
	 
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //设置中断分组
		NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  //TIM中断
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //从优先级
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
		NVIC_Init(&NVIC_InitStructure); 		
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除更新中断请求位
		TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE );//允许更新	
		TIM_Cmd(TIM2, ENABLE); 
}


u8 TIM2_FLAG=0;
//定时器中断服务程序	 
void TIM2_IRQHandler(void)
{ 
		if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)  //更新中断 
		{ 			
				TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清中断标志
				TIM2_FLAG++;
				if(TIM2_FLAG == 1)
				{
						if(KEY_STATE&KEY_PWR && KEYPWR == 0)
						{
								KEY_TYPE = KEY_PWR;
						}
				}
				else if(TIM2_FLAG==5)
				{
						TIM2_FLAG=0;
						TIM_Cmd(TIM2, DISABLE); 
						TIM2->CNT=0;
						KEY_STATE=0;
				}
		}		
}



//定时器初始化
//按键检测
void TIM3_Init(u16 arr,u16 psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
		NVIC_InitTypeDef NVIC_InitStructure;

		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能 
	 
		TIM_TimeBaseStructure.TIM_Period = arr-1;; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 
		TIM_TimeBaseStructure.TIM_Prescaler =psc-1; //设置用来作为TIMx时钟频率除数的预分频值 
		TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
	 
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //设置中断分组
		NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM中断
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //从优先级
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
		NVIC_Init(&NVIC_InitStructure); 
		
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除更新中断请求位
		TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE );//允许更新										
		TIM_Cmd(TIM3, DISABLE); 
}

//定时器中断服务程序	 
void TIM3_IRQHandler(void)
{ 
		if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //更新中断 
		{ 
				TIM_ClearITPendingBit(TIM3, TIM_IT_Update);//清中断标志
			
				TIM_Cmd(TIM3, DISABLE); 
				TIM3->CNT=KEYDOWNLONGTIME; //长按键
		}				   
}




//定时器初始化
void TIM4_Init(u16 arr,u16 psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
		NVIC_InitTypeDef NVIC_InitStructure;

		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能 
	 
		TIM_TimeBaseStructure.TIM_Period = arr-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 
		TIM_TimeBaseStructure.TIM_Prescaler =psc-1; //设置用来作为TIMx时钟频率除数的预分频值 
		TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
		TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
	 
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //设置中断分组
		NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //TIM中断
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //从优先级
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
		NVIC_Init(&NVIC_InitStructure); 
		
		TIM_ClearITPendingBit(TIM4, TIM_IT_Update); //清除更新中断请求位
		TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE );//允许更新										
		TIM_Cmd(TIM4, ENABLE); 
}

u8 TIM4_Timing=0;
//定时器中断服务程序	 
void TIM4_IRQHandler(void)
{ 
    //更新中断
    if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)   //更新中断
    {  
				TIM4_Timing++;	
				if(lowpower==0) //低电量指示
				{
						if(	power_detective==1)//开启电量监测
						{
								BAT_Percent= ((ADCConvertedValue[0]-BAT_0)*100/BAT_LEVEL);
								//Main_printf("%d %d mV %d %%\r\n",ADCConvertedValue[0],ADCConvertedValue[0]*3300/4096*2,BAT_Percent);
								if(BAT_Percent<10)//低电量
								{
										lowpower=1;
										TIM4_Init(1000,7200);//系统指示灯+电量监测		
										DS2=LED_ON;							
								}
						}
				}
			
				DS1=!DS1;	
				TIM_ClearITPendingBit(TIM4, TIM_IT_Update);//清中断标志
				
    }     				   
}




/*先说TIM_ARRPreloadConfig设置为DISABLE 和ENABLE的问题，他的作用只是允许或禁止在定时器工作时
向ARR的缓冲器中写入新值，以便在更新事件发生时载入覆盖以前的值。在开始初始化的时候你已经把
TIM_TimeBaseStructure.TIM_Period=2000;   //ARR的值
后来也一直是这个值，原因是你没有编写中断服务函数或者你在中断服务函数中根本就没有给ARR缓冲器重新
写入新值，所以设置为DISABLE 和ENABLE都没有影响。*/
//TIM_ARRPreloadConfig(TIM3, ENABLE);   //使能TIMx在ARR上的预装载寄存器	


//如：关掉TIM3_CH2的PWM输出：TIM3->CCER&=0xFFEF;	关闭 PWM输出
//TIM_OC1NPolarityConfig(TIM1,TIM_OCNPolarity_Low); //关闭输出后，再强制输出电平




//		TIM3->CNT=0;//清空定时器计数器
//		TIM3->ARR=arr;  	//设定计数器自动重装值//刚好1ms    
//		TIM3->PSC=psc;  	//预分频器7200,得到10Khz的计数时钟		  
//		TIM3->DIER|=1<<0;   //允许更新中断	  
//		TIM3->CR1|=0x01;    //使能定时器3

 