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

📄 ns81l.c

📁 nl81,收音头开发资料
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "target.h"
#include "tuner.h"
#include "ns81l.h"

#ifdef __TUNER__


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define LOW 		0
#define HIGH 		1
#define SUCCESS 	0
#define FAIL 		1


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

typedef struct {
	unsigned char addrofeeprom;
	unsigned char mask;
	unsigned char addrofns81;
	signed char shiftbits;
} NS81PARA;

NS81PARA code ns81l2paraarray[]=
{
	{0x01,BIT0|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6|BIT7,0,0},//R007_FMIXC
	{0x02,BIT0|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6,0,8},//R08E_FMDET
	{0x03,BIT0|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6|BIT7,1,0},//R107_DACA-M
	{0x04,BIT0|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6|BIT7,1,8},//R18F_DACB-M
	{0x0a,BIT4|BIT5,2,3},//R278-SRFB
	{0x0a,BIT0|BIT1,2,5},//R256-DAG
	{0x0b,BIT0|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6,2,9},//R29F-SEL
	{0x0c,BIT4|BIT5,3,-1},//R334-SME
	{0x0c,BIT0|BIT1|BIT2,3,0},//R302-SMS
	{0x0d,BIT0|BIT1|BIT2,3,11},//R3BD-HC
	{0x0e,BIT4|BIT5|BIT6,4,7},//R4BD-RSS
	{0x0e,BIT0|BIT1|BIT2|BIT3,4,0},//R403-SN
	{0x0f,BIT0|BIT1|BIT2|BIT3|BIT4|BIT5,4,4},//R449-SEP
	{0x10,BIT4|BIT5|BIT6,5,0},//R546_CFA
	{0x10,BIT0|BIT1|BIT2,5,0},//R502_BMUT
	{0x11,BIT4|BIT5|BIT6|BIT7,5,6},//R5AD_PCN
	{0x11,BIT0|BIT1,5,8},//R589_PDOF
	{0x14,BIT0|BIT1|BIT2|BIT3,6,7},//R67A_NAGC
	{0x15,BIT4|BIT5,7,4},//R789_ADET
	{0x15,BIT0|BIT1,7,6},//R767_CSM
	{0x16,BIT0|BIT1|BIT2|BIT3,7,11},//R7BE_RNAGC
	{0x17,BIT4|BIT5,8,10},//R8EF_IS
	{0x17,BIT0|BIT1|BIT2,8,11},//R8BD_IM
	{0x1e,BIT4|BIT5,13,6},//RDAB_NSENS
	{0x1e,BIT0|BIT1|BIT2,13,7},//RD79_NPW
	{0x1f,BIT4|BIT5,13,10},//RDEF_NCG
	{0x1f,BIT0|BIT1,13,12},//RDCD_Y
	{0x20,BIT7,3,1},//F38_PENC
	{0x22,BIT6,7,4},//F7A_HFG
	{0x25,BIT4,13,-3}//FD1_NCC
};

NS81PARA code rb15_fmlo={0x1a,BIT0|BIT1|BIT2|BIT3|BIT4,11,1};
NS81PARA code rb15_amlo={0x1b,BIT0|BIT1|BIT2|BIT3|BIT4,11,1};
NS81PARA code rb15_fmdx={0x18,BIT0|BIT1|BIT2|BIT3|BIT4,11,1};
NS81PARA code rb15_amdx={0x19,BIT0|BIT1|BIT2|BIT3|BIT4,11,1};
NS81PARA code r623_fmmag={0x12,BIT0|BIT1,6,2};
NS81PARA code r645_fmwag={0x12,BIT4|BIT5,6,0};
NS81PARA code r623_ammag={0x13,BIT0|BIT1,6,2};
NS81PARA code r645_amwag={0x13,BIT4|BIT5,6,0};
NS81PARA code rb7a_j_am={0x1c,BIT4|BIT5|BIT6|BIT7,11,3};
NS81PARA code rb7a_j_fm={0x1c,BIT0|BIT1|BIT2|BIT3,11,7};

extern bit bIsLocal,bIsStereo,bIsFm ;
extern unsigned short idata FmFreq , AmFreq ;
extern unsigned char idata TunerArea;
unsigned int idata ns81_reg[14];

typedef struct {
    unsigned char m[3];
    unsigned int ifcount[3];
    unsigned int ifcounttime;//J lm 2003 2 19 
    unsigned char div[3];
    unsigned char s[3];
} NS81FREQ;

NS81FREQ code ns81_fm = {
    {M_100KHZ,M_50KHZ,M_100KHZ},
    {1070,1070,1070},
    J_32MS_5K,
    {DIV_BY_10,DIV_BY_10,DIV_BY_6},
    {S_10260_228,S_10251,S_10260_228}
};

NS81FREQ code ns81_am = {
    {M_100KHZ,M_90KHZ,M_9KHZ},
    {10710,10701,10710},
    J_16MS_1K,//2003 03 12 liming 
    {DIV_BY_10,DIV_BY_10,DIV_BY_6},
    {S_10260_228,S_10251,S_10260_228}
};


// p27=sda , because it can not input or output at the same time , so change it to p17

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#define ns_SetSda() 		SetP17()
#define ns_ClrSda() 		ClrP17()
#define ns_GetSda() 		GetP17()
#define ns_SetScl()		SetP31()
#define ns_ClrScl()		ClrP31()
#define read_ns_stjo()  	GetP16()
#define ns_SetStrb()  		SetP25()
#define ns_ClrStrb()  		ClrP25()

