lgxx1d.c
来自「DVB软件,基于CT216软件的开发源程序.」· C语言 代码 · 共 340 行
C
340 行
#include <string.h>
#include "ct_sys.h"
#include "ct_iic.h"
#include "ct_nim.h"
#include "LGXX1D.h"
//#define IIC_UART
#define TUNER_ADD 0xC0
//static u8 szBuffer[8];
#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 LG252D tuner
*******************************************************************************/
bool8 CT221_ProgramTuner_LGXX1D(u32 RfFreq, u8 dwBWandFreqkHz)
{
#if 1
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 = RfFreq / 1000.0;
// Calculate divider N with step size = 166.67 kHz.
N = (unsigned int)(TunerFreq * 6 + 217 + 0.5);
N &= NMask;
//printf("\n\r LG252D freq %ld", (u32)(TunerFreq * 1000));
// Set divider byte 1 and 2.
// - Note: Divider byte 1 is WriteBytes[0].
// Divider byte 2 is WriteBytes[1].
WriteBytes[0] = N >> ByteShift & ByteMask;
WriteBytes[1] = N & ByteMask;
// Set control byte and band switch byte.
// - Note: Control byte is WriteBytes[2].
// Band switch byte is WriteBytes[3].
//WriteBytes[2] = 0x90;
WriteBytes[2] = 0x80 | (0x04<<3); //TOP=103
/* ------------- UHF -----------------*/
if (TunerFreq > 700 && TunerFreq <= 862)
WriteBytes[3] = 0xc8;
else if(TunerFreq > 430 && TunerFreq <= 700)
WriteBytes[3] = 0x88; // 10 | 0|1 |1000
/* ------------- VHF High -----------------*/
else if(TunerFreq > 300 && TunerFreq <= 430)
WriteBytes[3] = 0xc2;
else if(TunerFreq > 145 && TunerFreq <= 300)
WriteBytes[3] = 0x82;
/* ------------- VHF Low -----------------*/
else if(TunerFreq > 100 && TunerFreq <= 145)
WriteBytes[3] = 0xc1;
else if(TunerFreq <= 100)
WriteBytes[3] = 0x81;
else
WriteBytes[3] = 0x81;
if (dwBWandFreqkHz != 8)
WriteBytes[3] &= ~0x10;
else
WriteBytes[3] |= 0x10;
// Set auxiliary byte.
// - Note: Auxiliary byte is WriteBytes[4].
WriteBytes[4] = 0xc3; // 11 00 0010
status = Tuner_Write(&WriteBytes[0],5);
//status = RegWrite(TUNER_ID, WriteBytes[0], &WriteBytes[1], 4);
return TRUE;
#else
u8 status;
u8 data[6];
u8 ab;
long maincounter;
float TunerFreq;
float if_freq = 36.167;
float stepsize = 166.67;
u8 if_out_level = 106;//112, 109, 106, 103, 100, 97, 94
TunerFreq = RfFreq / 1000.0;
//Tuner Address
data[0] = TUNER_ADD;
maincounter = (long)((TunerFreq+if_freq)/(stepsize*0.001) + 0.5);
// Divider Byte 1 & 2
data[1] = (u8)((maincounter>>8)&0x007f);
data[2] = (u8)(maincounter&0x00ff);
// Control Byte 1
data[3] = 0x80;
//Rf AGC current set bit(ATC)
//data[5] = 0xc0;//Enable XTALOUT 4MHz output
data[5] = 0xc1;//Disable XTALOUT 4MHz output
//Time Constant
data[5] |= 0x00;//300nA
//data[5] |= 0x20;//15uA
ab = data[3];
if(if_out_level == 112)
{
data[5] |= 0x02;
ab |= 0x00;
}
else if(if_out_level == 109)
{
data[5] |= 0x02;
ab |= 0x08;
}
else if(if_out_level == 106)
{
data[5] |= 0x02;
ab |= 0x10;
}
else if(if_out_level == 103)
{
data[5] |= 0x02;
ab |= 0x18;
}
else if(if_out_level == 100)
{
data[5] |= 0x02;
ab |= 0x20;
}
else if(if_out_level == 97)
{
data[5] |= 0x02;
ab |= 0x28;
}
else if(if_out_level == 94)
{
data[5] |= 0x02;
ab |=0x30;
}
else
{
data[5] |= 0x02;
ab |= 0x38;
}
data[3] = ab;
//Reference Divider Ratio bit set[**** * RS2 RS1 RS0]
if(stepsize == 166.67)
data[3] |= 0x00;//166.67KHz
else if(stepsize == 142.85)
data[3] |= 0x01;//142.85KHz
else if (stepsize == 80)
data[3] |= 0x02;//80KHz
else if(stepsize == 62.5)
data[3] |= 0x03;//62.5KHz
else if(stepsize == 31.25)
data[3] |= 0x04;//31.25KHz
else
data[3] |= 0x05;//50KHz
// Band-switch Byte
data[4] = 0x00;
// Charge Pump current set bit[CP1 CP0 ** ****]
if(TunerFreq <= 100.0)
{
data[4] |= 0x80;
}
else if(TunerFreq <= 145.0)
{
data[4] |= 0xC0;
}
else if(TunerFreq <= 300.0)
{
data[4] |= 0x80;
}
else if(TunerFreq <= 430.0)
{
data[4] |= 0xC0;
}
else if(TunerFreq <= 700.0)
{
data[4] |= 0x80;
}
else
{
data[4] |= 0xc0;
}
// Band selection set[Port control set : *** P5 BS4 BS3 BS2 BS1]
if(TunerFreq < 149.5)
data[4] |= 0x01;//VHF LOW[BS1]
else if(TunerFreq < 434.0)
data[4] |= 0x02;//VHF HIGH[BS2]
else
data[4] |= 0x18;//UHF[P5 BS4]
//RF AGC detector input selection bit
data[4] |= 0x20;//IF amplifier
//data[4] &= ~(0x20);//Mixer output
// Control Byte 2
//ATC settings
//data[5] |= 0x10; //search mode
data[5] &= ~(0x10); //normal mode
//Power Standby mode-control bit
//data[5] |= 0x01; //Standby mode
data[5] &= (~(0x01)); //Normal mode
/* I2C Data Write Sequences[07.02.24. By csd]
1. Start-ADB-DB1-DB1-CB1-BB-CB2-Stop ==> COM.I2cReadWrite(WRITE,*data, &data[1],5);
2. Start-ADB-DB1-CB1-Stop. Start-ADB-DB1-BB--CB2-Stop
==> COM.I2cReadWrite(WRITE,*data, &data[1],2);
Sleep(50);
COM.I2cReadWrite(WRITE,*data, &data[3],3);
Sleep(10);
3. Both write sequences is ok!! */
Tuner_Write(&data[1],5);
return TRUE;
#endif
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?