最近在用TEA5767做收音机。。。。以下是程序,但是出了问题。。#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define usd unsigned
#define max_freq 108000 //108Mhz
#define min_freq 87500 //87.5Mhz
#define max_pll 0x339b //108MHz 时的 pll.
#define min_pll 0x299d //87.5MHz 时的 pll.
#define Add_Freq 1
#define Dec_Freq 0
#define REFERENCE_FREQ 32.768
#define ATIIcxxDriverAddressW 0xC0
#define ATIIcxxDriverAddressR 0xC1
#define LED P0
void Initialization(void);
void Get_Pll(void);
void Get_Frequency(void);
void Search(uchar mode);
uchar GetKey();
void Led_Display(ulong i);
uchar GetKey();
void ATIICxx_PWrite(uchar *McuAddress,uchar count);
void ATIICxx_PRead(uchar *McuAddress,uchar count);
void I2C_Send_Byte(uchar sendbyte);
uchar I2C_Receive_Byte(void);
void I2C_Start(void);/*开始数据传输*/
void I2C_Stop(void); /*停止数据传输*/
void I2C_Ack(void); /*应答信号*/
void I2C_Noack(void); /*无应答方案*/
sbit I2C_SCK=P3^0; /*实时时钟时钟线引脚 */
sbit I2C_SDA=P3^1;/*实时时钟数据线引脚 */
sbit k1=P1^0;
sbit k2=P1^2;
/*定义显示位置*/
sbit ge=P2^3;
sbit shi=P2^2;
sbit bai=P2^1;
sbit qan=P2^0;
sbit suo=P3^6;
uchar tab[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//共阳 0, 1, 2 3 4 5 6 7 8 9
uchar radio_write_data[5]={0x2d,0x56,0x20,0x11,0x00};
//初始化写入 TEA5767 的数据(FM89.8Mhz)
uchar radio_read_data[5];
uint Pll_Data;
ulong Frequency_Data;
void delay()
{ ;; }
//读 TEA5767 状态,并转换成频率
void Radio_Read(void) {
uchar temp_l,temp_h;
Pll_Data = 0;
ATIICxx_PRead(&radio_read_data[0],5);
temp_l = radio_read_data[1];
temp_h = radio_read_data[0];
temp_h &= 0x3f;
Pll_Data = temp_h*256+temp_l;
Get_Frequency(); }
//由 PLL 计算频率
void Get_Frequency(void) {
uchar hlsi;
uint npll = 0;
npll = Pll_Data;
hlsi = radio_read_data[2]&0x10;
if (hlsi)
Frequency_Data=(ulong)((float)(npll)*(float)REFERENCE_FREQ*(float)0.25-225);
//频率单位:KHz
else
Frequency_Data=(ulong)((float)(npll)*(float)REFERENCE_FREQ*(float)0.25+225);
//由频率计算 PLL
}
void Get_Pll(void)
{
uchar hlsi;
hlsi = radio_read_data[2]&0x10;
if(hlsi)
Pll_Data=(uint)((float)((Frequency_Data+225)*4)/(float)REFERENCE_FREQ);
//频率单位:k
else
Pll_Data=(uint)((float)((Frequency_Data-225)*4)/(float)REFERENCE_FREQ);
//频率单位:k
//手动设置频率,
//mode=1,+0.1MHz;
//mode="0:-0.1MHz
}
void Search(uchar mode)
{
Radio_Read();
if(mode)
{ Frequency_Data += 100;
if(Frequency_Data > max_freq)
Frequency_Data = min_freq; }
else
{ Frequency_Data -= 100;
if(Frequency_Data < min_freq)
Frequency_Data = max_freq; }
Get_Pll();
radio_write_data[0] =Pll_Data/256;
radio_write_data[1] =Pll_Data%256;
radio_write_data[2] =0x41;
radio_write_data[3] =0x03;
radio_write_data[4] = 0x40;
ATIICxx_PWrite(&radio_write_data[0],5);
}
void main(void)
{
ulong temp;
radio_write_data[0]=0x2a;
radio_write_data[1]=0xb6;
radio_write_data[2]=0x41;
radio_write_data[3]=0x03;
radio_write_data[4]=0x40;
ATIICxx_PWrite(&radio_write_data[0],5);//初始化 TEA5767(89.8Mhz)
Frequency_Data = 89800;
while(1)
{ temp= Frequency_Data;
Led_Display(Frequency_Data);
if( k1 == 0)
{
delay();
if(k1==0)
{
while(k1 == 0)k1=1;//等待键松开
Search(Add_Freq); }}
if( k2 == 0)
{ delay();
if(k2==0)
{
while(k2 == 0)k2=1;//等待键松开
Search(Dec_Freq); }}
}}
//启动 I2C 总线,退出时 SCL 为低
void I2C_Start(void) //开始信号
{
I2C_SDA=1;
delay();
I2C_SCK=1;
delay();
I2C_SDA=0;
delay();
}
void I2C_Stop() //停止
{
I2C_SDA=0;
delay();
I2C_SCK=1;
delay();
I2C_SDA=1;
delay();
}
bit I2C_WaitAck(void)
{ uchar ucErrTime = 200; //因故障接收方无 ACK,超时值。
I2C_SCK=0;
I2C_SDA=1;
I2C_SCK=1;
while(I2C_SDA)
{ ucErrTime--;
if (ucErrTime == 0)
{ I2C_Stop(); break; }
}
I2C_SCK=0; return 1; } //MCU 应答信
void I2C_Ack(void)
{ uchar i;
I2C_SCK=1;
delay();
while((I2C_SDA==1)&&(i<250))i++;
I2C_SCK=0;
delay();}
//MCU 发送非应答信号
void I2C_Noack(void)
{ I2C_SCK=0; I2C_SDA=1;
delay();
I2C_SCK=1;
delay();
I2C_SCK=0; }
void I2C_Send_Byte(uchar sendbyte)
{
uchar i,j;
j=sendbyte;
for(i=0;i<8;i++)
{
j=j<<1;
I2C_SCK=0;
delay();
I2C_SDA=CY;
delay();
I2C_SCK=1;
delay();
}
I2C_SCK=0;
delay();
I2C_SDA=1;
delay();
}
static uchar I2C_Receive_Byte(void)
{ uchar i,k;
I2C_SCK=0;
delay();
I2C_SDA=1;
delay();
for(i=0;i<8;i++)
{
I2C_SCK=1;
delay();
k=(k<<1)|I2C_SDA;
I2C_SCK=0;
delay();
}
return k;}
void ATIICxx_PWrite(uchar *McuAddress,unsigned char count)
{ I2C_Start();
I2C_Send_Byte( ATIIcxxDriverAddressW );
while(count--)
{ I2C_Send_Byte( *(uchar*)McuAddress );
((uchar*)McuAddress)++; }
I2C_Stop(); }
void ATIICxx_PRead(uchar *McuAddress,uchar count)
{ I2C_Start();
I2C_Send_Byte( ATIIcxxDriverAddressR );
while(count--)
{ *McuAddress = I2C_Receive_Byte();
I2C_Ack(); McuAddress++; }
I2C_Noack();
I2C_Stop(); }
//LED显示函数
void Led_Display(ulong i)
{suo=1;
suo=0;
LED = tab[i/100000];
qan = 0;
bai = 1;
shi = 1;
ge = 1;
suo=1;
suo=0;
LED = tab[(i%100000)/10000];
qan = 1;
bai = 0;
shi = 1;
ge = 1;
suo=1;
suo=0;
LED = tab[((i%100000)%10000)/1000]&0x7f;
qan = 1; bai = 1; shi = 0; ge = 1;
suo=1;
suo=0;
LED=tab[(((i%100000)%10000)%1000)/100];
qan = 1; bai = 1; shi = 1; ge = 0;
suo=1;
suo=0;
} |