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

📄 initial.c

📁 ADSP 与高速AD器件无缝链接的源代码
💻 C
字号:
/***************************************************************************** * Initial.c
 * Frank.liux    2008-12-17 *****************************************************************************/
#include	"..\include\AD7877.h"
#include	"..\include\AD7877_Register.h"
//````````````````````````````````````````````````````````````````````
//that would set the PLL for Core  working in iffrent frequency
//````````````````````````````````````````````````````````````````````
//````````````````````````````````````````````````````````````````````

void Set_PLL(int pmsel,int pssel)
{
	int new_PLL_CTL;	
	*pPLL_DIV = pssel;
	asm("ssync;");
	new_PLL_CTL = (pmsel & 0x3f) << 9;		
//	*pSIC_IWR1 |= 0xffffffff;
	if (new_PLL_CTL != *pPLL_CTL) 
	{
		*pPLL_CTL = new_PLL_CTL;
		asm("ssync;");
		asm("idle;");
	}
} 
//````````````````````````````````````````````````````````````````````
//Initials the SPI0 for transmission
//````````````````````````````````````````````````````````````````````
//````````````````````````````````````````````````````````````````````

void	Initial_SPI0				(void)
{
	/*Refer to Manuel_1 Page 571*/
	*pPORTE_MUX	 =  0x0	;	
	/*Refer to Manuel_1 Page 594 Define the GPIO to be peripheral*/			   		
	*pPORTE_FER	|=	PE5|PE0|PE2|PE1;  			
	*pSPI0_FLG	|=	FLS2;
	/*thus AD7877 can recive 25Mhz Spi Buad ,but for slowly ,we take 16.5Mhz replace	*/
	/*SCKx frequency = (peripheral clock frequency SCLK)/(2 x SPIx_BAUD) */
	*pSPI0_BAUD	 =	4;//0x1ff;				
	/*Master Mode; 16-bits;when write to TDBR start transfer;Start SPI										*/
	*pSPI0_CTL	|= 	MSTR|SIZE|TIMOD_1|SPE;
	/*SPIxSCK toggles from middle of the first data bit, slave selectpins controlled by hardware*/
	*pSPI0_CTL	&=	~CPHA;					
}

//````````````````````````````````````````````````````````````````````
//for use gpio to get a interrupt from AD7877
//````````````````````````````````````````````````````````````````````
//````````````````````````````````````````````````````````````````````
void	Initial_AD7877_Interrupt	(void)
{
	/* assign PINT0 interrupt to IVG7*/
	register_handler(ik_ivg7, IsrPB4_5);			

	*pSIC_IAR2 = 0xFFFF0FFFL;
	*pSIC_IMASK0 = IRQ_PINT0;											
	/* enable the PB4 PB5 input driver */
	*pPORTB_INEN = PB4|PB5;
	/* assign PB4 PB 5 to PINT0 byte 1 */
	*pPINT0_ASSIGN = B0MAP_PBL;	
	/*  low edge sensitivity */
	*pPINT0_INVERT_CLEAR =  PIQ4;//|PIQ5;
	*pPINT0_EDGE_SET 	 =	PIQ4;//|PIQ5;
	*pPINT0_LATCH 		 =	PIQ4;//|PIQ5;	
	/* unmask interrupts */
	*pPINT0_MASK_SET = PIQ4;//|PIQ5;

		
}
//````````````````````````````````````````````````````````````````````
//add register and command together to be a 16bit SPI Command   word
//````````````````````````````````````````````````````````````````````
//````````````````````````````````````````````````````````````````````
unsigned short Command (char Register,short Mode)
{
	short	AD7877_Command;
	AD7877_Command		  = Register;
	AD7877_Command		  = (AD7877_Command<<12) + Mode;
	return		(AD7877_Command);	
}
//````````````````````````````````````````````````````````````````````
// SPI Write to AD7877
//````````````````````````````````````````````````````````````````````
//````````````````````````````````````````````````````````````````````
void	AD7877_Program	(char Reg,short Mod)
{
	short Stat;
											
	*pSPI0_TDBR	  = Command(Reg,Mod);		
	/*we did not use TDBR to jugement the finish of SPI, becuase : when TDBR shifrt into TX register
	The SPI_STAT TXS flg has set up then,but due to the SPI_BUAD the SPI TX could not send out 
	all the datas in the mean time ,so it's will be a wrong jugment */	
	while	(!(*pSPI0_STAT & RXS));
	/*use RDBR to jugement the finish of SPI ,becareful to clear the RDBR register every time or it may take a
	error*/
    Stat	=	*pSPI0_RDBR;		
}
//````````````````````````````````````````````````````````````````````
//A soft delay function ,use ASM language "nop" to delay a time
//````````````````````````````````````````````````````````````````````
//````````````````````````````````````````````````````````````````````
void	delay 			(short time)
{
	short iTime=0;
	for (iTime<time;iTime++;)
		{
			asm	("nop;");
		}
	
}
//````````````````````````````````````````````````````````````````````
//SPI read AD7877
//````````````````````````````````````````````````````````````````````
//````````````````````````````````````````````````````````````````````
short	AD7877_Read		(void)
{
	short 	AD7877_Value;
	/*write to TDBR start  SPI transfer*/
	*pSPI0_TDBR	  = 0x0000;													
	while	(!(*pSPI0_STAT & RXS));
	AD7877_Value	=	*pSPI0_RDBR;
	return(AD7877_Value);
}

⌨️ 快捷键说明

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