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

📄 ad98502.c

📁 基于凌阳和ad9850实现的正弦波信号发生器程序
💻 C
字号:
#include "SPCE061V004.H"
extern ctr_word[5];
extern float a;
extern unsigned long int f_value;
const unsigned long freq_int[]={2755,	8249,	13693,	19052,	24293,
								29385,	34296,	38996,	43456,	47648,	
								51546,	55127,	58369,	61250,	63755,	
								65867,	67574,	68864,	69730,	70166,	
								70171,	69743,	68886,	67604,	65906,
								63802,	61305,	58431,	55196,	51622,
								47730,	43544,	39089,	34394,	29487,
								24398,	19159,	13803,	8360,	2867,
								2867,	8360,	13803,	19159,	24398,
								29487,	34394,	39089,	43544,	47730,
								51622,	55196,	58431,	61305,	63802,
								65906,	67604,	68886,	69743,	70171,
								70166,	69730,	68864,	67574,	65867,
								63755,	61250,	58369,	55127,	51546,
								47648,	43456,	38996,	34296,	29385,
								24293,	19052,	13693,  8249,	2755};
extern void irq_on(void);			
extern void irq_off(void);
extern unsigned int trige;
//=======================================================================================
//
//=======================================================================================
void wr_word(void)
{
	int i,j;
	*P_IOB_Buffer |= 0x4000;
	for(j=0;j<5;j++)
	{
		*P_IOB_Buffer=ctr_word[j];			//5 byte ctr_word
		*P_IOB_Buffer |= 0x8000;
		*P_IOB_Buffer &= 0x7fff;
	}
	*P_IOB_Buffer |= 0x4000;				//update
}
//=======================================================================================
//
//=======================================================================================
void step_forword(void)
{
	int i,j;
	f_value |= ctr_word[1];
	f_value <<= 8;
	f_value |= ctr_word[2];
	f_value <<= 8;
	f_value |= ctr_word[3];
	f_value <<= 8;
	f_value |= ctr_word[4];

	f_value += 0x22f4;
	if(f_value > 0x50000000)
		f_value = 0x50000000;
	
	ctr_word[4] = f_value;
	ctr_word[4] &= 0x00ff;
	f_value >>= 8;
	ctr_word[3] = f_value;
	ctr_word[3] &= 0x00ff;
	f_value >>= 8;
	ctr_word[2] = f_value;
	ctr_word[2] &= 0x00ff;
	f_value >>= 8;
	ctr_word[1] = f_value;
	ctr_word[1] &= 0x00ff;
	wr_word();
}
//=======================================================================================
//
//=======================================================================================
void step_backword(void)
{
	int i,j;
	f_value |= ctr_word[1];
	f_value <<= 8;
	f_value |= ctr_word[2];
	f_value <<= 8;
	f_value |= ctr_word[3];
	f_value <<= 8;
	f_value |= ctr_word[4];

	f_value -= 0x22f4;
	if(f_value < 0x015d86)
		f_value = 0x015d86;
	
	ctr_word[4] = f_value;
	ctr_word[4] &= 0x00ff;
	f_value >>= 8;
	ctr_word[3] = f_value;
	ctr_word[3] &= 0x00ff;
	f_value >>= 8;
	ctr_word[2] = f_value;
	ctr_word[2] &= 0x00ff;
	f_value >>= 8;
	ctr_word[1] = f_value;
	ctr_word[1] &= 0x00ff;
	wr_word();
}
//=======================================================================================
//
//=======================================================================================
void sin_send(unsigned long int f_con)
{
	int i,j;
	a=89.4784853*f_con;
	f_value = (long)a;
	ctr_word[4] = f_value;
	ctr_word[4] &= 0x00ff;
	f_value >>= 8;
	ctr_word[3] = f_value;
	ctr_word[3] &= 0x00ff;
	f_value >>= 8;
	ctr_word[2] = f_value;
	ctr_word[2] &= 0x00ff;
	f_value >>= 8;
	ctr_word[1] = f_value;
	ctr_word[1] &= 0x00ff;
	wr_word();
}
//=======================================================================================
//
//=======================================================================================
void fm_generate_5k(void)
{
	int yjm_sw=0,tk;		//global
	unsigned long int yjm_f_value = 0;		//global
	unsigned long int f_value2 = 0;		//global
	int fm_count=0;					//global
	
	int fm_out,key_value;
	int f_value_reg;
	trige=0;
	
	*P_IOB_Attrib=0xffff;
	*P_IOB_Dir=0xffff;
	*P_IOB_Data=0xffff;
	
	*P_SystemClock=0x74;
	 tk=1000;
	 while(tk--);
	irq_off();
	*P_TimerA_Data=0xfffd;
	*P_TimerA_Ctrl=0x0031;			//use clk a 
	*P_INT_Ctrl=0x1000;
	irq_on();
	
	f_value2 |= ctr_word[1];
	f_value2 <<= 8;
	f_value2 |= ctr_word[2];
	f_value2 <<= 8;
	f_value2 |= ctr_word[3];
	f_value2 <<= 8;
	f_value2 |= ctr_word[4];
	f_value_reg = f_value2;
		
	fm_out = 1;
	while(fm_out)
	{
		if(trige)
		{
			trige=0;
			fm_count++;
			if(fm_count < 40)
			{
				f_value2 += freq_int[fm_count];
				yjm_f_value=f_value2;
				ctr_word[4] = yjm_f_value;
				ctr_word[4] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[3] = yjm_f_value;
				ctr_word[3] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[2] = yjm_f_value;
				ctr_word[2] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[1] = yjm_f_value;
				ctr_word[1] &= 0x00ff;
				for(yjm_sw=0;yjm_sw<5;yjm_sw++)
				{
					*P_IOB_Buffer=ctr_word[yjm_sw];			//5 byte ctr_word
					*P_IOB_Buffer |= 0x8000;
					*P_IOB_Buffer &= 0x7fff;
				}
				*P_IOB_Buffer |= 0x4000;				//update
			}
			else
			{
				f_value2 -= freq_int[fm_count];
				yjm_f_value=f_value2;
				ctr_word[4] = yjm_f_value;
				ctr_word[4] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[3] = yjm_f_value;
				ctr_word[3] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[2] = yjm_f_value;
				ctr_word[2] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[1] = yjm_f_value;
				ctr_word[1] &= 0x00ff;
				for(yjm_sw=0;yjm_sw<5;yjm_sw++)
				{
					*P_IOB_Buffer=ctr_word[yjm_sw];			//5 byte ctr_word
					*P_IOB_Buffer |= 0x8000;
					*P_IOB_Buffer &= 0x7fff;
				}
				*P_IOB_Buffer |= 0x4000;				//update
			}
		}
		if(fm_count >= 78)
		{
			fm_count=0;
			key_value=(*P_IOA_Data & 0x00ff);
			if(key_value != 0x00ff)
				fm_out=0;
		}
		*P_Watchdog_Clear = 1;
	}
	irq_off();
	*P_INT_Ctrl=0x0000;
	*P_SystemClock=0x50;
	
	yjm_f_value=f_value_reg;
	ctr_word[4] = yjm_f_value;
	ctr_word[4] &= 0x00ff;
	yjm_f_value >>= 8;
	ctr_word[3] = yjm_f_value;
	ctr_word[3] &= 0x00ff;
	yjm_f_value >>= 8;
	ctr_word[2] = yjm_f_value;
	ctr_word[2] &= 0x00ff;
	yjm_f_value >>= 8;
	ctr_word[1] = yjm_f_value;
	ctr_word[1] &= 0x00ff;
}
//=======================================================================================
//
//=======================================================================================
void fm_generate_10k(void)
{
	int yjm_sw=0,tk;		//global
	unsigned long int yjm_f_value = 0;		//global
	unsigned long int f_value2 = 0;		//global
	int fm_count=0;					//global
	
	int fm_out,key_value;
	int f_value_reg;
	trige=0;
	
	*P_IOB_Attrib=0xffff;
	*P_IOB_Dir=0xffff;
	*P_IOB_Data=0xffff;
	
	*P_SystemClock=0x74;
	tk=1000;
	while(tk--);
	irq_off();
	*P_TimerA_Data=0xfffd;
	*P_TimerA_Ctrl=0x0031;			//use clk a 
	*P_INT_Ctrl=0x1000;
	irq_on();
	
	f_value2 |= ctr_word[1];
	f_value2 <<= 8;
	f_value2 |= ctr_word[2];
	f_value2 <<= 8;
	f_value2 |= ctr_word[3];
	f_value2 <<= 8;
	f_value2 |= ctr_word[4];
	f_value_reg = f_value2;
	
	fm_out = 1;
	while(fm_out)
	{
		if(trige)
		{
			trige=0;
			fm_count++;
			if(fm_count < 40)
			{
				f_value2 += freq_int[fm_count];
				f_value2 += freq_int[fm_count];
				yjm_f_value=f_value2;
				ctr_word[4] = yjm_f_value;
				ctr_word[4] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[3] = yjm_f_value;
				ctr_word[3] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[2] = yjm_f_value;
				ctr_word[2] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[1] = yjm_f_value;
				ctr_word[1] &= 0x00ff;
				for(yjm_sw=0;yjm_sw<5;yjm_sw++)
				{
					*P_IOB_Buffer=ctr_word[yjm_sw];			//5 byte ctr_word
					*P_IOB_Buffer |= 0x8000;
					*P_IOB_Buffer &= 0x7fff;
				}
				*P_IOB_Buffer |= 0x4000;				//update
			}
			else
			{
				f_value2 -= freq_int[fm_count];
				f_value2 -= freq_int[fm_count];
				yjm_f_value=f_value2;
				ctr_word[4] = yjm_f_value;
				ctr_word[4] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[3] = yjm_f_value;
				ctr_word[3] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[2] = yjm_f_value;
				ctr_word[2] &= 0x00ff;
				yjm_f_value >>= 8;
				ctr_word[1] = yjm_f_value;
				ctr_word[1] &= 0x00ff;
				for(yjm_sw=0;yjm_sw<5;yjm_sw++)
				{
					*P_IOB_Buffer=ctr_word[yjm_sw];			//5 byte ctr_word
					*P_IOB_Buffer |= 0x8000;
					*P_IOB_Buffer &= 0x7fff;
				}
				*P_IOB_Buffer |= 0x4000;				//update
			}
		}
		if(fm_count >= 78)
		{
			fm_count=0;
			key_value=(*P_IOA_Data & 0x00ff);
			if(key_value != 0x00ff)
				fm_out=0;
		}
		*P_Watchdog_Clear = 1;
	}
	irq_off();
	*P_INT_Ctrl=0x0000;
	*P_SystemClock=0x50;
	
	yjm_f_value=f_value_reg;
	ctr_word[4] = yjm_f_value;
	ctr_word[4] &= 0x00ff;
	yjm_f_value >>= 8;
	ctr_word[3] = yjm_f_value;
	ctr_word[3] &= 0x00ff;
	yjm_f_value >>= 8;
	ctr_word[2] = yjm_f_value;
	ctr_word[2] &= 0x00ff;
	yjm_f_value >>= 8;
	ctr_word[1] = yjm_f_value;
	ctr_word[1] &= 0x00ff;
}

⌨️ 快捷键说明

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