/*******************************************************************
	NS81 I2C SUBRUTINES
*******************************************************************/
#define I2C_DELAY_TIME				4
#define CHECK_I2C_ACK_TIME		200

#define iic_ClrSda()		ns_ClrSda()
#define iic_SetSda()		ns_SetSda()
#define iic_ClrScl()		ns_ClrScl()
#define iic_SetScl()		ns_SetScl()
#define iic_GetSda()		ns_GetSda()

/*When scl is high, set sda from high to low*/
static void Start_24c02()
{
	iic_ClrScl();
	iic_SetSda();
	iic_SetScl();
	iic_ClrSda();
	iic_ClrScl();
}

/*when scl is high, set sda from low to high*/
static void Stop_24c02()
{
	iic_ClrScl();
	iic_ClrSda();
	iic_SetScl();
	iic_SetSda();
//	iic_ClrScl();//to avoid err action
}

static void Skip_Ack_24c02()//the nineth clock for ack
{
	iic_ClrScl();
	iic_SetSda();//if sda is low, still low, wire ORed
	iic_SetScl();
	iic_ClrScl();
}				

static void Ack_24c02()//the nine clock for ack
{
	iic_ClrScl();//when scl is low,change the sda
	iic_ClrSda();
	iic_SetScl();
	iic_ClrScl();
	iic_SetSda();//acording to output acknowledge at24c64.pdf page6
}

static void Send_Byte_24c02(unsigned char ucvalue)
{
	unsigned char i;
	iic_ClrScl();
	for(i=0;i<8;i++)
	{
		if(ucvalue & 0x80){
			iic_SetSda();
		}
		else{
			iic_ClrSda();
		}//when scl is low, change sda
		iic_SetScl();//postive change,data clock into the eeprom
		iic_ClrScl();
		ucvalue<<=1;
	}
}

static unsigned char Receive_Byte_24c02(void)
{
	unsigned char i,value=0;
	iic_ClrScl();
	for(i=0;i<8;i++)
	{
		value<<=1;
		iic_SetScl();//latch the data 
		if(iic_GetSda())
			value|=1;
		iic_ClrScl();//negative change,data clock out of the eeprom
	}
	return value;
}

/*******************************************************************
	Read data from I2c Slave device
	addr: 	Slave device address
	subadr: Sub address 
	ptrdata: point a buffer that store one unsigned char data
	len:the length of data which to be read
*******************************************************************/
static unsigned char at24c02_ReadBytes(unsigned char addr,unsigned char subadr,unsigned char *ptrdata,unsigned char len)
{
	unsigned char i;
	if(len ==0) return 1;
	
	for(i=0;i<10;i++)
	{
		Start_24c02();
		Send_Byte_24c02(addr & 0xfe);
		iic_SetSda();//if sda is low,still low
		iic_SetScl();//the nineth clock for ack
		if(!iic_GetSda()){
			iic_ClrScl();
			break;
		}
	}
	
	if(i== 10) return 1;//ack failure

	Send_Byte_24c02(subadr);
	Skip_Ack_24c02();
	Start_24c02();
	Send_Byte_24c02(addr | 0x01);
	Skip_Ack_24c02();
	for(i=0;i<len-1;i++)
	{
		*ptrdata++=Receive_Byte_24c02();
		Ack_24c02();
	}
	*ptrdata=Receive_Byte_24c02();
	Skip_Ack_24c02();
	Stop_24c02();
	return 0;
}


/*******************************************************************
	send bytes to I2c device
	addr: 	Slave device address
	subadr: Sub address 
	ptrdata: point a buffer which to been send
	len:the length of data which to be send
*******************************************************************/
/*
static unsigned char at24c02_WriteBytes(unsigned char addr,unsigned char subadr,unsigned char *ptrdata,unsigned char len)
{
#define PAGE_SIZE 8  
	unsigned char i;
	if(len ==0) return 1;	

	while(1)
	{
		for(i=0;i<10;i++)
		{
			Start_24c02();
			Send_Byte_24c02(addr & 0xfe);
			iic_SetSda();//if sda is low,still low
			iic_SetScl();//the nineth clock for ack
			if(!iic_GetSda()){
				iic_ClrScl();
				break;
			}
		}
	
		if(i== 10) return 1;//ack failure

		if(len==0) break;
		Send_Byte_24c02(subadr);
		Skip_Ack_24c02();
	
		while(1)
		{
			Send_Byte_24c02(*ptrdata++);
			Skip_Ack_24c02();
			len--;
			subadr++;
			if(((subadr%PAGE_SIZE)==0)||(len==0)) break;
		}
		Stop_24c02();
	}
	return 0;//write ok
}
*/
/*******************************************************************
	Func:	write the value  into interal register 
	argu:	addr , the address of interal register
			uidata, the value to be written 
    return: none
*******************************************************************/
void Write_Word_Ns81(unsigned char addr,unsigned int uidata)
{
	unsigned char i;
	ns_ClrStrb();
	ns_ClrScl();
	ns_ClrSda();
	
	for(i=0;i<4;i++)
	{
		if(addr&BIT0)
			ns_SetSda();
		else

⌨️ 快捷键说明

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