📄 tunner.c
字号:
/*--------------------------------------------------
Function : Radio Tunner Controller
IC : TC9257
Author : Pei Feng
Date : 2007/3/8
Modified : 2007/3/28
---------------------------------------------------*/
#include "mb95100.h"
#include "IOdef.h"
#include "TYPEdef.h"
#define FM_D0 0x9c // FM,crystal : 7.2MHZ ,reference frequency : 25KHZ
#define AM_D0 0x89 // AM,crystal : 7.2MHZ ,reference frequency : 9KHZ
#define FM_D2 0x4e // open FM's power,OT-3 output low level
#define FM_ST_RQ 0x0e // IF high during search channel,normal is low
#define AM_D2 0x4c // close FM'S power,OT-3 output high level
#define AM_ST_RQ 0x0c // IF high during search channel,normal is low
#define IF_START 0x6553 // IF Counter Star,64ms GATE,I/O8 output
#define IF_STOP 0x6453
#define PLL_RESET 0x64d3 // Reset PLL UNLOCK bit ,I/O8 output
#ifdef TEF6606
unsigned char Write_Buff[16];
unsigned char Read_Buff[5];
unsigned char Write_Phase;
unsigned char Read_Phase;
unsigned char IIC_Error;
unsigned char Tuner_Counter;
unsigned char Write_Length;
#endif
unsigned char FMTable[19]={
0x18,0x01,0x1a,0x6a,0xba,0xce,0x01,
0x01,0x1a,0x6a,0xba,0xce,0x01,
0x01,0x01,0x01,0x01,0x01,0x01};
unsigned char AMTable[13]={
0x12,0x01,0x0a,0x35,0x62,0x7b,0x01,
0x01,0x01,0x01,0x01,0x01,0x01};
RadioMode Radio;
unsigned long IF;
unsigned char S_Meter_On_Off;
unsigned char S_Meter_Value;
extern KeyMode Key;
extern TIME watch;
extern SystemParameter System;
extern void Delay(unsigned char x);
extern void LCD_Clear_all(void);
extern void LCD_NO_display(unsigned char wei,unsigned char content);
extern void LCD_AMPM_OFF(void);
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
//for BUS delay time
void BusDelay(void) //about 1.0us
{
__wait_nop();
__wait_nop();
__wait_nop();
__wait_nop();
__wait_nop();
__wait_nop();
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void LCD_Clear_Radio(void)
{
SEG22_COM0 = 0; // <-
SEG30_COM0 = 0; // ->
SEG10_COM0 = 0; // scan_search
SEG01_COM2 = 0; // search_save
SEG00_COM0 = 0; // auto_search
SEG02_COM0 = 0; // handle_search
SEG00_COM2 = 0; // AM
SEG00_COM1 = 0; // FM
SEG01_COM0 = 0; //FM 1
SEG02_COM1 = 0; //FM 2
SEG03_COM0 = 0; //FM 3
SEG01_COM1 = 0; //AM 1
SEG02_COM2 = 0; //AM 2
if(System.Special_disp_Timer==0)
{
SEG15_COM2 = 0; // up point
SEG24_COM2 = 0; // down point
}
LCD_NO_display(7,10); //"M"
LCD_NO_display(8,10); //Chanel NUM
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void RadioStateDis(void)
{
SEG22_COM0 = 0; // <-
SEG30_COM0 = 0; // ->
SEG10_COM0 = 0; // undisplay scan_search
SEG01_COM2 = 0; // undisplay search_save
SEG00_COM0 = 0; // undisplay auto_search
SEG02_COM0 = 0; // undisplay handle_search
if(Radio.smode==0)
SEG00_COM0 = 1; // display auto_search
if(Radio.smode==2)
{
SEG02_COM0 = 1; // display handle_search
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//display radio information
void LCD_radio_display(unsigned int content)
{
LCD_Clear_Radio();
if(System.Special_disp_Timer==0) //NO temp display
{
LCD_AMPM_OFF();
if(content/1000)
LCD_NO_display(1,content/1000); // display frequence
else
LCD_NO_display(1,10);
LCD_NO_display(2,content/100-content/1000*10);
LCD_NO_display(3,content/10-content/100*10);
LCD_NO_display(4,content-content/10*10);
LCD_NO_display(5,10);
if(Radio.band/3==0)
SEG24_COM2 = 1; // FM display dot
}
if(Radio.chanel)
LCD_NO_display(8,Radio.chanel);
switch(Radio.band)
{
case 0: SEG00_COM1 = 1; //FM 1
SEG01_COM0 = 1;
LCD_NO_display(7,13); //"M"
break;
case 1: SEG00_COM1 = 1; //FM 2
SEG02_COM1 = 1;
LCD_NO_display(7,13);
break;
case 2: SEG00_COM1 = 1; //FM 3
SEG03_COM0 = 1;
LCD_NO_display(7,13);
break;
case 3: SEG00_COM2 = 1; //AM 1
SEG01_COM1 = 1;
break;
case 4: SEG00_COM2 = 1; //AM 2
SEG02_COM2 = 1;
break;
}
switch(Radio.smode)
{
case 0: SEG00_COM0=1; //auto_search
break;
case 1: SEG10_COM0=1; //scan_search
break;
case 2: SEG02_COM0=1; //handle_search
break;
case 3: SEG01_COM2=1; //search_save
break;
}
if(Radio.down)
SEG22_COM0 = 1; //direction
else
SEG30_COM0 = 1;
if(Radio.stop==1)
RadioStateDis();
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//write 24 bit data to tc9257
#ifdef LA1787
void WriteTC9257(unsigned char address,unsigned int data1,unsigned char data2)
{
unsigned char i;
DA_OUT_INPUT = 1; // data line output enable
PLL_PRD = 0; // set low level,start
for(i=0;i<8;i++)
{
if(address&0x01)
PLL_DA = 1;
else
PLL_DA = 0;
address >>= 1;
PLL_CLK = 0;
BusDelay();
PLL_CLK = 1;
BusDelay();
}
PLL_PRD = 1; // period return to high level
for(i=0;i<16;i++)
{
if(data1&0x01)
PLL_DA = 1;
else
PLL_DA = 0;
data1 >>= 1;
PLL_CLK = 0;
BusDelay();
PLL_CLK = 1;
BusDelay();
}
for(i=0;i<8;i++)
{
if(data2&0x01)
PLL_DA = 1;
else
PLL_DA = 0;
data2 >>= 1;
PLL_CLK = 0;
BusDelay();
PLL_CLK = 1;
BusDelay();
}
PLL_PRD = 0; // stop
BusDelay();
PLL_PRD = 1; //
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//read 24 bit data from tc9257
unsigned long ReadTC9257(unsigned char address)
{
unsigned char i;
unsigned long data;
DA_OUT_INPUT = 1; // data line output enable
PLL_PRD = 0; // set low level,start
data = 0;
for(i=0;i<8;i++)
{
if(address&0x01)
PLL_DA = 1;
else
PLL_DA = 0;
address >>= 1;
PLL_CLK = 0;
BusDelay();
PLL_CLK = 1;
BusDelay();
}
PLL_PRD = 1; // period return to high level
DA_OUT_INPUT = 0; // data line input enable
for(i=0;i<24;i++)
{
data >>= 1;
PLL_CLK = 0;
BusDelay();
if(PLL_DA)
data |= 0x00800000;
PLL_CLK = 1;
BusDelay();
}
PLL_PRD = 0; //stop
BusDelay();
PLL_PRD = 1;
return data;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// FM/AM search
void RadioTune(unsigned char d0,unsigned char d2,unsigned int frequency)
{
unsigned char i,j;
WriteTC9257(0xd2,IF_STOP,d2); //set ST/RQ high first
WriteTC9257(0xd0,frequency,d0);
for(i=0;i<16;i++)
{
WriteTC9257(0xd2,PLL_RESET,d2);
for(j=0;j<16;j++)
{
if((ReadTC9257(0xd3)|0xfffffffe)!=0xfffffffe) // wait PLL enable
break;
}
if((ReadTC9257(0xd3)|0xfffffffd)==0xfffffffd) //PLL OK
break;
}
for(i=0;i<120;i++)
Delay(60); // Modified for accelerating searching(60ms)
if(!SD_ST) //SD low, no channel
{
IF = 0; //Channel_OK = 0x00;
return;
}
Delay(20); //19us
WriteTC9257(0xd2,IF_START,d2);
for(i=0;i<120;i++) //120
{
if((ReadTC9257(0xd1)&0x00200000)==0) //busy?--0:idle
break;
}
if((ReadTC9257(0xd1)&0x00100000)==0) //over flow ?
IF = (ReadTC9257(0xd1))&0x000fffff;
Delay(20); //96us
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//handle up/down tune FM
void HandleTuneFM(void)
{
unsigned int k;
Radio.chanel=0;
if(Radio.down)
Radio.fm--;
else
Radio.fm++;
if(Radio.fm<875)
Radio.fm = 1080;
if(Radio.fm>1080)
Radio.fm = 875;
k = Radio.fm+107 ;
k = k<<2; //eaqual (fr.fm+107)*4
RadioTune(FM_D0,FM_D2,k); //d0,d2,frequency
WriteTC9257(0xd2,IF_STOP,FM_ST_RQ); //FM_ST_RQ 0x0e
LCD_radio_display(Radio.fm);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//auto up/down one step tune FM
void AutoTuneFM(void)
{
unsigned char i;
unsigned int k,lastfrequce;
lastfrequce = Radio.fm;
Radio.chanel=0;
Mute_ON();
for(i=0;i<206;i++) //step 0.1Mhz
{
if(Radio.stop)
break;
if(Radio.down)
Radio.fm--;
else
Radio.fm++;
if(Radio.fm<875)
Radio.fm = 1080;
if(Radio.fm>1080)
Radio.fm = 875;
k = Radio.fm+107 ;
k = k<<2; //eaqual (fr.fm+107)*4
RadioTune(FM_D0,FM_D2,k); //d0,d2,frequency
WriteTC9257(0xd2,IF_STOP,FM_ST_RQ); //FM_ST_RQ 0x0e
LCD_radio_display(Radio.fm);
if((IF<0xA7080)||(IF>0xA7580)) // 10.69M to 10.71M
{
IF = 0;
continue ;
}
/* if(Radio.down)
{
if(Radio.fm==lastfrequce-1) // repeat chanel check
{
IF = 0;
continue ;
}
}
else
{
if(Radio.fm==lastfrequce+1) // repeat chanel check
{
IF = 0;
continue ;
}
}*/
break;
}
Radio.stop=1;
RadioStateDis(); //search ending,undisplay state information
Mute_OFF();
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//up/down scan tune FM
void ScanTuneFM(void)
{
unsigned char i,timer,LastRadioMode;
unsigned int k,lastfrequce;
LastRadioMode = Radio.smode;
Radio.chanel=0;
Radio.smode = 1;
lastfrequce = Radio.fm;
Mute_ON();
for(i=0;i<206;i++) //step 0.1Mhz
{
if(Radio.stop)
break;
if(Radio.down)
Radio.fm--;
else
Radio.fm++;
if(Radio.fm<875)
Radio.fm = 1080;
if(Radio.fm>1080)
Radio.fm = 875;
k = Radio.fm+107 ;
k = k<<2; //eaqual (fr.fm+107)*4
RadioTune(FM_D0,FM_D2,k); //d0,d2,frequency
WriteTC9257(0xd2,IF_STOP,FM_ST_RQ); //FM_ST_RQ 0x0e
LCD_radio_display(Radio.fm);
if((IF<0xA7080)||(IF>0xA7580)) // 10.69M to 10.71M
{
IF = 0;
continue ;
}
/*if(Radio.down)
{
if(Radio.fm==lastfrequce-1) // repeat chanel check
{
IF = 0;
continue ;
}
}
else
{
if(Radio.fm==lastfrequce+1) // repeat chanel check
{
IF = 0;
continue ;
}
}*/
lastfrequce = Radio.fm;
timer=watch.second+5; //PLAY 5 SECONDS
if(timer>59)
timer=timer-60;
Mute_OFF();
while(1)
{
if(Radio.stop)
break;
if(Radio.down)
{
if(watch.ms125%8<4) //down
SEG22_COM0=1;
else
SEG22_COM0=0;
}
else
{
if(watch.ms125%8<4) //up
SEG30_COM0=1;
else
SEG30_COM0=0;
}
if(timer==watch.second)
{
Mute_ON();
break;
}
}
}
Radio.stop = 1;
Radio.fm=lastfrequce; //Retain the last chanel
k = (Radio.fm+107)<<2;
RadioTune(FM_D0,FM_D2,k);
WriteTC9257(0xd2,IF_STOP,FM_ST_RQ);
Radio.smode= LastRadioMode;
LCD_radio_display(Radio.fm);
Mute_OFF();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//up scan and store tune FM
void ScanSaveFM(void)
{
unsigned char i,j,timer,LastRadioMode;
unsigned int k,lastfrequce;
Mute_ON();
Radio.chanel=0;
lastfrequce=0;
LastRadioMode = Radio.smode;
Radio.smode = 3;
Radio.down = 0;
FMTable[0]=0;
for(j=1;j<19;j++)
FMTable[j]=1;
j = 0;
k = 3924; //(874+107)*4
for(i=0;i<206;i++) //step 0.1Mhz
{
if(Radio.stop)
break;
k = k+4; //eaqual (fr.fm+107)*4
RadioTune(FM_D0,FM_D2,k); //d0,d2,frequency
WriteTC9257(0xd2,IF_STOP,FM_ST_RQ); //FM_ST_RQ 0x0e
switch(j/6)
{
case 0: Radio.band=0; //FM1
break;
case 1: Radio.band=1; //FM2
break;
case 2: Radio.band=2; //FM3
break;
}
Radio.fm=(k>>2)-107;
LCD_radio_display(Radio.fm);
if((IF<0xA7080)||(IF>0xA7580)) // 10.69M to 10.71M
{
IF = 0;
continue ;
}
/*if(k==lastfrequce+4) // repeat chanel check
{
IF = 0;
continue ;
}*/
lastfrequce = k;
Radio.store = 1; //default--store the chanel
timer=watch.second+5; //PLAY 5 SECONDS
if(timer>59)
timer=timer-60;
Mute_OFF();
while(1)
{
if(Radio.stop)
break;
if(watch.ms125%8<4) // up arrow
SEG30_COM0=1;
else
SEG30_COM0=0;
if(j<18)
LCD_NO_display(8,(j%6)+1);
if(timer==watch.second||!Radio.store)
{
Mute_ON();
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -