⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tlv2544.c

📁 AD器件TLV2544的驱动程序 AD器件TLV2544的驱动程序
💻 C
字号:
/**********************************Copyright (c)****************
**                               
**                               Delphifx Auto
**                               
**--------------File Info----------------------------------------
** File name:           TLV2544.c
** Last modified Date:  
** Last Version:        
** Descriptions:        

**----------------------------------------------------------------
** Created by:  Delphifx       
** Created date: 2007/12/30       
** Version:             
** Descriptions: This is Driver File for TLV2544       
**
******************************************************************/


#include "TLV2544.h"



/*-----------------------------------------------------------------

调用方式:uint read1543(uchar port)
函数说明:read1543()返回10位AD芯片TLC1543的port通道采样值。
-------------------------------------------------------------------*/


uint16 TLV2544(uint8 port)
{
  //TLV2544_Once(port);
  //TLV2544_Once(port);
  return (TLV2544_Once(port));
}

void TLV2544_Init(void)
{
  SET_TLV2544_CSTART;
  DelayMs(100);
  
  TLV2544_wb ( 0xA804 );
  DelayMs(100);
  TLV2544_wb ( 0xA804 );
  //TLV2544_wb ( 0xA800 );
}

uint16 TLV2544_Once(uint8 ch)
{
    uint8 i;
    uint16 AD_RESULT=0;
    
	ch<<=4;
	   
	CLRB_TLV2544_DATACLK;
	
	//--Delay_us(4);//  
	//Delay_us(1);//  
    asm("nop");asm("nop");
	
	SETB_TLV2544_CS;//片选禁止(可选)
	
	//--Delay_us(4);//
	//Delay_us(1);//  
	asm("nop");asm("nop");
    CLRB_TLV2544_CS; 
	
	//--Delay_us(4);//
	//Delay_us(1);//  
	asm("nop");asm("nop");
    i=4;
    do
    {
	  	AD_RESULT<<=1;
        if(ch>=0x80) 
              SETB_TLV2544_DATAIN;        //时钟上升沿锁存SDI
        else  
              CLRB_TLV2544_DATAIN;
        ch<<=1;	        
        if(GETB_TLV2544_DATAIN) 
              AD_RESULT|=0x0001;
			  
		
		
        SETB_TLV2544_DATACLK;
		//--Delay_us(4);//
		//Delay_us(1);//  
		asm("nop");asm("nop");
		
        CLRB_TLV2544_DATACLK;
		//--Delay_us(4);//
		//Delay_us(1);//  
		asm("nop");asm("nop");
    }while(--i);
       
    i=8;                            //开始读取12bit结果
    do
    {
        AD_RESULT<<=1;
        if(GETB_TLV2544_DATAIN) 
            AD_RESULT|=0x0001;      //用时钟的上升沿读取
        SETB_TLV2544_DATACLK;  
		
        //--Delay_us(4);//
		//Delay_us(1);//  
		asm("nop");asm("nop");
		  
        CLRB_TLV2544_DATACLK;   
		
		//--Delay_us(4);//      
		//Delay_us(1);//   
		asm("nop");asm("nop");
				 
    }while(--i);

    i=4;                            //开始读取12bit结果
    do
    {
        SETB_TLV2544_DATACLK;  
		
		//--Delay_us(4);//
		//Delay_us(1);//  
		asm("nop");asm("nop");
          
        CLRB_TLV2544_DATACLK;  
		
		//--Delay_us(4);//    
		//Delay_us(1);//  
		asm("nop");asm("nop");
		     
    }while(--i);
      
    SETB_TLV2544_DATACLK;    
	
	//--Delay_us(4);//
	//Delay_us(1);//  
	asm("nop");asm("nop");
	                                  
    SETB_TLV2544_CS;  
	
    AD_RESULT&=0xfff;  
    
    return AD_RESULT;                        
}





//---------------- 向TLV2544写16bit数据 -----------------------
void TLV2544_wb ( uint16 C_dat )
{
    uint8 i;
    
	 
	CLRB_TLV2544_DATACLK;
	
	Delay_us(10);//  
    
	SETB_TLV2544_CS;//片选禁止(可选)
	
	Delay_us(10);//
	
    CLRB_TLV2544_CS; 
	
	Delay_us(10);//
    for(i=0;i<16;i++)
    {
        if(C_dat>=0x8000) 
             SETB_TLV2544_DATAIN;	       //时钟上升沿锁存SDI
        else         
             CLRB_TLV2544_DATAIN;	
        SETB_TLV2544_DATACLK;                            //开始发送命令字
		
		Delay_us(10);//
		
        CLRB_TLV2544_DATACLK;                           //时钟脉冲,一共16个
        
		Delay_us(10);//
		
		C_dat<<=1;
    }
    SETB_TLV2544_DATACLK;     
	Delay_us(10);//           
    SETB_TLV2544_CS; 
}





/*--------------------------------------------------------------------

调用方式:unsigned filter(); @2007/07/25
函数说明:Filter()返回采样数为N的中值滤波值,N+2次测量中,去掉一个最高值,去掉一个最低值,返回Sum/N
---------------------------------------------------------------------*/
uint16 Filter(uint8 path,uint16 N)
{
 long int Sum = 0;
 uint16 count;
 uint16 Max,Min,VD;
 
 Max=Min=TLV2544(path);//TLV2544(path);
 Sum+=Max;
 for ( count=0;count<N+1;count++)
 {
  
  
  VD=TLV2544(path);
  Sum+=(long)VD;
  if (VD>Max) Max=VD;
  if (VD<Min) Min=VD;
  
  //Delay_us(100);
  //DelayMs(1);
 }
 Sum=Sum-(Max+Min);
 return ( (Sum + (N>>1) )/N );

} 


/*--------------------------------------------------------------------
 (); @2007/07/25
函数说明:Filter()返回采样数为N的中值滤波值,N+2次测量中,去掉一个最高值,去掉一个最低值,返回Sum/N
---------------------------------------------------------------------*/
uint16 FilterMv(uint8 path,uint16 RefVD,uint16 N)
{
 long int Sum = 0;
 uint16 count;
 uint16 Max,Min,VD,Average;
 uint16 Result;
 
 
 Max=Min=TLV2544(path);//TLV2544(path);
 Sum+=Max;
 for ( count=0;count<N+1;count++)
 {
  
  
  VD=TLV2544(path);
  Sum+=(long)VD;
  if (VD>Max) Max=VD;
  if (VD<Min) Min=VD;
  //Delay_us(100);
  //DelayMs(1);
 }
 Sum=Sum-(Max+Min);
 
 Average=(Sum+(N>>1))/N;
 Result=((long)RefVD*Average+2048)/4096;
  
 return Result;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -