Ad.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /*****************************************
  2. *AD程序
  3. *编写:李平
  4. *版本号:V1.00
  5. *日期:2011-7-16
  6. *文件名:Ad.c
  7. */
  8. #include "Ad.h"
  9. #include "flash.h"
  10. #include "usart.h"
  11. #include "buffer.h"
  12. #include "weight.h"
  13. #define ZERO 500000
  14. #define SPANZERO ZERO
  15. #define TRN_INI 2
  16. extern unsigned char usart1_send_data[64];
  17. extern void USART_Send_DMA(unsigned char j,int n);
  18. extern unsigned char WeightConst_ChanCheShu;
  19. ulng keyword=0;
  20. uchar stas_s0=0;//状态标志变量 0:称重状态 1:工厂标定和工厂设置状态 2:用户设置状态
  21. uchar fs[16]= {0}; //工厂参数
  22. uchar ZeroPass=0;
  23. uchar IsRealZero=0;
  24. uchar IsZeroFlag=0;
  25. uchar num_stbn=0;
  26. uchar n_stbn=0;
  27. uchar num_trn=0;
  28. uchar stbn=0;
  29. uchar IsStb=0;
  30. uchar Isol=0;
  31. uchar ad_nag_flag=0;
  32. uchar nag=0;
  33. uchar IsTare=0;
  34. ulng ad_lcd=0;
  35. ulng ad_lcd1=0;
  36. ulng ad_read=0;
  37. ulng ad_start=0;
  38. ulng ad_sample=0;
  39. ulng ad_zero=0;
  40. ulng real_zero=0;
  41. ulng ad_last=0;
  42. ulng ad_tare=0;
  43. ulng Maxval=0;
  44. uchar Inteval=0;
  45. uchar point1=0;
  46. uchar point2=0;
  47. float spanz=0;
  48. ulng Low_w=0;
  49. ulng Up_w=0;
  50. ulng err_cnt=0;
  51. ulng ok_cnt=0;
  52. uchar STB_N=2;
  53. uchar start_loop;
  54. ulng AD=0;
  55. /*******************************************/
  56. float Avad[6]= {0};
  57. float Bvad[6]= {0};
  58. float Cvad[6]= {0};
  59. float Dvad[6]= {0};
  60. float Evad[6]= {0};
  61. float Fvad[6]= {0};
  62. float Gvad[6]= {0};
  63. float Hvad[6]= {0};
  64. float Ivad[6]= {0};
  65. float Jvad[6]= {0};
  66. float AD_result=0;
  67. uchar ad_ini_flag=0;
  68. uchar ad_ini_flag1=0;
  69. float Ad_tol=0;
  70. uchar Ad_cnt=0;
  71. uchar Ad_I=0;
  72. uchar AVG_CNT=4;
  73. uchar f_zeroset=1;
  74. uchar FLT_CNT=16;
  75. uchar FLT_XF_CNT=1;
  76. ulng FilterArray[128];
  77. uchar FilterPos=0;
  78. uchar dat[32]= {0};
  79. ulng Kalman_FilterArray[128];
  80. uchar Kalman_ini_flag=0;
  81. uchar Kalman_FilterPos=0;
  82. uchar key_CNT_f=0;
  83. float percent_fl=1.000f;
  84. ulng percent_ul=999;
  85. extern uchar ini_flag1;
  86. uchar Rsbuffer[7]= {0}; //RS232使用
  87. uchar RsWeight[]=
  88. {
  89. 0x30,0x31,0x32,0x33,0x34,0x35,
  90. // "0" "1" "2" "3" "4" "5"
  91. 0x36,0x37,0x38,0x39
  92. // "6" "7" "8" "9"
  93. };
  94. float x1=0,x2=0,x0=0,y0=0,y1=0,y2=0;
  95. unsigned char buf[4] = {0,0,0,0};
  96. unsigned char isIRQ;
  97. ulng ad_sample_temp;
  98. extern void Delay(__IO uint32_t nCount);
  99. #define _nop_() Delay(1)
  100. ulng get_weight(void)
  101. {
  102. ulng tempval=0;
  103. ulng tempvalw=0;
  104. isIRQ=0;
  105. tempval = Ad_Sample1();
  106. if (tempval<0xFFFFFFFF)
  107. {
  108. ad_sample_temp = tempval;
  109. }
  110. // if (ad_sample_temp>0 && ad_sample_temp<0xFFFFF && isIRQ==0)
  111. if (isIRQ==0 && tempval<0xFFFFFFFF)
  112. ad_sample=ad_sample_temp;
  113. // ad_sample_=ad_sample;
  114. // ad_sample=(int)((float)ad_sample * 0.01 + (1.0 - 0.01) * (float)ad_sample);
  115. // ad_sample = AdFilter_xf(ad_sample); //限幅滤波 FLT_XF_CNT=32;
  116. ad_sample = middleFilter(ad_sample);
  117. tempvalw = ad_sample;
  118. switch(fs[1])
  119. {
  120. case 1:
  121. ad_sample = Kalman_Filter(ad_sample, 0.5, 0.5);
  122. break;
  123. case 2:
  124. ad_sample = Kalman_Filter(ad_sample, 0.1, 0.5);
  125. break;
  126. case 3:
  127. ad_sample = Kalman_Filter(ad_sample, 0.05, 0.5);
  128. break;
  129. case 4:
  130. ad_sample = Kalman_Filter(ad_sample, 0.01, 0.5);
  131. break;
  132. case 5:
  133. ad_sample = Kalman_Filter(ad_sample, 0.1, 0.5);
  134. break;
  135. case 6:
  136. ad_sample = Kalman_Filter(ad_sample, 0.05, 0.5);
  137. break;
  138. case 7:
  139. ad_sample = Kalman_Filter(ad_sample, 0.1, 0.5);
  140. break;
  141. default:
  142. ad_sample = Kalman_Filter(ad_sample, 0.125, 0.5);
  143. break;
  144. }
  145. //ad_sample = AdFilter(ad_sample); //滑动滤波 FLT_CNT=32;
  146. if (tempWeighttime<100){
  147. tempWeighttime ++;
  148. ad_sample = tempvalw;
  149. }
  150. ad_read = adcalculate(ad_sample);
  151. Common(); //称量状态
  152. // RS232weight(ad_lcd1);
  153. return ad_lcd1;
  154. }
  155. //---------------------------------
  156. //Function that writes to the AD7190 via the SPI port.
  157. //--------------------------------------------------------------------------------
  158. void WriteToAD7190(unsigned char count, unsigned char *buf)
  159. {
  160. unsigned char ValueToWrite = 0;
  161. unsigned char i = 0;
  162. unsigned char j = 0;
  163. // __disable_irq();
  164. AD_CLK_1;
  165. _nop_();
  166. for(i=count; i>0; i--)
  167. {
  168. ValueToWrite = *(buf + i - 1);
  169. for(j=0; j<8; j++)
  170. {
  171. AD_CLK_0;
  172. if(0x80 == (ValueToWrite & 0x80))
  173. {
  174. AD_DIN_1; //Send one to SDO pin
  175. }
  176. else
  177. {
  178. AD_DIN_0; //Send zero to SDO pin
  179. }
  180. _nop_();
  181. AD_CLK_1;
  182. _nop_();
  183. ValueToWrite <<= 1; //Rotate data
  184. }
  185. }
  186. AD_DIN_0;
  187. // __enable_irq();
  188. }
  189. //---------------------------------
  190. //Function that reads from the AD7190 via the SPI port.
  191. //--------------------------------------------------------------------------------
  192. void ReadFromAD7190(unsigned char count, unsigned char *buf)
  193. {
  194. unsigned char i = 0;
  195. unsigned char j = 0;
  196. unsigned int iTemp = 0;
  197. unsigned char RotateData = 0;
  198. // __disable_irq();
  199. AD_CLK_1;
  200. _nop_();
  201. for(j=count; j>0; j--)
  202. {
  203. for(i=0; i<8; i++)
  204. {
  205. AD_CLK_0;
  206. RotateData <<= 1; //Rotate data
  207. _nop_();
  208. iTemp = AD_R_DI; //Read SDI of AD7190
  209. AD_CLK_1;
  210. if(0x00000001 == (iTemp & 0x00000001))
  211. {
  212. RotateData |= 1;
  213. }
  214. _nop_();
  215. }
  216. *(buf + j - 1)= RotateData;
  217. }
  218. // __enable_irq();
  219. }
  220. // extern void led_change(void);
  221. /***********************************
  222. *读AD7190函数
  223. */
  224. ulng Ad_Sample1(void)
  225. {
  226. uchar i;
  227. ulng j=0;
  228. ulng AdSample=0XFFFFFFFF;
  229. // __disable_irq();
  230. AD_CLK_1;
  231. _nop_();
  232. // _nop_();
  233. while(AD_R_DI==1) {
  234. j++;
  235. if (j>1000) break;
  236. }
  237. if (j<1000)
  238. {
  239. AdSample=0;
  240. for(i=0; i<24; i++)
  241. {
  242. AD_CLK_0;
  243. // _nop_();
  244. _nop_();
  245. AdSample<<=1;
  246. if(AD_R_DI==1)
  247. AdSample++;
  248. AD_CLK_1;
  249. _nop_();
  250. // _nop_();
  251. }
  252. /*
  253. if ((AdSample&0x00800000)!=0)
  254. {
  255. AdSample=~AdSample+1;
  256. AdSample&=0x007fffff;
  257. AdSample=0x007fffff-AdSample;
  258. }
  259. else
  260. {
  261. AdSample&=0x007fffff;
  262. AdSample=0x007fffff+AdSample;
  263. }
  264. */
  265. if (fs[1]<=4 && fs[1]>0 )
  266. AdSample = AdSample>>3;
  267. else if (fs[1]>4 && fs[1]<=6 )
  268. AdSample = AdSample>>2;
  269. else
  270. AdSample = AdSample;
  271. // led_change();
  272. }
  273. //__enable_irq();
  274. return(AdSample);
  275. }
  276. /*********************************************
  277. *AD7190上电初始化处理
  278. */
  279. void adFs(unsigned char fs_)
  280. {
  281. Delay(100);
  282. AD_CS_0;
  283. AD_SYNC_1;
  284. Delay(100);
  285. //Reset AD7190
  286. buf[2]= 0xff;
  287. buf[1]= 0xff;
  288. buf[0]= 0xff;
  289. WriteToAD7190(3,buf);
  290. buf[1]= 0xff;
  291. buf[0]= 0xff;
  292. WriteToAD7190(2,buf);
  293. Delay(200);
  294. /***********************************
  295. 通信寄存器
  296. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  297. 0 0 0 0 1 0 0 0
  298. */
  299. /***********************************
  300. 模式寄存器
  301. MD2|MD1|MD0|DAT_STA|CLK1|CLK0| 0 | 0
  302. 0 0 0 0 0 0 0 0
  303. SINC3| 0 |ENPAR| 0 |SINGLE|REJ60|FS9|FS8
  304. 0 0 0 0 0 1 0 0
  305. FS7|FS6|FS5|FS4|FS3|FS2|FS1|FS0
  306. 0 0 0 0 0 1 0 1
  307. */
  308. //mode register
  309. buf[0] = 0x08;
  310. WriteToAD7190(1,buf);
  311. buf[2] = 0x00;
  312. buf[1] = 0x04;
  313. // buf[0] = 0x50;//xxx Hz
  314. if (fs_==0)
  315. buf[0] = 80;//xxx Hz
  316. else
  317. /*if (fs_==1)
  318. {
  319. buf[1] = buf[1]|((480>>8)&3);//xxx Hz
  320. buf[0] = 480&0xff;//xxx Hz
  321. }
  322. else if (fs_==2)
  323. {
  324. buf[1] = buf[1]|((480>>8)&3);//xxx Hz
  325. buf[0] = 480&0xff;//xxx Hz
  326. }
  327. else if (fs_==3)
  328. {
  329. buf[1] = buf[1]|((640>>8)&3);//xxx Hz
  330. buf[0] = 640&0xff;//xxx Hz
  331. }
  332. else if (fs_==4)
  333. {
  334. buf[1] = buf[1]|((640>>8)&3);//xxx Hz
  335. buf[0] = 640&0xff;//xxx Hz
  336. }
  337. else if (fs_==5)
  338. {
  339. buf[1] = buf[1]|((1023>>8)&3);//xxx Hz
  340. buf[0] = 1023&0xff;//xxx Hz
  341. }
  342. else if (fs_==6)
  343. {
  344. buf[1] = buf[1]|((1023>>8)&3);//xxx Hz
  345. buf[0] = 1023&0xff;//xxx Hz
  346. }
  347. else if (fs_==7)
  348. */
  349. {
  350. buf[1] = buf[1]|((120>>8)&3);//xxx Hz
  351. buf[0] = 120&0xff;//xxx Hz
  352. }
  353. /*
  354. else
  355. {
  356. buf[0] = 80;//xxx Hz
  357. }
  358. */
  359. WriteToAD7190(3,buf);
  360. /***********************************
  361. 通信寄存器
  362. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  363. 0 0 0 1 0 0 0 0
  364. */
  365. /*********************************
  366. 配置寄存器
  367. CHOP| 0 | 0 |REFSEL| 0 | 0 | 0| 0
  368. 0 0 0 0 0 0 0 0
  369. CH7|CH6|CH5|CH4|CH3|CH2|CH1|CH0
  370. 0 0 0 0 0 0 1 0
  371. BURN|REFDET| 0 |BUF|U/B|G2|G1|G0
  372. 0 0 0 1 0 1 1 1
  373. */
  374. //Configuration register
  375. buf[0] = 0x10;
  376. WriteToAD7190(1,buf);
  377. buf[2] = 0x80; //buf[2] = 0x80;
  378. buf[1] = 0x02;
  379. buf[0] = 0x17;
  380. WriteToAD7190(3,buf);
  381. /***********************************
  382. 通信寄存器
  383. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  384. 0 0 1 0 1 0 0 0
  385. */
  386. /***********************************
  387. GPOCON寄存器
  388. 0 |BPDSW|GP32EN|GPIOEN|P3DAT|P2DAT|P1DAT|P0DAT
  389. 0 1 0 0 0 0 0 0
  390. */
  391. //GPOCON register
  392. buf[0] = 0x28;
  393. WriteToAD7190(1,buf);
  394. buf[0] = 0x40;
  395. WriteToAD7190(1,buf);
  396. /***********************************
  397. 通信寄存器
  398. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  399. 0 1 0 1 1 1 0 0
  400. */
  401. //通信寄存器,连续读数据寄存器
  402. buf[0] = 0x5c;
  403. WriteToAD7190(1,buf);
  404. Delay(5);
  405. }
  406. /*********************************************
  407. *AD7190上电初始化处理
  408. */
  409. void adini(void)
  410. {
  411. Delay(100);
  412. AD_CS_0;
  413. AD_SYNC_1;
  414. Delay(100);
  415. //Reset AD7190
  416. buf[2]= 0xff;
  417. buf[1]= 0xff;
  418. buf[0]= 0xff;
  419. WriteToAD7190(3,buf);
  420. buf[1]= 0xff;
  421. buf[0]= 0xff;
  422. WriteToAD7190(2,buf);
  423. Delay(200);
  424. /***********************************
  425. 通信寄存器
  426. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  427. 0 0 0 0 1 0 0 0
  428. */
  429. /***********************************
  430. 模式寄存器
  431. MD2|MD1|MD0|DAT_STA|CLK1|CLK0| 0 | 0
  432. 0 0 0 0 0 0 0 0
  433. SINC3| 0 |ENPAR| 0 |SINGLE|REJ60|FS9|FS8
  434. 0 0 0 0 0 1 0 0
  435. FS7|FS6|FS5|FS4|FS3|FS2|FS1|FS0
  436. 0 0 0 0 0 1 0 1
  437. */
  438. //mode register
  439. buf[0] = 0x08;
  440. WriteToAD7190(1,buf);
  441. buf[2] = 0x00;
  442. buf[1] = 0x04;
  443. buf[0] = 0x50;//xxx Hz
  444. WriteToAD7190(3,buf);
  445. /***********************************
  446. 通信寄存器
  447. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  448. 0 0 0 1 0 0 0 0
  449. */
  450. /*********************************
  451. 配置寄存器
  452. CHOP| 0 | 0 |REFSEL| 0 | 0 | 0| 0
  453. 0 0 0 0 0 0 0 0
  454. CH7|CH6|CH5|CH4|CH3|CH2|CH1|CH0
  455. 0 0 0 0 0 0 1 0
  456. BURN|REFDET| 0 |BUF|U/B|G2|G1|G0
  457. 0 0 0 1 0 1 1 1
  458. */
  459. //Configuration register
  460. buf[0] = 0x10;
  461. WriteToAD7190(1,buf);
  462. buf[2] = 0x00;
  463. buf[1] = 0x02;
  464. buf[0] = 0x17;
  465. WriteToAD7190(3,buf);
  466. /***********************************
  467. 通信寄存器
  468. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  469. 0 0 1 0 1 0 0 0
  470. */
  471. /***********************************
  472. GPOCON寄存器
  473. 0 |BPDSW|GP32EN|GPIOEN|P3DAT|P2DAT|P1DAT|P0DAT
  474. 0 1 0 0 0 0 0 0
  475. */
  476. //GPOCON register
  477. buf[0] = 0x28;
  478. WriteToAD7190(1,buf);
  479. buf[0] = 0x40;
  480. WriteToAD7190(1,buf);
  481. /***********************************
  482. 通信寄存器
  483. WEN|R/W|RS2|RS1|RS0|CREAD| 0 | 0
  484. 0 1 0 1 1 1 0 0
  485. */
  486. //通信寄存器,连续读数据寄存器
  487. buf[0] = 0x5c;
  488. WriteToAD7190(1,buf);
  489. Delay(5);
  490. }
  491. /************************
  492. *AD采样速度选择函数
  493. */
  494. void Speed(void)
  495. {
  496. switch(fs[1])
  497. {
  498. case 0:
  499. ad_ini_flag=0;
  500. ad_ini_flag1=0;
  501. FLT_CNT=4; //滑动滤波
  502. AVG_CNT=4; //平均滤波
  503. break;
  504. case 1:
  505. ad_ini_flag1=0;
  506. FLT_CNT=24;
  507. AVG_CNT=6;
  508. break;
  509. case 2:
  510. ad_ini_flag1=0;
  511. FLT_CNT=24;
  512. AVG_CNT=6;
  513. break;
  514. case 3:
  515. ad_ini_flag1=0;
  516. FLT_CNT=24;
  517. AVG_CNT=6;
  518. break;
  519. case 4:
  520. ad_ini_flag1=0;
  521. FLT_CNT=24;
  522. AVG_CNT=6;
  523. break;
  524. case 5:
  525. ad_ini_flag1=0;
  526. FLT_CNT=24;
  527. AVG_CNT=6;
  528. break;
  529. case 6:
  530. ad_ini_flag1=0;
  531. FLT_CNT=24;
  532. AVG_CNT=6;
  533. break;
  534. case 7:
  535. ad_ini_flag1=0;
  536. FLT_CNT=24;
  537. AVG_CNT=6;
  538. break;
  539. default:
  540. ad_ini_flag1=0;
  541. FLT_CNT=32;
  542. AVG_CNT=4;
  543. break;
  544. }
  545. }
  546. /****限幅滤波**********************/
  547. uchar count_xf;
  548. ulng AdFilter_xf_value;
  549. uchar ad_ini_flag_xf=0;
  550. ulng AdFilter_xf(ulng x) {
  551. uint32_t FILTER_A = 0x7FFF; //限幅100公斤
  552. int32_t FILTER_1 ;
  553. if(ad_ini_flag_xf==0)
  554. {
  555. ad_ini_flag_xf=1;
  556. count_xf=0;
  557. AdFilter_xf_value=x;
  558. return x;
  559. }
  560. FILTER_1 = x - AdFilter_xf_value;
  561. if (FILTER_1<0)
  562. FILTER_1 = FILTER_1 * -1;
  563. if(FILTER_1 > FILTER_A && count_xf<=FLT_XF_CNT)
  564. {
  565. count_xf++;
  566. }
  567. else
  568. {
  569. count_xf=0;
  570. AdFilter_xf_value = x;
  571. }
  572. return AdFilter_xf_value;
  573. }
  574. /***kalman滤波*************/
  575. /*
  576. Q:过程噪声,Q增大,动态响应变快,收敛稳定性变坏
  577. R:测量噪声,R增大,动态响应变慢,收敛稳定性变好
  578. */
  579. ulng Kalman_Filter(const double ResrcData, double ProcessNiose_Q,double MeasureNoise_R)
  580. {
  581. int32_t ad_sum = 0;
  582. ulng ad_avg = 0;
  583. double R = MeasureNoise_R;
  584. double Q = ProcessNiose_Q;
  585. static double x_last;
  586. static double p_last;
  587. double x_mid;
  588. double x_now;
  589. double p_mid ;
  590. double p_now;
  591. double kg;
  592. uchar i;
  593. uchar x__=3;
  594. uchar y__=1<<x__;
  595. if(Kalman_ini_flag==0)
  596. {
  597. for(i=0; i<y__; i++)
  598. Kalman_FilterArray[i]=0;
  599. Kalman_ini_flag=1;
  600. }
  601. Kalman_FilterPos++;
  602. if (Kalman_FilterPos>=y__)
  603. Kalman_FilterPos=0;
  604. Kalman_FilterArray[Kalman_FilterPos]=ResrcData;
  605. ad_sum = 0;
  606. for (i=0; i<y__; i++)
  607. ad_sum+=Kalman_FilterArray[i];
  608. ad_avg = ad_sum>>x__;
  609. ad_sum = 0;
  610. // for (i=0; i<y__; i++)
  611. // ad_sum+=(int)(Kalman_FilterArray[i]-ad_avg)*(int)(Kalman_FilterArray[i]-ad_avg);
  612. //
  613. // R = (ad_sum>>(x__+2));
  614. for (i=0; i<y__; i++)
  615. ad_sum+=(Kalman_FilterArray[i]-ad_avg);
  616. if (ad_sum <0) ad_sum=ad_sum*-1;
  617. // R = 1<<ad_sum;
  618. x_mid=x_last; //x_last=x(k-1|k-1),x_mid=x(k|k-1)
  619. p_mid=p_last+Q; //p_mid=p(k|k-1),p_last=p(k-1|k-1),Q=噪声
  620. kg=p_mid/(p_mid+R); //kg为kalman filter,R为噪声
  621. x_now=x_mid+kg*(ResrcData-x_mid); //估计出的最优值
  622. p_now=(1-kg)*p_mid; //最优值对应的covariance
  623. p_last = p_now; //更新covariance值
  624. x_last = x_now; //更新系统状态值
  625. return x_now;
  626. }
  627. ulng middleFilterdata[128];
  628. ulng middleFilter(ulng x)
  629. {
  630. ulng i;
  631. ulng j;
  632. ulng temp;
  633. for (j = 0; j < 31-1; j++)
  634. middleFilterdata[j] = middleFilterdata[j+1];
  635. middleFilterdata[31] = x;
  636. for (j = 0; j < 31-1; j++)
  637. {
  638. for (i = 0; i < 31-j; i++)
  639. {
  640. if (middleFilterdata[i] > middleFilterdata[i+1])
  641. {
  642. temp = middleFilterdata[i];
  643. middleFilterdata[i] = middleFilterdata[i+1];
  644. middleFilterdata[i+1] = temp;
  645. }
  646. }
  647. }
  648. return middleFilterdata[floor((FLT_CNT-1)/2)];
  649. }
  650. /****************************/
  651. ulng AdFilter(ulng x)
  652. {
  653. ulng ad_sum = 0;
  654. uchar i;
  655. if(ad_ini_flag1==0)
  656. {
  657. for(i=0; i<FLT_CNT; i++)
  658. {
  659. FilterArray[i]=x;
  660. }
  661. ad_ini_flag1=1;
  662. return x;
  663. }
  664. FilterPos++;
  665. if (FilterPos>=FLT_CNT)
  666. {
  667. FilterPos=0;
  668. }
  669. FilterArray[FilterPos]=x;
  670. for (i=0; i<FLT_CNT; i++)
  671. {
  672. ad_sum+=FilterArray[i];
  673. }
  674. return(ad_sum/(FLT_CNT));
  675. }
  676. /***************************
  677. *标定系数保存
  678. *地址:33--52
  679. */
  680. void ArgSave(void)
  681. {
  682. ad_Maxval = Maxval;
  683. ad_Inteval = Inteval;
  684. ad_Point = point1;
  685. ad_Spanz = spanz;
  686. ad_Zero = ad_zero;
  687. write_Flash("ad_Maxval", &ad_Maxval, sizeof(ad_Maxval));
  688. write_Flash("ad_Inteval", &ad_Inteval, sizeof(ad_Inteval));
  689. write_Flash("ad_Point", &ad_Point, sizeof(ad_Point));
  690. write_Flash("ad_Spanz", &ad_Spanz, sizeof(ad_Spanz));
  691. write_Flash("ad_Zero", &ad_Zero, sizeof(ad_Zero));
  692. W_fs();
  693. if((stas_s0==0)||(stas_s0==1))
  694. Zero_Save();
  695. }
  696. /***************************
  697. *标定系数读出
  698. *地址:32--52
  699. */
  700. void ArgRead(void)
  701. {
  702. Maxval = ad_Maxval;
  703. Inteval = ad_Inteval;
  704. point1 = ad_Point;
  705. spanz = ad_Spanz;
  706. ad_zero = ad_Zero;
  707. point2=0;
  708. adini();
  709. R_fs();
  710. Zero_Read();
  711. Speed();
  712. adFs(fs[1]);
  713. }
  714. /**********************************
  715. *写工厂设置参数
  716. *固定地址:1-16
  717. */
  718. void W_fs(void)
  719. {
  720. ad_FS = fs[1];
  721. write_Flash("ad_FS", &ad_FS, sizeof(ad_FS));
  722. }
  723. /***************************
  724. *读工厂设置参数
  725. *固定地址:1-16
  726. */
  727. void R_fs(void)
  728. {
  729. fs[1] = ad_FS;
  730. }
  731. /************************************
  732. *标定参数默认值
  733. */
  734. void ArgIni(void)
  735. {
  736. fs[1]=0;
  737. Maxval=3000;
  738. spanz=0.5;
  739. Inteval=1;
  740. point1=3;
  741. point2=0;
  742. ad_zero=ZERO;
  743. }
  744. /************************************
  745. *零点参数保存
  746. *地址 53--56
  747. */
  748. void Zero_Save(void)
  749. {
  750. ad_Tare = ad_tare;
  751. write_Flash("ad_Tare", &ad_Tare, sizeof(ad_Tare));
  752. }
  753. /************************************
  754. *零点参数读出
  755. *地址 53--56
  756. */
  757. void Zero_Read(void)
  758. {
  759. ad_tare = ad_Tare;
  760. }
  761. /*************************************
  762. Description: 转换十进制至bcd准备显示(重量)
  763. Argument: none
  764. Return: none
  765. *************************************/
  766. void DisplayWeight_ww(__IO ulng dis_data)
  767. {
  768. uchar digit;
  769. ulng divider=100000; //除数
  770. uchar quotient=0; //商
  771. if(dis_data>999999)
  772. {
  773. dis_data=0; //超过显示最大值送零
  774. }
  775. for(digit=0; digit<6; digit++)
  776. {
  777. quotient=0;
  778. while(dis_data>(divider-1))
  779. {
  780. dis_data-=divider;
  781. quotient++;
  782. }
  783. divider/=10;
  784. Rsbuffer[digit]=RsWeight[quotient];
  785. }
  786. }
  787. void RS232weight(__IO ulng x)
  788. {
  789. DisplayWeight_ww(x);
  790. //2D 33 36 35 34 0D
  791. //02 2B 30 30 30 30 30 30 33 03
  792. // 30 30 2E 30 30 30 0D 0A
  793. if (WeightConst_ChanCheShu==9)
  794. {
  795. usart1_send_data[0] = 0x04;
  796. usart1_send_data[1] = (ad_sample_temp>>24)&0xFF;
  797. usart1_send_data[2] = (ad_sample_temp>>16)&0xFF;
  798. usart1_send_data[3] = (ad_sample_temp>>8)&0xFF;
  799. usart1_send_data[4] = ad_sample_temp&0xFF;
  800. }
  801. else
  802. {
  803. usart1_send_data[0] = 0x06;
  804. if (nag)
  805. {
  806. usart1_send_data[1] = 0x2D;
  807. for(int i=2; i<6; i++) usart1_send_data[i] = Rsbuffer[i];
  808. }
  809. else
  810. for(int i=1; i<6; i++) usart1_send_data[i] = Rsbuffer[i];
  811. }
  812. usart1_send_data[6] = 0x0D;
  813. rt_sem_take(uart1_lock, RT_WAITING_FOREVER);
  814. HAL_UART_Transmit_DMA(&huart1, usart1_send_data, usart1_send_data[0]);
  815. rt_sem_release(uart1_lock);
  816. }
  817. /*********************************
  818. *小数点位 1-5
  819. */
  820. void setPoint(__IO uchar val_)
  821. {
  822. point1 = val_;
  823. ArgSave();
  824. }
  825. /*********************************
  826. *分度值 1、2、5、10
  827. */
  828. void setInteval(__IO uchar val_)
  829. {
  830. Inteval=val_;
  831. ArgSave();
  832. }
  833. /*********************************
  834. *滤波级别 1-8
  835. */
  836. void setFS(__IO uchar val_)
  837. {
  838. fs[1] = val_;
  839. adFs(fs[1]);
  840. Speed();
  841. ArgSave();
  842. }
  843. /*********************************
  844. *校正百分比
  845. */
  846. void setPercent(__IO int val_)
  847. {
  848. percent_fl=(float)val_/1000;
  849. spanz = spanz + spanz * percent_fl; //保存新的校正
  850. ArgSave();
  851. }
  852. /*********************************
  853. *最大称量
  854. */
  855. void setMaxval(__IO ulng val_)
  856. {
  857. Maxval = val_;
  858. ArgSave();
  859. }
  860. /*********************************
  861. *0点校正
  862. */
  863. void setZeroCal(void)
  864. {
  865. if ((ad_sample>0)&&(ad_sample<50000000))
  866. {
  867. zerocalculate(); //0点计算
  868. ArgSave();
  869. }
  870. }
  871. /*********************************
  872. *砝码校正
  873. */
  874. void setFullCal(__IO ulng val_)
  875. {
  876. keyword = val_;
  877. if ((ad_sample-ad_zero)>=(keyword/Inteval*10)&&(ad_sample>ad_zero)&&(keyword!=0))
  878. {
  879. fullcalculate(); //砝码计算
  880. ArgSave();
  881. }
  882. }
  883. /*********************************
  884. *置零
  885. */
  886. void setZero(void)
  887. {
  888. Zero_Pro();
  889. }
  890. /*********************************
  891. *称量计算显示函数
  892. */
  893. void Common(void)
  894. {
  895. if(f_zeroset==0)
  896. {
  897. CommonModeIni();
  898. }
  899. CheckZero(ad_read);
  900. Do();
  901. Dis_Weight();
  902. }
  903. /*********************************************
  904. *零点检测
  905. */
  906. void CheckZero(__IO ulng check_data)
  907. {
  908. ulng zero_range;
  909. zero_range=24;
  910. if(check_data>real_zero)
  911. {
  912. check_data-=real_zero;
  913. }
  914. else
  915. {
  916. check_data=real_zero-check_data;
  917. }
  918. IsRealZero=(check_data<zero_range);
  919. IsZeroFlag=IsRealZero;
  920. }
  921. /*********************************************
  922. *初始零点建立
  923. */
  924. void CommonModeIni(void)
  925. {
  926. IsStb=0;
  927. while(!IsStb)
  928. {
  929. ad_sample=Ad_Sample1();
  930. ad_sample=AdFilter(ad_sample);
  931. ad_sample=EX_QR_Filter(ad_sample);
  932. ad_read=adcalculate(ad_sample);
  933. Check_Wen1();
  934. ad_last=ad_read;
  935. }
  936. Zero_Read();
  937. ad_read=ad_tare;
  938. ad_lcd=ad_read;
  939. ad_start=ad_read;
  940. ad_last=ad_read;
  941. real_zero=ad_read;
  942. f_zeroset=1;
  943. }
  944. /*********************************************
  945. *开机稳定判别
  946. */
  947. void Check_Wen1(void)
  948. {
  949. ulng check_Wdata1;
  950. ulng check_Wdata2;
  951. ulng stb_w;
  952. stb_w=50;
  953. check_Wdata1 = ad_last - stb_w;
  954. check_Wdata2 = ad_last + stb_w;
  955. if(((ad_read>check_Wdata1)&&(ad_read<check_Wdata2) ))
  956. {
  957. if(n_stbn>=20)
  958. {
  959. n_stbn=0;
  960. IsStb=1;
  961. }
  962. else
  963. {
  964. n_stbn++;
  965. }
  966. }
  967. else
  968. {
  969. n_stbn=0;
  970. }
  971. }
  972. /*********************************************
  973. *稳定判别
  974. */
  975. void Check_Wen(void)
  976. {
  977. ulng check_Wdata1;
  978. ulng check_Wdata2;
  979. ulng stb_w;
  980. stb_w=5;
  981. STB_N=5;
  982. check_Wdata1 = ad_last - stb_w;
  983. check_Wdata2 = ad_last + stb_w;
  984. if((ad_read>check_Wdata1)&&(ad_read<check_Wdata2))
  985. {
  986. if(n_stbn>=STB_N)
  987. {
  988. n_stbn=0;
  989. IsStb=1;
  990. }
  991. else
  992. {
  993. n_stbn++;
  994. }
  995. }
  996. else
  997. {
  998. n_stbn=0;
  999. }
  1000. }
  1001. /*********************************************
  1002. *不稳定判别
  1003. */
  1004. void Check_Not_Wen(void)
  1005. {
  1006. ulng check_data1;
  1007. ulng check_data2;
  1008. ulng nstb_w;
  1009. nstb_w=10;
  1010. check_data1=ad_lcd-nstb_w;
  1011. check_data2=ad_lcd+nstb_w;
  1012. if((ad_read<check_data1)||(ad_read>check_data2))
  1013. {
  1014. IsStb=0;
  1015. }
  1016. }
  1017. /*********************************************
  1018. *开机零点检测
  1019. *Return: 0 开机零点超出范围
  1020. * 1 开机零点在范围内
  1021. */
  1022. uchar Check_Start(void)
  1023. {
  1024. ulng check1=0;
  1025. ulng check2=0;
  1026. check1=SPANZERO-(Maxval/(ulng)Inteval*2);//20%
  1027. check2=SPANZERO+(Maxval/(ulng)Inteval*2);
  1028. if((ad_read<check1)||(ad_read>check2))
  1029. {
  1030. return(0);
  1031. }
  1032. else
  1033. {
  1034. return(1);
  1035. }
  1036. }
  1037. /*********************************************
  1038. *零点跟踪
  1039. */
  1040. void Zero_Trace(void)
  1041. {
  1042. ulng check_data1;
  1043. ulng check_data2;
  1044. ulng ZeroTrace_w;
  1045. uchar trn_cnt;
  1046. ZeroTrace_w = 5;
  1047. check_data1=ad_start-Maxval/((ulng)Inteval*2)/10;//600;
  1048. check_data2=ad_start+Maxval/((ulng)Inteval*2)/10;//600;
  1049. if((ad_read<check_data1)||(ad_read>check_data2))
  1050. {
  1051. num_trn=0;
  1052. return;
  1053. }
  1054. check_data1=real_zero-1;
  1055. check_data2=real_zero+1;
  1056. if((ad_read>=check_data1)&&(ad_read<=check_data2))
  1057. {
  1058. num_trn=0;
  1059. return;
  1060. }
  1061. check_data1=real_zero-ZeroTrace_w;
  1062. check_data2=real_zero+ZeroTrace_w;
  1063. if((ad_read<=check_data1)||(ad_read>=check_data2))
  1064. {
  1065. num_trn=0;
  1066. return;
  1067. }
  1068. trn_cnt=5;
  1069. if(num_trn>=trn_cnt)
  1070. {
  1071. if (ad_read>=real_zero)
  1072. {
  1073. real_zero++;
  1074. ad_tare++;
  1075. }
  1076. else
  1077. {
  1078. real_zero--;
  1079. ad_tare--;
  1080. }
  1081. num_trn=0;
  1082. }
  1083. else
  1084. {
  1085. num_trn++;
  1086. }
  1087. }
  1088. /*********************************************
  1089. *四舍五入计算
  1090. */
  1091. ulng RoundOff(__IO ulng data1,__IO ulng data2)
  1092. {
  1093. ulng result;
  1094. ulng remainder;
  1095. result=data1/data2;
  1096. remainder=data1%data2 ;
  1097. if ((remainder*2)>=data2)
  1098. {
  1099. result++;
  1100. }
  1101. return (result);
  1102. }
  1103. /*********************************************
  1104. *外码计算
  1105. */
  1106. void Dis_Weight(void)
  1107. {
  1108. ulng Ovcheck;
  1109. ad_dis_calcu();
  1110. AD=ad_lcd1;//保存ad码
  1111. if(ad_lcd>=real_zero)
  1112. {
  1113. Ovcheck=ad_lcd-real_zero;
  1114. }
  1115. else
  1116. {
  1117. Ovcheck=real_zero-ad_lcd;
  1118. }
  1119. if(Ovcheck<=((Maxval/Inteval)*10+90))
  1120. {
  1121. Isol=0;
  1122. }
  1123. else
  1124. {
  1125. Isol=1;
  1126. }
  1127. ad_lcd1=RoundOff(ad_lcd1,10);
  1128. ad_lcd1 = ad_lcd1 * Inteval;
  1129. if(ad_lcd1==0)
  1130. {
  1131. ad_nag_flag=0;
  1132. }
  1133. if (ad_lcd1<=Inteval*3)
  1134. {
  1135. nag=0;
  1136. }
  1137. if (ad_lcd1<=20*Inteval)
  1138. {
  1139. ZeroPass=0;
  1140. }
  1141. }
  1142. /***************************************
  1143. *皮重计算
  1144. */
  1145. void ad_dis_calcu(void)
  1146. {
  1147. if(ad_lcd>=ad_tare)
  1148. {
  1149. ad_nag_flag=0;
  1150. nag=0;
  1151. ad_lcd1=ad_lcd-ad_tare;
  1152. }
  1153. else
  1154. {
  1155. ad_nag_flag=1;
  1156. nag=1;
  1157. ad_lcd1=ad_tare-ad_lcd;
  1158. }
  1159. if(ad_tare>=real_zero)
  1160. {
  1161. if((ad_tare-real_zero)<5)
  1162. {
  1163. IsTare=0;
  1164. }
  1165. else
  1166. {
  1167. IsTare=1;
  1168. }
  1169. }
  1170. else
  1171. {
  1172. if((real_zero-ad_tare)<5)
  1173. {
  1174. IsTare=0;
  1175. }
  1176. else
  1177. {
  1178. IsTare=1;
  1179. }
  1180. }
  1181. }
  1182. /*********************************************
  1183. Description:
  1184. Argument :
  1185. Return :
  1186. **********************************************/
  1187. void Do()
  1188. {
  1189. if(IsStb)
  1190. {
  1191. Check_Not_Wen();
  1192. Zero_Trace();
  1193. ad_lcd = ad_read;
  1194. }
  1195. else
  1196. {
  1197. Check_Wen();
  1198. ad_lcd = ad_read;
  1199. }
  1200. CheckZero(ad_read);
  1201. ad_last=ad_read;
  1202. }
  1203. /*********************************************/
  1204. /******************************
  1205. *置零处理
  1206. */
  1207. void Zero_Pro(void)
  1208. {
  1209. //ulng Start_w;
  1210. if(stas_s0==0)
  1211. {
  1212. //Start_w=((Maxval/(ulng)Inteval)*5)/10;//5%
  1213. //if ((IsStb)&&(ad_lcd<=(ad_start+Start_w))&&(ad_lcd>=(ad_start-Start_w))&&(!IsTare))
  1214. if ((IsStb)&&(!IsTare))
  1215. // if (IsStb)
  1216. {
  1217. ad_tare=ad_tare+(ad_lcd-real_zero);
  1218. real_zero=ad_lcd;
  1219. Zero_Save();
  1220. }
  1221. return;
  1222. }
  1223. }
  1224. /***************************
  1225. *最大去皮量计算
  1226. */
  1227. ulng maxtare(void)
  1228. {
  1229. return(Maxval-Inteval);
  1230. }
  1231. /***************************
  1232. *去皮处理
  1233. */
  1234. void Tare_Pro(void)
  1235. {
  1236. if ((IsStb)&&(stas_s0==0))
  1237. {
  1238. if ((ad_lcd>=real_zero) && ((ad_lcd-real_zero)<=((maxtare()/Inteval)*10)))
  1239. {
  1240. ad_tare = ad_lcd;
  1241. }
  1242. }
  1243. if ((IsStb)&&(IsZeroFlag)&&(stas_s0==0))
  1244. {
  1245. ad_tare = ad_lcd;
  1246. real_zero = ad_lcd;
  1247. IsTare = 0;
  1248. }
  1249. if ((IsStb)&&(IsTare)&&(ad_lcd<real_zero)&&(stas_s0==0))
  1250. {
  1251. ad_tare = real_zero;
  1252. }
  1253. }
  1254. /*************************************************/
  1255. /******************************
  1256. *置零处理
  1257. */
  1258. void Zero_Pro1(void)
  1259. {
  1260. ulng Start_w;
  1261. if(stas_s0==0)
  1262. {
  1263. Start_w=((Maxval/(ulng)Inteval)*5)/10;//5%
  1264. if ((IsStb)&&(ad_lcd<=(ad_start+Start_w))&&(ad_lcd>=(ad_start-Start_w))&&(!IsTare))
  1265. {
  1266. ad_tare=ad_tare+(ad_lcd-real_zero);
  1267. real_zero=ad_lcd;
  1268. Zero_Save();
  1269. }
  1270. return;
  1271. }
  1272. }
  1273. /***********************************
  1274. *重量标定计算
  1275. */
  1276. void fullcalculate(void)
  1277. {
  1278. float SpanFull=0;
  1279. float spanz1=0;
  1280. SpanFull=((float)keyword/(float)Inteval*10);
  1281. spanz1=SpanFull;
  1282. spanz=(spanz1/((float)ad_sample-(float)ad_zero));
  1283. ad_read=adcalculate(ad_zero); //建立零点
  1284. ad_last=ad_read;
  1285. ad_lcd= ad_read;
  1286. ad_start=ad_read;
  1287. ad_tare=ad_read;
  1288. real_zero=ad_read;
  1289. f_zeroset=1;
  1290. }
  1291. /******************************************
  1292. *标定零位计算
  1293. */
  1294. void zerocalculate(void)
  1295. {
  1296. ad_zero=ad_sample;
  1297. }
  1298. /***********************************
  1299. *AD值计算
  1300. */
  1301. ulng adcalculate(__IO ulng ads)
  1302. {
  1303. float adcal=0;
  1304. adcal=(float)ads-((float)ad_zero-((float)ZERO/spanz)); // ZERO=500000; ad_zero=500000; spanz=1。2010-01-24
  1305. adcal=adcal*spanz;
  1306. return((ulng)(adcal+0.5));
  1307. }
  1308. /***************************************/
  1309. /*************************************
  1310. *滤波函数
  1311. *输入:采样值x
  1312. *输出:返回滤波后的结果值
  1313. */
  1314. ulng EX_QR_Filter(ulng x)
  1315. {
  1316. uchar i;
  1317. if(ad_ini_flag==0)
  1318. {
  1319. for(i=0; i<4; i++)
  1320. {
  1321. Avad[i]=x;
  1322. Bvad[i]=x;
  1323. Cvad[i]=x;
  1324. Dvad[i]=x;
  1325. Evad[i]=x;
  1326. Fvad[i]=x;
  1327. Gvad[i]=x;
  1328. Hvad[i]=x;
  1329. Ivad[i]=x;
  1330. Jvad[i]=x;
  1331. }
  1332. ad_ini_flag=1;
  1333. return(x);
  1334. }
  1335. else
  1336. {
  1337. Ad_tol+=x;
  1338. Ad_cnt++;
  1339. if(Ad_cnt>=AVG_CNT)
  1340. {
  1341. Avad[3]=Avad[2];
  1342. Avad[2]=Avad[1];
  1343. Avad[1]=Avad[0];
  1344. Avad[0]=Ad_tol/AVG_CNT;
  1345. Ad_cnt=0;
  1346. Ad_tol=0;
  1347. }
  1348. else
  1349. {
  1350. return((ulng)(AD_result+0.5f));
  1351. }
  1352. }
  1353. Bvad[3]=Bvad[2];
  1354. Bvad[2]=Bvad[1];
  1355. Bvad[1]=Bvad[0];
  1356. Bvad[0]=0.1f*Avad[0]+0.2f*Avad[1]+0.3f*Avad[2]+0.4f*Avad[3];
  1357. Cvad[3]=Cvad[2];
  1358. Cvad[2]=Cvad[1];
  1359. Cvad[1]=Cvad[0];
  1360. Cvad[0]=0.1f*Bvad[0]+0.2f*Bvad[1]+0.3f*Bvad[2]+0.4f*Bvad[3];
  1361. Dvad[3]=Dvad[2];
  1362. Dvad[2]=Dvad[1];
  1363. Dvad[1]=Dvad[0];
  1364. Dvad[0]=0.1f*Cvad[0]+0.2f*Cvad[1]+0.3f*Cvad[2]+0.4f*Cvad[3];
  1365. Evad[3]=Evad[2];
  1366. Evad[2]=Evad[1];
  1367. Evad[1]=Evad[0];
  1368. Evad[0]=0.1f*Dvad[0]+0.2f*Dvad[1]+0.3f*Dvad[2]+0.4f*Dvad[3];
  1369. Fvad[3]=Fvad[2];
  1370. Fvad[2]=Fvad[1];
  1371. Fvad[1]=Fvad[0];
  1372. Fvad[0]=0.1f*Evad[0]+0.2f*Evad[1]+0.3f*Evad[2]+0.4f*Evad[3];
  1373. if (fs[1]==0)
  1374. AD_result=Avad[0];
  1375. else if (fs[1]==1)
  1376. AD_result=Avad[0];
  1377. else if (fs[1]==2)
  1378. AD_result=Avad[0];
  1379. else if (fs[1]==3)
  1380. AD_result=Avad[0];
  1381. else if (fs[1]==4)
  1382. AD_result=Avad[0];
  1383. else if (fs[1]==5)
  1384. AD_result=Bvad[0];
  1385. else if (fs[1]==6)
  1386. AD_result=Cvad[0];
  1387. else
  1388. AD_result=Dvad[0];
  1389. return((ulng)(AD_result+0.5f));
  1390. }
  1391. /****************************************************
  1392. * 这是一个延迟函数
  1393. */
  1394. void Delay(__IO uint32_t nCount)
  1395. {
  1396. /* __IO 就是volatile, 加上这个后可以避免延迟函数被编译器优化掉 */
  1397. for(; nCount != 0; nCount--);
  1398. }
  1399. /********************************************************/