📄 ns81l.c
字号:
ns_ClrSda();
addr>>=1;
ns_SetScl();
ns_ClrScl();
}
for(i=0;i<16;i++)
{
if(uidata & BIT0)
ns_SetSda();
else
ns_ClrSda();
uidata>>=1;
ns_SetScl();
ns_ClrScl();
}
ns_SetStrb();
for(i=0;i<4;i++);
ns_ClrStrb();
}
/*******************************************************************
Func: read the parameters from ns81 24c02
Set them in to ns81_reg[0-13]
argu: ns81para (struct NS81PARA)
return: none
*******************************************************************/
void SetNs81Para(NS81PARA ns81para)
{
unsigned char uctemp;
unsigned int uitemp ;
at24c02_ReadBytes(0xa0,ns81para.addrofeeprom,&uctemp,1);
uitemp = uctemp;
uitemp &= ns81para.mask;
if(ns81para.shiftbits > 0){
uitemp <<= ns81para.shiftbits;
ns81_reg[ns81para.addrofns81] &= ~(ns81para.mask << ns81para.shiftbits);
}
else if(ns81para.shiftbits < 0){
ns81para.shiftbits &= 0x7f;
uitemp >>= ns81para.shiftbits;
ns81_reg[ns81para.addrofns81] &= ~(ns81para.mask >> ns81para.shiftbits);
}
else if(ns81para.shiftbits ==0){
ns81_reg[ns81para.addrofns81] &= ~ns81para.mask;
}
ns81_reg[ns81para.addrofns81] |= uitemp;
}
/*******************************************************************
Func: 收音机的初始化.
read the default values from ns81 24c02 to ns81_reg[0-13]
Set ns81_reg[0-13] to interal registers
include band and freq,
then can heard voice of the station
argu: none
return: none
*******************************************************************/
void TunerInit()
{
unsigned char i;
for(i=0;i<14;i++) ns81_reg[i]=0;
for(i=0;i<30;i++) SetNs81Para(ns81l2paraarray[i]);
ns81_reg[4]|=OD_OPENDRAIN;
ns81_reg[12]|=(BAND_FM|PRAD_ANALOG_ON|PS_ALL_ON|PDIG_DIGITAL_ON);
Write_Word_Ns81(12, ns81_reg[12]);//reset ns81
Write_Word_Ns81(10,0x02);
for(i=0;i<255;i++);
Write_Word_Ns81(10,0x00);
for(i=0;i<14;i++) Write_Word_Ns81(i,ns81_reg[i]);
Set_Band();
GotoFreq();
}
/*******************************************************************
Func: Start seek action
argu: none
return: none
*******************************************************************/
void StartSeekStation()
{
ns81_reg[3]&=~(BITE|BITF);
ns81_reg[11]&=~(BIT6|BITC);//IFCST=0 CKE=0
ns81_reg[2]|=SEEK_ENABLE;
ns81_reg[3]|=SDT_STATIONDECTION;
Write_Word_Ns81(2,ns81_reg[2]);//seek=1
Write_Word_Ns81(3,ns81_reg[3]);//sdt =1
Write_Word_Ns81(11,ns81_reg[11]);//cke =0 ifcst=0 j
}
/*******************************************************************
Func: Start count if action
argu: none
return: none
*******************************************************************/
void StartCountIf()
{
ns81_reg[3]&=~(BITE|BITF);
ns81_reg[3]|=SDT_IFCOUNTER;
ns81_reg[11]|=(CKE_ON|IFCST_COUNTSTART);
Write_Word_Ns81(3,ns81_reg[3]);//sdt =2
Write_Word_Ns81(11,ns81_reg[11]);//cke =1 ifcst=1
}
void ResetIfcount()
{
ns81_reg[11]&=~(BIT6);//IFCST=0
Write_Word_Ns81(11,ns81_reg[11]);//cke =0 ifcst=0 j
}
/*******************************************************************
Func: Stop seek and count if action
and set as normal play mode
argu: none
return: none
*******************************************************************/
void StopSeekStation()
{
ns81_reg[2]&=~(SEEK_ENABLE);//seek=0;
ns81_reg[3]&=~(BITE|BITF);//sdt=0;
ns81_reg[11]&=~(BIT6|BITB|BITC);//IFCST=0 CKE=0 FC107=0
Write_Word_Ns81(2,ns81_reg[2]);//seek =0
Write_Word_Ns81(3,ns81_reg[3]);//sdt =0
Write_Word_Ns81(11,ns81_reg[11]);//cke =0 ifcst=0
}
/*******************************************************************
Func: Set local or dx
argu: bIsLocal = 1 local
=0 distance (normal )
return: none
*******************************************************************/
void set_local()
{
if(bIsLocal == 1){
if(bIsFm){
SetNs81Para(rb15_fmlo);
ns81_reg[6] |= FLOC_LOCAL;//lm 2003 2 19
}
else{
SetNs81Para(rb15_amlo);
ns81_reg[7] |= ALOC_LOCAL;//lm 2003 2 19
}
}
else{//STATE_DX
if(bIsFm){
SetNs81Para(rb15_fmdx);
ns81_reg[6] &= ~FLOC_LOCAL;
}
else{
SetNs81Para(rb15_amdx);
ns81_reg[7] &= ~ALOC_LOCAL;
}
}
Write_Word_Ns81(6,ns81_reg[6]);
Write_Word_Ns81(7,ns81_reg[7]);
Write_Word_Ns81(11,ns81_reg[11]);
}
/*******************************************************************
Func: Set stereo or mono
argu: bIsStereo = 1 stereo
=0 mono
return: none
*******************************************************************/
void Set_Stereo()
{
if(bIsStereo){//stereo
ns81_reg[5]&=~(BIT3);//stereo
ns81_reg[5]&=~(BIT8|BIT9);//pdof
ns81_reg[4]&=~(BIT4|BIT5|BIT6|BIT7|BIT8|BIT9);//sep
}
else{//mono
ns81_reg[5]|=(BIT3);//mono
ns81_reg[5]|=(BIT8|BIT9);//PDOF
ns81_reg[4]&=~(BIT4|BIT5|BIT6|BIT7|BIT8|BIT9);//sep
//ns81_reg[4]|=(BIT4|BIT5|BIT6|BIT7|BIT8|BIT9); //sep
}
ns81_reg[3]&=~(BITE|BITF);
Write_Word_Ns81(3,ns81_reg[3]);//sdt =0
Write_Word_Ns81(4,ns81_reg[4]);
Write_Word_Ns81(5,ns81_reg[5]);
}
/*******************************************************************
Func:Convert Freq to Value which will be write into Ns81
INPUT
BAND_FM range 8750-10800 every 1 means 10khz
BAND_AM range 522-1620 every 1 means 1khz
OUTPUT
Value which will be write into Ns81
*******************************************************************/
unsigned int PcmValueOfFreq()
{
extern unsigned char GetCurrentStep();
unsigned int uitemp;
if(bIsFm ==0 && TunerArea==AREA_JAPAN){
long ltemp;
ltemp = AmFreq + ns81_am.ifcount[TunerArea];
ltemp *= 6;
ltemp /= 9;
uitemp = ltemp&0xffff;
}
else{
if(bIsFm ==0){
uitemp = AmFreq + ns81_am.ifcount[TunerArea];
uitemp /= GetCurrentStep();
}
else{
uitemp = FmFreq + ns81_fm.ifcount[TunerArea];
uitemp /= GetCurrentStep();
}
}
return uitemp;
}
/*******************************************************************
Func: calculate the value of pcm
and write the value into ns81_reg[9]
argu: FmFreq,AmFreq
return: none
*******************************************************************/
void GotoFreq()
{
ns81_reg[9]=PcmValueOfFreq();
Write_Word_Ns81(9,ns81_reg[9]);
}
/*******************************************************************
Func: Set band as Am or fm, and different with area
argu: bIsFm,FmFreq,AmFreq
return: none
*******************************************************************/
void Set_Band()
{
if(bIsFm){//fm
SetNs81Para(r623_fmmag);
SetNs81Para(r645_fmwag);
SetNs81Para(rb7a_j_fm);
ns81_reg[7] &= ~(BIT2|BIT3);
ns81_reg[8] &= ~(BIT0|BIT1|BIT2|BIT3|BIT5);//step PEP2
ns81_reg[7] |= ns81_fm.div[TunerArea];
ns81_reg[8] |= ns81_fm.m[TunerArea];
ns81_reg[12]|= BAND_FM;
}
else{//am
SetNs81Para(r623_ammag);
SetNs81Para(r645_amwag);
SetNs81Para(rb7a_j_am);
ns81_reg[7] &= ~(BIT2|BIT3);//DIV
ns81_reg[8] &= ~(BIT0|BIT1|BIT2|BIT3|BIT5);//step PEP2
ns81_reg[10] &= ~(BIT2|BIT3);//S
ns81_reg[7] |= ns81_am.div[TunerArea];
ns81_reg[8] |= ns81_am.m[TunerArea]|PEP2_SUBBPLLON;
ns81_reg[10] |= ns81_am.s[TunerArea];
ns81_reg[12] &= ~BAND_FM;//BAND_AM
}
Write_Word_Ns81(6,ns81_reg[6]);
Write_Word_Ns81(7,ns81_reg[7]);//div
Write_Word_Ns81(8,ns81_reg[8]);//step
Write_Word_Ns81(10,ns81_reg[10]);//s
Write_Word_Ns81(11,ns81_reg[11]);//J
Write_Word_Ns81(12,ns81_reg[12]);//BAND
}
/*******************************************************************
Func: detect if it is stereo station
return: 1 stereo
0 mono
*******************************************************************/
unsigned char IsStereo()
{
return read_ns_stjo();
}
/*******************************************************************
Func: detect if the ifcount is 10.7MHz(fm) or 450KHz
return: 1 right
0 wrong
*******************************************************************/
unsigned char IsIfOk()
{
return read_ns_stjo();
}
/*******************************************************************
Func: detect if the sd(station detect) signal is strong enough
return: 1 strong
0 less
*******************************************************************/
unsigned char IsStation()
{
return read_ns_stjo();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -