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

📄 72131-radio.txt

📁 lc72131部分程序供参考学习 lc72131部分程序供参考学习
💻 TXT
字号:


/*Radio tuner    PLL IC:SC72131  */
//---------------------------------------------------------------------------
// WR_SC72131_Addr
//  addr: the addr of the control register
// value: the valude to set to 
//---------------------------------------------------------------------------
static void WR_SC72131_Addr(byte addr)
{
   byte i;

  RadioTuner_PLL_CE = 0;
  RadioTuner_PLL_CLK = 1; 
  DelayNOP();
  	for (i = 8; i>0 ; --i)
  	  { 
   		 RadioTuner_PLL_CLK = 0;
    	 DelayNOP();

    		if (addr & 0x01) 
				PIN_TUNER_DI=1; 
			else 
				PIN_TUNER_DI=0;
    	addr >>= 1;

    	DelayNOP();

    	RadioTuner_PLL_CLK = 1;

    	DelayNOP();
  	 }
}




//---------------------------------------------------------------------------
// WR_SC72131
//  addr: the addr of the control register
// value: the valude to set to 
//---------------------------------------------------------------------------
static void WR_SC72131(	byte addr, dword value	)
{
	register byte i;   									
  	WR_SC72131_Addr(addr); 			// Send the addr byte first 
  	RadioTuner_PLL_CE = 1;
 	DelayNOP();   								  
  	for (i = 24; i>0; --i)			// Send the control value in following
  		{ 
    		RadioTuner_PLL_CLK = 0;
    		DelayNOP();
    		if (value & 0x01) 
				PIN_TUNER_DI=1; 
			else 
				PIN_TUNER_DI=0;
    		value >>= 1;
    		DelayNOP();
    		RadioTuner_PLL_CLK = 1;
    		DelayNOP();
  		}

  	RadioTuner_PLL_CE = 0;
}




//---------------------------------------------------------------------------
// Tuner_Initialize
//---------------------------------------------------------------------------
/*tuner Initialize*/
void Tuner_Initialize(void)
{
  b_amstep   = Read_EEPROM(EE_AMSTEP) & 0x01;
  band       = Read_EEPROM(EE_BAND) & 0x01;

  cur_count[band] = Read_EEPROM(EE_CURCNT_LOW) + Read_EEPROM(EE_CURCNT_HIGH) * 0x100;

  Tuner_Set_Frequency_Int();

  dwPLL_Shadow_IN2 |= BO2_ON;           // Unmute
  WR_SC72131(MODE_IN1, dwPLL_Shadow_IN1);  
  WR_SC72131(MODE_IN2, dwPLL_Shadow_IN2);  
}

//-----------------------
// Tuner_Shutdown
//-----------------------
void Tuner_Shutdown(BOOL bonoff)
{

  if (bonoff)
    {

      	WR_SC72131(MODE_IN1, (dwPLL_Shadow_IN1 & 0x0fffff) | PLL_OFF_REF);  
  	}
  else
  	{
    	WR_SC72131(MODE_IN1, dwPLL_Shadow_IN1);  
  	}
}

/* Tuner_Set_AMStep	*/

BOOL Tuner_Set_AMStep(byte ctrl)
{
   byte INQUIRE;

  if (ctrl != INQUIRE)
   {
    b_amstep = ! b_amstep;
    Write_EEPROM(EE_AMSTEP, b_amstep);
   }

  return b_amstep;
}

//--------------------------
// Tuner_Set_FMStep
//--------------------------
BOOL Tuner_Set_FMStep(byte ctrl)
{

  if (ctrl != INQUIRE)
  	{
    	b_fmstep = ! b_fmstep;
  	}

  return b_fmstep;
}

//--------------------------
// Tuner_Set_Band
//--------------------------
byte Tuner_Set_Band( byte ctrl, Word *freq)
{

  if (ctrl != INQUIRE)
  
   	{

    	Temp_Mute(1);

    	if (band != BAND_AM) band = BAND_AM; else band = BAND_FM;   // 0: am, 1: fm

    	Write_EEPROM(EE_BAND, band);
  
    	Tuner_Set_Frequency_Int();
    
    	dwPLL_Shadow_IN2 |= BO2_ON;           // Unmute

    	WR_SC72131( MODE_IN1, dwPLL_Shadow_IN1 );  
    	WR_SC72131( MODE_IN2, dwPLL_Shadow_IN2 );  

    	Temp_Mute(0);
 	 }
  
  *freq = (Count_To_Freq());
  
  return band;
}

//-------------------------
// Tuner_Set_Mono
//-------------------------
BOOL Tuner_Set_Mono(byte ctrl)
{

  if (ctrl != INQUIRE)
  {
     Temp_Mute(1);
	 bST_MONO = ! bST_MONO;	
     dwPLL_Shadow_IN2 &= ~BO3_ON;
	  
    if (bST_MONO) 
		{
			dwPLL_Shadow_IN2 |= BO3_ON;				 // Stereo
		}    
  
  													//  WR_SC72131(MODE_IN1, dwPLL_Shadow_IN1);  
    WR_SC72131(MODE_IN2, dwPLL_Shadow_IN2);  

    Temp_Mute(0);
  }

  return bST_MONO;
}

//--------------------------
// Tuner_Save_Station
//--------------------------
void Tuner_Save_Station(byte station)
{
   byte ee_addr;

  if (station > MAX_PRESET_NUMBER) 
     station = MAX_PRESET_NUMBER; 

     ee_addr = EE_PRESET_BASE + band * (MAX_PRESET_NUMBER + 1) * 2 + station *2;

  Write_EEPROM(ee_addr, cur_count[band]/0x100); 
  Write_EEPROM(ee_addr +1, cur_count[band]&0xff);
}

//--------------------------
// Tuner_Restore_Station
//--------------------------
Word Tuner_Restore_Station(byte station)
{
	byte ee_addr;

  	Temp_Mute(1);

  	if (station > MAX_PRESET_NUMBER)
		 station = MAX_PRESET_NUMBER; 

  		 ee_addr = EE_PRESET_BASE + band * (MAX_PRESET_NUMBER + 1) * 2 + station *2;

  		 cur_count[band]= Read_EEPROM(ee_addr) * 0x100 + Read_EEPROM(ee_addr +1);
  
  Tuner_Set_Frequency_Int();

  dwPLL_Shadow_IN2 |= BO2_ON;           // Unmute
  WR_SC72131(MODE_IN1, dwPLL_Shadow_IN1);  
  WR_SC72131(MODE_IN2, dwPLL_Shadow_IN2);  

  // save the current count
  Write_EEPROM(EE_CURCNT_LOW, cur_count[band] & 0xff); 
  Write_EEPROM(EE_CURCNT_HIGH, cur_count[band] >> 8);

  Temp_Mute(0);

  return (Count_To_Freq());
}

//-------------------------
// Tuner_Set_Frequency
//-------------------------
Word Tuner_Set_Frequency(byte ctrl, Word freq){
BAND_INFO code *pBInfo;

  switch(band){
    case BAND_AM:
      	if (b_amstep) 
	  		pBInfo = &BandInfo_AM_10KHz;
	 	else 
	 		pBInfo = &BandInfo_AM_9KHz; 
	 	break;

    case BAND_FM:
      	if (b_fmstep) 
	  		pBInfo = &BandInfo_FM_100KHz;
	  	else 
	  		pBInfo = &BandInfo_FM_50KHz; 
		break;

    case BAND_SW:
      	break;
  }

  switch (ctrl)
  {
    case INQUIRE:
      return (Count_To_Freq()); 
	  break;
    case UP:
      cur_count[band] += pBInfo -> CntStep; 
	  break;

    case DOWN:
      cur_count[band] -= pBInfo -> CntStep;
	   break;

    case SET:
      	if ((band == BAND_AM) || (band == BAND_SW))		 //return cur_count[band] * pBInfo -> FRef - 450;
	  		{
        
        		cur_count[band] = (freq + 450) / (pBInfo -> FRef);
      		}
     	else 
	  		{														//return cur_count[band] * ((FM_FREF *100)/1000) - 1070;
        
        		cur_count[band] = ((freq + 1070) * 1000) / (FM_FREF *100);
      		}
      	break;
  }

  Tuner_Set_Frequency_Int();

  dwPLL_Shadow_IN2 |= BO2_ON;           // Unmute

  WR_SC72131(MODE_IN1, dwPLL_Shadow_IN1);  
  WR_SC72131(MODE_IN2, dwPLL_Shadow_IN2);  

  // save the current count
  Write_EEPROM(EE_CURCNT_LOW, cur_count[band] & 0xff); Write_EEPROM(EE_CURCNT_HIGH, cur_count[band] >> 8);

  return (Count_To_Freq());
}

