收音机模块TEA5767的C51控制程序
时间:2023-09-29 10:33来源: 作者: 点击: 次采用I2C或者三线接口,我是用的I2C,单片机用STC89C52,带1K EEPROM,可以掉电存台,1602LCD显示.
读写数据都是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}; //要写入的数据
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;i5;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;i5;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
}