Demo - Copy.C 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. v2.0
  3. this program use old code used in another experiment
  4. "int" is used for the ADC and DAC data although it should be unsigned,
  5. it should be not a problem at all as this data are limited to 12bits...
  6. */
  7. #include<ADuC7020.h>
  8. #include<stdlib.h>
  9. #include<stdio.h>
  10. #include<string.h>
  11. void My_IRQ_Handler(void);
  12. short i = 0;
  13. unsigned char Byte_addr = 0;
  14. int first = 1;
  15. int i2c_cnt = 0;
  16. #define BIGDAT_SZ 256
  17. unsigned short BigDat[BIGDAT_SZ];
  18. char text[512];
  19. unsigned short Vout[4];
  20. unsigned short Vlearn;
  21. double Vin,Vin_gnd;
  22. unsigned short start, stop,start_gnd,stop_gnd,start2,stop2,Vset,wf_len, step_max,N;
  23. unsigned char transf,enab_gnd,busy,remote_trigg;
  24. unsigned int trigg_cnt,v_cnt,wf_cnt;
  25. float Gain;
  26. unsigned char pga_state = 0x08; // gain 1 channel 8
  27. //char format = 0;
  28. unsigned char* pbuff;
  29. unsigned char* plist[256];
  30. void delay (int length)
  31. {
  32. while (length >=0)
  33. length--;
  34. }
  35. // conversion of the read value into it corresponding 12bits integer
  36. int ADCtoDAT(unsigned long ADC)
  37. {
  38. return (ADC&0xFFF0000)>>16;
  39. }
  40. unsigned long DATtoADC(int DAT)
  41. {
  42. unsigned long ADC;
  43. ADC=DAT;
  44. return ADC<<16;
  45. }
  46. unsigned long DATtoDAC(unsigned short DAT)
  47. {
  48. unsigned int ADC;
  49. ADC=DAT;
  50. return ADC<<16;
  51. }
  52. int Read_Digital(int n)
  53. {
  54. return ((GP0DAT&0x000000FF)>>n)&0x1;
  55. }
  56. void Write_Digital(int n, int state)
  57. {
  58. if(state==1)
  59. GP2DAT=(0x00000001<<(n+16))|GP2DAT;
  60. else
  61. GP2DAT=~((0x00000001<<(n+16))|(~GP2DAT));
  62. }
  63. void ADCpoweron(int time)
  64. {
  65. ADCCON = 0x620; // power-on the ADC
  66. while (time >=0) // wait for ADC to be fully powered on
  67. time--;
  68. }
  69. void get_DACs(void)
  70. {
  71. Vout[0]=DAC0DAT>>16;
  72. Vout[1]=DAC1DAT>>16;
  73. Vout[2]=DAC2DAT>>16;
  74. Vout[3]=DAC3DAT>>16;
  75. }
  76. void set_DACs(void)
  77. {
  78. DAC0DAT=DATtoDAC(Vout[0]);
  79. DAC1DAT=DATtoDAC(Vout[1]);
  80. DAC2DAT=DATtoDAC(Vout[2]);
  81. DAC3DAT=DATtoDAC(Vout[3]);
  82. }
  83. void set_pga(){
  84. SPITX = 0x3A; // transmit command or any dummy data
  85. while ((SPISTA & 0x02) != 0x02) ; // wait for data received status bit
  86. SPITX = pga_state; // transmit command or any dummy data
  87. while ((SPISTA & 0x02) != 0x02) ; // wait for data received status bit
  88. }
  89. void get_pga(){
  90. SPITX = 0x7A;//0x3A; // transmit command or any dummy data
  91. while ((SPISTA & 0x02) != 0x02) ; // wait for data received status bit
  92. SPITX = 0;//pga_state; // transmit command or any dummy data
  93. while ((SPISTA & 0x02) != 0x02) ; // wait for data received status bit
  94. delay(500);
  95. SPITX = 0;//0x3A; // transmit command or any dummy data
  96. while ((SPISTA & 0x02) != 0x02) ; // wait for data received status bit
  97. SPITX = 0;//pga_state; // transmit command or any dummy data
  98. while ((SPISTA & 0x02) != 0x02) ; // wait for data received status bit
  99. }
  100. void lock_StabPulse_i2c(void)
  101. {
  102. // define variables
  103. unsigned int cnt_N;
  104. double sum,sum_gnd;
  105. unsigned short Vout1, Vout2;
  106. //double Vin,Vin_gnd;
  107. //int Vmean;
  108. int step = 100;
  109. short armed; // this is used to detect the trigger :
  110. // when the trigger input is low armed is set to 1
  111. // when a measurement start it is set to 0
  112. short valid_data;
  113. int k;
  114. unsigned short Data[256];
  115. //unsigned int tmp_dat[256];
  116. POWKEY1 = 0x01;
  117. POWCON = 0x00; // 41.78MHz
  118. POWKEY2 = 0xF4;
  119. //GP1CON = 0x00000000; // IO initialization
  120. //GP1DAT = 0xFF000000; // set P1.n as digital output
  121. GP0CON = 0x00000000; // IO initialization
  122. //GP0DAT = 0x00000000; // set P0.n as digital input
  123. // ADC&DAC setting
  124. ADCpoweron(20000); // power on ADC
  125. REFCON = 0x01; // internal 2.5V reference
  126. DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  127. DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  128. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  129. DAC3CON = 0x12; // AGND-ARef range 0x12 2.5V
  130. ADCCP = 0x03; // conversion on ADC0
  131. ADCCON = 0x3E4; // continuous conversion
  132. // IO setting
  133. GP2CON = 0x00000000; // IO initialization
  134. GP2DAT = 0xFF000000; // set P2.n as digital output
  135. GP0CON = 0x00000000; // IO initialization
  136. GP0DAT = 0x00000000; // set P0.n as digital input
  137. // locking parameters initialization
  138. // cnt = 0; //
  139. N = 10; // number of measume,ts for averaging
  140. Vin = 0; // initialize the voltage of first step
  141. Vin_gnd = 0;
  142. // Vmean = 2000;
  143. start = 20;
  144. stop = 30;
  145. start_gnd = 0;
  146. stop_gnd = 10;
  147. wf_len = 200;
  148. Vset = 200;
  149. Vlearn = 2000;
  150. step = 50;
  151. step_max = 100;
  152. Gain = 1;
  153. GP1CON = 0x22220022; // configure SPI such the P1.0 and P1.1 are set for I2C0
  154. // P1.2 and P1.3 are set for GPIO (connected on 50Ohms driver TTL)
  155. // P1.4-7 on SPI
  156. SPIDIV = 0xCC; // set SPI clock 40960000/(2x(1+SPIDIV))
  157. // 0xCC = 100kHz
  158. //
  159. SPICON = 0x1043; // enable SPI master in continuous transfer mode
  160. // slave select will stay low during the all transmission
  161. IRQ = My_IRQ_Handler;
  162. IRQEN = 0x200; // I2C0 Slave Interupt
  163. I2C0CFG = 0x04001;
  164. // Slave ID
  165. I2C0ID0 = (0x50)<<1;//(0x50 + (((GP0DAT&0x000000FF)>>5)&0x1)+(((GP0DAT&0x000000FF)>>7)&0x1)*2)<<1;
  166. I2C0STX = 0x00;
  167. I2C0STX = 0x00;
  168. // assignation of the different pointers for the I2C exchange of data
  169. for (k=0;k<16;k++){
  170. plist[k] = (unsigned char*)(BigDat+k*16);
  171. plist[k+50] = (unsigned char*)(text+k*32);
  172. }
  173. for(i=0;i<BIGDAT_SZ;i++)
  174. BigDat[i]=0;
  175. sprintf(text,"pulse stabilization v2.0 => %s\ncompiled: %s\nbecause we can!",__func__,__DATE__);
  176. for (k=0;k<4;k++){
  177. plist[100+k] = (unsigned char*)(Vout+k);
  178. }
  179. // 104 to execute the function get_DACs
  180. // 105 to execute the function set_DACs
  181. plist[110] = (unsigned char*)&Vlearn;
  182. plist[111] = (unsigned char*)&Vin;
  183. plist[112] = (unsigned char*)&Vin_gnd;
  184. plist[113] = (unsigned char*)&wf_cnt;
  185. plist[114] = (unsigned char*)&v_cnt;
  186. plist[115] = (unsigned char*)&remote_trigg;
  187. plist[120] = (unsigned char*)&I2C0ID0;
  188. plist[121] = (unsigned char*)&ADCCP;
  189. plist[122] = (unsigned char*)&transf;
  190. plist[123] = (unsigned char*)&wf_len;
  191. plist[124] = (unsigned char*)&trigg_cnt;
  192. plist[125] = (unsigned char*)&Vset;
  193. plist[126] = (unsigned char*)&N;
  194. plist[127] = (unsigned char*)&start;
  195. plist[128] = (unsigned char*)&stop;
  196. plist[129] = (unsigned char*)&start2;
  197. plist[130] = (unsigned char*)&stop2;
  198. plist[131] = (unsigned char*)&start_gnd;
  199. plist[132] = (unsigned char*)&stop_gnd;
  200. plist[133] = (unsigned char*)&enab_gnd;
  201. plist[134] = (unsigned char*)&Gain;
  202. plist[135] = (unsigned char*)&step_max;
  203. plist[136] = (unsigned char*)&pga_state;
  204. DAC0DAT = DATtoADC(10);
  205. DAC1DAT = DATtoADC(20);
  206. DAC2DAT = DATtoADC(2000);
  207. DAC3DAT = DATtoADC(40);
  208. Vset=0;
  209. Vout[3] = 111;
  210. set_DACs();
  211. transf = 0;
  212. trigg_cnt = 0;
  213. wf_cnt = 0;
  214. v_cnt = 0;
  215. cnt_N = 0;
  216. enab_gnd = 0;
  217. remote_trigg = 0;
  218. valid_data=0;
  219. // main loop for the locking
  220. while(1){
  221. Write_Digital(0, 0);
  222. // trigg in is on p0.3 => we check that it is low first (rising edge detection)
  223. if((((GP0DAT&0x000000FF)>>3)&0x1)==0){
  224. armed = 1;
  225. }
  226. // now p0.3 is high => this is our rising edge
  227. if(((((GP0DAT&0x000000FF)>>3)&0x1)==1 && armed==1) || remote_trigg){
  228. armed = 0;
  229. //busy = 0;
  230. //*** aquisition of the waveform ***
  231. /*for(k=0;k<wf_len;k++){
  232. while(!ADCSTA){} // wait for the end of ADC conversion
  233. tmp_dat[k]= ADCDAT; // read voltage from ADC0
  234. }
  235. for(k=0;k<wf_len;k++){
  236. Data[k]= (tmp_dat[k]&0xFFF0000)>>16; // read voltage from ADC0
  237. }*/
  238. for(k=0;k<wf_len;k++){
  239. while(!ADCSTA){} // wait for the end of ADC conversion
  240. Data[k]= (unsigned short)(ADCDAT >> 16); // read voltage from ADC0
  241. }
  242. trigg_cnt++;
  243. if(busy==0){ // supposed to guaranty that no i2c transfer has been performed during the wf acquisition
  244. //*** copy for the i2c ***
  245. memcpy(BigDat,Data,256*sizeof(short));
  246. wf_cnt++;
  247. cnt_N++;
  248. //sum of the data
  249. for(k=start;k<stop;k++){
  250. sum += Data[k];
  251. //cnt++;
  252. }
  253. for(k=start_gnd;k<stop_gnd;k++){
  254. sum_gnd += Data[k];
  255. //cnt_gnd++;
  256. }
  257. if(cnt_N>=N){
  258. Vin = sum/(cnt_N*(stop-start)); // calculate average value
  259. Vin_gnd = sum_gnd/(cnt_N*(stop_gnd-start_gnd)); // calculate average value
  260. valid_data = 1;
  261. v_cnt++;
  262. sum = 0; // initialization of the measurement
  263. sum_gnd = 0;
  264. cnt_N = 0;
  265. }
  266. }
  267. else busy = 0;
  268. //*** feedback *** (mode is on pin p0.6)
  269. Write_Digital(0, 1);
  270. // LOCK MODE
  271. if((((GP0DAT&0x000000FF)>>6)&0x1)==1){
  272. if(valid_data==1){
  273. valid_data=0;
  274. cnt_N=0;
  275. //cnt_gnd=0;
  276. step = (Vset-Vin+Vin_gnd*enab_gnd)*Gain;
  277. if (step>step_max)
  278. step = step_max;
  279. else if (step<-step_max)
  280. step = -step_max;
  281. Vout2 = Vout2 + step;
  282. // Adjust Vout1 if necessary
  283. if (Vout2 < 2000 || Vout2 > 4000){
  284. Vout1 = Vout1 + (Vout2-3000)/20;
  285. Vout2 = 3000;
  286. }
  287. if(Vout1>4095){
  288. //Write_Digital(0,1);
  289. Vout1 = 4095;
  290. }
  291. else if(Vout1<1){
  292. //Write_Digital(0,1);
  293. Vout1 = 1;
  294. }else{
  295. //Write_Digital(0,0);
  296. }
  297. /*if (step>step_max)
  298. step = step_max;
  299. else if (step<-step_max)
  300. step = -step_max;
  301. Vout2 = Vout2 + step;
  302. if(Vout2>4095){
  303. Write_Digital(0,1);
  304. Vout2 = 4090;
  305. }
  306. else{
  307. Write_Digital(0,0);
  308. }*/
  309. }
  310. }
  311. // LEARN MODE
  312. // we set the outputs to the average voltage
  313. // and save the current input level as the set point of the next locking enable
  314. else{
  315. if(valid_data==1){
  316. valid_data=0;
  317. Vset = Vin-Vin_gnd*enab_gnd;
  318. cnt_N=0;
  319. }
  320. Vout1 = Vlearn;
  321. Vout2 = Vlearn;
  322. }
  323. //this line could also be inserted in the if conditions and thus not set every loops
  324. DAC1DAT = DATtoADC(Vout1); // output voltage
  325. //DAC2DAT = DATtoADC(Vout2); // output voltage
  326. }
  327. }
  328. }
  329. int main(void)
  330. {
  331. lock_StabPulse_i2c();
  332. return 0;
  333. }
  334. /*************************************************/
  335. /*************************************************/
  336. /************ IRQ Service Routine *************/
  337. /*************************************************/
  338. /*************************************************/
  339. void My_IRQ_Handler()
  340. {
  341. int status = I2C0SSTA;
  342. busy = 1;
  343. // Slave Recieve
  344. if ((status & 0x08)==0x08) // Slave Recieve IRQ
  345. {
  346. if(first==1){
  347. first=0;
  348. Byte_addr=I2C0SRX;
  349. I2C0FSTA|= 1 << 8;
  350. i2c_cnt = 0;
  351. if(Byte_addr==122)
  352. transf = 1;
  353. if(Byte_addr==104)
  354. get_DACs();
  355. if(Byte_addr==105)
  356. set_DACs();
  357. if(Byte_addr==137)
  358. set_pga();
  359. if(Byte_addr==138)
  360. get_pga();
  361. Write_Digital(2,0);
  362. pbuff = plist[Byte_addr];
  363. I2C0STX = pbuff[0];
  364. }
  365. else {
  366. pbuff[i2c_cnt] = I2C0SRX;
  367. i2c_cnt++;
  368. }
  369. }
  370. // Slave Transmit
  371. else if ((status & 0x04)==0x04) // Slave Transmit IRQ
  372. {
  373. i2c_cnt ++;
  374. I2C0STX = pbuff[i2c_cnt];
  375. I2C0ADR = 0xA1;
  376. //if(Byte_addr>=110 && Byte_addr<=113 && i2c_cnt==1)
  377. //set_DACs();
  378. }
  379. else if((status & 0x0400)==0x0400) //
  380. {
  381. first = 1;
  382. //Write_Digital(2,1);
  383. }
  384. busy=1;
  385. }