Motor.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. #include "MOTOR.h"
  2. #include "delay.h"
  3. #include "DS18B20.h"
  4. #include "Instrument_EEPROM_Param.h"
  5. extern unsigned char Motor_Status;
  6. unsigned char g_ucMotorXFinishFlag,g_ucMotorYFinishFlag,g_ucMotorZFinishFlag,g_ucMotor4FinishFlag,g_ucMotorZ2FinishFlag ;
  7. unsigned int g_uiAccIndex ;
  8. unsigned long int g_uliRunningPulse ;
  9. unsigned long int g_uliPulseCount ;
  10. unsigned int g_uiReagentMotorPeriod[REAGENT_MOTOR_ACC],g_uiReagentMotorPeriod2[REAGENT_MOTOR_ACC2],g_uiReagentMotorPeriod3[REAGENT_MOTOR_ACC3],g_uiReagentMotorPeriod4[REAGENT_MOTOR_ACC4];
  11. unsigned long PulseNum;
  12. unsigned char Motor1_HOME_Flag,Motor2_HOME_Flag,Motor3_HOME_Flag,Motor4_HOME_Flag,Motor5_HOME_Flag;
  13. unsigned int Moving_Speed;
  14. unsigned int Reset_Speed;
  15. struct MotorParam g_tReagentMotorParam;
  16. struct MotorParamEEPROM Motor1;
  17. struct MotorParamEEPROM Motor2;
  18. struct MotorParamEEPROM Motor3;
  19. struct MotorParamEEPROM Motor4;
  20. //struct MotorParamEEPROM Moor5;
  21. void CalculateSModelLine( unsigned int * period, unsigned int len, unsigned int fre_max, unsigned int fre_min, float flexible)
  22. {
  23. int i,ttt;
  24. float deno ;
  25. float melo ;
  26. float fre;
  27. float Fdelt = fre_max-fre_min;
  28. if(len>ACCLTH) len=ACCLTH;
  29. for(i=0; i<len; i++)
  30. {
  31. melo = flexible * (2.0*i/len-1) ;
  32. deno = 1.0 / (1 + expf(-melo));
  33. fre = Fdelt * deno + fre_min;
  34. ttt=period[i] = (unsigned short)(625000.0/fre);
  35. }
  36. }
  37. void InitMotor1PWM(void)
  38. {
  39. PMCONbits.PTBEEN=0;
  40. _RP25R = 0x12;
  41. OC1CON = 0;
  42. OC1CON = 0;
  43. OC1CONbits.OCM = 0;
  44. OC1CONbits.OCTSEL = 0x00;
  45. OC1R = 312;
  46. OC1RS =625;
  47. T2CON = 0;
  48. T2CONbits.TCKPS = 2;
  49. PR2 = 625;
  50. OC1CONbits.OCM = 4;
  51. T2CONbits.TON = 0;
  52. IEC0bits.T2IE = 0;
  53. IFS0bits.T2IF = 0;
  54. IPC0bits.OC1IP = 6;
  55. IEC0bits.OC1IE = 0;
  56. IFS0bits.OC1IF = 0;
  57. }
  58. void InitMotor2PWM(void)
  59. {
  60. PMCONbits.PTBEEN=0;
  61. _RP4R = 0x13;
  62. OC2CON=0;
  63. OC2CON = 0;
  64. OC2CONbits.OCM = 0;
  65. OC2CONbits.OCTSEL = 0x00 ;
  66. OC2R = 312;
  67. OC2RS = 625;
  68. T2CON = 0;
  69. T2CONbits.TCKPS = 2;
  70. PR2 = 625;
  71. OC2CONbits.OCM = 4;
  72. T2CONbits.TON = 0;
  73. IEC0bits.T2IE = 0;
  74. IFS0bits.T2IF = 0;
  75. IPC1bits.OC2IP = 6;
  76. IEC0bits.OC2IE = 0;
  77. IFS0bits.OC2IF = 0;
  78. }
  79. void InitMotor3PWM(void)
  80. {
  81. PMCONbits.PTBEEN=0;
  82. _RP5R = 0x14;
  83. OC3CON = 0;
  84. OC3CON = 0;
  85. OC3CONbits.OCM = 0;
  86. OC3CONbits.OCTSEL = 0x00;
  87. OC3R = 312;
  88. OC3RS = 625;
  89. T2CON = 0;
  90. T2CONbits.TCKPS = 2;
  91. PR2 = 625;
  92. OC3CONbits.OCM = 4;
  93. T2CONbits.TON = 0;
  94. IEC0bits.T2IE = 0;
  95. IFS0bits.T2IF = 0;
  96. IPC6bits.OC3IP = 6;
  97. IEC1bits.OC3IE = 0;
  98. IFS1bits.OC3IF = 0;
  99. }
  100. void InitMotor4PWM(void)
  101. {
  102. PMCONbits.PTBEEN=0;
  103. _RP13R = 0x15;
  104. OC4CON = 0;
  105. OC4CON = 0;
  106. OC4CONbits.OCM = 0;
  107. OC4CONbits.OCTSEL = 0x00;
  108. OC4R = 312;
  109. OC4RS = 625;
  110. T2CON = 0;
  111. T2CONbits.TCKPS = 2;
  112. PR2 = 625;
  113. OC4CONbits.OCM = 4;
  114. T2CONbits.TON = 0;
  115. IEC0bits.T2IE = 0;
  116. IFS0bits.T2IF = 0;
  117. IPC6bits.OC4IP = 6;
  118. IEC1bits.OC4IE = 0;
  119. IFS1bits.OC4IF = 0;
  120. }
  121. void SetMotor1(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  122. {
  123. unsigned int i;
  124. unsigned int Prd;
  125. Prd=625000/spd;
  126. *AccPLS = 0;
  127. for(i=0;i<REAGENT_MOTOR_ACC;i++)
  128. {
  129. if((Prd < g_uiReagentMotorPeriod[i])&&(Prd >= g_uiReagentMotorPeriod[i+1]))
  130. {
  131. *AccPLS=i+1;
  132. break;
  133. }
  134. else if(Prd == g_uiReagentMotorPeriod[i])
  135. {
  136. *AccPLS=i;
  137. break;
  138. }
  139. }
  140. if(Pulses >= *AccPLS + *AccPLS)
  141. {
  142. *constVPLS = Pulses- *AccPLS;
  143. }
  144. else if(Pulses < *AccPLS + *AccPLS)
  145. {
  146. *AccPLS = Pulses/2;
  147. *constVPLS = *AccPLS;
  148. }
  149. }
  150. void SetMotor2(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  151. {
  152. unsigned int i;
  153. unsigned int Prd;
  154. Prd=625000/spd;
  155. *AccPLS = 0;
  156. for(i=0;i<ACC_pulse2;i++)
  157. {
  158. if((Prd < g_uiReagentMotorPeriod2[i])&&(Prd >= g_uiReagentMotorPeriod2[i+1]))
  159. {
  160. *AccPLS=i+1;
  161. break;
  162. }
  163. else if(Prd == g_uiReagentMotorPeriod2[i])
  164. {
  165. *AccPLS=i;
  166. break;
  167. }
  168. }
  169. if(Pulses >= (*AccPLS + *AccPLS))
  170. {
  171. *constVPLS = Pulses- *AccPLS;
  172. }
  173. else if(Pulses < (*AccPLS + *AccPLS))
  174. {
  175. *AccPLS = Pulses/2;
  176. *constVPLS = *AccPLS;
  177. }
  178. }
  179. void SetMotor3(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  180. {
  181. unsigned int i;
  182. unsigned int Prd;
  183. Prd=625000/spd;
  184. *AccPLS = 0;
  185. if(spd>REAGENT_MOTOR_FREQMAX3||spd<REAGENT_MOTOR_FREQMIN3)
  186. {
  187. }
  188. else
  189. {
  190. for(i=0;i<REAGENT_MOTOR_ACC3;i++)
  191. {
  192. if((Prd < g_uiReagentMotorPeriod3[i])&&(Prd >= g_uiReagentMotorPeriod3[i+1]))
  193. {
  194. *AccPLS=i+1;
  195. break;
  196. }
  197. else if(Prd == g_uiReagentMotorPeriod3[i])
  198. {
  199. *AccPLS=i;
  200. break;
  201. }
  202. }
  203. if(Pulses >= (*AccPLS + *AccPLS))
  204. {
  205. *constVPLS = Pulses- *AccPLS;
  206. }
  207. else if(Pulses < (*AccPLS + *AccPLS))
  208. {
  209. *AccPLS = Pulses/2;
  210. *constVPLS = *AccPLS;
  211. }
  212. }
  213. }
  214. void SetMotor4(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  215. {
  216. unsigned int i;
  217. unsigned int Prd;
  218. Prd=625000/spd;
  219. *AccPLS = 0;
  220. if(spd>REAGENT_MOTOR_FREQMAX4||spd<REAGENT_MOTOR_FREQMIN4)
  221. {
  222. }
  223. else
  224. {
  225. for(i=0;i<REAGENT_MOTOR_ACC4;i++)
  226. {
  227. if((Prd < g_uiReagentMotorPeriod4[i])&&(Prd >= g_uiReagentMotorPeriod4[i+1]))
  228. {
  229. *AccPLS=i+1;
  230. break;
  231. }
  232. else if(Prd == g_uiReagentMotorPeriod4[i])
  233. {
  234. *AccPLS=i;
  235. break;
  236. }
  237. }
  238. if(Pulses >= (*AccPLS + *AccPLS))
  239. {
  240. *constVPLS = Pulses- *AccPLS;
  241. }
  242. else if(Pulses < (*AccPLS + *AccPLS))
  243. {
  244. *AccPLS = Pulses/2;
  245. *constVPLS = *AccPLS;
  246. }
  247. }
  248. }
  249. void Motor1Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  250. {
  251. unsigned int Freq;
  252. if(g_tReagentMotorParam.ulZiPosition>Z_limitation1||g_tReagentMotorParam.ulZiPosition>Z1_limitation1)
  253. {
  254. if(X_move_protection_flag==0)
  255. {
  256. Motor_Status=13;
  257. return;
  258. }
  259. }
  260. Motor1_HOME_Flag=1;
  261. Motor2_HOME_Flag=0;
  262. Motor3_HOME_Flag=0;
  263. Motor4_HOME_Flag=0;
  264. Motor5_HOME_Flag=0;
  265. OC1CON = 0;
  266. g_tReagentMotorParam.ucXDirection = MOTOR1_FORWARD;
  267. Delay1ms(2);
  268. TMR5=0;
  269. PulseNum=0;
  270. if(MOTOR1_HOME == 1)
  271. {
  272. MOTOR1_DIR = MOTOR1_FORWARD;
  273. Freq=5000000/LEAVspd;
  274. TMR5=0;
  275. PR5=Freq;
  276. IFS1bits.T5IF = 0;
  277. T5CONbits.TON=1;
  278. while(MOTOR1_HOME)
  279. {
  280. if(PulseNum > Motor1.Limit_Pulse*2){ Motor_Status=11;break;}
  281. }
  282. T5CONbits.TON=0;
  283. MOTOR1_PWM=0;
  284. }
  285. else
  286. {
  287. MOTOR1_DIR = MOTOR1_REVERSAL;
  288. Freq=5000000/MOVEspd;
  289. TMR5=0;
  290. PR5=Freq;
  291. IFS1bits.T5IF = 0;
  292. T5CONbits.TON=1;
  293. while(PulseNum < Motor1.Limit_Pulse*2)
  294. {
  295. if(MOTOR1_HOME)
  296. break;
  297. }
  298. if(PulseNum ==Motor1.Limit_Pulse*2||PulseNum >Motor1.Limit_Pulse*2) { Motor_Status=11;}
  299. T5CONbits.TON=0;
  300. Freq=5000000/LEAVspd;
  301. TMR5=0;
  302. PR5=Freq;
  303. IFS1bits.T5IF = 0;
  304. T5CONbits.TON=1;
  305. PulseNum=0;
  306. while(PulseNum < Motor1.Overshoot_Pulse*2);
  307. T5CONbits.TON=0;
  308. MOTOR1_DIR = MOTOR1_FORWARD;
  309. Freq=5000000/LEAVspd;
  310. TMR5=0;
  311. PR5=Freq;
  312. IFS1bits.T5IF = 0;
  313. T5CONbits.TON=1;
  314. PulseNum=0;
  315. while(MOTOR1_HOME)
  316. {
  317. if(PulseNum >= Motor1.Limit_Pulse*2) { Motor_Status=11;break;}
  318. }
  319. T5CONbits.TON=0;
  320. MOTOR1_PWM=0;
  321. }
  322. MOTOR1_DIR = MOTOR1_FORWARD;
  323. Freq=5000000/LEAVspd;
  324. TMR5=0;
  325. PR5=Freq;
  326. IFS1bits.T5IF = 0;
  327. T5CONbits.TON=1;
  328. PulseNum=0;
  329. while(!MOTOR1_HOME)
  330. {
  331. if(PulseNum >= pulseoffset*2 ) break;
  332. }
  333. T5CONbits.TON=0;
  334. MOTOR1_PWM=0;
  335. InitMotor1PWM();
  336. g_uliRunningPulse = 0;
  337. g_tReagentMotorParam.ulXiPosition =0;
  338. }
  339. void Motor2Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  340. {
  341. unsigned int Freq;
  342. Motor1_HOME_Flag=0;
  343. Motor2_HOME_Flag=1;
  344. Motor3_HOME_Flag=0;
  345. Motor4_HOME_Flag=0;
  346. Motor5_HOME_Flag=0;
  347. OC2CON = 0;
  348. g_tReagentMotorParam.ucZDirection = MOTOR2_FORWARD;
  349. Delay1ms(2);
  350. T5CONbits.TON=0;
  351. TMR5=0;
  352. PulseNum=0;
  353. if(MOTOR2_HOME == 1)
  354. {
  355. MOTOR2_DIR =MOTOR2_FORWARD ;
  356. Freq=5000000/LEAVspd;
  357. TMR5=0;
  358. PR5=Freq;
  359. IFS1bits.T5IF = 0;
  360. T5CONbits.TON=1;
  361. while(MOTOR2_HOME)
  362. {
  363. if(PulseNum > Motor2.Limit_Pulse*2) {Motor_Status=21;break;}
  364. }
  365. T5CONbits.TON=0;
  366. MOTOR2_PWM=0;
  367. }
  368. else
  369. {
  370. MOTOR2_DIR = MOTOR2_REVERSAL;
  371. Freq=5000000/MOVEspd;
  372. TMR5=0;
  373. PR5=Freq;
  374. IFS1bits.T5IF = 0;
  375. T5CONbits.TON=1;
  376. while(PulseNum < Motor2.Limit_Pulse*2)
  377. {
  378. if(MOTOR2_HOME)
  379. break;
  380. }
  381. if(PulseNum ==Motor2.Limit_Pulse*2||PulseNum >Motor2.Limit_Pulse*2) { Motor_Status=21;}
  382. T5CONbits.TON=0;
  383. Freq=5000000/LEAVspd;
  384. TMR5=0;
  385. PR5=Freq;
  386. IFS1bits.T5IF = 0;
  387. T5CONbits.TON=1;
  388. PulseNum=0;
  389. while(PulseNum < Motor2.Overshoot_Pulse*2);
  390. T5CONbits.TON=0;
  391. MOTOR2_DIR = MOTOR2_FORWARD;
  392. Freq=5000000/LEAVspd;
  393. TMR5=0;
  394. PR5=Freq;
  395. IFS1bits.T5IF = 0;
  396. T5CONbits.TON=1;
  397. PulseNum=0;
  398. while(MOTOR2_HOME)
  399. {
  400. if(PulseNum > Motor2.Limit_Pulse*2) {Motor_Status=21;break;}
  401. }
  402. T5CONbits.TON=0;
  403. MOTOR2_PWM=0;
  404. }
  405. MOTOR2_DIR = MOTOR2_FORWARD;
  406. Freq=5000000/LEAVspd;
  407. TMR5=0;
  408. PR5=Freq;
  409. IFS1bits.T5IF = 0;
  410. T5CONbits.TON=1;
  411. PulseNum=0;
  412. while(!MOTOR2_HOME)
  413. {
  414. if(PulseNum >= pulseoffset*2 ) break;
  415. }
  416. T5CONbits.TON=0;
  417. MOTOR2_PWM=0;
  418. InitMotor2PWM();
  419. g_uliRunningPulse = 0;
  420. g_tReagentMotorParam.ulZiPosition =0;
  421. }
  422. void Motor3Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  423. {
  424. unsigned int Freq;
  425. Motor1_HOME_Flag=0;
  426. Motor2_HOME_Flag=0;
  427. Motor3_HOME_Flag=1;
  428. Motor4_HOME_Flag=0;
  429. Motor5_HOME_Flag=0;
  430. OC3CON = 0;
  431. g_tReagentMotorParam.ucZ1Direction = MOTOR3_FORWARD;
  432. Delay1ms(2);
  433. T5CONbits.TON=0;
  434. TMR5=0;
  435. PulseNum=0;
  436. if(MOTOR3_HOME == 1)
  437. {
  438. MOTOR3_DIR = MOTOR3_FORWARD;
  439. Freq=5000000/LEAVspd;
  440. TMR5=0;
  441. PR5=Freq;
  442. IFS1bits.T5IF = 0;
  443. T5CONbits.TON=1;
  444. while(MOTOR3_HOME)
  445. {
  446. if(PulseNum > Motor3.Limit_Pulse*2) {Motor_Status=31;break;}
  447. }
  448. T5CONbits.TON=0;
  449. MOTOR3_PWM=0;
  450. }
  451. else
  452. {
  453. MOTOR3_DIR = MOTOR3_REVERSAL;
  454. Freq=5000000/MOVEspd;
  455. TMR5=0;
  456. PR5=Freq;
  457. IFS1bits.T5IF = 0;
  458. T5CONbits.TON=1;
  459. while(PulseNum < Motor3.Limit_Pulse*2)
  460. {
  461. if(MOTOR3_HOME)
  462. break;
  463. }
  464. if(PulseNum ==Motor3.Limit_Pulse*2||PulseNum >Motor3.Limit_Pulse*2) { Motor_Status=31;}
  465. T5CONbits.TON=0;
  466. Freq=5000000/LEAVspd;
  467. TMR5=0;
  468. PR5=Freq;
  469. IFS1bits.T5IF = 0;
  470. T5CONbits.TON=1;
  471. PulseNum=0;
  472. while(PulseNum < Motor3.Overshoot_Pulse*2);
  473. T5CONbits.TON=0;
  474. MOTOR3_DIR = MOTOR3_FORWARD;
  475. Freq=5000000/LEAVspd;
  476. TMR5=0;
  477. PR5=Freq;
  478. IFS1bits.T5IF = 0;
  479. T5CONbits.TON=1;
  480. PulseNum=0;
  481. while(MOTOR3_HOME)
  482. {
  483. if(PulseNum > Motor3.Limit_Pulse*2) {Motor_Status=31;break;}
  484. }
  485. T5CONbits.TON=0;
  486. MOTOR3_PWM=0;
  487. }
  488. MOTOR3_DIR = MOTOR3_FORWARD;
  489. Freq=5000000/LEAVspd;
  490. TMR5=0;
  491. PR5=Freq;
  492. IFS1bits.T5IF = 0;
  493. T5CONbits.TON=1;
  494. PulseNum=0;
  495. while(!MOTOR3_HOME)
  496. {
  497. if(PulseNum >= pulseoffset*2 ) break;
  498. }
  499. T5CONbits.TON=0;
  500. MOTOR3_PWM=0;
  501. InitMotor3PWM();
  502. g_uliRunningPulse = 0;
  503. g_tReagentMotorParam.ulZ1iPosition =0;
  504. }
  505. void Motor4Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  506. {
  507. unsigned int Freq;
  508. Motor1_HOME_Flag=0;
  509. Motor2_HOME_Flag=0;
  510. Motor3_HOME_Flag=0;
  511. Motor4_HOME_Flag=1;
  512. Motor5_HOME_Flag=0;
  513. OC4CON = 0;
  514. g_tReagentMotorParam.uc4Direction = MOTOR4_FORWARD;
  515. Delay1ms(2);
  516. T5CONbits.TON=0;
  517. TMR5=0;
  518. PulseNum=0;
  519. if(MOTOR4_HOME == 1)
  520. {
  521. MOTOR4_DIR = MOTOR4_FORWARD;
  522. Freq=5000000/LEAVspd;
  523. TMR5=0;
  524. PR5=Freq;
  525. IFS1bits.T5IF = 0;
  526. T5CONbits.TON=1;
  527. while(MOTOR4_HOME)
  528. {
  529. if(PulseNum > Motor4.Limit_Pulse*2) {Motor_Status=41;break;}
  530. }
  531. T5CONbits.TON=0;
  532. MOTOR4_PWM=0;
  533. }
  534. else
  535. {
  536. MOTOR4_DIR = MOTOR4_REVERSAL;
  537. Freq=5000000/MOVEspd;
  538. TMR5=0;
  539. PR5=Freq;
  540. IFS1bits.T5IF = 0;
  541. T5CONbits.TON=1;
  542. while(PulseNum < Motor4.Limit_Pulse*2)
  543. {
  544. if(MOTOR4_HOME)
  545. break;
  546. }
  547. if(PulseNum ==Motor4.Limit_Pulse*2||PulseNum >Motor4.Limit_Pulse*2) { Motor_Status=41;}
  548. T5CONbits.TON=0;
  549. Freq=5000000/LEAVspd;
  550. TMR5=0;
  551. PR5=Freq;
  552. IFS1bits.T5IF = 0;
  553. T5CONbits.TON=1;
  554. PulseNum=0;
  555. while(PulseNum < Motor4.Overshoot_Pulse*2);
  556. T5CONbits.TON=0;
  557. MOTOR4_DIR = MOTOR4_FORWARD;
  558. Freq=5000000/LEAVspd;
  559. TMR5=0;
  560. PR5=Freq;
  561. IFS1bits.T5IF = 0;
  562. T5CONbits.TON=1;
  563. PulseNum=0;
  564. while(MOTOR4_HOME)
  565. {
  566. if(PulseNum > Motor4.Limit_Pulse*2) {Motor_Status=41;break;}
  567. }
  568. T5CONbits.TON=0;
  569. MOTOR4_PWM=0;
  570. }
  571. MOTOR4_DIR = MOTOR4_FORWARD;
  572. Freq=5000000/LEAVspd;
  573. TMR5=0;
  574. PR5=Freq;
  575. IFS1bits.T5IF = 0;
  576. T5CONbits.TON=1;
  577. PulseNum=0;
  578. while(!MOTOR4_HOME)
  579. {
  580. if(PulseNum >= pulseoffset*2 ) break;
  581. }
  582. T5CONbits.TON=0;
  583. MOTOR4_PWM=0;
  584. InitMotor4PWM();
  585. g_uliRunningPulse = 0;
  586. g_tReagentMotorParam.ul4iPosition =0;
  587. }
  588. void Motor1MoveTo(unsigned long int pulse,unsigned int spd)
  589. {
  590. unsigned long int uliMotorPulse = 0;
  591. if(pulse > g_tReagentMotorParam.ulXiPosition)
  592. {
  593. uliMotorPulse = pulse - g_tReagentMotorParam.ulXiPosition;
  594. Motor1Move(uliMotorPulse,!Motor1.Reset_Direction,spd);
  595. }
  596. else if(pulse < g_tReagentMotorParam.ulXiPosition)
  597. {
  598. uliMotorPulse = g_tReagentMotorParam.ulXiPosition - pulse;
  599. Motor1Move(uliMotorPulse,Motor1.Reset_Direction,spd);
  600. }
  601. else
  602. {
  603. }
  604. }
  605. void Motor2MoveTo_ok(unsigned long int pulse,unsigned int spd)
  606. {
  607. if(Z_di_bu_position>21000)
  608. {
  609. pulse=pulse+(Z_di_bu_position-21000);
  610. }
  611. else
  612. {
  613. pulse=pulse-(21000-Z_di_bu_position);
  614. }
  615. Motor2MoveTo(pulse,spd);
  616. }
  617. void Motor2MoveTo(unsigned long int pulse,unsigned int spd)
  618. {
  619. unsigned long int uliMotorPulse = 0;
  620. if(pulse > g_tReagentMotorParam.ulZiPosition)
  621. {
  622. uliMotorPulse = pulse - g_tReagentMotorParam.ulZiPosition;
  623. Motor2Move(uliMotorPulse,!Motor2.Reset_Direction,spd);
  624. }
  625. else if(pulse < g_tReagentMotorParam.ulZiPosition)
  626. {
  627. uliMotorPulse = g_tReagentMotorParam.ulZiPosition - pulse;
  628. Motor2Move(uliMotorPulse,Motor2.Reset_Direction,spd);
  629. }
  630. else
  631. {
  632. }
  633. }
  634. void Motor3MoveTo(unsigned long int pulse,unsigned int spd)
  635. {
  636. unsigned long int uliMotorPulse = 0;
  637. if(pulse > g_tReagentMotorParam.ulZ1iPosition)
  638. {
  639. uliMotorPulse = pulse - g_tReagentMotorParam.ulZ1iPosition;
  640. Motor3Move(uliMotorPulse,!Motor3.Reset_Direction,spd);
  641. }
  642. else if(pulse < g_tReagentMotorParam.ulZ1iPosition)
  643. {
  644. uliMotorPulse = g_tReagentMotorParam.ulZ1iPosition - pulse;
  645. Motor3Move(uliMotorPulse,Motor3.Reset_Direction,spd);
  646. }
  647. else
  648. {
  649. }
  650. }
  651. void Motor4MoveTo(unsigned long int pulse,unsigned int spd)
  652. {
  653. unsigned long int uliMotorPulse = 0;
  654. if(pulse > g_tReagentMotorParam.ul4iPosition)
  655. {
  656. uliMotorPulse = pulse - g_tReagentMotorParam.ul4iPosition;
  657. Motor4Move(uliMotorPulse,!Motor4.Reset_Direction,spd);
  658. }
  659. else if(pulse < g_tReagentMotorParam.ul4iPosition)
  660. {
  661. uliMotorPulse = g_tReagentMotorParam.ul4iPosition - pulse;
  662. Motor4Move(uliMotorPulse,Motor4.Reset_Direction,spd);
  663. }
  664. else
  665. {
  666. }
  667. }
  668. /*******************************************************************/
  669. void Motor1Move(unsigned long int pulse,unsigned char dir,unsigned int spd)
  670. {
  671. if(g_tReagentMotorParam.ulZiPosition>Z_limitation1||g_tReagentMotorParam.ulZiPosition>Z1_limitation1)
  672. {
  673. if(X_move_protection_flag==0)
  674. {
  675. Motor_Status=13;
  676. return;
  677. }
  678. }
  679. InitMotor1PWM();
  680. if(pulse ==0) return;
  681. MOTOR1_DIR = dir;
  682. if(dir==MOTOR1_FORWARD)
  683. {
  684. if(( g_tReagentMotorParam.ulXiPosition+pulse)>Motor1.Limit_Pulse)
  685. {
  686. Motor_Status=12;
  687. return;
  688. }
  689. }
  690. if(dir==MOTOR1_REVERSAL)
  691. {
  692. if( g_tReagentMotorParam.ulXiPosition<pulse)
  693. {
  694. Motor_Status=12;
  695. return;
  696. }
  697. }
  698. g_uiAccIndex=0;
  699. g_uliRunningPulse=0;
  700. g_ucMotorXFinishFlag = 0;
  701. g_uliPulseCount = pulse;
  702. g_tReagentMotorParam.ucXDirection=dir?1:0;
  703. if(spd>REAGENT_MOTOR_FREQMIN)
  704. {
  705. if(spd>REAGENT_MOTOR_FREQMAX)
  706. {
  707. spd=REAGENT_MOTOR_FREQMAX;
  708. }
  709. SetMotor1(pulse,spd,&(g_tReagentMotorParam.ulXiAccPulse),&(g_tReagentMotorParam.ulXiConstPulse));
  710. g_tReagentMotorParam.uiXSpeed_switch=0;
  711. g_tReagentMotorParam.uiXSpeed=spd;
  712. }
  713. else
  714. {
  715. g_tReagentMotorParam.uiXSpeed_switch=1;
  716. if(spd<MOTOR_FREQLIMIT_L)
  717. {
  718. spd=MOTOR_FREQLIMIT_L;
  719. }
  720. if(spd>MOTOR_FREQLIMIT_H)
  721. {
  722. spd=MOTOR_FREQLIMIT_H;
  723. }
  724. g_tReagentMotorParam.uiXSpeed=spd;
  725. g_tReagentMotorParam.uiXprd=(unsigned short)(625000.0/ g_tReagentMotorParam.uiXSpeed);
  726. }
  727. IEC0bits.OC1IE = 1;
  728. T2CONbits.TON=1;
  729. while(!g_ucMotorXFinishFlag);
  730. OC1CON=0;
  731. }
  732. void Motor2Move(unsigned long int pulse,unsigned char dir,unsigned int spd)
  733. {
  734. InitMotor2PWM();
  735. if(pulse ==0) return;
  736. MOTOR2_DIR = dir;//r
  737. g_tReagentMotorParam.ucZDirection = dir?1:0;
  738. if(dir==MOTOR2_FORWARD)
  739. {
  740. if(( g_tReagentMotorParam.ulZiPosition+pulse)>Motor2.Limit_Pulse)
  741. if( g_tReagentMotorParam.ulXiPosition<pulse)
  742. {
  743. Motor_Status=22;
  744. return;
  745. }
  746. }
  747. if(dir==MOTOR2_REVERSAL)
  748. {
  749. if( g_tReagentMotorParam.ulZiPosition<pulse)
  750. if( g_tReagentMotorParam.ulXiPosition<pulse)
  751. {
  752. Motor_Status=22;
  753. return;
  754. }
  755. }
  756. g_uiAccIndex=0;
  757. g_uliRunningPulse=0;
  758. g_ucMotorZFinishFlag = 0;
  759. g_uliPulseCount = pulse;
  760. if(spd>REAGENT_MOTOR_FREQMIN2)
  761. {
  762. if(spd>Motor2.Limit_speed)
  763. {
  764. spd=Motor2.Limit_speed;
  765. }
  766. SetMotor2(pulse,spd,&(g_tReagentMotorParam.ulZiAccPulse),&(g_tReagentMotorParam.ulZiConstPulse));
  767. g_tReagentMotorParam.uiZSpeed_switch=0;
  768. g_tReagentMotorParam.uiZSpeed=spd;
  769. }
  770. else
  771. {
  772. g_tReagentMotorParam.uiZSpeed_switch=1;
  773. if(spd<MOTOR_FREQLIMIT_L)
  774. {
  775. spd=MOTOR_FREQLIMIT_L;
  776. }
  777. if(spd>MOTOR_FREQLIMIT_H)
  778. {
  779. spd=MOTOR_FREQLIMIT_H;
  780. }
  781. g_tReagentMotorParam.uiZSpeed=spd;
  782. g_tReagentMotorParam.uiZprd=(unsigned short)(625000.0/ g_tReagentMotorParam.uiZSpeed);
  783. }
  784. IEC0bits.OC2IE = 1;
  785. T2CONbits.TON=1;
  786. while(!g_ucMotorZFinishFlag);
  787. OC2CON=0;
  788. }
  789. void Motor3Move(unsigned long int pulse,unsigned char dir,unsigned int spd)//spd --HZ
  790. {
  791. InitMotor3PWM();
  792. if(pulse ==0) return;
  793. MOTOR3_DIR = dir;
  794. if(dir==MOTOR3_FORWARD)
  795. {
  796. if(( g_tReagentMotorParam.ulZ1iPosition+pulse)>Motor3.Limit_Pulse)
  797. {
  798. Motor_Status=32;
  799. return;
  800. }
  801. }
  802. if(dir==MOTOR3_REVERSAL)
  803. {
  804. if( g_tReagentMotorParam.ulZ1iPosition<pulse)
  805. {
  806. Motor_Status=32;
  807. return;
  808. }
  809. }
  810. g_tReagentMotorParam.ucZ1Direction = dir?1:0;
  811. g_uiAccIndex=0;
  812. g_uliRunningPulse=0;
  813. g_ucMotorZ1FinishFlag = 0;
  814. g_uliPulseCount = pulse;
  815. if(spd>REAGENT_MOTOR_FREQMIN3)
  816. {
  817. if(spd>REAGENT_MOTOR_FREQMAX3)
  818. {
  819. spd=REAGENT_MOTOR_FREQMAX3;
  820. }
  821. SetMotor3(pulse,spd,&(g_tReagentMotorParam.ulZ1iAccPulse),&(g_tReagentMotorParam.ulZ1iConstPulse));
  822. g_tReagentMotorParam.uiZ1Speed_switch=0;
  823. g_tReagentMotorParam.uiZ1Speed=spd;
  824. }
  825. else
  826. {
  827. g_tReagentMotorParam.uiZ1Speed_switch=1;
  828. if(spd<MOTOR_FREQLIMIT_L)
  829. {
  830. spd=MOTOR_FREQLIMIT_L;
  831. }
  832. if(spd>MOTOR_FREQLIMIT_H)
  833. {
  834. spd=MOTOR_FREQLIMIT_H;
  835. }
  836. g_tReagentMotorParam.uiZ1Speed=spd;
  837. g_tReagentMotorParam.uiZ1prd=(unsigned short)(625000.0/ g_tReagentMotorParam.uiZ1Speed);
  838. }
  839. IEC1bits.OC3IE = 1;
  840. T2CONbits.TON=1; //
  841. while(!g_ucMotorZ1FinishFlag);
  842. OC3CON=0;
  843. }
  844. void Motor4Move(unsigned long int pulse,unsigned char dir,unsigned int spd)//spd --HZ
  845. {
  846. InitMotor4PWM();
  847. if(pulse ==0) return;
  848. MOTOR4_DIR = dir;
  849. if(dir==MOTOR4_FORWARD)
  850. {
  851. if(( g_tReagentMotorParam.ul4iPosition+pulse)>Motor4.Limit_Pulse)
  852. {
  853. Motor_Status=42;
  854. return;
  855. }
  856. }
  857. if(dir==MOTOR4_REVERSAL)
  858. {
  859. if( g_tReagentMotorParam.ul4iPosition<pulse)
  860. {
  861. Motor_Status=42;
  862. return;
  863. }
  864. }
  865. g_tReagentMotorParam.uc4Direction = dir?1:0;
  866. g_uiAccIndex=0;
  867. g_uliRunningPulse=0;
  868. g_ucMotor4FinishFlag = 0;
  869. g_uliPulseCount = pulse;
  870. if(spd>REAGENT_MOTOR_FREQMIN4)
  871. {
  872. if(spd>REAGENT_MOTOR_FREQMAX4)
  873. {
  874. spd=REAGENT_MOTOR_FREQMAX4;
  875. }
  876. SetMotor4(pulse,spd,&(g_tReagentMotorParam.ul4iAccPulse),&(g_tReagentMotorParam.ul4iConstPulse));
  877. g_tReagentMotorParam.ui4Speed_switch=0;
  878. g_tReagentMotorParam.ui4Speed=spd;
  879. }
  880. else
  881. {
  882. g_tReagentMotorParam.ui4Speed_switch=1;
  883. if(spd<MOTOR_FREQLIMIT_L)
  884. {
  885. spd=MOTOR_FREQLIMIT_L;
  886. }
  887. if(spd>MOTOR_FREQLIMIT_H)
  888. {
  889. spd=MOTOR_FREQLIMIT_H;
  890. }
  891. g_tReagentMotorParam.ui4Speed=spd;
  892. g_tReagentMotorParam.ui4prd=(unsigned short)(625000.0/ g_tReagentMotorParam.ui4Speed);
  893. }
  894. IEC1bits.OC4IE = 1;
  895. T2CONbits.TON=1; //
  896. while(!g_ucMotor4FinishFlag);
  897. OC4CON=0;
  898. }
  899. void Move_to_by_volume(unsigned int volume,unsigned int spd)//spd --HZ
  900. {
  901. unsigned int pulse;
  902. if(volume<40)
  903. {
  904. pulse=(unsigned int)(Z_di_bu_position-(volume*12.5+0));
  905. }
  906. else if(volume<400)
  907. {
  908. pulse=(unsigned int)(Z_di_bu_position-(volume*10.05139057+103.5066505));
  909. }
  910. else if(volume<1100)
  911. {
  912. pulse=(unsigned int)(Z_di_bu_position-(volume*6.833333333+1366.666667));
  913. }
  914. else
  915. {
  916. return;
  917. }
  918. Motor2MoveTo( pulse, spd);
  919. }