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

📄 ad9835.c

📁 基于FPGA 的直接数字频率合成信号发生器(DDS)设计
💻 C
字号:
//use uart , mode 1, send
#define IN_SPICommu
#include "SPICommunication.h"

unsigned char daoUChar(unsigned char uc)
{
	unsigned char i, temChar;

	temChar = 0;
	for( i=0; i<8; i++ ){
		temChar = temChar<<1;
		if( uc & 0x01 )
			temChar += 1;
		uc = uc>>1;
	}
	return temChar;
}

void write2Byte(unsigned char uc1, unsigned char uc2)
{
	uc1 = daoUChar(uc1);
	uc2 = daoUChar(uc2);

	SPI_SS = 0;
	 
 	SBUF = uc1;
	while( !TI );
	TI = 0;

 	SBUF = uc2;
	while( !TI );
	SPI_SS = 1;

	TI = 0;
}

void testAD9875(unsigned long writedata);
void writeControl_1(unsigned char i)
{
	unsigned char firstByte;
	if( i )
		firstByte = 0xF8;// sleep,reset,clr
	else
		firstByte = 0xc0;
	write2Byte(firstByte, 0);
}

void writeControl_0()
{
	unsigned char firstByte;
	firstByte = 0xa0;// set sync and selsrc
	write2Byte(firstByte, 0);
}
void writeFreReg(unsigned long dat, unsigned char whichFre)
{
	unsigned char byte, baseAddr;
	if( whichFre==1 )
		baseAddr = 4;
	else
		baseAddr = 0;
		
	byte = (dat>>24) & 0xFF;
	write2Byte(0x30+baseAddr+0x03, byte);
	byte = (dat>>16) & 0xFF;
	write2Byte(0x20+baseAddr+0x02, byte);

	byte = (dat>>8) & 0xFF;
	write2Byte(0x30+baseAddr+0x01, byte);
	byte = dat & 0xFF;
	write2Byte(0x20+baseAddr+0x00, byte);
}

void writePhase(unsigned int dat, unsigned char whichFre)
{
	unsigned char byte, baseAddr;
	if( whichFre==0 )
		baseAddr = 8;
	else if( whichFre==1 ){
		baseAddr = 10;
	}
	else if( whichFre==2 ){
		baseAddr = 12;
	}
	else
		baseAddr = 14;

	byte = (dat>>8) & 0xFF;
	write2Byte(0x10+baseAddr+0x01, byte);
	byte = dat & 0xFF;
	write2Byte(0x00+baseAddr+0x00, byte);
}


void testAD9875(unsigned long writedata)
{
	unsigned long longData;
	longData = 85.89934592 * writedata + 0.5;

	writeControl_1(1);
	writeControl_0();

	writeFreReg(longData, 0);
	writeFreReg(0, 1);
	writePhase(0x0400, 0);
	writePhase(0, 1);
	writePhase(0x0c00, 2);
	writePhase(0, 3);

	writeControl_1(0);
}

⌨️ 快捷键说明

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