📄 ad.txt
字号:
#include<iom128v.h>
#include<macros.h>
#include "stdio.h"
#include "string.h"
#include "math.h"
//#include "intrins.h"
//#include "kky.h"
#define INT8U unsigned char
#define INT16U unsigned int
#define INT32U long unsigned int
#define ulong unsigned long int
#define uchar unsigned char
#define uint unsigned int
#define max_data 4294967296
// 30MHZ
#define data_eight_bit PORTA
//DDS_AD9850数据接口
#define F 180*1E6
//外部时钟频率 //100MHZ
#define N 4294967296
//2的32次方=4294967296
//#define xf 0.023 // f F/N=0.041909515857696533203125// 间隔频率
#define W_Clk PB7
//数据时钟
#define FQ_UD PB4
//数据更新信号
#define RESET PB6
//复位按钮
//float prefrdata;//当前频率值和键盘预算值
unsigned long int frequency_cal(ulong); //频率计算 OK
void AD9851initial(void); //初始化子程序定义 OK
void clock(void); //数据时钟装载定义 OK
void FQ_UPON(void); //数据装载完毕定义 OK
void phase_data(unsigned char); //相位.DDS待机模式控制 NO
void fre_ctrl(ulong); //数据装载子程序定义 OK
//void frequency_scan(ulong minimum_f,ulong max_f,ulong alternation,int n); //频率扫描子程序定义 OK
//void frequency_MHZ(float); //MHZ产生子程序定义 OK
void frequency_KHZ(float); //KHZ产生子程序定义 OK
//void frequency_HZ(float); //HZ产生子程序定义 OK
//void delay10ms(void); //
//void mega128initial(void);
void WDTinitial(void);
/**************************************************************************/
/* void mega128initial()
{
DDRA=0xff;
DDRB=(1<<DDB4)|(1<<DDB6)|(1<<DDB7) ;
//stop errant interrupts until set up
// CLI(); //disable all interrupts
MCUCR =0x00;
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
XMCRB=0x00;
//timer1_init();
SFIOR =0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
TCCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCCR1C=0X00;
TCCR3A=0x00;
TCCR3B=0x00;
TCCR3C=0X00;
ADCSRA=0x00;//shut dowm ADC power save
// SEI(); //re-enable interrupts
//all peripherals are now initialized
} */
/**********************************************************************/
void WDTinitial(void)
{
WDTCR=(1<<WDCE)|(1<<WDE);
WDTCR=(0<<WDCE)|(1<<WDE)|(0<<WDP2)|(0<<WDP1)|(1<<WDP0);
}
/***********************************************************************************************/
void AD9851initial() //芯片初始化
{
PORTB&=~(1<<W_Clk)&~(1<<RESET)&~(1<<FQ_UD);
data_eight_bit=0x00;
PORTB|=(1<<RESET);
NOP();
NOP();
PORTB&=~(1<<RESET);
NOP();
NOP();
phase_data(0x09);
NOP();
}
/***********************************************************************************************/
void clock() //数据时钟
{
PORTB|=(1<<W_Clk);
asm("nop");
PORTB&=~(1<<W_Clk);
asm("nop");
}
/************************************************************************************************/
void FQ_UPON() //数据完毕
{
PORTB|=(1<<FQ_UD);
asm("nop");
PORTB&=~(1<<FQ_UD);
asm("nop");
}
/***************************************************************************************************/
void phase_data(unsigned char first_data)
{
data_eight_bit=first_data;
clock();
}
/************************************************************************************************/
void fre_ctrl(unsigned long int x)
{
int i;
unsigned char *p; //定义指针,指向32位装载数据的最高8位
WDR();
PORTB|=(1<<RESET);
NOP();
NOP();
PORTB&=~(1<<RESET);
NOP();
NOP();
phase_data(0x09);
NOP(); //40位数据中的最高8位
/*if(x>238609294){ //频率不能高于10MHZ,高于10MHZ,to 1KHZ;
x=23861;
} //频率不能 低于1KHZ,低于1KHZ,to 10MHZ;
if(x<23861)
{
x=238609294;
} */
WDR();
NOP();
p=(char*)&x+3; //取浮点型数据首地址;因为是8位数据,所以,字符型指针指向8位数据
for(i=0;i<4;i++){
data_eight_bit=*p;
clock();
p--;
WDR();
}
FQ_UPON(); //送数据完毕
}
/********************************************************************************************************/
/*void frequency_scan(ulong minimum_f,ulong max_f,ulong alternation,int n) //alternation(扫描频宽)
{
int j;
ulong fx;
ulong nx;
for(j=0;j<n;j++){
for(fx=minimum_f;fx<max_f;fx+=alternation){ //大于最大频率30MHZ的时候,中断循环;跳出for 循环
nx=frequency_count(fx);
if(0<fx<1*1e3){delay10ms();}
else{
if(1*1e3<fx<10*1e3){delay10ms();}
else{
if(10*1e3<fx<100*1e3){delay10ms();}
else{}
}
}
data_in(nx);
}
for(fx=max_f;fx>minimum_f;fx-=alternation){
nx=frequency_count(fx);
if(0<fx<1000){}
else{
if(1000<fx<10000){}
else{
if(10000<fx<1000000){}
else{}
}
}
data_in(nx);
}
}
} */
/*********************************************************************************************************/
/*********************************************************************************************************/
unsigned long int frequency_cal(unsigned long f)
{
unsigned long int z;
z=f*23.86092;
return(z);
}
/*********************************************************************************************************/
/*************************************************************************************************************/
/*void frequency_HZ(float x)
{
ulong m;
m=frequency_cal(x);
fre_ctrl(m);
}
*/
/**************************************************************************************************************/
void frequency_KHZ(float x)
{
ulong m;
ulong y;
x=x*1000; //KHZ
y=x;
m=frequency_cal(y);
fre_ctrl(m);
}
/*********************************************************************************************************/
/*void frequency_MHZ(float x)
{
ulong m;
ulong y;
x=x*1e6; //MHZ
y=x;
m=frequency_cal(y);
fre_ctrl(m);
}*/
/**********************************************************************************************************/
/*void delay10ms(void)
{ unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=4;j>0;j--)
for(k=248;k>0;k--);
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -