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

📄 hal80110c.c

📁 威望公司MP3 + USB MCU 的参考软件
💻 C
字号:

#include "utiltypedef.h"
#include "common.h"
#include "hal80110c.h"
/*
The 80110C device is a low cost and low power stereo FM radio frequency transmitter
IC for global FM radio band.

Write Access Timing:
1. 	The state of the Data is changed at the falling edge of each clock.
2. 	8 data bits (D7~D0) are sent immediately after the 8 address bits (A7~A0) with the same timing.
	The bit of 0 between A5 and A4 indicate a Write access.
3. 	En line is taken HIGH after the last data bit (D0) to indicate the arrival of the final data bit. Then
	the CLK line pulses one more pulse and then is held at 0 for at least one cycle before a new
	access is started. There for EN line is held HIGH for at least two cycles.

Read Access Timing:
1. 	The bit of 1 between A5 and A4 indicate a Read access.
2. 	During a read access, the addressed device generates data on the Data to be read by the controlling device.
3.	Following the 8 address bits, there is a turn around bit which lasts for half a clock cycle as
	depicted with black in the figure, has the effect of realigning the Data timing such that now the
	addressed device will load bits onto the Data at the rising edge of the CLK. Therefore the bits
	are read at falling edge of the CLK.
4.	After the final data bit, the CLK is again held at 0 for at least one cycle before the next access.

Transaction: ADDR[7:5] + R/W0 + ADDR[4:0] + DATA[7:0]
*/

#if (FM_TRANS_MODULE == FM_TRANS_80110C)

#if 1
void halSPIInit(void)
{
	//set gpio direction, value
	SPI_CLK = 0;
	SPI_EN0 = 1;
	SPI_CLK_OUT();
	SPI_EN0_OUT();
	
	return;
}
#endif
#if 0
void _halSPISend0(void){
	//SPI_EN should be setup before this function

	SPI_DAT_OUT();
	SPI_CLK = 0;		//Here is the falling edge
	SPI_DAT = 0;
	DelayUs(SPI_CLK_TIME);
	SPI_CLK = 1;		// the raising edge
	DelayUs(SPI_CLK_TIME);

	return;
}

void _halSPISend1(void){
	//SPI_EN should be setup before this function

	SPI_DAT_OUT();
	SPI_CLK = 0;		//Here is the falling edge
	SPI_DAT = 1;
	DelayUs(SPI_CLK_TIME);
	SPI_CLK = 1;		// the raising edge
	DelayUs(SPI_CLK_TIME);

	return;
}
#endif
void _halSPIWrite(BYTE regaddr, BYTE regval)
{
	BYTE data i;
	WORD data u16Resp;
	SPI_CLK = 0;	
	SPI_DAT = 0;
	SPI_EN0 = 0;
	
	DelayUs(500);
	SPI_CLK = 1;
	SPI_CLK = 0;

	u16Resp=((((WORD)regaddr)<<8)&0xff00 |((WORD)regval)&0x00ff);
	for(i=0; i<16; i++)
	{
		if(u16Resp&0x8000)
		{
			SPI_DAT=1;
		}else{
			SPI_DAT=0;
		}

		u16Resp<<=1;
		SPI_CLK = 1;
		SPI_CLK = 0;
	}		
#if 0
	//Register address
	for(i=0;i<8;i++){
		if(regaddr&0x80){
			_halSPISend1();
		}else{
			_halSPISend0();
		}
		regaddr <<= 1;

		if(i == 2)
			_halSPISend0(); //R/W0
	}

	//Register value
	for(i=0;i<8;i++){
		if(regval&0x80){
			_halSPISend1();
		}else{
			_halSPISend0();
		}
		regval <<= 1;
	}
#endif

	SPI_EN0 = 1;
//	_halSPISend0();	//Used to generate 2 cycle
//	_halSPISend0();
	SPI_CLK = 0;
	SPI_CLK = 1;
	SPI_CLK = 0;
	
	return;
}

#if 0
BYTE _halSPIRead(BYTE regaddr)
{
	SBYTE i;
	BYTE regval;

	SPI_EN0 = 0;
	//Register address
	for(i=0;i<8;i++){
		if(regaddr&0x80){
			_halSPISend1();
		}else{
			_halSPISend0();
		}
		regaddr <<= 1;

		if(i == 2)
			_halSPISend1(); //R/W0
	}

	//Register value
	SPI_DAT_IN();
	for(i=0;i<8;i++){
		SPI_CLK = 0;		//Here is the falling edge
		DelayUs(SPI_CLK_TIME);
		SPI_CLK = 1;		// the raising edge
		DelayUs(SPI_CLK_TIME);	
		
		regval <<= 1;
		if(SPI_DAT)
			regval |= 0x01;
	}
	SPI_EN0 = 1;

	for(i=0;i<2;i++){
		SPI_CLK = 0;		//Here is the falling edge
		DelayUs(SPI_CLK_TIME);
		SPI_CLK = 1;		// the raising edge
		DelayUs(SPI_CLK_TIME);	
	}
	SPI_CLK = 0;
	
	return regval;
}
#endif

