//引用代??
#ifndef _GETURM37_
#define _GETURM37_

#include "ZMkernel.h"
#include <math.h>
#include "command.h"

//端口硬件信息定?
#ifndef _HARDWAREPORT_H_
#define _HARDWAREPORT_H_
//A1~A16模?端口
#define A1  384
#define A2  256
#define A3  128
#define A4  0
#define A5  416
#define A6  288
#define A7  160
#define A8  32
#define A9  512
#define A10 640
#define A11 768
#define A12 896
#define A13 544
#define A14 672
#define A15 16
#define A16 144
//D1~D8 一控制??字端口
#define D1  1168
#define D2  912
#define D3  1040
#define D4  784
#define D5  1712
#define D6  1968
#define D7  1584
#define D8  1840
//D1~D8 2控制??字端口
#define D12 117904//GND靠上
#define D34 101392
#define D56 253616
#define D78 237104

#define D21 150416//GND靠下
#define D43 133904
#define D65 221104
#define D87 204592

#endif

#define URM37_TIME_OVER_EN	1	//URM37??据超???使能
#define TIM_OVER_CNTR_MAX	5000000//超?定?		
u32	PWM_PORT;
u8	PWM_BIT;

#define SetPWM_DDR 		GPIO_Setting((GPIO_TypeDef*)PWM_PORT,PWM_BIT,GPIO_Mode_OUT,GPIO_OType_PP,GPIO_PuPd_NOPULL)
#define ClsPWM_DDR 		GPIO_Setting((GPIO_TypeDef*)PWM_PORT,PWM_BIT,GPIO_Mode_IN,GPIO_OType_PP,GPIO_PuPd_NOPULL)
#define PWM_State		GPIO_ReadInputDataBit((GPIO_TypeDef*)PWM_PORT,(1<<PWM_BIT))
#define SetPWM     			GPIO_SetBits((GPIO_TypeDef*)PWM_PORT,(1<<PWM_BIT))
#define ClsPWM     			GPIO_ResetBits((GPIO_TypeDef*)PWM_PORT,(1<<PWM_BIT))
#define Read_PWM_PIN_State()		PWM_State

#define F_CLK					120000000
#define TIM					TIM2
#define RCC_APB1Periph_TIM		RCC_APB1Periph_TIM2

#define StartTime()			TIM_Cmd(TIM,ENABLE)
#define CloseTime()		TIM_Cmd(TIM,DISABLE)
#define ClearTimeCnt()		(TIM->CNT=0)
#define GetTimeCnt()		(TIM->CNT)
#define SettingPWM_DDR()	{ClsPWM_DDR;SetPWM;}

void URM37_PinInit(void)
{
	ClsPWM_DDR;
	SetPWM;
}
/*-----------------------------------------------------------------------
   tim2    配置
--------------------------------------------------------------------*/
void URM37_TIM_Config(void)
{
   	u16 period,psc;
	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

   	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM,ENABLE); 

	TIM_DeInit(TIM); //复位定?器

	psc=F_CLK/1000000-1;
	period=32000;//32ms
	
	TIM_TimeBaseStructure.TIM_Period = period;//32000;
	TIM_TimeBaseStructure.TIM_Prescaler=psc;//64-1;         	//???分? 1MHz=1us
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;  		//??分割
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; 	//向上??模式
	TIM_TimeBaseInit(TIM,&TIM_TimeBaseStructure);   			//初始化定?器的值

	TIM_ClearFlag(TIM,TIM_FLAG_Update);							//清除定?器中??志  
	TIM_ITConfig(TIM,TIM_IT_Update,ENABLE);						//使能中?
	
	TIM->CNT=0;
	TIM_Cmd(TIM,ENABLE);            		//????
	//TIM_Cmd(TIM,DISABLE);  
}
//初始化
u8 URM37_FirstInitFlag=0;
u8 URM37_Init(void)
{
	if(URM37_FirstInitFlag)return 0;
	URM37_FirstInitFlag=1;
	URM37_PinInit();
	URM37_TIM_Config();
	return 1;
}


unsigned int GetURM37(unsigned long which)
//?程?
{
   struct select *information=&which;
   u32 port1=portarray[information->group1];
   u8 bit1=information->bit1;

    PWM_PORT=port1;
    PWM_BIT=bit1;
    
    	u16 Distance=0;
	URM37_Init();

#if    URM37_TIME_OVER_EN > 0//超?判?
	u8 timeoverflag=0;
    	u32 timeovercntr=TIM_OVER_CNTR_MAX;
	if(Read_PWM_PIN_State())
	{
		while(Read_PWM_PIN_State()&&timeovercntr)
		{
			timeovercntr--;
		}
		if(timeovercntr==0)timeoverflag=1;//超?
	}
	if(timeoverflag)
	{
		Distance=999;
	}
	else
	{
		timeovercntr=TIM_OVER_CNTR_MAX;
		while(Read_PWM_PIN_State()==0&&timeovercntr)//等待被拉低
		{
			timeovercntr--;
		}	
		if(timeovercntr==0)timeoverflag=1;//超?
		if(timeoverflag==0)
		{	
			ClearTimeCnt();//定?器?据寄存器清零
			StartTime();	//使能定?器??
			timeovercntr=TIM_OVER_CNTR_MAX;
			while(Read_PWM_PIN_State()&&timeovercntr)//等待接收端?高?平  
			{
				timeovercntr--;
			}
			CloseTime();	//?定?器
			if(timeovercntr==0)timeoverflag=1;//超?	
			if(timeoverflag==0)
			{	
				Distance=2*GetTimeCnt()/10;  //?取距离(cm)50us?1cm
			}
		}
		if(timeoverflag)
		{
			Distance=999;
		}
		
	}
	
#else//不用超?判?
	if(Read_PWM_PIN_State()){while(Read_PWM_PIN_State());}
	while(Read_PWM_PIN_State()==0);		//等待被拉低
	ClearTimeCnt();//定?器?据寄存器清零
	StartTime();	//使能定?器??
	while(Read_PWM_PIN_State());	//等待接收端?高?平  
	CloseTime();	//?定?器
	Distance=2*GetTimeCnt()/10;  //?取距离(cm)50us?1cm
#endif
	return Distance; 
}
#endif

