- UID
- 852722
|
收音机模块TEA5767的C语言控制程序
TEA5767采用I2C或者三线接口控制,我是用的I2C,单片机用STC89C52,带1K EEPROM,可以掉电存台,1602LCD显示.
TEA5767读写数据都是5个字节,其中PLL参数14位,具体数据格式请看Datasheet.以下程序中的函数可以直接在主程序中调用.
#Include "Iic.C"
#Define Max_freq 108000
#Define Min_freq 87500
Unsigned Char Radio_write_data[5]={0x29,0xc2,0x20,0x11,0x00}; //要写入TEA5767的数据
Unsigned Char Radio_read_data[5]; //TEA5767读出的状态
Unsigned Int Default_pll=0x29c2;//0x29f9; //默认存台的Pll,87.8MHz
//Unsigned Int Max_pll=0x339b; //108MHz时的Pll,
//Unsigned Int Min_pll=9000; //70MHz时的Pll
Void Radio_write(Void)
{
Unsigned Char I;
Iic_start();
Iic_write8bit(0xc0); //TEA5767写地址
If(!Iic_testack())
{
For(I=0;I<5;I++)
{
Iic_write8bit(Radio_write_data[I]);
Iic_ack();
}
}
Iic_stop();
}
//读TEA5767状态,并转换成频率
Void Radio_read(Void)
{
Unsigned Char I;
Unsigned Char Temp_l,Temp_h;
Pll=0;
Iic_start();
Iic_write8bit(0xc1); //TEA5767读地址
If(!Iic_testack())
{
For(I=0;I<5;I++)
{
Radio_read_data[I]=Iic_read8bit();
Iic_ack();
}
}
Iic_stop();
Temp_l=Radio_read_data[1];
Temp_h=Radio_read_data[0];
Temp_h&=0x3f;
Pll=Temp_h*256+Temp_l;
Get_frequency();
}
//由频率计算PLL
Void Get_pll(Void)
{
Unsigned Char Hlsi;
Unsigned Int Twpll=0;
Hlsi=Radio_write_data[2]&0x10;
If (Hlsi)
Pll=(Unsigned Int)((Float)((Frequency+225)*4)/(Float)32.768); //频率单位:K
Else
Pll=(Unsigned Int)((Float)((Frequency-225)*4)/(Float)32.768); //频率单位:K
}
//由PLL计算频率
Void Get_frequency(Void)
{
Unsigned Char Hlsi;
Unsigned Int Npll=0;
Npll=Pll;
Hlsi=Radio_write_data[2]&0x10;
If (Hlsi)
Frequency=(Unsigned Long)((Float)(Npll)*(Float)8.192-225); //频率单位:KHz
Else
Frequency=(Unsigned Long)((Float)(Npll)*(Float)8.192+225); //频率单位:KHz
}
//手动设置频率,Mode=1,+0.1MHz; Mode=0:-0.1MHz ,不用考虑TEA5767用于搜台的相关位:SM,SUD
Void Search(Bit Mode)
{
Radio_read();
If(Mode)
{
Frequency+=100;
If(Frequency>Max_freq)
Frequency=Min_freq;
}
Else
{
Frequency-=100;
If(Frequency
Frequency=Max_freq;
}
Get_pll();
Radio_write_data[0]=Pll/256;
Radio_write_data[1]=Pll%256;
Radio_write_data[2]=0x20;
Radio_write_data[3]=0x11;
Radio_write_data[4]=0x00;
Radio_write();
}
//自动搜台,Mode=1,频率增加搜台; Mode=0:频率减小搜台,不过这个好像不能循环搜台
Void Auto_search(Bit Mode)
{
Radio_read();
Get_pll();
If(Mode)
Radio_write_data[2]=0xa0;
Else
Radio_write_data[2]=0x20;
Radio_write_data[0]=Pll/256+0x40;
Radio_write_data[1]=Pll%256;
Radio_write_data[3]=0x11;
Radio_write_data[4]=0x00;
Radio_write();
Radio_read();
While(!(Radio_read_data[0]&0x80)) //搜台成功标志
{
Radio_read();
Disp_freq(9,1);
}
} |
|