tdad4.c

来自「DVB软件,基于CT216软件的开发源程序.」· C语言 代码 · 共 198 行

C
198
字号
//ALPS TUNER TDAD4
#include <stdio.h>
#include "ct_os.h"
#include "ct_sys.h"
#include "ct_iic.h"
#include "ct_nim.h"
#include "TDAD4.h"

#define TUNER_ADD   	0xC0
#define TUNER_IFKHZ   	36167

//#define IIC_UART

//static u8 szBuffer[8];
//static u8 stval = 0x08;
#if 0
#ifdef IIC_UART
/*******************************************************************************************/
bool8 Tuner_Write(u8 *pu8Buffer , u8 u8Length)
{
    if ((pu8Buffer == NULL)||
        (u8Length > MAX_IIC_BURST_LENGTH)||
        (u8Length==0))
    {        
        return FALSE;
    }

    if ( IIC2UART_Write(TUNER_ADD, *pu8Buffer, (pu8Buffer+1), (u8Length-1)) == FALSE )
	{		    
	    return FALSE;
	}
    return TRUE;
}
/*******************************************************************************************/
bool8 Tuner_Read(u8 *pu8Buffer, u8 u8Length)
{
    if ((pu8Buffer == NULL)||(u8Length > MAX_IIC_BURST_LENGTH))
    {
        return FALSE;
    }    
    
	if ( IIC2UART_Read(TUNER_ADD, 0, pu8Buffer, u8Length, FALSE) == FALSE )
	{
	    return FALSE;
	}

    return TRUE;
}
#else
/*******************************************************************************************/
bool8 Tuner_Write(u8 *pu8Buffer , u8 u8Length)
{
    u32 u32Handle;	
    
	if (CT_NIM_GetBus() == EN_CT_NIM_BUS_0)
	{
		if( CT_IIC_Open(EN_CT_IIC_BUS_0, TUNER_ADD, &u32Handle)!= DRV_OK)
		{		
			printf("\nCT_IIC_Open 0 Error");
	        return FALSE;
	    }
	}
	else
	{
		if( CT_IIC_Open(EN_CT_IIC_BUS_1, TUNER_ADD, &u32Handle)!= DRV_OK)
		{		
			printf("\nCT_IIC_Open 1 Error");
	        return FALSE;
	    }		
	}
#ifdef NIM_SW_IIC
	if(CT_SW_IIC_WriteData(u32Handle, pu8Buffer, u8Length) != DRV_OK )
#else
	if(CT_IIC_WriteData(u32Handle, pu8Buffer, u8Length) != DRV_OK )
#endif
	{		
		printf("\nCT_IIC_WriteData Error");
		CT_IIC_Close(u32Handle);
		return(FALSE);
	}
	
	CT_IIC_Close(u32Handle);
	return (TRUE);     
}
/*******************************************************************************************/
bool8 Tuner_Read(u8 *pu8Buffer, u8 u8Length)
{    
    u32 u32Handle;
		
	if (CT_NIM_GetBus() == EN_CT_NIM_BUS_0)
	{
		if( CT_IIC_Open(EN_CT_IIC_BUS_0, TUNER_ADD, &u32Handle)!= DRV_OK)
		{
			printf("\nR CT_IIC_Open 0 Error");
        	return FALSE;
        }
	}
	else
	{
		if( CT_IIC_Open(EN_CT_IIC_BUS_1, TUNER_ADD, &u32Handle)!= DRV_OK)
		{
			printf("\nR CT_IIC_Open 1 Error");
        	return FALSE;		
        }
	}

#ifdef NIM_SW_IIC
	if(CT_SW_IIC_ReadData(u32Handle, pu8Buffer, u8Length) != DRV_OK )
#else
	if(CT_IIC_ReadData(u32Handle, pu8Buffer, u8Length) != DRV_OK )
#endif
	{
		printf("\nR CT_IIC_ReadData Error");
		CT_IIC_Close(u32Handle);		
		return FALSE;
	}
	
	CT_IIC_Close(u32Handle);
	return (TRUE) ;
}
#endif
#endif
/*******************************************************************************
 * Program TUNER_TDAD4 tuner
 *******************************************************************************/
bool8 CT221_ProgramTuner_TDAD4(u32 dwFreqkHz, u8 dwBWandFreqMHz)
{

	unsigned int N;
	u8           WriteBytes[5];
	float        TunerFreq;
	u8           status;
//	u8           TUNER_ID  = 0xc0;
	u8           ByteMask  = 0xff;
	unsigned int NMask     = 0x7fff;
	unsigned int ByteShift = 8;

	TunerFreq = dwFreqkHz / 1000.0;

	// Calculate divider N with step size = 166.67 kHz.
	N = (unsigned int)(TunerFreq * 6 + 217 + 0.5);
	N &= NMask;

	// Set divider byte 1 and 2.
	//   - Note: Divider byte 1 is WriteBytes[0].
	//           Divider byte 2 is WriteBytes[1].
	WriteBytes[0] = (u8)(N >> ByteShift & ByteMask);
	WriteBytes[1] = (u8)(N & ByteMask);

	WriteBytes[2] = 0xA0;

	if (TunerFreq > 781) // 782+-0.2
	{
		WriteBytes[3] = (0x00 | 0x20 | 0x0c);  //ALPS comment: 0x00:cp=1200;  0x0c:UHF;  0x20:XTAL Output=OFF
		//WriteBytes[3] = (0xc0 | 0x20 | 0x0c);  //0xc0:cp=700;  0x0c:UHF+8M;  0x20:XTAL Output=OFF
	}
	else if(TunerFreq > 469) //470(+-0.2)~782
	{
		WriteBytes[3] = (0xc0 | 0x20 | 0x0c);  //ALPS comment:0xc0:cp=700;  0x0c:UHF;  0x20:XTAL Output=OFF
		//WriteBytes[3] = (0x80 | 0x20 | 0x0c);  //0x80:cp=300;  0x0c:UHF+8M;  0x20:XTAL Output=OFF
	}
	else                                // 174~230 
	{
		WriteBytes[3] = (0x80 | 0x20 | 0x02);  //ALPS comment:0x80:cp=300;  0x02:VHF+7M;  0x20:XTAL Output=OFF
		//WriteBytes[3] = (0x40 | 0x20 | 0x02);  //0x40:cp=150;  0x02:VHF+7M;  0x20:XTAL Output=OFF
	}	

	if (dwBWandFreqMHz == 8)
		WriteBytes[3] |= 0x04;
	else
		WriteBytes[3] &= ~0x04;

	WriteBytes[4] = 0xC6;

	status = Tuner_Write(&WriteBytes[0], 5);

	CT_OS_MS_Delay(10);
	return TRUE; 	       

}

#if 0
/*******************************************************************************
 * Check TDAD4 tuner lock
 *******************************************************************************/
bool8 Check_tuner_lock(void)
{   
    u8			    buffer[1];
   
	Tuner_Read(buffer, 1);
	if((buffer[0] !=255)&&(buffer[0] &0x40))
		return	TRUE;
	else
		return	FALSE;
}
#endif

⌨️ 快捷键说明

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