static BYTE data s_FMIdx;
void halFMSet(BYTE byChIdx)	//byChIdx: 0-> 203
{
	WORD wProgCnt;
	
	if(byChIdx > FM_CHIDX_MAX)
		s_FMIdx = FM_CHIDX_DEFAULT;
	else
		s_FMIdx = byChIdx;
	
	wProgCnt = (FM_FREQ_BASE + (WORD)byChIdx)<<1;

	UartOutText("-I-: FM Idx 0x");UartOutValue(byChIdx, 2);
	
	halSPIInit();
	
	_halSPIWrite(0x14, (wProgCnt >> 8) & 0x7F);
	_halSPIWrite(0x15, wProgCnt & 0xFF);
	_halSPIWrite(0x16, 0x00);
	_halSPIWrite(0x17, 0x80);
	_halSPIWrite(0x18, 0x01);
	_halSPIWrite(0x19, 0x00);
	return;
}

void halFMUp(void)	//byChIdx: 0-> 203
{
	s_FMIdx++;
	if(s_FMIdx > FM_CHIDX_MAX){//Over Max
		s_FMIdx = FM_CHIDX_MIN;		
	}	

	halFMSet(s_FMIdx);	
	
	return;
}

void halFMDown(void)	//byChIdx: 0-> 203
{
	if(s_FMIdx == 0){//Minmal
		s_FMIdx = FM_CHIDX_MAX;//turn around		
	}else
		s_FMIdx--;

	halFMSet(s_FMIdx);	
	
	return;
}

void halStepAddFM()
{
	if(s_FMIdx == FM_CHIDX_MAX)
		s_FMIdx = FM_CHIDX_DEFAULT;
	else if((s_FMIdx + FM_STEP_1M) > FM_CHIDX_MAX)
		s_FMIdx -= 200;
	else
		s_FMIdx += FM_STEP_1M;

	//UartOutText("s_FMIdx \t");UartOutValue(s_FMIdx, 2);
	
	halFMSet(s_FMIdx);
	return;
}

void halStepSubFM()
{
	if(s_FMIdx == 0)
		s_FMIdx = FM_CHIDX_MAX;
	else if(s_FMIdx < FM_STEP_1M)
		s_FMIdx += 200;
	else
		s_FMIdx -= FM_STEP_1M;	
	
	//UartOutText("s_FMIdx \t");UartOutValue(s_FMIdx, 2);
	
	halFMSet(s_FMIdx);
	return;
}

WORD halFMGetFreq(void)
{
//	FMDigital = FM_FREQ_BASE+FMLevel;
	WORD wTmp = FM_FREQ_BASE + (WORD)s_FMIdx;

	//UartOutText("-I-: halFMGetFreq 0x");UartOutValue(wTmp, 4);
	return wTmp;
}

BYTE halFMGetIdx(void)
{
//	FMDigital = FM_FREQ_BASE+FMLevel;
	//WORD wTmp = FM_FREQ_BASE + s_FMIdx;

	return s_FMIdx;
}

BYTE halFMFreq2Idx(WORD wFreq)
{
	WORD wTmp = wFreq;

	//Checking parameter validation
	if((wFreq>=FM_FREQ_BASE) && (wFreq<=FM_FREQ_MAX)){
		wTmp = wTmp - FM_FREQ_BASE; 
	}else{
		wTmp = FM_CHIDX_INVALID;
	}		
	
	return (BYTE)wTmp;
}

#if 0
void halSPITest(void)
{
	static BYTE invert = 0;
	BYTE regval;

	halSPIInit();

	if(invert == 0){
		invert = 1;
		UartOutText("-I-: _halSPIWrite(0xaa, 0x55) \r\n");
		_halSPIWrite(0xaa, 0x55);
	}else{
		invert = 0;
		regval = _halSPIRead(0x55);
		UartOutText("-I-: _halSPIRead(0x55)=");UartOutValue(regval, 2);
	}
	
	return;
}
#endif

#endif

⌨️ 快捷键说明

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