今天参考ouravr的几个TEA5767的帖子,修改了他的程序,用在FM板上,可以显示和收到台了。
我的程序还没整理好,我的程序比较大,keil编译后大于2K,2051装不下,我用的4051。等过几天整理好了,我会上传我的程序。
===============================
以下内容为原帖的程序,供大家参考,版权归原帖作者所有。
感谢OURAVR论坛的网友提供的资料。
===============================
#include"AT89X52.H"
#define uchar unsigned char
unsigned int iic_delay_time = 60;
sbit scl="P2"^6;
sbit sda="P2"^7;
sbit INT_0=P3^2;
sbit INT_1=P3^3;
sbit INT_2=P3^4;
sbit have="P0"^0;
#define max_freq 108000
#define min_freq 87500
unsigned char radio_write_data[5]; //要写入TEA5767的数据
unsigned char radio_read_data[5]; //TEA5767读出的状态
unsigned int default_pll=0x2d56;//0x2d56; //默认存台的pll,95.3MHz 江苏交广
unsigned int pll;
unsigned long frequency;
//unsigned int max_pll=0x339b; //108MHz时的pll,
//unsigned int min_pll=0x9000; //70MHz时的pll`
// delay
void iic_delay(unsigned int m)
{
unsigned int n;
for(n=0;n<m;n++);
}
//启动I2C总线的函数,当scl为高电平时使sda产生一个负跳变
void iic_start()
{
sda="1";
scl="1";
iic_delay(iic_delay_time);
sda="0";
iic_delay(iic_delay_time);
scl="0";
iic_delay(iic_delay_time/2);
}
//终止I2C总线,当scl为高电平时使sda产生一个正跳变
void iic_stop()
{
sda="0";
scl="1";
iic_delay(iic_delay_time);
sda="1";
iic_delay(iic_delay_time);
}
//发送应答信号
void iic_ack()
{
sda="0";
scl="1";
scl="0";
sda="1";
}
//等待应答信号
bit waitack(void)
{
unsigned char errtime="255";//因故障接收方无ACK,超时值为25。
sda="1";
iic_delay(iic_delay_time);
while(sda)
{
errtime--;
if (!errtime)
{
iic_stop();
return 0;
}
}
scl="1";
iic_delay(iic_delay_time);
scl="0";
return 1;
}
//发送一个字节
void iic_write8bit(unsigned char input)
{
unsigned char temp;
for(temp=0;temp<8;temp++)
{
if((input<<temp)&0x80)
sda="1";
else
sda="0";
scl="1";
iic_delay(iic_delay_time);
scl="0";
}
}
//读一个字节
unsigned char iic_read8bit()
{
unsigned char temp,rbyte=0;
for(temp=0;temp<8;temp++)
{
scl="1";
iic_delay(iic_delay_time);
if(sda)
rbyte=(rbyte<<1)+1;
else
rbyte="rbyte"<<1;
scl="0";
}
return(rbyte);
}
/********************************/
//由频率计算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
}
//写5767
void radio_write(void)
{
unsigned char i;
iic_start();
iic_write8bit(0xc0); //TEA5767写地址
waitack();
for(i=0;i<5;i++)
{
iic_write8bit(radio_write_data);
waitack();
}
iic_stop();
}
//读TEA5767状态,并转换成频率
void radio_read(void)
{
unsigned char i;
unsigned char temp_l,temp_h;
pll="0";
iic_start();
iic_write8bit(0xc1); //TEA5767读地址
waitack();
for(i=0;i<5;i++)
{
radio_read_data=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();
}
//手动设置频率,mode=1,+0.1MHz; mode="0:-0".1MHz ,不用考虑TEA5767用于搜台的相关位:SM,SUD
void search()
{
radio_read();
frequency+=100;
if(frequency>max_freq)
frequency="min"_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();
}
//自动搜台,频率增加搜台
void auto_searchup()
{
radio_read();
if(frequency>max_freq)
frequency="min"_freq;
get_pll();
radio_write_data[2]=0xa0;
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();
}
//自动搜台,频率减小搜台
void auto_searchdown()
{
radio_read();
if(frequency<min_freq)
frequency="max"_freq;
get_pll();
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();
}
/**********************************************/
void delay(void)
{
unsigned char i,j;
for(i=20;i>0;i++)
for(j=248;j>0;j--);
}
//向上搜台
void key_process0()
{
delay();
if(INT_0==0)
{
auto_searchup();
}
}
//向下搜台
void key_process1()
{
delay();
if(INT_1==0)
{
auto_searchdown();
}
}
//手动搜台
key_process2()
{
delay();
if(INT_2==0)
{
search();
}
}
void main()
{
unsigned char radio_write_data[5]={0x2d,0x56,0x20,0x11,0x00};
radio_write();
while(1)
{
radio_read();
if((radio_read_data[0]&0x80)) //搜台成功标志
{
have="0";
}
if(INT_0==0)
{
key_process0();
}
if(INT_1==0)
{
key_process1();
}
/*************手动搜台*******************************************************************/
if(INT_2==0)
{
key_process2();
}
}
}
文章评论(0条评论)
登录后参与讨论