//------------------------
// Tuner_Step
//---------------------------------------------------------------------------
Word Tuner_Step(byte ctrl)
{

  if (ctrl != SET)
  	return Tuner_Set_Frequency(ctrl, 0);
}

//---------------------------------------------------------------------------
// Tuner_Scan
//---------------------------------------------------------------------------
int Tuner_Scan(byte ctrl)
{
	BAND_INFO code *pBInfo;
Word temp;
register byte j;

  switch(band){
    case BAND_AM:
      	if (b_amstep)
			 pBInfo = &BandInfo_AM_10KHz; 
	  	else 
			 pBInfo = &BandInfo_AM_9KHz; 
		break;

    case BAND_FM:
      	if (b_fmstep)
	  	 	pBInfo = &BandInfo_FM_100KHz; 
	  	else 
			pBInfo = &BandInfo_FM_50KHz; 
	  	break;

    case BAND_SW:
      	break;
  }

  if (ctrl == INQUIRE)
  	{
    	return (Count_To_Freq());
  	}
  else if (ctrl == UP)
  	{
    cur_count[band] += pBInfo -> CntStep;
  	}
  else if (ctrl == DOWN)
  	{ 
    cur_count[band] -= pBInfo -> CntStep;
  	}

//  Temp_Mute(1);

  Tuner_Set_Frequency_Int();

  WR_SC72131(MODE_IN1, dwPLL_Shadow_IN1);  
  WR_SC72131(MODE_IN2, dwPLL_Shadow_IN2);  
  
  // Wait PLL Lock -----------------------------------------------------------
  Pause(1);                    //
  
  for (j=0; j<255; j++)
  	{
    	if (PIN_TUNER_DO)break;
  }

  dwPLL_Shadow_IN2 &= DO_MODE_MASK;        // set DO mode, end uc
  dwPLL_Shadow_IN2 |= DO_MODE_END_UC;

  dwPLL_Shadow_IN1 |= CTE_ON;            // IF count start
  
  WR_SC72131(MODE_IN1, dwPLL_Shadow_IN1);  
  WR_SC72131(MODE_IN2, dwPLL_Shadow_IN2);  

  // Wait Gate over -----------------------------------------------------
  for (j=0; j<10; j++)
  	{
   	 	if (!PIN_TUNER_DO) break;          // break when gate ends
    	os_wait2(K_TMO, 1);
  	}

                                          // Gate time over
  temp = Read_72131() & 0x0fffff;

  dwPLL_Shadow_IN2 |= BO2_ON;             // Unmute
  WR_SC72131(MODE_IN2, dwPLL_Shadow_IN2);  

  // save the current count
  Write_EEPROM(EE_CURCNT_LOW, cur_count[band] & 0xff); Write_EEPROM(EE_CURCNT_HIGH, cur_count[band] >> 8);

  if ((temp >= pBInfo -> IFCntMin) && (temp <= pBInfo -> IFCntMax))
  	{
    	return -(Count_To_Freq());
  	}
  else 
  	{
    return (Count_To_Freq());
 	 }
}

//---------------------
// Tuner_Get_Stereo
//---------------------
BOOL Tuner_Get_Stereo(void){

  if (band == BAND_FM) 
  	{
    	return (!(Read_72131() & 0x800000));
  	}
  else 
    	return 0;
}

