/*----------- TEA5767 驱动-----------*/
/*------ 外部晶振为 12MHz-------*/
/*--------最后修改.02.26-------*/
#include "STC12C5620AD.H"
#include "IIC.H"
#include "delay.h"
#include "Radio.H"
#include "Channel_Handle.H"
#define Max_Freq 108000
#define Min_Freq 87500
/********************** 声明外部变量**************************/
extern uchar data Display_FM[9]; //From Main.c
/********************** 定义变量**************************/
uchar TEA5767_Write_Data[5]={0x29,0x9e,0x40,0x12,0x40}; //要写入TEA5767的数据
uchar TEA5767_Read_Data[5];
unsigned long Frequency;
unsigned int PLL=0x299e;
/********************** TEA5767 写入数据**************************/
void TEA5767_Write(void)
{
uchar i;
IIC_Start();
IIC_Write_Byte(0xc0); //TEA5767写地址
if(!IIC_Test_ACK())
{
for(i=0;i<5;i++)
{
IIC_Write_Byte(TEA5767_Write_Data[i]);
IIC_ACK();
}
}
IIC_Stop();
}
/********************** 由PLL获取频率**************************/
void TEA5767_Get_Frequency(void)
{
uchar HLSI;
uint NPLL=0;
NPLL=PLL;
HLSI=TEA5767_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
}
/**********************显示当前频率**************************/
void TEA5767_Display_Frequency()
{
TEA5767_Get_Frequency(); //由PLL得到频率
Display_FM[0]=(Frequency/100000);
if(Display_FM[0]==0)
Display_FM[0]=0x20; //第一位为0的话,不显示
else
Display_FM[0]|=0x30;
Frequency=Frequency%100000;
Display_FM[1]=0x30|(Frequency/10000);
Frequency=Frequency%10000;
Display_FM[2]=0x30|(Frequency/1000);
Frequency=Frequency%1000;
Display_FM[4]=0x30|(Frequency/100);
}
/**********************初始化TEA5767**************************/
void TEA5767_Init()
{
TEA5767_Write(); //写入初始化参数
//EEPROM_Save_PLL(1,0x29de); //预置第1频道
//EEPROM_Save_PLL(2,0x2bb0); //预置第2频道
//EEPROM_Save_PLL(3,0x2cc9); //预置第3频道
//EEPROM_Save_PLL(4,0x2fa5); //预置第4频道
//EEPROM_Save_PLL(5,0x3050); //预置第5频道
//EEPROM_Save_PLL(6,0x309a); //预置第6频道
//EEPROM_Save_PLL(7,0x328e); //预置第7频道
//EEPROM_Save_PLL(8,0x2fa5); //预置第8频道
//EEPROM_Save_PLL(9,0x2fa5); //预置第9频道
//EEPROM_Save_PLL(10,0x2fa5); //预置第10频道
//EEPROM_Save_PLL(11,0x2fa5); //预置第11频道
//EEPROM_Save_PLL(12,0x2fa5); //预置第12频道
//EEPROM_Save_PLL(13,0x2fa5); //预置第13频道
//EEPROM_Save_PLL(14,0x2fa5); //预置第14频道
//EEPROM_Save_PLL(15,0x2fa5); //预置第15频道 注:第一次编译使用预置频道
}
/**********************手动搜索**************************/
void TEA5767_User_Search(uchar Mode)
{
switch (Mode) //向上搜索
{
case 1:
PLL+=1;
if(PLL>0x3370) //108MHz时的PLL,
PLL=0x298d; //87.5MHz时的PLL,
break;
case 0: //向下搜索
PLL-=1; //改变该值可提高精度,但步进变得缓慢
if(PLL<0x298d)
PLL=0x3270;
break;
}
TEA5767_Write_Data[0]=(PLL/256)&0x7f;
TEA5767_Write_Data[1]=PLL%256;
TEA5767_Write(); //重新写入参数
}
/**********************更新PLL**************************/
void TEA5767_Update_PLL(void)
{
TEA5767_Write_Data[0]=(PLL/256)&0x7f;
TEA5767_Write_Data[1]=PLL%256;
TEA5767_Write(); //重新写入参数
}