⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7540 demo code.c

📁 收音头7540代码
💻 C
📖 第 1 页 / 共 2 页
字号:
signed char xdata TestPointVal ;  
unsigned int xdata TestPointFreq ;
signed int xdata Slope  ;
signed int xdata Slope2  ;
unsigned char xdata radiobuffer[53];
//================================================================
#define LW_FREQ						300
#define AM_FREQ						2000
#define FM_FREQ						2000
#define FM_WB_START_FREQ			2300
#define FM_WB_STOP_FREQ			2600
#define FM_NORMAL_START_FREQ		8000
#define FM_OIRT_START_FREQ			6000
#define FM_OIRT_STOP_FREQ			7500
#define FM_JAPAN_START_FREQ		7500
#define FM_JAPAN_STOP_FREQ		9100
#define FreqInFM(x)					(x>AM_FREQ)
#define FreqInAM(x)					(x<FM_FREQ)
#define FreqInNormalFM(x)			(x>FM_NORMAL_START_FREQ)
#define FreqInOirtFM(x)				((x>FM_OIRT_START_FREQ && x<FM_OIRT_STOP_FREQ)&&Radio.Area==AREA_OIRT)
#define FreqInJapanFM(x)				(x>FM_JAPAN_START_FREQ && x<FM_JAPAN_STOP_FREQ&&Radio.Area==AREA_JAPAN)
#define FreqInWBFM(x)				(x>FM_WB_START_FREQ && x<FM_WB_STOP_FREQ)
#define FreqInMWAM(x)				(x>LW_FREQ && x<FM_FREQ)
#define FreqInLWAM(x)				(x<LW_FREQ)

//====================================================//
//=================== frequency  define======================//
//====================================================//
//FM frequency define
#define FM80_0MHZ         					8000  
#define FM83_0MHZ         					8300
#define FM87_0MHZ         					8700
#define FM106_1MHZ						10610	// FM 106.1MHz
#define FM93_3MHZ						9330	// FM 93.3MHz
#define FM94_5MHZ						9450
#define FM98_6MHZ						9860
#define FM106_9MHZ						10690
#define FM98_1MHZ						9810
#define FM70_1MHZ           				7010
#define FM87_5MHZ						8750
#define FM89_5MHz           				8950
#define FM92_7MHz           				9270
#define FM96_1MHz           				9610
#define FM99_6MHz           				9960
#define FM103_1MHz         				10310
#define FM106_5MHz         				10650
#define FM108MHZ						10800
//ORIT
#define FM65_0MHZ						6500
#define FM67_0MHZ           				6700
#define FM71_0MHZ           				7100
#define FM74_0MHZ           				7400
//JAPAN
#define FM76_0MHZ						7600
#define FM80_0MHZ						8000
#define FM85_0MHZ           				8500
#define FM90_0MHZ						9000
//AM
#define AM530KHZ						530 
#define AM900KHZ						900 
#define AM720KHZ             				720
#define AM999KHZ						999
#define AM1404KHZ           				1404
//====================================================//
//================ FST register bit define======================//
//====================================================//
//================ TDA7540 register bit define======================//
//====================================================//
//For PLL current Tun_Buff[0]
//Bit3~0
#define PLL_HICURRENT_0MA       			0x00
#define PLL_HICURRENT_0_5mA     		0x01
#define PLL_HICURRENT_1MA       			0x02
#define PLL_HICURRENT_1_5mA     		0x03
#define PLL_HICURRENT_2MA       			0x04
#define PLL_HICURRENT_2_5mA     		0x05
#define PLL_HICURRENT_3MA       			0x06
#define PLL_HICURRENT_3_5mA     		0x07
#define PLL_HICURRENT_4MA       			0x08
#define PLL_HICURRENT_4_5mA     		0x09
#define PLL_HICURRENT_5MA       			0x0A
#define PLL_HICURRENT_5_5mA     		0x0B
#define PLL_HICURRENT_6MA       			0x0C
#define PLL_HICURRENT_6_5mA     		0x0D
#define PLL_HICURRENT_7MA       			0x0E
#define PLL_HICURRENT_7_5mA     		0x0F
//Bit 5~4
#define PLL_LOWCURRENT_0uA      			0x00
#define PLL_LOWCURRENT_50uA     		0x10
#define PLL_LOWCURRENT_100uA    		0x20
#define PLL_LOWCURRENT_150uA    		0x30
//Bit6
#define PLL_LOWCURRENT_MODE     		0x00
#define PLL_HIGHCURRENT_MODE    		0x40
//Bit7
#define PLL_FMSEEK_ON           			0x00
#define PLL_FMSEEK_OFF          			0x80

