I2C总线高频头控制程序(PLL芯片为TSA5522系列)【转载】

I2C总线高频头控制程序(PLL芯片为TSA5522系列)【转载】

 
0
推荐
/*I2C总线高频头控制程序(PLL芯片为TSA5522系列)                               */
//--------------------------------------------------------------------------//
//                                源程序大公开                              //
//                    (c) Copyright 2001-2003 xuwenjun                     /
//                            All Rights Reserved                           //
//                                    V1.00                                 //
//--------------------------------------------------------------------------//
//标 题: I2C总线高频头控制程序(PLL芯片为TSA5522系列)                       //
//文件名: xwj_fi1256.c                                                      //
//版 本: V1.00                                                             //
//修改人: 徐文军                         E-mail:[email protected]           //
//日 期: 06-02-26 首次公开                                                 //
//描 述: I2C总线高频头控制程序(PLL芯片为TSA5522系列)                       //
//声 明:                                                                   //
//        以下代码仅免费提供给学习用途,但引用或修改后必须在文件中声明出处. //
//        如用于商业用途请与作者联系.    E-mail:[email protected]           //
//        有问题请mailto [email protected]   欢迎与我交流!                  //
//--------------------------------------------------------------------------//
//老版本: 无                             老版本文件名:                      //
//创建人: 徐文军                         E-mail:[email protected]           //
//日 期: 06-02-26                                                          //
//描 述:                                                                   //
//--------------------------------------------------------------------------//

                                                                                                 /* 频率单位为KHz     */
#define FUENCY 38900                                                        /* 中频频率      电视机的图像中频信号是38MHZ.  音频的中频信号是6.5MHZ.      */
#define PLLdataH(f) ((f+FUENCY)*16/1000/256)        /* 频率数据高 第1字节*/
#define PLLdataL(f) ((f+FUENCY)*16/1000%256)        /* 频率数据低 第2字节*/
#define PLLCON1 0x8e                                                        /* 控制字1    第3字节*/
                                                                                                 /* 控制字2    第4字节*/
#define PLLCON2(f) (((f)<(168000))?(0xa0):(((f)<(450000))?(0x90):(0x30)))
#define PLLdata3(fchan) PLLdataH (fchan),PLLdataL (fchan),PLLCON2 (fchan)