//-------------------------
// Tuner_Set_Frequency_Int
//-------------------------
static void Tuner_Set_Frequency_Int(void){
BAND_INFO code *pBInfo;

  switch(band){
    case BAND_AM:
      	if (b_amstep) 
	  		pBInfo = &BandInfo_AM_10KHz;
	  	else 
	  		pBInfo = &BandInfo_AM_9KHz; 
	 	break;


    case BAND_FM:
      	if (b_fmstep)
		 	pBInfo = &BandInfo_FM_100KHz;
		else
		  	pBInfo = &BandInfo_FM_50KHz;
		break;


    case BAND_SW:
      break;
  }

  dwPLL_Shadow_IN1 = 0; dwPLL_Shadow_IN2 = 0;

  // Check Boundary -----------------------------------------------------
  if (cur_count[band] > pBInfo -> CntMax){
    cur_count[band] = pBInfo -> CntMin;       // Cycling
  }
  else if (cur_count[band] < pBInfo -> CntMin){
    cur_count[band] = pBInfo -> CntMax;       // Cycling
  }

  // IN2 Mode -----------------------------------------------------------
  dwPLL_Shadow_IN2 |= GATE_TIME_32MS;          // Gate Time = 4ms
  dwPLL_Shadow_IN2 |= 0x100000;            // IFS

  if (band == BAND_AM)
  	{                // Set Band 
    	dwPLL_Shadow_IN2 |= BO1_ON; 
  	}
  else if (band == BAND_FM)
  	{
    	dwPLL_Shadow_IN2 |= BO4_ON;
  	}
    
  if (!bST_MONO) 
  	{
  		dwPLL_Shadow_IN2 |= BO3_ON;
	}    // Stereo

//  dwPLL_Shadow_IN2 &= DO_MODE_MASK;
  dwPLL_Shadow_IN2 |= DO_MODE_UNLOCK;          //

  // IN1 Mode -----------------------------------------------------------

  if (band == BAND_AM)
  	{                // Set Frequency
    	dwPLL_Shadow_IN1 |= (cur_count[band] << 4); 
  	}
  else 
  	{
    	dwPLL_Shadow_IN1 |= cur_count[band];       
  	}

  dwPLL_Shadow_IN1 |= pBInfo -> DivMode;        // Set DIv Mode
  dwPLL_Shadow_IN1 |= pBInfo -> DivRefPara;      // Set DIV Ref Para
}

//-----------------------
// Count_To_Freq
//-----------------------
static Word Count_To_Freq(void){
BAND_INFO code *pBInfo;

  switch(band){
    case BAND_AM:
      	if (b_amstep) 
	  		pBInfo = &BandInfo_AM_10KHz; 
		else 
			pBInfo = &BandInfo_AM_9KHz; 
		break;

    case BAND_FM:
      	if (b_fmstep) 
	  		pBInfo = &BandInfo_FM_100KHz; 
		else 
			pBInfo = &BandInfo_FM_50KHz; 
		break;

    case BAND_SW:
      	break;
  }

  if ((band == BAND_AM) || (band == BAND_SW))
  	{
    	return cur_count[band] * pBInfo -> FRef - 450;
  	}
  else 
  	{
    	return cur_count[band] * ((FM_FREF *100)/1000) - 1070;
  	}
}


//-------------------------------
// Read_LC72131
//  addr: the addr of the control register
// return value 
//-------------------------------
static dword Read_72131(void)
{
register byte i;
dword temp = 0;

 // Send the addr byte first ------------//
  WR_SC72131_Addr(MODE_OUT);

  RadioTuner_PLL_CE = 1;
  DelayNOP();

 // read the value dword ------------//
  for (i=24; i>0; --i){
    RadioTuner_PLL_CLK = 0;

    DelayNOP();

    temp <<= 1;
    if (PIN_TUNER_DO) temp |= 1;

    RadioTuner_PLL_CLK = 1;
    DelayNOP();
  }

  RadioTuner_PLL_CE = 0;

  return temp;
}

⌨️ 快捷键说明

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