#include "delay.h"
#include "stm32f10x_tim.h"
#include "TypeDefine.h"
//延时nus
//nus为要延时的us数.		    								   
//void delay_us(uint16_t nus)
//{	
//	uint8_t  temp=0;		 
//	for(;nus>0;nus--)
//	{
//	  for(temp=0; temp< 100;temp++);
//	}
//	
//}
//uint8_t hornTXDFlag = 0;

void delay_us(uint16_t nus)
{			 
////	TIM3_Int_Init(nus-1,8000-1);
//	TIM_SetAutoreload(TIM3,nus);
//	TIM_ITConfig(TIM3, TIM_FLAG_Update,ENABLE);
//	TIM_Cmd(TIM3, ENABLE);  //使能TIMx		
//	while(hornTXDFlag == 0)
//	{
//		
//	}
//	hornTXDFlag = 0;
	
	

	uint16_t counter=nus;
    TIM_Cmd(TIM3,ENABLE);  
    TIM_SetCounter(TIM3,counter); //设置计数值  
  
    while(counter>0)  
    {  
        counter=TIM_GetCounter(TIM3);  
    }  
    TIM_Cmd(TIM3,DISABLE);  
}

void TIM3_Int_Init(uint16_t  arr,uint16_t psc)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	//NVIC_InitTypeDef NVIC_InitStructure;
	RCC_HCLKConfig(RCC_SYSCLK_Div1);
	RCC_PCLK1Config(RCC_HCLK_Div1);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
	
	//定时器TIM3初始化
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	TIM_ITConfig(TIM3, TIM_FLAG_Update,ENABLE);
	TIM_Cmd(TIM3, ENABLE);  //使能TIMx					 
}

//void TIM3_Int_Init(uint16_t  arr,uint16_t psc)
//{
//	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
//	NVIC_InitTypeDef NVIC_InitStructure;
//	RCC_HCLKConfig(RCC_SYSCLK_Div1);
//	RCC_PCLK1Config(RCC_HCLK_Div1);

//	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
//	
//	//定时器TIM3初始化
//	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
//	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
//	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
//	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
//	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
// 
//	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断

//	//中断优先级NVIC设置
//	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
//	NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器

//	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
//	TIM_Cmd(TIM3, DISABLE);  //使能TIMx					 
//}
////定时器3中断服务程序
//void TIM3_IRQHandler(void)   //TIM3中断
//{
//	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
//	{
//		TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx更新中断标志 
//		TIM_ClearFlag(TIM3, TIM_FLAG_Update);
//		TIM_ITConfig(TIM3, TIM_FLAG_Update,DISABLE);
//		TIM_Cmd(TIM3, DISABLE);  //使能TIMx	
//		hornTXDFlag = 1;
////		RLED = !RLED;
//	}
//}