unsigned  char  code uc dataTab[][3]=
{
        PLL data3(49750),         /* 频道DS1 (VL)*/
        PLL data3(57750),         /* 频道DS2*/
        PLL data3(65750),         /* 频道DS3 */
        PLL data3(77250),         /* 频道DS4 */
        PLL data3(85250),         /* 频道DS5 */
        PLL data3(112250),         /* 频道Z1 */
        PLL data3(120250),         /* 频道Z2 */
        PLL data3(128250),         /* 频道Z3 */
        PLL data3(136250),         /* 频道Z4 */
        PLL data3(144250),         /* 频道Z5 */
        PLL data3(152250),         /* 频道Z6 */
        PLL data3(160250),         /* 频道Z7 */

        PLL data3(168250),         /* 频道DS6 (VH)*/
        PLL data3(176250),         /* 频道DS7 */
        PLL data3(184250),         /* 频道DS8 */
        PLL data3(192250),         /* 频道DS9 */
        PLL data3(200250),         /* 频道DS10 */
        PLL data3(208250),         /* 频道DS11 */
        PLL data3(216250),         /* 频道DS12 */
        PLL data3(224250),         /* 频道Z8 */
        PLL data3(232250),         /* 频道Z9 */
        PLL data3(240250),         /* 频道Z10 */
        PLL data3(248250),         /* 频道Z11 */
        PLL data3(256250),         /* 频道Z12 */
        PLL data3(264250),         /* 频道Z13 */
        PLL data3(272250),         /* 频道Z14 */
        PLL data3(280250),         /* 频道Z15 */
        PLL data3(288250),         /* 频道Z16 */
        PLL data3(296250),         /* 频道Z17 */
        PLL data3(304250),         /* 频道Z18 */
        PLL data3(312250),         /* 频道Z19 */
        PLL data3(320250),         /* 频道Z20 */
        PLL data3(328250),         /* 频道Z21 */
        PLL data3(336250),         /* 频道Z22 */
        PLL data3(344250),         /* 频道Z23 */
        PLL data3(352250),         /* 频道Z24 */
        PLL data3(360250),         /* 频道Z25 */
        PLL data3(368250),         /* 频道Z26 */
        PLL data3(376250),         /* 频道Z27 */
        PLL data3(384250),         /* 频道Z28 */
        PLL data3(392250),         /* 频道Z29 */
        PLL data3(400250),         /* 频道Z30 */
        PLL data3(408250),         /* 频道Z31 */
        PLL data3(416250),         /* 频道Z32 */
        PLL data3(424250),         /* 频道Z33 */
        PLL data3(432250),         /* 频道Z34 */
        PLL data3(440250),         /* 频道Z35 */
        PLL data3(448250),         /* 频道Z36 */

        PLL data3(456250),         /* 频道Z37 (UHF)*/
        PLL data3(464250),         /* 频道Z38 */
        PLL data3(471250),         /* 频道DS13 */
        PLL data3(479250),         /* 频道DS14 */
        PLL data3(487250),         /* 频道DS15 */
        PLL data3(495250),         /* 频道DS16 */
        PLL data3(503250),         /* 频道DS17 */
        PLL data3(511250),         /* 频道DS18 */
        PLL data3(519250),         /* 频道DS19 */
        PLL data3(527250),         /* 频道DS20 */
        PLL data3(535250),         /* 频道DS21 */
        PLL data3(543250),         /* 频道DS22 */
        PLL data3(551250),         /* 频道DS23 */
        PLL data3(559250),         /* 频道DS24 */
        PLL data3(607250),         /* 频道DS25 */
        PLL data3(615250),         /* 频道DS26 */
        PLL data3(623250),         /* 频道DS27 */
        PLL data3(631250),         /* 频道DS28 */
        PLL data3(639250),         /* 频道DS29 */
        PLL data3(647250),         /* 频道DS30 */
        PLL data3(655250),         /* 频道DS31 */
        PLL data3(663250),         /* 频道DS32 */
        PLL data3(671250),         /* 频道DS33 */
        PLL data3(679250),         /* 频道DS34 */
        PLL data3(687250),         /* 频道DS35 */
        PLL data3(695250),         /* 频道DS36 */
        PLL data3(703250),         /* 频道DS37 */
        PLL data3(711250),         /* 频道DS38 */
        PLL data3(719250),         /* 频道DS39 */
        PLL data3(727250),         /* 频道DS40 */
        PLL data3(735250),         /* 频道DS41 */
        PLL data3(743250),         /* 频道DS42 */
        PLL data3(751250),         /* 频道DS43 */
        PLL data3(759250),         /* 频道DS44 */
        PLL data3(767250),         /* 频道DS45 */
        PLL data3(775250),         /* 频道DS46 */
        PLL data3(783250),         /* 频道DS47 */
        PLL data3(791250),         /* 频道DS48 */
        PLL data3(799250),         /* 频道DS49 */
        PLL data3(807250),         /* 频道DS50 */
        PLL data3(815250),         /* 频道DS51 */
        PLL data3(823250),         /* 频道DS52 */
        PLL data3(831250),         /* 频道DS53 */
        PLL data3(839250),         /* 频道DS54 */
        PLL data3(847250),         /* 频道DS55 */
        PLL data3(855250),         /* 频道DS56 */
        PLL data3(863250),         /* 频道DS57 */

        PLL data3(90100-6500), /* 经济台 (调频收音)*/
        PLL data3(91000-6500), /* 经济台 */
        PLL data3(91800-6500), /* 交通台 */
        PLL data3(97500-6500), /* 文艺台 */
        PLL data3(100300-6500), /* 交通台 */
        PLL data3(103200-6500), /* 新闻台 */
        PLL data3(105000-6500), /* 星沙之声 */
        PLL data3(106500-6500), /* 音乐台 */
}; 

                                  /*  有关I2C变量   */
#define AddPllWr 0xc2                                 /*TSA5522写地址*/
#define AddPllRd 0xc3                                 /*TSA5522读地址*/
sbit         SDA   = P0^4;                                   /*串行数据-----------P0.4*/
sbit         SCL   = P0^5;                                   /*串行时钟-----------P0.5*/

                                  /*  I2C相关函数   */
void Start( void); 
void Stop( void); 
void SendACK( void); 
void SendNoACK( void); 
unsigned  char SendByte( unsigned  char d); 
unsigned  char ReadByte( void); 

void SetChan( unsigned  char chan); 
unsigned  char ReadChan( void); 

/*--------选择频道--------------------------------------------*/
void SetChan( unsigned  char chan)
{
        Start();                                                  /* 发送I2C 总线起始条件  */
        SendByte(AddPllWr);                           /* 发送被控器总线写地址    */
        SendByte(uc dataTab[chan][0]);          /* 发送频率数据高 第1字节*/
        SendByte(uc dataTab[chan][1]);          /* 发送频率数据低 第2字节*/
        SendByte(PLLCON1);                                  /* 发送控制字1    第3字节*/
        SendByte(uc dataTab[chan][2]);          /* 发送控制字1    第4字节*/
        Stop();                                                  /*发送I2C 总线停止条件   */
}

/*-----------读状态----------------------------------------------*/
unsigned  char ReadChan( void)
{
         unsigned  char i; 
        Start();                                                  /* 发送I2C 总线起始条件*/
        SendByte(AddPllRd);                          /*发送被控器总线读地址*/
        i = ReadByte(); 
        SendNoACK();                                          /*最后一个字节,发送非应答信号*/
        Stop();                                                  /*发送I2C 总线停止条件*/
         return (i); 
}

/*------------字节数据传送子程序发送一个字节数据或地址给被控器---------------*/
unsigned  char SendByte( unsigned  char a)
{
         unsigned  char i,j; 
         for(i = 0;i < 8;i++)
        {
                if((a & 0x80) == 0)
                        SDA = 0; 
                 else
                        SDA = 1; 
                _nop_(); 
                        SCL = 1;  /*置时钟线为高通知被控器开始*/
                _nop_();_nop_(); /*保证时钟高周期大于4 us*/
                SCL = 0; 
                _nop_(); 
                a = a << 1; 
        }
        _nop_(); 
        SDA = 1; 
        _nop_(); 
                SCL = 1; 
        j = 0; 
         while (SDA == 0) 
                if ((++j) == 0) {SCL = 0; return (1);}  /*检测256次还未收到应答,返回1*/
        _nop_(); 
        SCL = 0; 
         return (0);   /*成功,返回0*/
}

/*---------------------------------------------------------------------------*/
unsigned  char ReadByte( void)                 /*数据接收子程序从被控器接收一个字节数据*/
{
         unsigned  char i; 
         unsigned  char  bdata Sin; 
        SDA = 1; 
         for (i = 0;i < 8;i++)
        {
                Sin <<= 1; 
                SCL = 0; 
                _nop_(); 
                SCL = 1; 
                if(SDA == 1)
                        Sin|= 0x01; 
                 else
                        Sin&= 0xfe; 
                _nop_(); 
        }
         return(Sin); 
}

/*-----------发送应答位--------------------------------------------------*/
void SendACK()
{
        SCL = 0; 
        _nop_(); 
        SDA = 0; 
        _nop_(); 
        SCL = 1; 
        _nop_(); 
        SCL = 0; 
}

/*------------发送非应答位-----------------------------------------------*/
void SendNoACK()
{
        SCL = 0; 
        _nop_(); 
        SDA = 1; 
        _nop_(); 
        SCL = 1; 
        _nop_(); 
        SCL = 0; 
}

/*---------------------------------------------------------------------------*/
void Start( void)                 /*START 启动I2C 总线子程序发送I2C 起始条件*/
{
        SDA = 1;                                  /* 发送起始条件的数据信号*/
        _nop_(); 
        SCL = 1;                                  /*发送起始条件的时钟信号*/
        _nop_();          /*起始条件建立时间大于4.7 s*/
        SDA = 0;                                  /*发送起始信号*/
        _nop_();          /*起始条件锁定时间大于4 s*/
        SCL = 0;                                  /*钳住I2C 总线准备发送或接收数据*/
}

/*-----------STOP 停止I2C 总线子程序发送I2C 总线停止条件-------------------*/
void Stop( void)
{
        SDA = 0; 
        _nop_(); 
        SCL = 1; 
        _nop_(); 
        SDA = 1; 
        _nop_(); 
}

你可能感兴趣的:(I2C总线高频头控制程序(PLL芯片为TSA5522系列)【转载】)