comparison DDSBes.c @ 0:d9aae7d7f2c6

Import
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 03 Jul 2012 10:38:59 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d9aae7d7f2c6
1 #include <utility.h>
2 #include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */
3 #include <userint.h>
4 #include "DDSBes.h"
5 #include <ansi_c.h>
6 #include <gpib.h>
7
8 #define PPORT 0x378
9 #define PPCTRL PPORT+2
10 #define UPDate 0x040
11 #define nWR 0x080
12 #define MReset 0x0C0
13 #define tempo 20
14
15 // Private functions, hence declared as static ...
16
17 static int delai(int t) {
18 int j ;
19 for (j=0;j<t; j++) ;
20 return(0) ;
21 }
22
23
24 static int Strb_adress(unsigned char ad) {
25 outp(PPCTRL,0x08) ;
26 outp(PPORT,ad) ;
27 outp(PPCTRL,0x09) ;
28 delai(tempo) ;
29 outp(PPCTRL,0x08) ;
30 delai(tempo);
31 return(0) ;
32 }
33
34 static int Strb_data(unsigned char da) {
35 outp(PPCTRL,0x00) ;
36 outp(PPORT,da) ;
37 outp(PPCTRL,0x01) ;
38 delai(tempo) ;
39 outp(PPCTRL,0x00) ;
40 delai(tempo) ;
41 return(0) ;
42 }
43
44 static int Strb_nWrite(void) {
45 outp(PPCTRL,0x08) ;
46 outp(PPORT,nWR) ;
47 outp(PPCTRL,0x09) ;
48 delai(tempo) ;
49 outp(PPCTRL,0x08) ;
50 delai(tempo) ;
51 return(0) ;
52 }
53
54 static int Strb_Update(void) {
55 outp(PPCTRL,0x08) ;
56 outp(PPORT,UPDate) ;
57 outp(PPCTRL,0x09) ;
58 delai(tempo) ;
59 outp(PPCTRL,0x08) ;
60 delai(tempo) ;
61 return(0) ;
62 }
63
64 static int Strb_MReset(void) {
65 outp(PPCTRL,0x08) ;
66 outp(PPORT,MReset) ;
67 outp(PPCTRL,0x09) ;
68 delai(tempo) ;
69 outp(PPCTRL,0x08) ;
70 delai(tempo) ;
71 return(0) ;
72 }
73
74 // Public functions
75
76 void DDSBes_Initialize(DDSBes_Data * Instance) {
77 Strb_MReset();
78 /* 0 dans 0x1F => update clock externe */
79 Strb_data(0x00);
80 Strb_adress(0x1F);
81 Strb_nWrite();
82
83 return ;
84 }
85
86 void DDSBes_Close(DDSBes_Data * Instance){
87
88 return ;
89 }
90
91 void DDSBes_SetClockFrequency(DDSBes_Data * Instance, double clock_frequency){
92
93 int fPLL ;
94 unsigned char oPLL;
95
96 fPLL = 1;
97 if(fPLL==1 || (fPLL>3 && fPLL<21)) {
98 Instance->ClockFrequency = clock_frequency*fPLL;
99 if (Instance->ClockFrequency>300.0e6) {
100 Instance->ClockFrequency = 300.0e6 ;
101 }
102 }
103
104 if (fPLL==1) oPLL=0x60 ;
105 else {
106 if (Instance->ClockFrequency>=2e8) oPLL=(unsigned char)(0x40 + fPLL) ;
107 else oPLL= (unsigned char)fPLL ;
108 }
109
110 Strb_data(oPLL);
111 Strb_adress(0x1E);
112 Strb_nWrite();
113
114 return ;
115 }
116
117 void DDSBes_SetFrequency(DDSBes_Data * Instance, double frequency){
118
119 double pas ;
120 double reste ;
121 double coeff;
122 double fdiv[7];
123 double p[6];
124
125 int i;
126
127 unsigned char o[6];
128
129 fdiv[6]=pow(2.0,48.0);
130 fdiv[5]=pow(2.0,40.0);
131 fdiv[4]=pow(2.0,32.0);
132 fdiv[3]=pow(2.0,24.0);
133 fdiv[2]=65536.0;
134 fdiv[1]=256.0;
135 fdiv[0]=1.0;
136
137 coeff=fdiv[6]/Instance->ClockFrequency;
138 pas=coeff*frequency;
139 reste=pas;
140
141 for (i=5;i>0;i--){
142 p[i]=(reste/fdiv[i]);
143 reste=fmod(pas,fdiv[i]);
144 }
145 p[0]=reste;
146
147 for (i=5;i>=0;i--){
148 o[i]=(unsigned char) p[i];
149 }
150
151 /* frequence (6 octets) */
152 Strb_adress(0x04);Strb_data(o[5]);Strb_nWrite();
153 Strb_adress(0x05);Strb_data(o[4]);Strb_nWrite();
154 Strb_adress(0x06);Strb_data(o[3]);Strb_nWrite();
155 Strb_adress(0x07);Strb_data(o[2]);Strb_nWrite();
156 Strb_adress(0x08);Strb_data(o[1]);Strb_nWrite();
157 Strb_adress(0x09);Strb_data(o[0]);Strb_nWrite();
158
159 Strb_Update();
160
161 // store in Instance->Frequency the real frequency generated by the DDS
162 // (and not "frequency", which is only the requested frequency and might differ by as much as ClockFrequency/2^48
163 Instance->Frequency = Instance->ClockFrequency/fdiv[6]
164 *(fdiv[5]*o[5]+fdiv[4]*o[4]+fdiv[3]*o[3]+fdiv[2]*o[2]+fdiv[1]*o[1]+fdiv[0]*o[0]) ;
165
166
167
168 return ;
169 }
170
171
172 void DDSBes_SetAmplitude(DDSBes_Data * Instance, int amplitude) {
173 unsigned char a[2];
174
175 if (amplitude > 0 && amplitude < 4096){
176 a[1]=(unsigned char)(amplitude>>8);
177 a[0]=(unsigned char)floor(fmod(amplitude,256.0));
178 Instance->Amplitude = amplitude ;
179 }
180
181 /* nibble fort adresse=0x21 data=a[1] */
182 Strb_data(a[1]);
183 Strb_adress(0x21);
184 Strb_nWrite();
185 /* octet faible adresse=0x22 data=a[0] */
186 Strb_data(a[0]);
187 Strb_adress(0x22);
188 Strb_nWrite();
189 /* mise a jour */
190 Strb_Update();
191
192 return ;
193 }