Interrupt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #define __INTERRUPT_DEF
  2. #include "ABIS_User.h"
  3. unsigned char g_ucENABLE = 1;
  4. unsigned int uiData_num = 0,uiData_buffer = 0;
  5. #define MAX_OFFLINE_TIME 10
  6. unsigned int printtempFlag=0;
  7. /*******************************************************************************
  8. * 函数名称:Init_T3
  9. *******************************************************************************/
  10. //void Init_T3(){
  11. // TMR3 = 0;
  12. // PR3 = 9375;
  13. // T3CON = 0x8020; // T6=1:64
  14. //
  15. // IFS0bits.T3IF = 0;
  16. // IPC2bits.T3IP = 4;
  17. // IEC0bits.T3IE = 1;
  18. // T3CONbits.TON = 1;
  19. //
  20. //}
  21. //void __attribute__((__interrupt__,auto_psv)) _T3Interrupt (void)
  22. //{
  23. // if(DmaBuffer == 0)
  24. // {
  25. // BufferA.Adc1Ch0[0] = AD_Get(0x00);
  26. // BufferA.Adc1Ch1[0] = AD_Get(0x01);
  27. // BufferA.Adc1Ch2[0] = AD_Get(0x02);
  28. // BufferA.Adc1Ch3[0] = AD_Get(0x03);
  29. // BufferA.Adc1Ch4[0] = AD_Get(0x04);
  30. // BufferA.Adc1Ch5[0] = AD_Get(0x05);
  31. // BufferA.Adc1Ch6[0] = AD_Get(0x06);
  32. // BufferA.Adc1Ch7[0] = AD_Get(0x07);
  33. // BufferA.Adc1Ch8[0] = AD_Get(0x08);
  34. // }
  35. // else
  36. // {
  37. // BufferB.Adc1Ch0[0] = AD_Get(0x00);
  38. // BufferB.Adc1Ch1[0] = AD_Get(0x01);
  39. // BufferB.Adc1Ch2[0] = AD_Get(0x02);
  40. // BufferB.Adc1Ch3[0] = AD_Get(0x03);
  41. // BufferB.Adc1Ch4[0] = AD_Get(0x04);
  42. // BufferB.Adc1Ch5[0] = AD_Get(0x05);
  43. // BufferB.Adc1Ch6[0] = AD_Get(0x06);
  44. // BufferB.Adc1Ch7[0] = AD_Get(0x07);
  45. // BufferB.Adc1Ch8[0] = AD_Get(0x08);
  46. // }
  47. // DmaBuffer ^= 1;
  48. // IFS0bits.T3IF = 0;
  49. //}
  50. /*******************************************************************************
  51. * 函数名称:T1Interrupt
  52. *******************************************************************************/
  53. void __attribute__((__interrupt__,auto_psv)) _T2Interrupt (void)
  54. {
  55. IFS0bits.T2IF = 0;
  56. T6CONbits.TON = 0; //修改为0
  57. TMR2 = 0;
  58. g_tMotorParam.ulXiPosition =( g_tMotorParam.ucXDirection == FORWARD )? (g_tMotorParam.ulXiPosition + 1) : (g_tMotorParam.ulXiPosition - 1);
  59. if(MOTORX_DIR_scan == 1){
  60. g_uiScanPulseX++;//记录当前脉冲值
  61. }else if(MOTORX_DIR_scan == 0){
  62. g_uiScanPulseX--;//记录当前脉冲值
  63. }else{}
  64. if(Alldata == 1){
  65. if(ADC7606Enable==1) {
  66. if (g_ucFAMLED == 1) {
  67. if ((g_uiScanPulseX >= (g_uiScanFAM[0] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[0] + SUM))) {
  68. FAMLED = 1;
  69. FAMData[0][FAMData_num] = AD_Get(0x09);
  70. FAMData_num++;
  71. } else if ((g_uiScanPulseX >= (g_uiScanFAM[1] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[1] + SUM))) {
  72. FAMLED = 1;
  73. FAMData[1][FAMData_num] = AD_Get(0x09);
  74. FAMData_num++;
  75. } else if ((g_uiScanPulseX >= (g_uiScanFAM[2] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[2] + SUM))) {
  76. FAMLED = 1;
  77. FAMData[2][FAMData_num] = AD_Get(0x09);
  78. FAMData_num++;
  79. } else if ((g_uiScanPulseX >= (g_uiScanFAM[3] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[3] + SUM))) {
  80. FAMLED = 1;
  81. FAMData[3][FAMData_num] = AD_Get(0x09);
  82. FAMData_num++;
  83. } else if ((g_uiScanPulseX >= (g_uiScanFAM[4] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[4] + SUM))) {
  84. FAMLED = 1;
  85. FAMData[4][FAMData_num] = AD_Get(0x09);
  86. FAMData_num++;
  87. } else if ((g_uiScanPulseX >= (g_uiScanFAM[5] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[5] + SUM))) {
  88. FAMLED = 1;
  89. FAMData[5][FAMData_num] = AD_Get(0x09);
  90. FAMData_num++;
  91. } else if ((g_uiScanPulseX >= (g_uiScanFAM[6] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[6] + SUM))) {
  92. FAMLED = 1;
  93. FAMData[6][FAMData_num] = AD_Get(0x09);
  94. FAMData_num++;
  95. } else if ((g_uiScanPulseX >= (g_uiScanFAM[7] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[7] + SUM))) {
  96. FAMLED = 1;
  97. FAMData[7][FAMData_num] = AD_Get(0x09);
  98. FAMData_num++;
  99. } else if ((g_uiScanPulseX >= (g_uiScanFAM[8] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[8] + SUM))) {
  100. FAMLED = 1;
  101. FAMData[8][FAMData_num] = AD_Get(0x09);
  102. FAMData_num++;
  103. } else if ((g_uiScanPulseX >= (g_uiScanFAM[9] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[9] + SUM))) {
  104. FAMLED = 1;
  105. FAMData[9][FAMData_num] = AD_Get(0x09);
  106. FAMData_num++;
  107. } else if ((g_uiScanPulseX >= (g_uiScanFAM[10] - SUM)) &&(g_uiScanPulseX < (g_uiScanFAM[10] + SUM))) {
  108. FAMLED = 1;
  109. FAMData[10][FAMData_num] = AD_Get(0x09);
  110. FAMData_num++;
  111. } else if ((g_uiScanPulseX >= (g_uiScanFAM[11] - SUM)) &&(g_uiScanPulseX < (g_uiScanFAM[11] + SUM))) {
  112. FAMLED = 1;
  113. FAMData[11][FAMData_num] = AD_Get(0x09);
  114. FAMData_num++;
  115. } else if ((g_uiScanPulseX >= (g_uiScanFAM[12] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[12] + SUM))) {
  116. FAMLED = 1;
  117. FAMData[12][FAMData_num] = AD_Get(0x09);
  118. FAMData_num++;
  119. }else if ((g_uiScanPulseX >= (g_uiScanFAM[13] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[13] + SUM))) {
  120. FAMLED = 1;
  121. FAMData[13][FAMData_num] = AD_Get(0x09);
  122. FAMData_num++;
  123. }else if ((g_uiScanPulseX >= (g_uiScanFAM[14] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[14] + SUM))) {
  124. FAMLED = 1;
  125. FAMData[14][FAMData_num] = AD_Get(0x09);
  126. FAMData_num++;
  127. }else if ((g_uiScanPulseX >= (g_uiScanFAM[15] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[15] + SUM))) {
  128. FAMLED = 1;
  129. FAMData[15][FAMData_num] = AD_Get(0x09);
  130. FAMData_num++;
  131. }else if ((g_uiScanPulseX >= (g_uiScanFAM[16] - SUM))&&(g_uiScanPulseX < (g_uiScanFAM[16] + SUM))) {
  132. FAMLED = 1;
  133. FAMData[16][FAMData_num] = AD_Get(0x09);
  134. FAMData_num++;
  135. } else {
  136. FAMLED = 0;
  137. FAMData_num = 0;
  138. }
  139. }
  140. }
  141. }
  142. else if(Alldata == 0){//获取全数据用
  143. if(Motstep == 10){
  144. Motstep = 0;
  145. if(MPPCnum == 0)
  146. ucBuffer_Data[FAMData_num] = AD_Get(0x09);
  147. else if(MPPCnum == 1)
  148. ucBuffer_Data[FAMData_num] = AD_Get(0x0B);
  149. else if(MPPCnum == 2)
  150. ucBuffer_Data[FAMData_num] = AD_Get(0x0A);
  151. else if(MPPCnum == 3)
  152. ucBuffer_Data[FAMData_num] = AD_Get(0x0C);
  153. FAMData_num++;
  154. }
  155. Motstep++;
  156. }
  157. else{};
  158. PR2 = g_uiMotorXPeriod[g_uiAccIndex];
  159. OC1RS = g_uiMotorXPeriod[g_uiAccIndex]/2;
  160. OC1R = 0;
  161. g_uliRunningPulse++;
  162. if(g_uliRunningPulse <= g_tMotorParam.ulXiAccPulse){
  163. g_uiAccIndex++;
  164. }
  165. else if(g_uliRunningPulse < g_tMotorParam.ulXiConstPulse){ //匀速运动,频率不变
  166. //g_uliRunningPulse--;
  167. }
  168. else if(g_uliRunningPulse < g_uliPulseCount){ //减速运动,频率减小
  169. g_uiAccIndex--;
  170. }
  171. else if(g_uliRunningPulse >= g_uliPulseCount){ //停止PWM输出
  172. TMR2 = 0;
  173. T2CONbits.TON = 0; //off the T1
  174. g_ucMotorXFinishFlag=1;
  175. ADC7606Enable =0;
  176. }
  177. T6CONbits.TON = 1; //修改为0
  178. }
  179. /*******************************************************************************
  180. * 函数名称:Init_T6
  181. *******************************************************************************/
  182. void Init_T6()
  183. {
  184. TMR6 = 0;
  185. PR6 = 11719;
  186. T6CON = 0x8030;
  187. IFS2bits.T6IF = 0;
  188. IPC11bits.T6IP = 2;
  189. IEC2bits.T6IE = 1;
  190. T6CONbits.TON = 1;
  191. }
  192. #define NORMALFLASH 20
  193. #define NORMAL_X2_FLASH 40
  194. #define BLINK_TIME 8
  195. void __attribute__((interrupt,auto_psv)) _T6Interrupt (void)
  196. {
  197. IFS2bits.T6IF = 0;
  198. static unsigned char Timer = 0 ;
  199. static unsigned int ledtimer = 0 ;
  200. static unsigned int blinkTime = 0 ;
  201. static unsigned int MaxblinkTime = 0 ;
  202. Timer++;
  203. ledtimer++;
  204. if(Timer == 20){
  205. Timer = 0;
  206. if(com_timer < MAX_OFFLINE_TIME){
  207. com_timer++;
  208. }else{
  209. baseSys_st |= SYS_ST_OFFLINE;
  210. }
  211. // STU1 = !STU1;
  212. }
  213. if(baseSys_st & SYS_ST_OFFLINE){
  214. MaxblinkTime = 1;
  215. }else if(baseSys_st & SYS_ST_MOTORERROR){
  216. MaxblinkTime = 2;
  217. }else if(baseSys_st & SYS_ST_PIDEER){
  218. MaxblinkTime = 3;
  219. }
  220. if((baseSys_st & ~SYS_ST_OK )== 0)
  221. {
  222. if(ledtimer == NORMALFLASH){
  223. ledtimer = 0;
  224. STU1 = !STU1;
  225. }
  226. }else{
  227. if(ledtimer == NORMAL_X2_FLASH*4){
  228. ledtimer = 0;
  229. blinkTime = 0;
  230. }
  231. if(ledtimer == 0){
  232. STU1 = 0;
  233. }
  234. if(ledtimer == NORMAL_X2_FLASH){
  235. STU1 = 1;
  236. blinkTime = 0;
  237. }
  238. if(ledtimer>=NORMAL_X2_FLASH*2){
  239. if(((ledtimer-NORMAL_X2_FLASH*2)%BLINK_TIME == 0)&&blinkTime<MaxblinkTime){
  240. STU1 = 0;
  241. }
  242. if(((ledtimer-NORMAL_X2_FLASH*2)%BLINK_TIME == BLINK_TIME/2)){
  243. STU1 = 1;
  244. blinkTime++;
  245. }
  246. }
  247. }
  248. // 待测试标定对应顺序 测试OK
  249. MCUAdcBuff[0][SampleNo] = AD_Get(0x05); //BLOCK1
  250. MCUAdcBuff[1][SampleNo] = AD_Get(0x04); //BLOCK2
  251. MCUAdcBuff[2][SampleNo] = AD_Get(0x03); //BLOCK3
  252. MCUAdcBuff[3][SampleNo] = AD_Get(0x02); //BLOCK4
  253. MCUAdcBuff[4][SampleNo] = AD_Get(0x06); //BLOCK5制冷池
  254. MCUAdcBuff[5][SampleNo] = AD_Get(0x07); //BLOCK6制冷池
  255. MCUAdcBuff[6][SampleNo] = AD_Get(0x02); //热盖
  256. // MCUAdcBuff[7][SampleNo] = AD_Get(0x07); //
  257. MCUAdcBuff[8][SampleNo] = AD_Get(0x08); // 散热片
  258. SampleNo++;
  259. if(SampleNo > 2) { //100ms
  260. SampleNo=0;
  261. T4CONbits.TON=1; //off the T4 for 1ms
  262. printtempFlag = 1;
  263. }
  264. }
  265. /*******************************************************************************
  266. * 函数名称:Init_T4
  267. *******************************************************************************/
  268. void Init_T4()
  269. {
  270. TMR4 = 0;
  271. PR4 = 9375;
  272. T4CON = 0x8020; // T4=1:64
  273. IFS1bits.T4IF = 0;
  274. IPC6bits.T4IP = 2; //值越高优先级越高
  275. IEC1bits.T4IE = 1; //使能中断
  276. T4CONbits.TON = 0;
  277. }
  278. void __attribute__((interrupt,auto_psv)) _T4Interrupt (void)
  279. {
  280. unsigned char i;
  281. unsigned char j;
  282. long TempAD;
  283. // STU2 = ~STU2;
  284. IFS1bits.T4IF = 0;
  285. T4CONbits.TON = 0;//off the T4 for 1ms
  286. for(i=0;i<9;i++){
  287. TempAD=0;
  288. for(j=0;j<3;j++){
  289. TempAD += MCUAdcBuff[i][j];
  290. }
  291. dTemperature[i]=TempAD/3;
  292. dTemperatureAD[i] = (unsigned int)dTemperature[i];
  293. }
  294. for(i=0;i<6;i++){
  295. TempChn[i].fCurrentTemperature = (double)(TempPara[i].fK1 * dTemperature[i]*dTemperature[i] + TempPara[i].fK2 * dTemperature[i] + TempPara[i].fB);
  296. if(TempChn[i].fCurrentTemperature > 90) TempChn[i].fCurrentTemperature = (double)(TempChn[i].fCurrentTemperature + TempPara[i].TTb);
  297. }
  298. if(g_ucENABLE == 1){
  299. PeltierCtrler();
  300. }
  301. for(i=6;i<8;i++){
  302. CAPReal_temp[i-6] =(double)(CAPTk[i-6]*dTemperature[i] + CAPTb[i-6]);
  303. CAPReal_temp[i-6] =(double)(CAPTTk[i-6]*CAPReal_temp[i-6] + CAPTTb[i-6]);
  304. }
  305. if((CAPReal_temp[0] > (g_tEE.CAPTarget_temp[0]-3))&&(CAPReal_temp[0] < 121.0 )){//当前热盖温度在规定范围内时系统状态灯常亮
  306. g_ucSTATE_CAP = 1;
  307. }
  308. CAPPeltierCtrler();//热盖温控
  309. g_fFINReal_Temp = (float)(g_tEE.fFINTTk * (g_tEE.fFINTk*dTemperature[8] + g_tEE.fFINTb)+ g_tEE.fFINTTb);
  310. g_ucSampleNo_STU++;
  311. if((g_ucSampleNo_STU >= 3)&&(g_ucSTATE_RUN == 2)){//实验开始,状态灯间隔0.5s闪烁
  312. g_ucSampleNo_STU = 0;
  313. POWER_STU1 = ~POWER_STU1;
  314. FANOC_Set(3,g_tEE.uiFanHPwm);
  315. }
  316. if((g_ucSTATE_RUN == 1)&&(g_ucSTATE_CAP == 1)){//实验结束或空闲,状态灯常亮
  317. g_ucSampleNo_STU = 0;
  318. POWER_STU1 = 1;
  319. FANOC_Set(3,g_tEE.uiFanLPwm);
  320. }
  321. if(g_ucSTATE_CAP == 0){
  322. g_ucSampleNo_STU = 0;
  323. POWER_STU1 = 0;
  324. FANOC_Set(3,g_tEE.uiFanLPwm);
  325. }
  326. }
  327. /*******************************************************************************
  328. * 函数名称:Init_T8
  329. *******************************************************************************/
  330. void Init_T8()
  331. {
  332. TMR8 = 0;
  333. PR8 = 781;
  334. T8CON = 0x8010;
  335. IFS3bits.T8IF = 0;
  336. IPC12bits.T8IP = 7;
  337. IEC3bits.T8IE = 1;
  338. }
  339. void __attribute__((interrupt,auto_psv)) _T8Interrupt (void)
  340. {
  341. IFS3bits.T8IF = 0;
  342. if(MotorXHomebuffer == 1){
  343. MOTORX_PWM = !MOTORX_PWM;
  344. PulseNum++;
  345. }
  346. if(MotorYHomebuffer == 1){
  347. MOTORY_PWM = !MOTORY_PWM;
  348. PulseNum++;
  349. }
  350. }