通过寄存器配置IO口并行配置a9854的信号输出频率

AD9854_SetSine(输出频率, 输出幅度);

调用格式:

1
2
AD9854_Init();
AD9854_SetSine(10000000.0, 4095); // 输出10MHz正弦波,幅度设置为最大(0~4095)

ad9854.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef __AD9854_H__
#define __AD9854_H__

#include <stdint.h>
#include "stm32f4xx_hal.h"

#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))

#define GPIOA_ODR_Addr (GPIOA_BASE+0x14) // F4的ODR偏移是0x14
#define GPIOB_ODR_Addr (GPIOB_BASE+0x14)
#define GPIOC_ODR_Addr (GPIOC_BASE+0x14)
#define GPIOD_ODR_Addr (GPIOD_BASE+0x14)
#define GPIOE_ODR_Addr (GPIOE_BASE+0x14)
#define GPIOF_ODR_Addr (GPIOF_BASE+0x14)
#define GPIOG_ODR_Addr (GPIOG_BASE+0x14)
#define GPIOH_ODR_Addr (GPIOH_BASE+0x14)
#define GPIOI_ODR_Addr (GPIOI_BASE+0x14)

#define GPIOA_IDR_Addr (GPIOA_BASE+0x10) // F4的IDR偏移是0x10
#define GPIOB_IDR_Addr (GPIOB_BASE+0x10)
#define GPIOC_IDR_Addr (GPIOC_BASE+0x10)
#define GPIOD_IDR_Addr (GPIOD_BASE+0x10)
#define GPIOE_IDR_Addr (GPIOE_BASE+0x10)
#define GPIOF_IDR_Addr (GPIOF_BASE+0x10)
#define GPIOG_IDR_Addr (GPIOG_BASE+0x10)
#define GPIOH_IDR_Addr (GPIOH_BASE+0x10)
#define GPIOI_IDR_Addr (GPIOI_BASE+0x10)

#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入

#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入

#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入

#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入

#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入

#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入

#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入


#define AD9854_RST PEout(2) //AD9854复位端口
#define AD9854_UDCLK PCout(1) //AD9854更新时钟
#define AD9854_WR PCout(2) //AD9854写使能,低有效
#define AD9854_RD PCout(0) //AD9854读使能,低有效

/****************/
// 未使用
#define AD9854_OSK PAout(2) //AD9854 OSK控制端
#define AD9854_FDATA PAout(10) //AD9854 FSK,PSK控制
/****************/

/****************/
// 未使用
#define AD9854_DataBus GPIOC->BSRR
#define AD9854_AdrBus GPIOC->BSRR
/****************/

#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long

void AD9854_Init(void);
void AD9854_SetSine(ulong Freq,uint Shape);
void AD9854_SetSine_double(double Freq,uint Shape);
void AD9854_InitFSK(void);
void AD9854_SetFSK(ulong Freq1,ulong Freq2);
void AD9854_InitBPSK(void);
void AD9854_SetBPSK(uint Phase1,uint Phase2);
void AD9854_InitOSK(void);
void AD9854_SetOSK(uchar RateShape);
void AD9854_InitAM(void);
void AD9854_SetAM(uint Shape);
void AD9854_InitRFSK(void);
void AD9854_SetRFSK(ulong Freq_Low,ulong Freq_High,ulong Freq_Up_Down,ulong FreRate);

#endif

ad9854.C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "ad9854.h"

// AD9854数据总线定义
#define AD9854_D0 PBout(4)
#define AD9854_D1 PBout(6)
#define AD9854_D2 PBout(8)
#define AD9854_D3 PEout(0)
#define AD9854_D4 PBout(5)
#define AD9854_D5 PBout(7)
#define AD9854_D6 PBout(9)
#define AD9854_D7 PEout(1)

// AD9854地址总线定义
#define AD9854_A0 PAout(8)
#define AD9854_A1 PAout(15)
#define AD9854_A2 PDout(4)
#define AD9854_A3 PDout(5)
#define AD9854_A4 PDout(6)
#define AD9854_A5 PDout(7)

static void set_data_bus(uint8_t data) {
AD9854_D0 = (data & 0x01) ? 1 : 0;
AD9854_D1 = (data & 0x02) ? 1 : 0;
AD9854_D2 = (data & 0x04) ? 1 : 0;
AD9854_D3 = (data & 0x08) ? 1 : 0;
AD9854_D4 = (data & 0x10) ? 1 : 0;
AD9854_D5 = (data & 0x20) ? 1 : 0;
AD9854_D6 = (data & 0x40) ? 1 : 0;
AD9854_D7 = (data & 0x80) ? 1 : 0;
}

static void set_addr_bus(uint8_t addr) {
AD9854_A0 = (addr & 0x01) ? 1 : 0;
AD9854_A1 = (addr & 0x02) ? 1 : 0;
AD9854_A2 = (addr & 0x04) ? 1 : 0;
AD9854_A3 = (addr & 0x08) ? 1 : 0;
AD9854_A4 = (addr & 0x10) ? 1 : 0;
AD9854_A5 = (addr & 0x20) ? 1 : 0;
}


uchar FreqWord[6]; //6个字节频率控制字

//**********************以下为系统时钟以及其相关变量设置**************************

/*
此处根据自己的需要设置系统时钟以及与其相关的因子,一次需且只需开启一个
CLK_Set为时钟倍频设置,可设置4~20倍倍频,但最大不能超过300MHZ
Freq_mult_ulong和Freq_mult_doulle均为2的48次方除以系统时钟,一个为长整形,一个为双精度型
*/

/*
#define CLK_Set 4
const ulong Freq_mult_ulong = 3518437;
const double Freq_mult_doulle = 3518437.2088832;
*/

/*
#define CLK_Set 5
const ulong Freq_mult_ulong = 2814750;
const double Freq_mult_doulle = 2814749.76710656;
*/

/*
#define CLK_Set 6
const ulong Freq_mult_ulong = 2345625;
const double Freq_mult_doulle = 2345624.80592213;
*/

/*
#define CLK_Set 7
const ulong Freq_mult_ulong = 2010536;
const double Freq_mult_doulle = 2010535.54793326;
*/

/*
#define CLK_Set 8
const ulong Freq_mult_ulong = 1759219;
const double Freq_mult_doulle = 1759218.6044416;
*/

/*
#define CLK_Set 9
const ulong Freq_mult_ulong = 1563750;
const double Freq_mult_doulle = 1563749.87061476;
*/

/*
#define CLK_Set 10
const ulong Freq_mult_ulong = 1407375;
const double Freq_mult_doulle = 1407374.88355328;
*/

/*
#define CLK_Set 11
const ulong Freq_mult_ulong = 1279432;
const double Freq_mult_doulle = 1279431.712321164;
*/


#define CLK_Set 12
const ulong Freq_mult_ulong = 1172812;
const double Freq_mult_doulle = 1172812.402961067;


/*
#define CLK_Set 13
const ulong Freq_mult_ulong = 1082596;
const double Freq_mult_doulle = 1082596.064271754;
*/

/*
#define CLK_Set 14
const ulong Freq_mult_ulong = 1005268;
const double Freq_mult_doulle = 1005267.773966629;
*/

/*
#define CLK_Set 15
const ulong Freq_mult_ulong = 938250;
const double Freq_mult_doulle = 938249.9223688533;

*/
//**************************修改硬件时要修改的部分********************************



//**************************以下部分为函数定义********************************


//====================================================================================
//函数名称:void AD9854_WR_Byte(uchar addr,uchar dat)
//函数功能:AD9854并行口写入数据
//入口参数:addr 6位地址
// dat 写入的数据
//出口参数:无
//====================================================================================
uint32_t dat1,dat2;

static void AD9854_WR_Byte(uint32_t addr,uint32_t dat)
{
// dat1= dat|(addr<<8);
//
// AD9854_AdrBus=(dat1&0x3fff)|((dat1^0x3fff)<<16);
set_addr_bus(addr);
set_data_bus(dat);

// AD9854_AdrBus = ((addr&0x3f)<<8) | ((addr^0x3f)<<(16+8));
// AD9854_DataBus = dat|(dat^0xff)<<16;
// dat1 = ((addr&0x3f)<<8) | ((addr^0x3f)<<(16+8));
// dat2 = (dat1&0x3fff)|((dat1^0x3fff)<<16);
AD9854_WR = 0;
AD9854_WR = 1;
}

