#include "timer.h"
#include "led.h"
//////////////////////////////////////////////////////////////////////////////////	 
//本程序只供学习使用，未经作者许可，不得用于其它任何用途
//ALIENTEK STM32F407开发板
//定时器 驱动代码	   
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//创建日期:2014/5/4
//版本：V1.0
//版权所有，盗版必究。
//Copyright(C) 广州市星翼电子科技有限公司 2014-2024
//All rights reserved									  
////////////////////////////////////////////////////////////////////////////////// 	 

void TIM1_PWM_Init(u16 arr,u16 psc)
{
       GPIO_InitTypeDef GPIO_InitStructure;  
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);  ///使能TIM1时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);

       GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1); 

	 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;           
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
	GPIO_Init(GPIOE,&GPIO_InitStructure);            

        GPIO_ResetBits(GPIOE, GPIO_Pin_9);
	
  TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //定时器分频
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);//初始化TIM3
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
	TIM_OCInitStructure.TIM_Pulse = 100;
	
	TIM_OC1Init(TIM1, &TIM_OCInitStructure);  
	
	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  
	
       TIM_ARRPreloadConfig(TIM1,ENABLE); 
	
	//TIM_Cmd(TIM3, ENABLE); 
	
}

//定时器3中断服务函数
void TIM1_IRQHandler(void)
{
}



void TIM2_Int_Init(u16 arr,u16 psc)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);  ///使能TIM3时钟
	
  TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //定时器分频
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//初始化TIM3
	
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //允许定时器3更新中断
	TIM_Cmd(TIM2,ENABLE); //使能定时器3
	
	NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; //定时器3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
}

u8 led_flag=0;
u16 index=0;

extern u16 real_dis_data[6];
extern u16 real_dis_data_bak[6];
extern u8 double_buffer_flag;

//定时器3中断服务函数
void TIM2_IRQHandler(void)
{
       char vcm_cmd[16]; 
	if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) //溢出中断
	{

#if 1           
                 if(led_flag == 0)
                  {
                        GPIO_SetBits(GPIOE, GPIO_Pin_0);
                        led_flag = 1;
			TIM_ClearITPendingBit(TIM2,TIM_IT_Update);  //清除中断标志位			
			return;			
                  }
                  if(led_flag == 1)
                  {
                        GPIO_ResetBits(GPIOE, GPIO_Pin_0);
                        led_flag = 0;
			TIM_ClearITPendingBit(TIM2,TIM_IT_Update);  //清除中断标志位			
			return;			
                  }
#endif
#if 0
                     sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data[index]);
                     usart_send(USART3, vcm_cmd);
		     usart_send(USART3, START_MACRO_NUM);
		     TIM_ClearITPendingBit(TIM2,TIM_IT_Update);  //清除中断标志位
		     return;
            
#endif
#if 0	
	        if(led_flag == 0)
	        {
                     sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data[index]);
                     usart_send(USART3, vcm_cmd);
			usart_send(USART3, START_MACRO_NUM);
			index++;
			TIM_ClearITPendingBit(TIM2,TIM_IT_Update);  //清除中断标志位
			return;
	        }
		 if(led_flag == 1)
	        {
                     sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data_bak[index]);
                     usart_send(USART3, vcm_cmd);
			usart_send(USART3, START_MACRO_NUM);
			TIM_ClearITPendingBit(TIM2,TIM_IT_Update);  //清除中断标志位
			return;
	        }	
		 if(index > 199 && led_flag == 0)
		 {
		        index = 0;
			 led_flag = 1;
                      double_buffer_flag = 0;
			 usart_send(USART1, "laishuju");
		 }
		 if(index > 199 && led_flag == 1)
		 {
		        index = 0;
			 led_flag = 0;
			 double_buffer_flag = 1;
			 usart_send(USART1, "laishuju");
		 }
#endif			
	}
	
}


void TIM2_PWM_Init(u16 arr,u16 psc)
{
       GPIO_InitTypeDef GPIO_InitStructure;  
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);  ///使能TIM1时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);

       GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_TIM1); 

	 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;           
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
	GPIO_Init(GPIOB,&GPIO_InitStructure);            

        GPIO_ResetBits(GPIOB, GPIO_Pin_9);
	
  TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //定时器分频
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//初始化TIM3
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
	TIM_OCInitStructure.TIM_Pulse = 100;
	
	TIM_OC1Init(TIM2, &TIM_OCInitStructure);  
	
	TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);  
	
       TIM_ARRPreloadConfig(TIM2,ENABLE); 
	
	//TIM_Cmd(TIM3, ENABLE); 
	
}



//通用定时器3中断初始化
//arr：自动重装值。
//psc：时钟预分频数
//定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us.
//Ft=定时器工作频率,单位:Mhz
//这里使用的是定时器3!
void TIM3_PWM_Init(u16 arr,u16 psc)
{
       GPIO_InitTypeDef GPIO_InitStructure;  
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  ///使能TIM3时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

       GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3); 

	 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;           
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
	GPIO_Init(GPIOA,&GPIO_InitStructure);            

        GPIO_ResetBits(GPIOA, GPIO_Pin_6);
	
  TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //定时器分频
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//初始化TIM3
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
	TIM_OCInitStructure.TIM_Pulse = 100;
	
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);  
	
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);  
	
       TIM_ARRPreloadConfig(TIM3,ENABLE); 
	
	//TIM_Cmd(TIM3, ENABLE); 
	
}

//定时器3中断服务函数
void TIM3_IRQHandler(void)
{
}

void TIM4_PWM_Init(u16 arr,u16 psc)
{
       GPIO_InitTypeDef GPIO_InitStructure;  
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);  ///使能TIM3时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);

       GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_TIM3); 

	 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;           
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
	GPIO_Init(GPIOD,&GPIO_InitStructure);            

        GPIO_ResetBits(GPIOD, GPIO_Pin_15);
	
  TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //定时器分频
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);//初始化TIM3
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
	TIM_OCInitStructure.TIM_Pulse = 100;
	
	TIM_OC1Init(TIM4, &TIM_OCInitStructure);  
	
	TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);  
	
       TIM_ARRPreloadConfig(TIM4,ENABLE); 
	
	//TIM_Cmd(TIM3, ENABLE); 
	
}

//定时器3中断服务函数
void TIM4_IRQHandler(void)
{
}

