DemoHD.C 9.2 KB

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