//====================================================================================
//函数名称:void AD9854_Init(void)
//函数功能:AD9854初始化
//入口参数:无
//出口参数:无
//====================================================================================
void AD9854_Init(void) {
AD9854_WR=1;//将读、写控制端口设为无效
AD9854_RD=1;
AD9854_UDCLK=0;
AD9854_RST=1; //复位AD9854
AD9854_RST=0;

AD9854_WR_Byte(0x1d,0x00); //关闭比较器
AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
AD9854_WR_Byte(0x1f,0x00); //设置系统为模式0,由外部更新
AD9854_WR_Byte(0x20,0x60); //设置为可调节幅度,取消插值补偿

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}
//====================================================================================
//函数名称:void Freq_convert(long Freq)
//函数功能:正弦信号频率数据转换
//入口参数:Freq 需要转换的频率,取值从0~SYSCLK/2
//出口参数:无 但是影响全局变量FreqWord[6]的值
//说明: 该算法位多字节相乘算法,有公式FTW = (Desired Output Frequency × 2N)/SYSCLK
// 得到该算法,其中N=48,Desired Output Frequency 为所需要的频率,即Freq,SYSCLK
// 为可编程的系统时钟,FTW为48Bit的频率控制字,即FreqWord[6]
//====================================================================================
static void Freq_convert(long Freq)
{
ulong FreqBuf;
ulong Temp=Freq_mult_ulong;

uchar Array_Freq[4]; //将输入频率因子分为四个字节
Array_Freq[0]=(uchar)Freq;
Array_Freq[1]=(uchar)(Freq>>8);
Array_Freq[2]=(uchar)(Freq>>16);
Array_Freq[3]=(uchar)(Freq>>24);

FreqBuf=Temp*Array_Freq[0];
FreqWord[0]=FreqBuf;
FreqBuf>>=8;

FreqBuf+=(Temp*Array_Freq[1]);
FreqWord[1]=FreqBuf;
FreqBuf>>=8;

FreqBuf+=(Temp*Array_Freq[2]);
FreqWord[2]=FreqBuf;
FreqBuf>>=8;

FreqBuf+=(Temp*Array_Freq[3]);
FreqWord[3]=FreqBuf;
FreqBuf>>=8;

FreqWord[4]=FreqBuf;
FreqWord[5]=FreqBuf>>8;
}

//====================================================================================
//函数名称:void AD9854_SetSine(ulong Freq,uint Shape)
//函数功能:AD9854正弦波产生程序
//入口参数:Freq 频率设置,取值范围为0~(1/2)*SYSCLK
// Shape 幅度设置. 为12 Bit,取值范围为(0~4095) ,取值越大,幅度越大
//出口参数:无
//====================================================================================
void AD9854_SetSine(ulong Freq,uint Shape)
{
uchar count;
uchar Adress;

Adress = 0x04; //选择频率控制字地址的初值

Freq_convert(Freq); //频率转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress++,FreqWord[--count]);
}

AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
AD9854_WR_Byte(0x22,(uchar)(Shape&0xff));

AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
AD9854_WR_Byte(0x24,(uchar)(Shape&0xff));

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void Freq_doublt_convert(double Freq)
//函数功能:正弦信号频率数据转换
//入口参数:Freq 需要转换的频率,取值从0~SYSCLK/2
//出口参数:无 但是影响全局变量FreqWord[6]的值
//说明: 有公式FTW = (Desired Output Frequency × 2N)/SYSCLK得到该函数,
// 其中N=48,Desired Output Frequency 为所需要的频率,即Freq,SYSCLK
// 为可编程的系统时钟,FTW为48Bit的频率控制字,即FreqWord[6]
//注意: 该函数与上面函数的区别为该函数的入口参数为double,可使信号的频率更精确
// 建议在100HZ以下用本函数,在高于100HZ的情况下用函数void Freq_convert(long Freq)
//====================================================================================
static void Freq_double_convert(double Freq)
{
ulong Low32;
uint High16;
double Temp=Freq_mult_doulle; //23ca99为2的48次方除以120M
Freq*=(double)(Temp);
// 1 0000 0000 0000 0000 0000 0000 0000 0000 = 4294967295
High16 =(int)(Freq/4294967295); //2^32 = 4294967295
Freq -= (double)High16*4294967295;
Low32 = (ulong)Freq;

FreqWord[0]=Low32;
FreqWord[1]=Low32>>8;
FreqWord[2]=Low32>>16;
FreqWord[3]=Low32>>24;
FreqWord[4]=High16;
FreqWord[5]=High16>>8;
}
//====================================================================================
//函数名称:void AD9854_SetSine_double(double Freq,uint Shape)
//函数功能:AD9854正弦波产生程序
//入口参数:Freq 频率设置,取值范围为0~1/2*SYSCLK
// Shape 幅度设置. 为12 Bit,取值范围为(0~4095)
//出口参数:无
//====================================================================================
void AD9854_SetSine_double(double Freq,uint Shape)
{
uchar count=0;
uchar Adress;

Adress=0x04; //选择频率控制字1地址的初值

Freq_double_convert(Freq); //频率转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress++,FreqWord[--count]);
}

AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
AD9854_WR_Byte(0x22,(uchar)(Shape&0xff));

AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
AD9854_WR_Byte(0x24,(uchar)(Shape&0xff));

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_InitFSK(void)
//函数功能:AD9854的FSK初始化
//入口参数:无
//出口参数:无
//====================================================================================
void AD9854_InitFSK(void)
{
AD9854_WR=1; //将读、写控制端口设为无效
AD9854_RD=1;
AD9854_UDCLK=0;
AD9854_RST=1; //复位AD9854
AD9854_RST=0;

AD9854_WR_Byte(0x1d,0x00); //关闭比较器
AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
AD9854_WR_Byte(0x1f,0x02); //设置系统为模式1,由外部更新
AD9854_WR_Byte(0x20,0x60); //设置为可调节幅度,取消插值补偿

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_SetFSK(ulong Freq1,ulong Freq2)
//函数功能:AD9854的FSK设置
//入口参数:Freq1 FSK频率1
// Freq2 FSK频率2
//出口参数:无
//====================================================================================
void AD9854_SetFSK(ulong Freq1,ulong Freq2)
{
uchar count=6;
uchar Adress1,Adress2;

const uint Shape=4000; //幅度设置. 为12 Bit,取值范围为(0~4095)

Adress1=0x04; //选择频率控制字1地址的初值
Adress2=0x0a; //选择频率控制字2地址的初值

Freq_convert(Freq1); //频率转换1

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress1++,FreqWord[--count]);
}

Freq_convert(Freq2); //频率转换2

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress2++,FreqWord[--count]);
}

AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
AD9854_WR_Byte(0x22,(uchar)(Shape&0xff));

AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
AD9854_WR_Byte(0x24,(uchar)(Shape&0xff));

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_InitBPSK(void)
//函数功能:AD9854的BPSK初始化
//入口参数:无
//出口参数:无
//====================================================================================
void AD9854_InitBPSK(void)
{
AD9854_WR=1; //将读、写控制端口设为无效
AD9854_RD=1;
AD9854_UDCLK=0;
AD9854_RST=1; //复位AD9854
AD9854_RST=0;

AD9854_WR_Byte(0x1d,0x00); //打开比较器,输出方波
AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
AD9854_WR_Byte(0x1f,0x08); //设置系统为模式4,由外部更新
AD9854_WR_Byte(0x20,0x60); //设置为可调节幅度,取消插值补偿

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_SetBPSK(uint Phase1,uint Phase2)
//函数功能:AD9854的BPSK设置
//入口参数:Phase1 调制相位1
// Phase2 调制相位2
//出口参数:无
//说明: 相位为14Bit,取值从0~16383,建议在用本函数的时候将Phase1设置为0,
// 将Phase1设置为8192,180°相位
//====================================================================================
void AD9854_SetBPSK(uint Phase1,uint Phase2)
{
uchar count;

const ulong Freq=60000;
const uint Shape=4000;

uchar Adress;
Adress=0x04; //选择频率控制字1地址的初值

AD9854_WR_Byte(0x00,Phase1>>8); //设置相位1
AD9854_WR_Byte(0x01,(uchar)(Phase1&0xff));

AD9854_WR_Byte(0x02,Phase2>>8); //设置相位2
AD9854_WR_Byte(0x03,(uchar)(Phase2&0xff));

Freq_convert(Freq); //频率转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress++,FreqWord[--count]);
}

AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
AD9854_WR_Byte(0x22,(uchar)(Shape&0xff));

AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
AD9854_WR_Byte(0x24,(uchar)(Shape&0xff));

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_InitOSK(void)
//函数功能:AD9854的OSK初始化
//入口参数:无
//出口参数:无
//====================================================================================
void AD9854_InitOSK(void)
{
AD9854_WR=1; //将读、写控制端口设为无效
AD9854_RD=1;
AD9854_UDCLK=0;
AD9854_RST=1; //复位AD9854
AD9854_RST=0;

AD9854_WR_Byte(0x1d,0x00); //关闭比较器
AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
AD9854_WR_Byte(0x1f,0x00); //设置系统为模式0,由外部更新
AD9854_WR_Byte(0x20,0x70); //设置为可调节幅度,取消插值补偿,通断整形内部控制

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_SetOSK(uchar RateShape)
//函数功能:AD9854的OSK设置
//入口参数: RateShape OSK斜率,取值为4~255,小于4则无效
//出口参数:无
//====================================================================================
void AD9854_SetOSK(uchar RateShape)
{
uchar count;

const ulong Freq=60000; //设置载频
const uint Shape=4000; //幅度设置. 为12 Bit,取值范围为(0~4095)

uchar Adress;
Adress=0x04; //选择频率控制字地址的初值

Freq_convert(Freq); //频率转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress++,FreqWord[--count]);
}

AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
AD9854_WR_Byte(0x22,(uchar)(Shape&0xff));

AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
AD9854_WR_Byte(0x24,(uchar)(Shape&0xff));


AD9854_WR_Byte(0x25,RateShape); //设置OSK斜率

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_InitAM(void)
//函数功能:AD9854的AM初始化
//入口参数:无
//出口参数:无
//====================================================================================
void AD9854_InitAM(void)
{
uchar count;

const ulong Freq=60000; //设置载频

uchar Adress;
Adress=0x04; //选择频率控制字地址的初值

AD9854_WR=1; //将读、写控制端口设为无效
AD9854_RD=1;
AD9854_UDCLK=0;
AD9854_RST=1; //复位AD9854
AD9854_RST=0;

AD9854_WR_Byte(0x1d,0x00); //关闭比较器
AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
AD9854_WR_Byte(0x1f,0x00); //设置系统为模式0,由外部更新
AD9854_WR_Byte(0x20,0x60); //设置为可调节幅度,取消插值补偿

Freq_convert(Freq); //频率转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress++,FreqWord[--count]);
}

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_SetAM(uchar Shape)
//函数功能:AD9854的AM设置
//入口参数:Shape 12Bit幅度,取值从0~4095
//出口参数:无
//====================================================================================
void AD9854_SetAM(uint Shape)
{
AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
AD9854_WR_Byte(0x22,(uchar)(Shape&0xff));

AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
AD9854_WR_Byte(0x24,(uchar)(Shape&0xff));

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_InitRFSK(void)
//函数功能:AD9854的RFSK初始化
//入口参数:无
//出口参数:无
//====================================================================================
void AD9854_InitRFSK(void)
{
AD9854_WR=1; //将读、写控制端口设为无效
AD9854_RD=1;
AD9854_UDCLK=0;
AD9854_RST=1; //复位AD9854
AD9854_RST=0;

AD9854_WR_Byte(0x1d,0x00); //关闭比较器
AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
AD9854_WR_Byte(0x1f,0x24); //设置系统为模式2,由外部更新,使能三角波扫频功能
AD9854_WR_Byte(0x20,0x60); //设置为可调节幅度,取消插值补偿

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}

//====================================================================================
//函数名称:void AD9854_SetRFSK(void)
//函数功能:AD9854的RFSK设置
//入口参数:Freq_Low RFSK低频率 48Bit
// Freq_High RFSK高频率 48Bit
// Freq_Up_Down 步进频率 48Bit
// FreRate 斜率时钟控制 20Bit
//出口参数:无
//注: 每两个脉冲之间的时间周期用下式表示(FreRate +1)*(System Clock ),一个脉冲,
// 频率 上升或者下降 一个步进频率
//====================================================================================
void AD9854_SetRFSK(ulong Freq_Low,ulong Freq_High,ulong Freq_Up_Down,ulong FreRate)
{
uchar count=6;
uchar Adress1,Adress2,Adress3;
const uint Shape=4000; //幅度设置. 为12 Bit,取值范围为(0~4095)

Adress1=0x04; //选择频率控制字地址的初值
Adress2=0x0a;
Adress3=0x10;

Freq_convert(Freq_Low); //频率1转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress1++,FreqWord[--count]);
}

Freq_convert(Freq_High); //频率2转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress2++,FreqWord[--count]);
}

Freq_convert(Freq_Up_Down); //步进频率转换

for(count=6;count>0;) //写入6字节的频率控制字
{
AD9854_WR_Byte(Adress3++,FreqWord[--count]);
}

AD9854_WR_Byte(0x1a,(uchar)((FreRate>>16)&0x0f)); //设置斜升速率
AD9854_WR_Byte(0x1b,(uchar)(FreRate>>8));
AD9854_WR_Byte(0x1c,(uchar)FreRate);

AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
AD9854_WR_Byte(0x22,(uchar)(Shape&0xff));

AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
AD9854_WR_Byte(0x24,(uchar)(Shape&0xff));

AD9854_UDCLK=1; //更新AD9854输出
AD9854_UDCLK=0;
}