#include "stm32f10x.h" // Device header
#include "USART.h"
#include "rs485.h"
#include "iwdg.h"
extern u8 ins_1;
extern u16 white;
extern u16 tick;
extern u8 Feed_Dog_Count;
void 	PWM1_Init(uint16_t Per,uint16_t Psc)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;    //TIM1_CH1
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period=Per;      //ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler=Psc;    //PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);//给结构体赋初始值
	
	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;  //CCR的值
	TIM_OC1Init(TIM1,&TIM_OCInitStructure);//TIM_OC1Init()函数用来初始化输出比较单元
	
	TIM_CtrlPWMOutputs(TIM1,ENABLE);	//MOE 主输出使能	

	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1预装载使能	 
	
	TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
	
	TIM_Cmd(TIM1,ENABLE);//定时器使能
	
}

void 	PWM2_Init(uint16_t Per,uint16_t Psc)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);使用引脚复用功能
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;    //TIM1_CH4
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period=Per;      //ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler=Psc;    //PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);//给结构体赋初始值
	
	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;  //CCR的值
	TIM_OC4Init(TIM1,&TIM_OCInitStructure);//TIM_OC1Init()函数用来初始化输出比较单元
	
	TIM_CtrlPWMOutputs(TIM1,ENABLE);	//MOE 主输出使能	

	TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1预装载使能	 
	
	TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器

	
	TIM_Cmd(TIM1,ENABLE);//定时器使能
	
}



void 	PWM3_Init(uint16_t Per,uint16_t Psc)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;     //PA1 TIM2_CH2
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period=Per;      //ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler=Psc;    //PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);//给结构体赋初始值
	
	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;  //CCR的值
	TIM_OC2Init(TIM2,&TIM_OCInitStructure);//TIM_OC1Init()函数用来初始化输出比较单元
	
	TIM_Cmd(TIM2,ENABLE);//定时器使能
	
}

void PWM_SetCompare1( uint16_t Compare)//更改占空比
{

	TIM_SetCompare1(TIM1,Compare);

}

void PWM_SetCompare2( uint16_t Compare)//更改占空比
{

	TIM_SetCompare2(TIM2,Compare);

}

void PWM_SetCompare4( uint16_t Compare)//更改占空比
{

	TIM_SetCompare4(TIM1,Compare);

}


 void TIM4_Init(u16 arr,u16 psc)
 {
     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
     NVIC_InitTypeDef NVIC_InitStructure;
 
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //设置TIM3时钟使能
 
 
     TIM_TimeBaseStructure.TIM_Period = arr; //设置重装载寄存器的周期
     TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置时钟频率除数的预分频值
     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割
     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上的计数模式
     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //TIM3的初始化
 
     TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //TIM3更新中断使能
 
 
     NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //TIM3中断
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先2级
    /// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从占优先3级
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
     NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器
 
 
     TIM_Cmd(TIM4, ENABLE);  //使能TIM3
 }
 //定时器4中断服务程序
 void TIM4_IRQHandler(void)   //TIM3中断
 {
     if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
         {
						TIM_ClearITPendingBit(TIM4, TIM_IT_Update  );  //清除TIMx更新中断标志
						tick++;
					  Feed_Dog_Count++;
					  if( Feed_Dog_Count>5)
						{
							IWDG_FeedDog();
							Feed_Dog_Count=0;
						}
//						if(white<=1000&&white>0)
//						{
//							 if(ins_1<=70)
//							{
//							 PWM_SetCompare4(ins_1);
//							 ins_1+=(white/100.0*2.8+30)/6;
//							 //ins_1+=((white/10)/5);
//								printf("white pwm\r\n");
//							}
//							else
//							{
//								ins_1=0;
//								PWM_SetCompare4(0);
//							}
//								
//						}
//						if(white==0){
//							PWM_SetCompare4(0);
//							printf("white is 0!!!");
//								}
						
         }
 }