//Sample time and IFC   Tun_Buff[5]
#define PLL_TSAMPLE_FM20_48_AM128	0x00
#define PLL_TSAMPLE_FM10_24_AM64    	0x01
#define PLL_TSAMPLE_FM5_12_AM32      	0x02
#define PLL_TSAMPLE_FM2_56_AM16      	0x03
#define PLL_TSAMPLE_FM1_28_AM8        	0x04
#define PLL_TSAMPLE_FM0_64_AM4        	0x05
#define PLL_TSAMPLE_FM0_32_AM2        	0x06
#define PLL_TSAMPLE_FM0_16_AM1        	0x07

#define PLL_IFC_DISABLE                       	0x00
#define PLL_IFC_ENABLE                        	0x08

#define PLL_IFC_FMMODE                    		0x10
#define PLL_IFC_AM450K                         	0x20
#define PLL_IFC_AM10_7M                      	0x30

#define DISABLE_MUTEONHOLD               	0x00
#define ENABLE_MUTEONHOLD                	0x40

#define PLL_LOCK_DISABLE                    	0x00
#define PLL_LOCK_ENABLE                     		0x80

//center freqency and delta window   Tun_Buff[5]
#define PLL_IFC_CENTER_10_7MHZ     		0x0F
#define PLL_IFC_CENTER_450KHZ      		0x01

#define PLL_IFC_EW_FM6_25_AM1K     		0x60
#define PLL_IFC_EW_FM12_5_AM2K     		0x80
#define PLL_IFC_EW_FM25_AM4K       		0xA0
#define PLL_IFC_EW_FM50_AM8K       		0xC0
#define PLL_IFC_EW_FM100_AM16K     		0xE0


//For reference freqency
#define PLL_REFERECE_50KHz      			0x20     
#define PLL_REFERECE_25KHz      			0x28
#define PLL_REFERECE_10KHz      			0x30
#define PLL_REFERECE_9KHz       			0x38
#define PLL_REFERECE_2KHz       			0x00

