Demo2016_05_13 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /*
  2. this program use old code used in another experiment
  3. "int" is used for the ADC and DAC data although it should be unsigned,
  4. it should be not a problem at all as this data are limited to 12bits...
  5. */
  6. #include <aduc7020.h>
  7. unsigned int uiPLLTST = 0;
  8. volatile unsigned int ucTest = 0;
  9. void delay (int length)
  10. {
  11. while (length >=0)
  12. length--;
  13. }
  14. // conversion of the read value into it corresponding 12bits integer
  15. int ADCtoDAT(unsigned long ADC)
  16. {
  17. return (ADC&0xFFF0000)>>16;
  18. }
  19. unsigned long DATtoADC(int DAT)
  20. {
  21. unsigned long ADC;
  22. ADC=DAT;
  23. return ADC<<16;
  24. }
  25. int Read_Digital(int n)
  26. {
  27. return ((GP0DAT&0x000000FF)>>n)&0x1;
  28. }
  29. void Write_Digital(int n, int state)
  30. {
  31. if(state==1)
  32. GP1DAT=(0x00000001<<(n+16))|GP1DAT;
  33. else
  34. GP1DAT=~((0x00000001<<(n+16))|(~GP1DAT));
  35. }
  36. void ADCpoweron(int time)
  37. {
  38. ADCCON = 0x620; // power-on the ADC
  39. while (time >=0) // wait for ADC to be fully powered on
  40. time--;
  41. }
  42. // use DAC0
  43. // it operates a finite number of iteration
  44. // -> to adapte in the while loop
  45. void Lock_min_fringe(int*pV, int step, int N_delay)
  46. {
  47. int flag, cnt;
  48. unsigned long temp1,temp2;
  49. // unsigned long V_th; // threshold
  50. unsigned long V;
  51. flag = 1;
  52. // V_th=DATtoADC(threshold);
  53. V = *pV;//DATtoADC(2000);
  54. temp1 = ADCDAT;
  55. cnt = 0;
  56. while(cnt<100){
  57. //if(Read_Digital(6)==0||temp1<V_th) break;
  58. //hold();
  59. //Write_Digital(0,1);
  60. V = V+flag*step;
  61. if(V>4096) // in case it is out of range restarting fro, the middle should converge
  62. V = 2048;
  63. //V = Bound_cir(V);
  64. DAC0DAT = DATtoADC(V);
  65. delay(N_delay); // if the system do not respond immediately
  66. temp2 = ADCDAT;
  67. if(temp2>temp1){ // to invert in order to lock the maximum
  68. flag = -1*flag;
  69. }
  70. temp1 = temp2;
  71. cnt++;
  72. }
  73. //Write_Digital(0,0);
  74. *pV = V;
  75. }
  76. void Lock_power(int*pV, int step, int N_delay)
  77. {
  78. int cnt,g;
  79. unsigned long temp;
  80. // unsigned long V_th; // threshold
  81. unsigned long V,V0;
  82. g = 1;
  83. // V_th=DATtoADC(threshold);
  84. V = *pV;//DATtoADC(2000);
  85. temp = ADCtoDAT(ADCDAT);
  86. cnt = 0;
  87. // we first check the targeted power on ADC1
  88. ADCCP = 0x01;
  89. V0 = ADCtoDAT(ADCDAT);
  90. // now we only read the PD on ADC0
  91. ADCCP = 0x00;
  92. while(cnt<100){
  93. //if(Read_Digital(6)==0||temp1<V_th) break;
  94. //hold();
  95. //Write_Digital(0,1);
  96. temp = ADCtoDAT(ADCDAT);
  97. V = V-(temp-V0)*g;
  98. //if(V>4096) // in case it is out of range restarting fro, the middle should converge
  99. // V = 2048;
  100. //V = Bound_cir(V);
  101. DAC1DAT = DATtoADC(V);
  102. delay(N_delay); // if the system do not respond immediately
  103. cnt++;
  104. }
  105. //Write_Digital(0,0);
  106. *pV = V;
  107. }
  108. void test_blink(void)
  109. {
  110. short V = -1;
  111. DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  112. // configures GPIO to flash LED P4.2
  113. GP4DAT = 0x04000000; // P4.2 configured as an output. LED is turned on
  114. GP0CON = 0x00000000; // setting IO
  115. GP0DAT = 0x00000000; // group P0.x as input
  116. DAC1DAT = DATtoADC(V); // output voltage on DAC0
  117. //GP4DAT ^= 0x00040000; // Complement P4.2
  118. while (1){
  119. V = -V;
  120. if(V>4096)
  121. V = 0;
  122. DAC1DAT = DATtoADC(V); // output voltage on DAC0
  123. if (Read_Digital(0)==1)
  124. GP4DAT ^= 0x00040000; // Complement P4.2
  125. delay(500000);
  126. }
  127. }
  128. void test_blink2(void)
  129. {
  130. short V = -1;
  131. short state = 1;
  132. DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  133. GP1CON = 0x00000000; // IO initialization
  134. GP1DAT = 0xFF000000; // set P1.n as digital output
  135. // configures GPIO to flash LED P4.2
  136. //GP0CON = 0x00000000; // setting IO
  137. //GP0DAT = 0x04000000; // P4.2 configured as an output. LED is turned on
  138. //GP0DAT = 0x00000000; // group P0.x as input
  139. DAC1DAT = DATtoADC(V); // output voltage on DAC0
  140. //GP4DAT ^= 0x00040000; // Complement P4.2
  141. while (1){
  142. V = -V;
  143. state=-state;
  144. if(V>4096)
  145. V = 0;
  146. DAC1DAT = DATtoADC(V); // output voltage on DAC0
  147. //if (Read_Digital(0)==1)
  148. // GP4DAT ^= 0x00040000; // Complement P4.2
  149. if(state>0)
  150. Write_Digital(4, 1);
  151. else
  152. Write_Digital(4, 0);
  153. //delay(500000);
  154. }
  155. }
  156. void test_clock(void)
  157. {
  158. //short V = -1;
  159. short state = 1;
  160. //DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  161. GP1CON = 0x00000000; // IO initialization
  162. GP1DAT = 0xFF000000; // set P1.n as digital output
  163. // configures GPIO to flash LED P4.2
  164. //GP0CON = 0x00000000; // setting IO
  165. //GP0DAT = 0x04000000; // P4.2 configured as an output. LED is turned on
  166. //GP0DAT = 0x00000000; // group P0.x as input
  167. //DAC1DAT = DATtoADC(V); // output voltage on DAC0
  168. //GP4DAT ^= 0x00040000; // Complement P4.2
  169. while (1){
  170. //V = -V;
  171. state=-state;
  172. //if(V>4096)
  173. // V = 0;
  174. //DAC1DAT = DATtoADC(V); // output voltage on DAC0
  175. //if (Read_Digital(0)==1)
  176. // GP4DAT ^= 0x00040000; // Complement P4.2
  177. if(state>0)
  178. Write_Digital(4, 1);
  179. else
  180. Write_Digital(4, 0);
  181. //delay(500000);
  182. }
  183. }
  184. void test_clock2(void)
  185. {
  186. short state = 1;
  187. int bit =0x00000001<<(20);
  188. POWKEY1 = 0x01;
  189. POWCON = 0x00; // 41.78MHz
  190. POWKEY2 = 0xF4;
  191. GP1CON = 0x00000000; // IO initialization
  192. GP1DAT = 0xFF000000; // set P1.n as digital output
  193. while (1){
  194. GP1DAT^=bit;
  195. }
  196. }
  197. void ramp(void)
  198. {
  199. int Vout=0;
  200. int step=0;
  201. // ADC&DAC setting
  202. ADCpoweron(20000); // power on ADC
  203. REFCON = 0x01; // internal 2.5V reference
  204. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  205. //DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  206. ADCCP = 0x00; // conversion on ADC0
  207. ADCCON = 0x6E4; // continuous conversion
  208. while(1){
  209. if(Vout>=4095)
  210. step=-1;
  211. if(Vout<=0)
  212. step=1;
  213. Vout=Vout+step;
  214. DAC2DAT = DATtoADC(Vout); // output voltage on DAC2
  215. }
  216. }
  217. void init_digital(void)
  218. {
  219. GP1CON = 0x00000000; // IO initialization
  220. GP1DAT = 0xFF000000; // set P1.n as digital output
  221. GP0CON = 0x00000000; // IO initialization
  222. GP0DAT = 0x00000000; // set P0.n as digital input
  223. }
  224. #define SWEEP_IN 0
  225. #define CLOCK_IN 4
  226. #define SWEEP_OUT 0
  227. void synchronizer(void)
  228. {
  229. short new_sweep_in=0 ;
  230. short new_clock_in=0 ;
  231. short last_sweep_in=0;
  232. short last_clock_in=0;
  233. short armed = 0;
  234. init_digital();
  235. // DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  236. while (1){
  237. new_sweep_in = Read_Digital(SWEEP_IN);
  238. new_clock_in = Read_Digital(CLOCK_IN);
  239. if(new_sweep_in==1 && last_sweep_in==0) // edge detection
  240. armed = 1;
  241. if(new_clock_in==1 && last_clock_in==0 // edge detection
  242. && armed==1) // + case for fireing output
  243. {
  244. Write_Digital(SWEEP_OUT,1);
  245. armed = 0;
  246. }
  247. if(new_clock_in==0 && last_clock_in==1) // edge detection
  248. Write_Digital(SWEEP_OUT,0);
  249. last_sweep_in = new_sweep_in; // memory of the previous state
  250. last_clock_in = new_clock_in; // memory of the previous state
  251. }
  252. }
  253. void synchronizer2(void)
  254. {
  255. short new_sweep_in=0 ;
  256. short new_clock_in=0 ;
  257. short last_sweep_in=0;
  258. short last_clock_in=0;
  259. // short armed = 0;
  260. init_digital();
  261. // DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  262. while (1){
  263. new_sweep_in = Read_Digital(SWEEP_IN);
  264. if(new_sweep_in==1 && last_sweep_in==0) // edge detection
  265. {
  266. while(1){
  267. new_clock_in = Read_Digital(CLOCK_IN);
  268. if(new_clock_in==1 && last_clock_in==0){ // edge detection
  269. Write_Digital(SWEEP_OUT,1);
  270. break;
  271. }
  272. last_clock_in = new_clock_in; // memory of the previous state
  273. }
  274. while(1){
  275. new_clock_in = Read_Digital(CLOCK_IN);
  276. if(new_clock_in==0 && last_clock_in==1){ // edge detection
  277. Write_Digital(SWEEP_OUT,0);
  278. break;
  279. }
  280. last_clock_in = new_clock_in; // memory of the previous state
  281. }
  282. }
  283. last_sweep_in = new_sweep_in; // memory of the previous state
  284. }
  285. }
  286. // Bound the output value of DAC
  287. // when V reaches the limit of DAC range, it will be shifted to the center
  288. /*unsigned long Bound(unsigned long V)
  289. {
  290. if (V > 0xFCE0000 || V < 0x320000)
  291. return 0x8000000;
  292. else
  293. return V;
  294. } */
  295. int Bound(int V)
  296. {
  297. if (V > 4000)
  298. return 500;
  299. else if(V < 100)
  300. return 3500;
  301. else
  302. return V;
  303. }
  304. #define SH_in 0 // high-> lock, low -> hold
  305. #define LH_mode 3 // high-> lock on max, low -> lock on min
  306. #define SH_disab 7 // disable the sampler & holder function
  307. // for the high mode the SH input has no effect
  308. //
  309. void lock_EOM(void)
  310. {
  311. // define variables
  312. int N_step, N_delay, flag, N, sum, k;
  313. int Vout, Vin1, Vin2;
  314. // ADC&DAC setting
  315. ADCpoweron(20000); // power on ADC
  316. REFCON = 0x01; // internal 2.5V reference
  317. DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  318. ADCCP = 0x00; // conversion on ADC0
  319. ADCCON = 0x6E4; // continuous conversion
  320. // IO setting
  321. init_digital();
  322. // locking parameters initialization
  323. N_step = 10; // step size
  324. N_delay = 100; // wait for certain time
  325. flag = 1; // indicator for searching direction
  326. N = 1000; // number for averaging measurement
  327. Vin1 = 0; // initialize the voltage of first step
  328. // main loop for the locking
  329. while(1){
  330. ///Sweep();
  331. // switch between sweep mode and locking mode;
  332. // note that the sweep mode is just for the convenience of the experiment,
  333. // for locking the phase, it is not necessary.
  334. ///Write_Digital(0,1); // Output a high level digital output on pin P1.0 for indicating the locking mode.
  335. if(Read_Digital(SH_disab)==1 || Read_Digital(SH_in)==1){
  336. Vout = Vout + flag * N_step; // calculate the voltage for next step
  337. Vout = Bound(Vout); // limit the range of V
  338. DAC0DAT = DATtoADC(Vout); // output voltage on DAC0
  339. delay(N_delay); //wait for a certain time for the response of PZT
  340. // input average over N samples in order to filter out highest frequencies noise
  341. sum = 0; // initialization
  342. for(k = 1; k <= N; k++){
  343. while(!ADCSTA){} // wait for the end of ADC conversion
  344. sum += ADCtoDAT(ADCDAT); // read voltage from ADC0
  345. }
  346. Vin2 = sum/N; // calculate average value for the voltage of second step
  347. if(Vin2 < Vin1 && Read_Digital(LH_mode)==1) flag = -1 * flag; // change maximum searching direction if V2 < V1
  348. else if(Vin2 > Vin1 && Read_Digital(LH_mode)==0) flag = -1 * flag; // change minimum searching direction if V2 > V1
  349. Vin1 = Vin2; // update the voltage of first step
  350. }
  351. //else -> it's hold for low locking
  352. }
  353. }
  354. void lock_EOM2(void)
  355. {
  356. // define variables
  357. int N_step, N_delay, flag0,flag1, N, sum, k;
  358. int Vout0, Vin0a, Vin0b;
  359. int Vout1, Vin1a, Vin1b;
  360. // ADC&DAC setting
  361. ADCpoweron(20000); // power on ADC
  362. REFCON = 0x01; // internal 2.5V reference
  363. DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  364. DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  365. ADCCP = 0x00; // conversion on ADC0
  366. ADCCON = 0x6E4; // continuous conversion
  367. // IO setting
  368. init_digital();
  369. // locking parameters initialization
  370. N_step = 20; // step size
  371. N_delay = 10; // wait for certain time
  372. flag0 = 1; // indicator for searching direction
  373. flag1 = 1; // indicator for searching direction
  374. N = 200; // number for averaging measurement
  375. Vin0a = 0; // initialize the voltage of first step
  376. Vin1a = 0; // initialize the voltage of first step
  377. // main loop for the locking
  378. while(1){
  379. ///Sweep();
  380. // switch between sweep mode and locking mode;
  381. // note that the sweep mode is just for the convenience of the experiment,
  382. // for locking the phase, it is not necessary.
  383. ///Write_Digital(0,1); // Output a high level digital output on pin P1.0 for indicating the locking mode.
  384. if(Read_Digital(SH_disab)==1 || Read_Digital(SH_in)==1){
  385. Vout0 = Vout0 + flag0 * N_step; // calculate the voltage for next step
  386. Vout0 = Bound(Vout0); // limit the range of V
  387. DAC0DAT = DATtoADC(Vout0); // output voltage on DAC0
  388. Vout1 = Vout1 + flag1 * N_step; // calculate the voltage for next step
  389. Vout1 = Bound(Vout1); // limit the range of V
  390. DAC1DAT = DATtoADC(Vout1); // output voltage on DAC0
  391. delay(N_delay); //wait for a certain time for the response of PZT
  392. // input average over N samples in order to filter out highest frequencies noise
  393. sum = 0; // initialization
  394. ADCCP = 0x00; // conversion on ADC0
  395. for(k = 1; k <= N; k++){
  396. while(!ADCSTA){} // wait for the end of ADC conversion
  397. sum += ADCtoDAT(ADCDAT); // read voltage from ADC0
  398. }
  399. Vin0b = sum/N; // calculate average value for the voltage of second step
  400. if(Vin0b < Vin0a && Read_Digital(LH_mode)==1) flag0 = -1 * flag0; // change maximum searching direction if V2 < V1
  401. else if(Vin0b > Vin0a && Read_Digital(LH_mode)==0) flag0 = -1 * flag0; // change minimum searching direction if V2 > V1
  402. Vin0a = Vin0b; // update the voltage of first step
  403. // input average over N samples in order to filter out highest frequencies noise
  404. sum = 0; // initialization
  405. ADCCP = 0x01; // conversion on ADC1
  406. for(k = 1; k <= N; k++){
  407. while(!ADCSTA){} // wait for the end of ADC conversion
  408. sum += ADCtoDAT(ADCDAT); // read voltage from ADC0
  409. }
  410. Vin1b = sum/N; // calculate average value for the voltage of second step
  411. if(Vin1b < Vin1a && Read_Digital(LH_mode)==1) flag1 = -1 * flag1; // change maximum searching direction if V2 < V1
  412. else if(Vin1b > Vin1a && Read_Digital(LH_mode)==0) flag1 = -1 * flag1; // change minimum searching direction if V2 > V1
  413. Vin1a = Vin1b; // update the voltage of first step
  414. }
  415. //else -> it's hold for low locking
  416. }
  417. }
  418. void Copy(void)
  419. {
  420. // define variables
  421. int Vout, Vin;
  422. // ADC&DAC setting
  423. ADCpoweron(20000); // power on ADC
  424. REFCON = 0x01; // internal 2.5V reference
  425. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  426. //DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  427. ADCCP = 0x03; // conversion on ADC4
  428. ADCCON = 0x6E4; // continuous conversion
  429. // IO setting
  430. init_digital();
  431. // main loop for the locking
  432. while(1){
  433. while(!ADCSTA){} // wait for the end of ADC conversion
  434. Vin = ADCtoDAT(ADCDAT); // read voltage from ADC4
  435. Vout = Vin;
  436. DAC2DAT = DATtoADC(Vout); // output voltage on DAC2
  437. //else -> it's hold for low locking
  438. }
  439. }
  440. void Copy_S_and_H(void)
  441. {
  442. // define variables
  443. int Vout, Vin;
  444. // ADC&DAC setting
  445. ADCpoweron(20000); // power on ADC
  446. REFCON = 0x01; // internal 2.5V reference
  447. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  448. //DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  449. ADCCP = 0x03; // conversion on ADC4
  450. ADCCON = 0x6E4; // continuous conversion
  451. // IO setting
  452. init_digital();
  453. // main loop for the locking
  454. while(1){
  455. if(Read_Digital(6)==1){
  456. while(!ADCSTA){} // wait for the end of ADC conversion
  457. Vin = ADCtoDAT(ADCDAT); // read voltage from ADC4
  458. Vout = Vin;
  459. DAC2DAT = DATtoADC(Vout); // output voltage on DAC2
  460. }
  461. //else -> it's hold for low locking
  462. }
  463. }
  464. void Copy_S_and_Hcnt(void)
  465. {
  466. // define variables
  467. int Vout, Vin, k, sum, Ncount;
  468. k=0;
  469. sum =0;
  470. Ncount=100;
  471. // ADC&DAC setting
  472. ADCpoweron(20000); // power on ADC
  473. REFCON = 0x01; // internal 2.5V reference
  474. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  475. //DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  476. ADCCP = 0x03; // conversion on ADC4
  477. ADCCON = 0x6E4; // continuous conversion
  478. // IO setting
  479. init_digital();
  480. // main loop for the locking
  481. while(1){
  482. if(Read_Digital(6)==1){
  483. while(!ADCSTA){} // wait for the end of ADC conversion
  484. Vin = ADCtoDAT(ADCDAT); // read voltage from ADC4
  485. sum+=Vin;
  486. k++;
  487. }
  488. if(k>=Ncount){
  489. k=0;
  490. Vout = sum/Ncount;
  491. DAC2DAT = DATtoADC(Vout); // output voltage on DAC2
  492. sum=0;
  493. }
  494. //else -> it's hold for low locking
  495. }
  496. }
  497. void ramp_with_TTLin(void)
  498. {
  499. int Vout=0;
  500. int step=0;
  501. // ADC&DAC setting
  502. ADCpoweron(20000); // power on ADC
  503. REFCON = 0x01; // internal 2.5V reference
  504. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  505. //DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  506. ADCCP = 0x00; // conversion on ADC0
  507. ADCCON = 0x6E4; // continuous conversion
  508. // IO setting
  509. init_digital();
  510. while(1){
  511. if(Vout>=4095)
  512. step=-1;
  513. if(Vout<=0)
  514. step=1;
  515. Vout=Vout+step;
  516. if(Read_Digital(3)==1){
  517. DAC2DAT = DATtoADC(Vout); // output voltage on DAC0
  518. }
  519. }
  520. }
  521. #define SH_in0 0 // high-> lock, low -> hold
  522. #define SH_in1 1 // high-> lock, low -> hold
  523. #define SH_disab 7 // disable the sampler & holder function
  524. void lock_StabPulse(void)
  525. {
  526. // define variables
  527. int N_step, N_delay, cnt0,cnt1, N, sum0, sum1;
  528. int Vout0, Vin0;
  529. int Vout1, Vin1;
  530. int Vset0, Vset1;
  531. int Vmean;
  532. int MeasureTTLin = 6;
  533. int ModeTTLin = 3;
  534. int bit =0x00000001<<(20);
  535. /*POWKEY1 = 0x01;
  536. POWCON = 0x00; // 41.78MHz
  537. POWKEY2 = 0xF4;
  538. */
  539. GP1CON = 0x00000000; // IO initialization
  540. GP1DAT = 0xFF000000; // set P1.n as digital output
  541. // ADC&DAC setting
  542. ADCpoweron(20000); // power on ADC
  543. REFCON = 0x01; // internal 2.5V reference
  544. //DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  545. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  546. ADCCP = 0x03; // conversion on ADC0
  547. ADCCON = 0x6E4; // continuous conversion
  548. // IO setting
  549. init_digital();
  550. // locking parameters initialization
  551. N_step = 5; // step size
  552. N_delay = 10; // wait for certain time
  553. cnt0 = 0; // indicator for searching direction
  554. cnt1 = 0; // indicator for searching direction
  555. N = 100; // number for averaging measurement
  556. Vin0 = 0; // initialize the voltage of first step
  557. Vin1 = 0; // initialize the voltage of first step
  558. Vmean = 2000;
  559. // main loop for the locking
  560. while(1){
  561. ///Write_Digital(0,1); // Output a high level digital output on pin P1.0 for indicating the locking mode.
  562. //GP1DAT^=bit;
  563. // for each loop we check if there is something to measure
  564. if(Read_Digital(MeasureTTLin)==1){
  565. ADCCP = 0x03; // conversion on ADC0
  566. if (cnt0==0)
  567. sum0 = 0; // initialization of the measurement
  568. while(!ADCSTA){} // wait for the end of ADC conversion
  569. sum0 += ADCtoDAT(ADCDAT); // read voltage from ADC0
  570. cnt0++;
  571. if(cnt0>=N)
  572. Vin0 = sum0/N; // calculate average value
  573. }
  574. //the same for the other chanel
  575. /*if(Read_Digital(SH_in1)==1){
  576. ADCCP = 0x01; // conversion on ADC1
  577. if (cnt1==0)
  578. sum1 = 0; // initialization of the measurement
  579. while(!ADCSTA){} // wait for the end of ADC conversion
  580. sum1 += ADCtoDAT(ADCDAT); // read voltage from ADC0
  581. cnt1++;
  582. if(cnt1==N)
  583. Vin1 = sum1/N; // calculate average value
  584. }*/
  585. // if we are in the learning mode
  586. // we set the outputs to the average voltage
  587. // and save the current input level as the set point of the next locking enable
  588. if(Read_Digital(ModeTTLin)==0){
  589. if(cnt0>=N)
  590. Vset0 = Vin0;
  591. //if(cnt1>=N)
  592. // Vset1 = Vin1;
  593. Vout0 = Vmean;
  594. //Vout1 = Vmean;
  595. }
  596. // if we are in the locking mode and each time we have a complete measurement
  597. if(Read_Digital(ModeTTLin)==1 && cnt0>=N){
  598. if(Vset0<Vin0)
  599. Vout0 = Vout0 - N_step; // calculate the voltage for next step
  600. if(Vset0>Vin0)
  601. Vout0 = Vout0 + N_step; // calculate the voltage for next step
  602. }
  603. //the same for the second chanel
  604. /*if(Read_Digital(SH_disab)==0 && cnt1==N){
  605. if(Vset1<Vin1)
  606. Vout1 = Vout1 + N_step; // calculate the voltage for next step
  607. if(Vset1>Vin1)
  608. Vout1 = Vout1 - N_step; // calculate the voltage for next step
  609. } */
  610. if(cnt0>=N)
  611. cnt0=0;
  612. //those line could also be inserted in the if conditions and thus not set every loops
  613. DAC2DAT = DATtoADC(Vout0); // output voltage on DAC0
  614. //DAC1DAT = DATtoADC(Vout1); // output voltage on DAC1
  615. //delay(N_delay); //wait for a certain time.. maybe not nessecary
  616. //else -> it's hold for low locking
  617. }
  618. }
  619. void lock_StabPulse3(void)
  620. {
  621. // define variables
  622. int N_step, N_delay, cnt0,cnt1, N, sum0;
  623. int Vout0, Vin0;
  624. int Vout1, Vin1;
  625. int Vset0, Vset1;
  626. int Vmean;
  627. int MeasureTTLin = 6;
  628. int ModeTTLin = 3;
  629. int bit =0x00000001<<(19);
  630. POWKEY1 = 0x01;
  631. POWCON = 0x00; // 41.78MHz
  632. POWKEY2 = 0xF4;
  633. GP1CON = 0x00000000; // IO initialization
  634. GP1DAT = 0xFF000000; // set P1.n as digital output
  635. // ADC&DAC setting
  636. ADCpoweron(20000); // power on ADC
  637. REFCON = 0x01; // internal 2.5V reference
  638. //DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  639. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  640. ADCCP = 0x03; // conversion on ADC0
  641. ADCCON = 0x3E4; // continuous conversion
  642. // IO setting
  643. init_digital();
  644. // locking parameters initialization
  645. N_step = 5; // step size
  646. N_delay = 10; // wait for certain time
  647. cnt0 = 0; // indicator for searching direction
  648. cnt1 = 0; // indicator for searching direction
  649. N = 100; // number for averaging measurement
  650. Vin0 = 0; // initialize the voltage of first step
  651. Vin1 = 0; // initialize the voltage of first step
  652. Vmean = 2000;
  653. // main loop for the locking
  654. while(1){
  655. ///Write_Digital(0,1); // Output a high level digital output on pin P1.0 for indicating the locking mode.
  656. GP1DAT^=bit;
  657. // for each loop we check if there is something to measure
  658. if((((GP0DAT&0x000000FF)>>6)&0x1)==1){
  659. //ADCCP = 0x03; // conversion on ADC0
  660. if (cnt0==0)
  661. sum0 = 0; // initialization of the measurement
  662. while(!ADCSTA){} // wait for the end of ADC conversion
  663. sum0 += ADCtoDAT(ADCDAT); // read voltage from ADC0
  664. cnt0++;
  665. if(cnt0>=N)
  666. Vin0 = sum0/N; // calculate average value
  667. }
  668. // if we are in the learning mode
  669. // we set the outputs to the average voltage
  670. // and save the current input level as the set point of the next locking enable
  671. if((((GP0DAT&0x000000FF)>>3)&0x1)==0){
  672. if(cnt0>=N){
  673. Vset0 = Vin0;
  674. cnt0=0;
  675. }
  676. Vout0 = Vmean;
  677. }
  678. // if we are in the locking mode and each time we have a complete measurement
  679. /*if((((GP0DAT&0x000000FF)>>3)&0x1)==1 && cnt0>=N){
  680. cnt0=0;
  681. if(Vset0<Vin0)
  682. Vout0 = Vout0 - N_step; // calculate the voltage for next step
  683. if(Vset0>Vin0)
  684. Vout0 = Vout0 + N_step; // calculate the voltage for next step
  685. }*/
  686. if((((GP0DAT&0x000000FF)>>3)&0x1)==1 && cnt0>=N){
  687. cnt0=0;
  688. Vout0 = Vout0 + (Vset0-Vin0)/2;
  689. }
  690. //those line could also be inserted in the if conditions and thus not set every loops
  691. DAC2DAT = DATtoADC(Vout0); // output voltage on DAC0
  692. }
  693. }
  694. void lock_StabPulse4(void)
  695. {
  696. // define variables
  697. int N_step, N_delay, cnt0,cnt1, N, sum0, sum1;
  698. int Vout0, Vin0;
  699. unsigned long int Vout1, Vin1;
  700. int Vset0, Vset1;
  701. int Vmean;
  702. int MeasureTTLin = 6;
  703. int ModeTTLin = 3;
  704. int bit =0x00000001<<(19);
  705. int step_max = 100;
  706. int step;
  707. POWKEY1 = 0x01;
  708. POWCON = 0x00; // 41.78MHz
  709. POWKEY2 = 0xF4;
  710. GP1CON = 0x00000000; // IO initialization
  711. GP1DAT = 0xFF000000; // set P1.n as digital output
  712. // ADC&DAC setting
  713. ADCpoweron(20000); // power on ADC
  714. REFCON = 0x01; // internal 2.5V reference
  715. //DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  716. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  717. ADCCP = 0x03; // conversion on ADC0
  718. ADCCON = 0x3E4; // continuous conversion
  719. // IO setting
  720. init_digital();
  721. // locking parameters initialization
  722. N_step = 5; // step size
  723. N_delay = 10; // wait for certain time
  724. cnt0 = 0; // indicator for searching direction
  725. cnt1 = 0; // indicator for searching direction
  726. N = 50; // number for averaging measurement
  727. Vin0 = 0; // initialize the voltage of first step
  728. Vin1 = 0; // initialize the voltage of first step
  729. Vmean = 2000;
  730. // main loop for the locking
  731. while(1){
  732. ///Write_Digital(0,1); // Output a high level digital output on pin P1.0 for indicating the locking mode.
  733. //GP1DAT^=bit;
  734. // for each loop we check if there is something to measure
  735. if((((GP0DAT&0x000000FF)>>6)&0x1)==1){
  736. //ADCCP = 0x03; // conversion on ADC0
  737. if (cnt0==0)
  738. sum0 = 0; // initialization of the measurement
  739. GP1DAT^=bit;
  740. while(!ADCSTA){} // wait for the end of ADC conversion
  741. GP1DAT^=bit;
  742. if((((GP0DAT&0x000000FF)>>6)&0x1)==1){
  743. sum0 += ADCtoDAT(ADCDAT); // read voltage from ADC0
  744. cnt0++;
  745. }
  746. if(cnt0>=N)
  747. Vin0 = sum0/N; // calculate average value
  748. }
  749. else if((((GP0DAT&0x000000FF)>>3)&0x1)==1){
  750. if(cnt0>=N){
  751. cnt0=0;
  752. step = (Vset0-Vin0)/4;
  753. if (step>step_max)
  754. step = step_max;
  755. else if (step<-step_max)
  756. step = -step_max;
  757. Vout0 = Vout0 + step;
  758. }
  759. }
  760. // if we are in the learning mode
  761. // we set the outputs to the average voltage
  762. // and save the current input level as the set point of the next locking enable
  763. else{
  764. if(cnt0>=N){
  765. Vset0 = Vin0;
  766. cnt0=0;
  767. }
  768. Vout0 = Vmean;
  769. }
  770. //those line could also be inserted in the if conditions and thus not set every loops
  771. DAC2DAT = DATtoADC(Vout0); // output voltage on DAC0
  772. }
  773. }
  774. void lock_StabPulse2(void)
  775. {
  776. // define variables
  777. int N_step, N_delay, cnt0,cnt1, N, sum0, sum1;
  778. int Vout0, Vin0;
  779. int Vout1, Vin1;
  780. int Vset0, Vset1;
  781. int Vmean;
  782. int MeasureTTLin = 6;
  783. int ModeTTLin = 3;
  784. int bit =0x00000001<<(20);
  785. POWKEY1 = 0x01;
  786. POWCON = 0x00; // 41.78MHz
  787. POWKEY2 = 0xF4;
  788. GP1CON = 0x00000000; // IO initialization
  789. GP1DAT = 0xFF000000; // set P1.n as digital output
  790. // ADC&DAC setting
  791. ADCpoweron(20000); // power on ADC
  792. REFCON = 0x01; // internal 2.5V reference
  793. //DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  794. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  795. ADCCP = 0x03; // conversion on ADC0
  796. ADCCON = 0x6E4; // continuous conversion
  797. // IO setting
  798. init_digital();
  799. // locking parameters initialization
  800. N_step = 5; // step size
  801. N_delay = 10; // wait for certain time
  802. cnt0 = 0; // indicator for searching direction
  803. cnt1 = 0; // indicator for searching direction
  804. N = 100; // number for averaging measurement
  805. Vin0 = 0; // initialize the voltage of first step
  806. Vin1 = 0; // initialize the voltage of first step
  807. Vmean = 2000;
  808. // main loop for the locking
  809. while(1){
  810. ///Write_Digital(0,1); // Output a high level digital output on pin P1.0 for indicating the locking mode.
  811. GP1DAT^=bit;
  812. // for each loop we check if there is something to measure
  813. if(((GP0DAT&0x000000FF)>>6)&0x1==1){
  814. ADCCP = 0x03; // conversion on ADC0
  815. if (cnt0==0)
  816. sum0 = 0; // initialization of the measurement
  817. while(!ADCSTA){} // wait for the end of ADC conversion
  818. sum0 += ADCtoDAT(ADCDAT); // read voltage from ADC0
  819. cnt0++;
  820. if(cnt0>=N)
  821. Vin0 = sum0/N; // calculate average value
  822. }
  823. // if we are in the learning mode
  824. // we set the outputs to the average voltage
  825. // and save the current input level as the set point of the next locking enable
  826. if(((GP0DAT&0x000000FF)>>3)&0x1==0){
  827. if(cnt0>=N){
  828. Vset0 = Vin0;
  829. cnt0=0;
  830. }
  831. //if(cnt1>=N)
  832. // Vset1 = Vin1;
  833. Vout0 = Vmean;
  834. //Vout1 = Vmean;
  835. }
  836. // if we are in the locking mode and each time we have a complete measurement
  837. if(((GP0DAT&0x000000FF)>>3)&0x1==1 && cnt0>=N){
  838. cnt0=0;
  839. if(Vset0<Vin0)
  840. Vout0 = Vout0 - N_step; // calculate the voltage for next step
  841. if(Vset0>Vin0)
  842. Vout0 = Vout0 + N_step; // calculate the voltage for next step
  843. }
  844. //those line could also be inserted in the if conditions and thus not set every loops
  845. DAC2DAT = DATtoADC(Vout0); // output voltage on DAC0
  846. //else -> it's hold for low locking
  847. }
  848. }
  849. void lock_EOM_and_AOM(void)
  850. {
  851. int step, N_delay, V_EOM;//, V_AOM;
  852. //unsigned long y_max, y_min;
  853. ADCpoweron(20000); // power on ADC
  854. ADCCP = 0x00; // conversion on ADC0
  855. DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  856. DAC1CON = 0x12;
  857. REFCON = 0x01; // internal 2.5V reference
  858. ADCCON = 0x6E4; // continuous conversion
  859. GP1CON = 0x00000000; // setting IO
  860. GP1DAT = 0xFF000000; // group P1.x as output
  861. GP0CON = 0x00000000; // setting IO
  862. GP0DAT = 0x00000000; // group P0.x as input
  863. step=5;
  864. N_delay=500;
  865. V_EOM = 2048;
  866. // V_AOM = 0;
  867. while(1){ // infinite loop
  868. // wait for the trigger of calibration
  869. if(Read_Digital(6)==1){
  870. Write_Digital(3,1);// LED on
  871. GP4DAT = 0x04000000;
  872. // calibration of the zero of the EOM
  873. Lock_min_fringe(&V_EOM,step, N_delay);
  874. // calibration of the power with the AOM
  875. }
  876. Write_Digital(3,0);// LED off
  877. GP4DAT ^= 0x00040000;
  878. }
  879. }
  880. int main(void)
  881. {
  882. //test_blink();
  883. //Copy();
  884. //Copy_S_and_Hcnt();
  885. //ramp();
  886. //ramp_with_TTLin();
  887. //lock_StabPulse();
  888. //lock_EOM2();
  889. //synchronizer2();
  890. lock_StabPulse4();
  891. //test_clock2();
  892. return 0;
  893. }