DemoPSD1p0.C 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. v1.0
  3. PSD
  4. */
  5. #include<ADuC7020.h>
  6. #include<stdlib.h>
  7. #include<stdio.h>
  8. #include<string.h>
  9. void My_IRQ_Handler(void);
  10. //short i = 0;
  11. unsigned char Byte_addr = 0;
  12. int first = 1;
  13. int i2c_cnt = 0;
  14. #define BIGDAT_SZ 256
  15. unsigned short BigDat[BIGDAT_SZ];
  16. char text[512];
  17. unsigned short Vout[4];
  18. //unsigned short Vlearn;
  19. double Vin[5];
  20. unsigned short remote_trigg,wf_len;
  21. //unsigned short start, stop,start_gnd,stop_gnd,start2,stop2,Vset, step_max,N;
  22. unsigned char busy,channel;
  23. unsigned int trigg_cnt,v_cnt,wf_cnt;
  24. //char format = 0;
  25. unsigned char* pbuff;
  26. unsigned char* plist[256];
  27. void delay (int length)
  28. {
  29. while (length >=0)
  30. length--;
  31. }
  32. // conversion of the read value into it corresponding 12bits integer
  33. int ADCtoDAT(unsigned long ADC)
  34. {
  35. return (ADC&0xFFF0000)>>16;
  36. }
  37. unsigned long DATtoADC(int DAT)
  38. {
  39. unsigned long ADC;
  40. ADC=DAT;
  41. return ADC<<16;
  42. }
  43. unsigned long DATtoDAC(unsigned short DAT)
  44. {
  45. unsigned int ADC;
  46. ADC=DAT;
  47. return ADC<<16;
  48. }
  49. int Read_Digital(int n)
  50. {
  51. return ((GP0DAT&0x000000FF)>>n)&0x1;
  52. }
  53. void Write_Digital(int n, int state)
  54. {
  55. if(state==1)
  56. GP2DAT=(0x00000001<<(n+16))|GP2DAT;
  57. else
  58. GP2DAT=~((0x00000001<<(n+16))|(~GP2DAT));
  59. }
  60. void ADCpoweron(int time)
  61. {
  62. ADCCON = 0x620; // power-on the ADC
  63. while (time >=0) // wait for ADC to be fully powered on
  64. time--;
  65. }
  66. void get_DACs(void)
  67. {
  68. Vout[0]=DAC0DAT>>16;
  69. Vout[1]=DAC1DAT>>16;
  70. Vout[2]=DAC2DAT>>16;
  71. Vout[3]=DAC3DAT>>16;
  72. }
  73. void set_DACs(void)
  74. {
  75. DAC0DAT=DATtoDAC(Vout[0]);
  76. DAC1DAT=DATtoDAC(Vout[1]);
  77. DAC2DAT=DATtoDAC(Vout[2]);
  78. DAC3DAT=DATtoDAC(Vout[3]);
  79. }
  80. void lock_StabPulse_i2c(void)
  81. {
  82. // define variables
  83. // unsigned int cnt_N;
  84. double sum;//,sum_gnd;
  85. // unsigned short Vout2;
  86. //double Vin,Vin_gnd;
  87. //int Vmean;
  88. // int step = 100;
  89. short armed; // this is used to detect the trigger :
  90. // when the trigger input is low armed is set to 1
  91. // when a measurement start it is set to 0
  92. // short valid_data;
  93. int k,i;
  94. unsigned short Data[256];
  95. //unsigned int tmp_dat[256];
  96. POWKEY1 = 0x01;
  97. POWCON = 0x00; // 41.78MHz
  98. POWKEY2 = 0xF4;
  99. //GP1CON = 0x00000000; // IO initialization
  100. //GP1DAT = 0xFF000000; // set P1.n as digital output
  101. GP0CON = 0x00000000; // IO initialization
  102. //GP0DAT = 0x00000000; // set P0.n as digital input
  103. // ADC&DAC setting
  104. ADCpoweron(20000); // power on ADC
  105. REFCON = 0x01; // internal 2.5V reference
  106. DAC0CON = 0x12; // AGND-ARef range 0x12 2.5V
  107. DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  108. DAC2CON = 0x12; // AGND-ARef range 0x12 2.5V
  109. DAC3CON = 0x12; // AGND-ARef range 0x12 2.5V
  110. ADCCP = 0x03; // conversion on ADC0
  111. ADCCON = 0x3E4; // continuous conversion
  112. // IO setting
  113. GP2CON = 0x00000000; // IO initialization
  114. GP2DAT = 0xFF000000; // set P2.n as digital output
  115. GP0CON = 0x00000000; // IO initialization
  116. GP0DAT = 0x00000000; // set P0.n as digital input
  117. // locking parameters initialization
  118. // cnt = 0; //
  119. // N = 1; // number of measume,ts for averaging
  120. // Vin = 0; // initialize the voltage of first step
  121. // Vin_gnd = 0;
  122. // Vmean = 2000;
  123. // start = 20;
  124. // stop = 30;
  125. // start_gnd = 0;
  126. // stop_gnd = 10;
  127. wf_len = 200;
  128. // Vset = 200;
  129. // Vlearn = 2000;
  130. // step = 50;
  131. // step_max = 100;
  132. // Gain = 1;
  133. // I2C on P1.0 and P1.1
  134. GP1CON = 0x22;
  135. IRQ = My_IRQ_Handler;
  136. IRQEN = 0x200; // I2C0 Slave Interupt
  137. I2C0CFG = 0x04001;
  138. // Slave ID
  139. I2C0ID0 = (0x50 + (((GP0DAT&0x000000FF)>>5)&0x1)+(((GP0DAT&0x000000FF)>>7)&0x1)*2)<<1;
  140. I2C0STX = 0x00;
  141. I2C0STX = 0x00;
  142. // assignation of the different pointers for the I2C exchange of data
  143. for (k=0;k<16;k++){
  144. plist[k] = (unsigned char*)(BigDat+k*16);
  145. plist[k+50] = (unsigned char*)(text+k*32);
  146. }
  147. for(i=0;i<BIGDAT_SZ;i++)
  148. BigDat[i]=0;
  149. sprintf(text,"pulse stabilization v1.2 => %s\ncompiled: %s\nbecause we can!",__func__,__DATE__);
  150. for (k=0;k<4;k++){
  151. plist[100+k] = (unsigned char*)(Vout+k);
  152. }
  153. // 104 to execute the function get_DACs
  154. // 105 to execute the function set_DACs
  155. //plist[110] = (unsigned char*)&(Vin[0]);
  156. //plist[111] = (unsigned char*)&(Vin[1]);
  157. //plist[112] = (unsigned char*)&(Vin[2]);
  158. //plist[113] = (unsigned char*)&(Vin[3]);
  159. //plist[114] = (unsigned char*)&(Vin[4]);
  160. for (k=0;k<5;k++){
  161. plist[110+k] = (unsigned char*)(Vin+k);
  162. }
  163. //plist[114] = (unsigned char*)&v_cnt;
  164. plist[115] = (unsigned char*)&remote_trigg;
  165. plist[120] = (unsigned char*)&I2C0ID0;
  166. plist[121] = (unsigned char*)&channel;
  167. // plist[122] = (unsigned char*)&transf;
  168. plist[123] = (unsigned char*)&wf_len;
  169. plist[124] = (unsigned char*)&trigg_cnt;
  170. // plist[125] = (unsigned char*)&Vset;
  171. // plist[126] = (unsigned char*)&N;
  172. // plist[127] = (unsigned char*)&start;
  173. // plist[128] = (unsigned char*)&stop;
  174. // plist[129] = (unsigned char*)&start2;
  175. // plist[130] = (unsigned char*)&stop2;
  176. // plist[131] = (unsigned char*)&start_gnd;
  177. // plist[132] = (unsigned char*)&stop_gnd;
  178. // plist[133] = (unsigned char*)&enab_gnd;
  179. // plist[134] = (unsigned char*)&Gain;
  180. // plist[135] = (unsigned char*)&step_max;
  181. // DAC0DAT = DATtoADC(10);
  182. // DAC1DAT = DATtoADC(20);
  183. // DAC2DAT = DATtoADC(2000);
  184. // DAC3DAT = DATtoADC(40);
  185. // Vset=0;
  186. // Vout[3] = 111;
  187. // set_DACs();
  188. // transf = 0;
  189. // trigg_cnt = 0;
  190. // wf_cnt = 0;
  191. // v_cnt = 0;
  192. // cnt_N = 0;
  193. // enab_gnd = 0;
  194. remote_trigg = 0;
  195. channel = 0;
  196. // valid_data=0;
  197. // main loop for the locking
  198. while(1){
  199. // trigg in is on p0.3 => we check that it is low first (rising edge detection)
  200. if((((GP0DAT&0x000000FF)>>3)&0x1)==0){
  201. armed = 1;
  202. }
  203. // now p0.3 is high => this is our rising edge
  204. if(((((GP0DAT&0x000000FF)>>3)&0x1)==1 && armed==1) || remote_trigg){
  205. armed = 0;
  206. //busy = 0;
  207. //*** aquisition of the waveform ***
  208. for(i=1;i<5;i++){
  209. ADCCP = i;
  210. for(k=0;k<wf_len;k++){
  211. while(!ADCSTA){} // wait for the end of ADC conversion
  212. Data[k]= (unsigned short)(ADCDAT >> 16); // read voltage from ADC0
  213. }
  214. //if(busy==0){ // supposed to guaranty that no i2c transfer has been performed during the wf acquisition
  215. //*** copy for the i2c ***
  216. if(i==channel){
  217. memcpy(BigDat,Data,256*sizeof(short));
  218. }
  219. sum = 0; // initialization of the measurement
  220. for(k=0;k<wf_len;k++){
  221. sum += Data[k];
  222. //cnt++;
  223. }
  224. Vin[i] = sum/wf_len; // calculate average value
  225. // valid_data = 1;
  226. //}
  227. // else busy = 0;
  228. }
  229. remote_trigg=0;
  230. }
  231. }
  232. }
  233. int main(void)
  234. {
  235. lock_StabPulse_i2c();
  236. return 0;
  237. }
  238. /*************************************************/
  239. /*************************************************/
  240. /************ IRQ Service Routine *************/
  241. /*************************************************/
  242. /*************************************************/
  243. void My_IRQ_Handler()
  244. {
  245. int status = I2C0SSTA;
  246. busy = 1;
  247. // Slave Recieve
  248. if ((status & 0x08)==0x08) // Slave Recieve IRQ
  249. {
  250. if(first==1){
  251. first=0;
  252. Byte_addr=I2C0SRX;
  253. I2C0FSTA|= 1 << 8;
  254. i2c_cnt = 0;
  255. // if(Byte_addr==122)
  256. // transf = 1;
  257. if(Byte_addr==104)
  258. get_DACs();
  259. if(Byte_addr==105)
  260. set_DACs();
  261. Write_Digital(2,0);
  262. pbuff = plist[Byte_addr];
  263. I2C0STX = pbuff[0];
  264. }
  265. else {
  266. pbuff[i2c_cnt] = I2C0SRX;
  267. i2c_cnt++;
  268. }
  269. }
  270. // Slave Transmit
  271. else if ((status & 0x04)==0x04) // Slave Transmit IRQ
  272. {
  273. i2c_cnt ++;
  274. I2C0STX = pbuff[i2c_cnt];
  275. I2C0ADR = 0xA1;
  276. //if(Byte_addr>=110 && Byte_addr<=113 && i2c_cnt==1)
  277. //set_DACs();
  278. }
  279. else if((status & 0x0400)==0x0400) //
  280. {
  281. first = 1;
  282. //Write_Digital(2,1);
  283. }
  284. busy=1;
  285. }