//For SD threshold
#define SSTOP_IFC                      			0x00
#define SSTOP_IFC_0_89V          			0x10    // IFC=H & FSU>0.89V
#define SSTOP_IFC_1_16V           			0x20    // IFC=H & FSU>1.16V
#define SSTOP_IFC_1_43V           			0x30    // IFC=H & FSU>1.43V
#define SSTOP_IFC_1_7V             			0x40    // IFC=H & FSU>1.7V
#define SSTOP_IFC_1_97V           			0x50
#define SSTOP_IFC_2_24V           			0x60
#define SSTOP_IFC_2_51V           			0x70
#define SSTOP_IFC_2_78V           			0x80
#define SSTOP_IFC_3_05V           			0x90
#define SSTOP_IFC_3_32V           			0xA0
#define SSTOP_IFC_3_59V           			0xB0
#define SSTOP_IFC_3_86V           			0xC0
#define SSTOP_IFC_4_13V           			0xD0
#define SSTOP_IFC_4_40V           			0xE0
#define SSTOP_IFC_4_67V           			0xF0
//================================================================
signed char ChangeTV2(unsigned char inputbuff)
{
	if(inputbuff&0x80)
		return (inputbuff&0x7F);
	else
		return (0-(inputbuff&0x7F));
}
unsigned char ChangeToTV2(signed char inputbuff)
{
	if(inputbuff&0x80)
		return (0x80-(inputbuff&0x7F));
	else
		return ((inputbuff&0x7F)+0x80);
}
unsigned char GetTV2(unsigned int inputfreq)
{
	signed char temp;

	if(FreqInAM(inputfreq))  //AM return 0x90
		return(0x90);
/*		
	if(FreqInOirtFM(inputfreq))
	{
		if(inputfreq<6650)
			return(radiobuffer[43]);
		else if(inputfreq<7050)
			return(radiobuffer[44]);
		else
			return(radiobuffer[45]);
	}
	if(FreqInJapanFM(inputfreq))
	{
		if(inputfreq<8100)
			return(radiobuffer[50]);//need to consistent with the module EEPROM table
		else if(inputfreq<8600)
			return(radiobuffer[51]);
		else
			return(radiobuffer[52]);
	}	
	if(FreqInWBFM(inputfreq))
	//	//return(EepromBuff[9]);//need to consistent with the module FE design and EEPROM table
		return(0xff);//tempory
*/	
	if(inputfreq<TestPointFreq)
		  temp = (signed char)((signed int)(TestPointFreq-inputfreq)/Slope+TestPointVal);
	else
      temp=(signed char)((signed int)(TestPointFreq-inputfreq)/Slope2+TestPointVal);

	return(ChangeToTV2(temp));
}
void InitRadio(void)
{
//first FM mode
  radiobuffer[1] = (((fm_freq+1070)/fm_step-32)&0xff); 
	radiobuffer[2] = ((((fm_freq+1070)/fm_step-32)>>8)&0xff); 

	TestPointFreq=9810;
	TestPointVal=ChangeTV2(radiobuffer[41]);

	if(radioTV2Eeprom[0]!=radiobuffer[41])
	{
		Slope=(signed int)(9810-8810)/(ChangeTV2(radiobuffer[40])-ChangeTV2(radiobuffer[41])); //for Xinfa
	}
	else
	{
		Slope=(signed int)(30000);
	}
		
	if(radioTV2Eeprom[41]!=radiobuffer[42])
	{
		Slope2=(signed int)(10750-9810)/(ChangeTV2(radiobuffer[41])-ChangeTV2(radiobuffer[42]));
	}
	else
	{
		Slope2=(signed int)(30000);
	}
}
void FM_AM(void)
{ 
    unsigned char i;

    if(band_fm)
	{
		  radiobuffer[0] &= 0x7F;
		  radiobuffer[0]= radiobuffer[0]|PLL_HIGHCURRENT_MODE|PLL_LOWCURRENT_100uA\
				              |Radio_PLL_FMISet(fm_step);	  
	    radiobuffer[1] = (((fm_freq+1070)/fm_step-32)&0xff); 
		  radiobuffer[2] = ((((fm_freq+1070)/fm_step-32)>>8)&0xff); 
      radiobuffer[4]= GetTV2(fm_freq);
      radiobuffer[5]&= 0x48;
      radiobuffer[5]= radiobuffer[5] | PLL_LOCK_ENABLE | PLL_IFC_FMMODE \
			                  |PLL_TSAMPLE_FM20_48_AM128;
      radiobuffer[6]= PLL_IFC_CENTER_10_7MHZ | PLL_IFC_EW_FM12_5_AM2K;	//IF Counter control 2
      		  
		  //Change tv2 offset and switch "ON" iss filter for weather band	
/*		  if(FreqInWBFM(fm_freq))
		  {
          radiobuffer[7] &= 0xCF;
          radiobuffer[7] |= 0x20;
          radiobuffer[8] |= 0x08;	
		  }
		  else*/
		  {
			   radiobuffer[7] &= 0xCF;
			   radiobuffer[8] &= 0xf7;
		  }
/*		  
	    //Vco divder and pll reference
		  if(FreqInOirtFM(fm_freq))
			    radiobuffer[14]= 0x36;//y00110110;
		  else if(FreqInJapanFM(fm_freq))
			    radiobuffer[14]=0x22;//y00100010;
		  else if(FreqInWBFM(fm_freq))
			    radiobuffer[14]=0x2f;//y00101111;
		  else
*/		  
			    radiobuffer[14]=0x25;//y00100101;
			//IF gain
		  radiobuffer[19] &= 0xf9;
		  radiobuffer[19] |=0x06;//18dB in FM
/8		
		  if(FreqInOirtFM(fm_freq))//For our ref module
		  {
			    radiobuffer[23]&=0xf3;
			    radiobuffer[23]=radiobuffer[23]|0x08;//y00001000;
		  } 
		  else*/
		  {
			    radiobuffer[23]&=0xf3;
			    radiobuffer[23]=radiobuffer[23]|0x0c;//y00001100;
		  }					
		  radiobuffer[31] &= 0xf3;//y11110011;
		  			
	    for(i=0; i<40; i++)
	    {
	       I2CWriteByteRadio(RADIO_ADDR, i,  radiobuffer[i]);
	    }
	 }
	 else 
	 {
	 	  radiobuffer[0]&= 0x80;
		  radiobuffer[0]=radiobuffer[0]| PLL_HIGHCURRENT_MODE | PLL_LOWCURRENT_50uA\
				              |Radio_PLL_AMISet(am_freq);				              
	    radiobuffer[1] = (((am_freq+10700)/1-32)&0xff);
		  radiobuffer[2] = (((am_freq+10700)/1-32)>>8);
      radiobuffer[4] = GetTV2(am_freq);
      //IF Counter control 1
		  radiobuffer[5]&= 0x48;
		  radiobuffer[5] = radiobuffer[5] | PLL_LOCK_ENABLE | PLL_IFC_AM450K\
						|PLL_TSAMPLE_FM10_24_AM64;
		  //IF Counter control 2
		  radiobuffer[6] = PLL_IFC_CENTER_450KHZ| PLL_IFC_EW_FM12_5_AM2K;	
		
		  //Vco divder and pll reference
		  radiobuffer[14]= 0x75;//y01110101;
		  //IF gain
		  radiobuffer[19] &= 0xf9;//9dB in AM

	    for(i=0; i<40; i++)
	    {
	        I2CWriteByteRadio(RADIO_ADDR, i,  radioAMEeprom[i]);
	    }
	 }
}
unsigned char Radio_PLL_FMISet(unsigned int FMfreq)
{
	if(FreqInOirtFM(FMfreq))//ORIT
	{
		if (FMfreq < FM67_0MHZ)
		{
			return (PLL_HICURRENT_2_5mA);
		}
		if (FMfreq < FM71_0MHZ)
		{
			return (PLL_HICURRENT_3MA);
		}
		return (PLL_HICURRENT_3_5mA);
	}
	else if(FreqInJapanFM(FMfreq))//Japan
	{
		if (FMfreq < FM80_0MHZ)
		{
			return (PLL_HICURRENT_1_5mA);
		}
		if (FMfreq < FM85_0MHZ)
		{
			return (PLL_HICURRENT_2MA);
		}
		return (PLL_HICURRENT_3MA);
	}
	if(FreqInWBFM(FMfreq))//Weather band
	{
		return (PLL_HICURRENT_0_5mA);
	}	
	else//Normal FM
	{
		if (FMfreq < FM94_5MHZ)
		{
			return (PLL_HICURRENT_1_5mA);
		}
		if (FMfreq < FM98_6MHZ)
		{
			return (PLL_HICURRENT_2_5mA);
		}
		if (FMfreq < FM106_9MHZ)
		{
			 return (PLL_HICURRENT_3MA);
		}
		return (PLL_HICURRENT_3_5mA);
	}	
}

unsigned char Radio_PLL_AMISet(unsigned int AMfreq)
{
	if (AMfreq < AM530KHZ)//LW
	{
		return (PLL_HICURRENT_2MA);
	}
	if (AMfreq < AM999KHZ)//MW
	{
		return (PLL_HICURRENT_2_5mA);
	}
	if (AMfreq < AM1404KHZ)
	{
		return (PLL_HICURRENT_3MA);
	}
	return (PLL_HICURRENT_3_5mA);
}
 
//我国调频电台频率间隔为0.1M
//台湾为88-108MHz
//ISS = intelligent selectivity system
//在AM下不支持立体功能

//Low FM band: (87.5MHz~92.0MHz) 2mA
//Mid FM band: (92.1MHz~98.6MHz) 3mA
//Mid-high FM band: (98.7MHz~106.9MHz)4mA
//High FM band: (107MHz~108MHz) 4.5mA

//Freq change: refresh the data in Tuner register 0,1,2	 
//Freq related: Register 0,1,2,4(different configuration due to the country) 
void Refresh_Radio( )
{ 
    if(band_fm)
	{ 
//--------Register 0, Page mode OFF---------------------------

    	 if(fm_freq<=9200)
    	 	 I2CWriteByte(ST7540_address, 0x00, 0xe6);  //0xE4   
    	 else if(fm_freq<=9860)
    	 	 I2CWriteByte(ST7540_address, 0x00, 0xe6);  //0xE6   
    	 else if(fm_freq<=10690)
    	 	 I2CWriteByte(ST7540_address, 0x00, 0xe7);  //0xE8   
    	 else
    	 	 I2CWriteByte(ST7540_address, 0x00, 0xe7);  //0xE9   

        //I2CWriteByte(ST7540_address, 0, EepromBuff[0]);   

	    RadioBuffer[1] = (((fm_freq+1070)/fm_step-32)&0xff); 
		RadioBuffer[2] = ((((fm_freq+1070)/fm_step-32)>>8)&0xff); 
		I2CWriteByte(ST7540_address, 1, RadioBuffer[1]);     
		I2CWriteByte(ST7540_address, 2, RadioBuffer[2]);     
        
		if(fm_freq <= 8810)
		    I2CWriteByte(ST7540_address, 4, RadioBuffer[40]); 
  	    else if(fm_freq <= 9810)
		    I2CWriteByte(ST7540_address, 4, RadioBuffer[41]); 
		else
		    I2CWriteByte(ST7540_address, 4, RadioBuffer[42]); 
	}
	else 
	{
	     //I2CWriteByte(ST7540_address, 0, EepromBuff[0]);   
		 if(am_freq<=720)  //PLL_HICURRENT_3MA
    	 	  I2CWriteByte(ST7540_address, 0x00, 0x56);  
    	 else if(am_freq<=999) //PLL_HICURRENT_4MA
    	 	  I2CWriteByte(ST7540_address, 0x00, 0x58);   
    	 else if(am_freq<=1404) //PLL_HICURRENT_5MA
    	 	  I2CWriteByte(ST7540_address, 0x00, 0x5a);  
    	 else //PLL_HICURRENT_6MA
    	 	  I2CWriteByte(ST7540_address, 0x00, 0x5c); 

	    RadioBuffer[1] = (((am_freq+10700)/1-32)&0xff);
		RadioBuffer[2] = (((am_freq+10700)/1-32)>>8); 

		I2CWriteByte(ST7540_address, 1, RadioBuffer[1]);     
		I2CWriteByte(ST7540_address, 2, RadioBuffer[2]);     
 	    I2CWriteByte(ST7540_address, 4, RadioBuffer[4]);   
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -