io_app.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "global.h"
  2. #if (LOCAL_INPUT_BITS_NUMBER < IO_PIN_INPUT_NUMBER)
  3. #undef LOCAL_INPUT_BITS_NUMBER
  4. #define LOCAL_INPUT_BITS_NUMBER IO_PIN_INPUT_NUMBER
  5. #endif
  6. #if (LOCAL_OUTPUT_BITS_NUMBER < IO_PIN_OUTPUT_NUMBER)
  7. #undef LOCAL_OUTPUT_BITS_NUMBER
  8. #define LOCAL_OUTPUT_BITS_NUMBER IO_PIN_OUTPUT_NUMBER
  9. #endif
  10. #define IO_FILTER_H 10 //IO滤波高通计数
  11. #define IO_FILTER_L 2 //IO滤波低通计数
  12. #define IO_FILTER_JIFTTER 1000 //IO滤波间隔,单位是微秒
  13. int io_inputs_buffer[IO_PIN_INPUT_NUMBER];
  14. //#if USE_EXTEND_INOUTPUT
  15. byte_bits_t io_inputs[(LOCAL_INPUT_BITS_NUMBER + EXTEND_IO_INPUT_NUMBER + 7)>> 3];
  16. byte_bits_t io_inputs_old[(LOCAL_INPUT_BITS_NUMBER + EXTEND_IO_INPUT_NUMBER + 7)>> 3];
  17. byte_bits_t io_outputs[(LOCAL_OUTPUT_BITS_NUMBER + EXTEND_IO_OUTPUT_NUMBER + 7)>> 3];
  18. //#else
  19. //byte_bits_t io_inputs[(LOCAL_INPUT_BITS_NUMBER + 7)>> 3];
  20. //byte_bits_t io_inputs_old[(LOCAL_INPUT_BITS_NUMBER + 7)>> 3];
  21. //byte_bits_t io_outputs[(LOCAL_OUTPUT_BITS_NUMBER + 7)>> 3];
  22. //#endif
  23. static sw_timer_t filter_timer;
  24. /**
  25. * IO应用初始化函数
  26. *
  27. * @author lxz (2019/5/29/周三)
  28. */
  29. void io_app_init(void)
  30. {
  31. memset(io_inputs, 0, sizeof(io_inputs));
  32. memset(io_inputs_old, 0, sizeof(io_inputs_old));
  33. memset(io_outputs, 0, sizeof(io_outputs));
  34. sw_timer_start(&filter_timer, 0, 0);
  35. hw_io_output_disable();
  36. }
  37. /**
  38. * IO滤波输入
  39. *
  40. * @author lxz (2019/5/29/周三)
  41. */
  42. void io_app_read_input(void)
  43. {
  44. int index = 0;
  45. //读取输
  46. memcpy(io_inputs_old, io_inputs, sizeof(io_inputs));
  47. {
  48. //保存上一次的IO状态
  49. for (index = 0; index < IO_PIN_INPUT_NUMBER; index++)
  50. {
  51. // #if()
  52. if(1)//(index != 14) || (JIAO_YA_DIN_CUN_CONFIG == 0))
  53. {
  54. if (hw_io_pin_input(index))
  55. {
  56. if (io_inputs_buffer[index] < IO_FILTER_H)
  57. {
  58. io_inputs_buffer[index]++;
  59. }
  60. else
  61. {
  62. io_inputs[index >> 3].bytes[0] |= 1 << (index & 0x07);
  63. }
  64. }
  65. else if (io_inputs_buffer[index] > IO_FILTER_L)
  66. {
  67. io_inputs_buffer[index]--;
  68. }
  69. else
  70. {
  71. io_inputs[index >> 3].bytes[0] &= ~(1 << (index & 0x07));
  72. }
  73. }
  74. else
  75. {
  76. if (hw_io_pin_input(index))
  77. {
  78. if (io_inputs_buffer[index] < IO_FILTER_H)
  79. {
  80. io_inputs_buffer[index]++;
  81. }
  82. else
  83. {
  84. io_inputs[index >> 3].bytes[0] |= 1 << (index & 0x07);
  85. }
  86. }
  87. else if (io_inputs_buffer[index] > (IO_FILTER_L))
  88. {
  89. io_inputs_buffer[index]--;
  90. }
  91. else
  92. {
  93. io_inputs[index >> 3].bytes[0] &= ~(1 << (index & 0x07));
  94. }
  95. }
  96. }
  97. }
  98. //IO数据挂到通讯变量,其实这个也可以不这么干
  99. memcpy(&middle_coils[(160 + 7) >> 3], io_inputs, (IO_PIN_INPUT_NUMBER+7)>>3);
  100. }
  101. /**
  102. * 主动输出
  103. *
  104. * @author lxz (2019/5/29/周三)
  105. */
  106. void io_app_write_ouput(void)
  107. {
  108. int index = 0;
  109. for (index = 0; index < IO_PIN_OUTPUT_NUMBER; index++)
  110. {
  111. hw_io_pin_output(index, (io_outputs[index >> 3].bytes[0] & (1 << (index & 0x07))) != 0);
  112. }
  113. //IO数据挂到通讯变量,其实这个也可以不这么干
  114. memcpy(&middle_coils[(240 + 7) >> 3], io_outputs, (IO_PIN_OUTPUT_NUMBER+7)>>3);
  115. }
  116. void InitTestAction(void)
  117. {
  118. user_datas[256] = 0;
  119. }
  120. unsigned char JiaoYan(void)
  121. {
  122. unsigned short i;
  123. for(i=0;i<127;i++)
  124. {
  125. if(GetData32bits(i*2) != i)
  126. {
  127. user_datas[256] = 0;
  128. return 0;
  129. }
  130. }
  131. return 1;
  132. }
  133. /**
  134. * IO应用行函数执
  135. *
  136. * @author lxz (2019/5/29/周三)
  137. */
  138. void TestAction(void)
  139. {
  140. #if TESTACITON_CONFIG
  141. static unsigned long dwOutTime = 0,dwMotorTime;
  142. static unsigned char index = 0;
  143. unsigned short i;
  144. if(dwTickCount > dwOutTime)
  145. {
  146. dwOutTime = dwTickCount + 200;
  147. if(index++ > 25)index = 0;
  148. }
  149. if(dwTickCount >= dwMotorTime)
  150. {
  151. if(X_DRV)
  152. {
  153. AxisDecStop(X_AXIS);
  154. }
  155. else
  156. {
  157. if(GetDir(X_AXIS))
  158. {
  159. AxisContinueMove(X_AXIS,20,0);
  160. dwMotorTime = dwTickCount + 1000;
  161. }
  162. else
  163. {
  164. AxisContinueMove(X_AXIS,20,1);
  165. dwMotorTime = dwTickCount + 1000;
  166. }
  167. }
  168. }
  169. if((io_inputs[0].value == 0) && (io_inputs[1].value == 0) && (io_inputs[2].value == 0) && (io_inputs[3].value == 0) && (io_inputs[4].value == 0))
  170. {
  171. switch(index)
  172. {
  173. case 0:
  174. io_outputs[0].value = 0;
  175. io_outputs[1].value = 0;
  176. io_outputs[2].value = 0;
  177. io_outputs[3].value = 0;
  178. break;
  179. case 1:
  180. Y00 = 1;
  181. break;
  182. case 2:
  183. Y01 = 1;
  184. break;
  185. case 3:
  186. Y02 = 1;
  187. break;
  188. case 4:
  189. Y03 = 1;
  190. break;
  191. case 5:
  192. Y04 = 1;
  193. break;
  194. case 6:
  195. Y05 = 1;
  196. break;
  197. case 7:
  198. Y06 = 1;
  199. break;
  200. case 8:
  201. Y07 = 1;
  202. break;
  203. case 9:
  204. Y10 = 1;
  205. break;
  206. case 10:
  207. Y11 = 1;
  208. break;
  209. case 11:
  210. Y12 = 1;
  211. break;
  212. case 12:
  213. Y13 = 1;
  214. break;
  215. case 13:
  216. Y14 = 1;
  217. break;
  218. case 14:
  219. Y15 = 1;
  220. break;
  221. case 15:
  222. Y16 = 1;
  223. break;
  224. case 16:
  225. Y17 = 1;
  226. break;
  227. case 17:
  228. Y20 = 1;
  229. break;
  230. case 18:
  231. Y21 = 1;
  232. break;
  233. case 19:
  234. Y22 = 1;
  235. break;
  236. case 20:
  237. Y23 = 1;
  238. break;
  239. case 21:
  240. Y24 = 1;
  241. break;
  242. case 22:
  243. Y25 = 1;
  244. break;
  245. case 23:
  246. Y26 = 1;
  247. break;
  248. case 24:
  249. Y27 = 1;
  250. break;
  251. case 25:
  252. Y30 = 1;
  253. break;
  254. }
  255. }
  256. else
  257. {
  258. Y00 = X00 | X23;
  259. Y01 = X01;
  260. Y02 = X02;
  261. Y03 = X03;
  262. Y04 = X04;
  263. Y05 = X05;
  264. Y06 = X06;
  265. Y07 = X07;
  266. Y10 = X10;
  267. Y11 = X11;
  268. Y12 = X12;
  269. Y13 = X13;
  270. Y14 = X14;
  271. Y15 = X15;
  272. Y16 = X16;
  273. Y17 = X17;
  274. Y20 = X20;
  275. Y21 = X21;
  276. Y22 = X22;
  277. Y23 = X24 | X32;
  278. Y24 = X25 | X33;
  279. Y25 = X26;
  280. Y26 = X27;
  281. Y27 = X30;
  282. Y30 = X31;
  283. index = 30;
  284. }
  285. if(M0000)
  286. {
  287. M0000 = 0;
  288. for(i=0;i<127;i++)
  289. {
  290. SetData32bits(i*2,i);
  291. }
  292. user_datas[256] = 1;
  293. }
  294. if(M0001)
  295. {
  296. M0001 = 0;
  297. hw_flash_data_read(POWEROFF_BACKUP_ADDRESS, user_datas, POWEROFF_SAVE_BLOCK_SIZE);
  298. /* if(user_datas[256] != 1)
  299. {
  300. if(JiaoYan())
  301. {
  302. for(i=0;i<127;i++)
  303. {
  304. SetData32bits(i*2,0);
  305. }
  306. user_datas[256] = 2;
  307. }
  308. else
  309. {
  310. user_datas[256] = 0;
  311. }
  312. }*/
  313. }
  314. #endif
  315. }
  316. /**
  317. * IO应用行函数执
  318. *
  319. * @author lxz (2019/5/29/周三)
  320. */
  321. static sw_timer_t iotimer;
  322. #define TEST_APP
  323. void io_app_run(void) {
  324. int index = 0;
  325. #ifdef TEST_APP
  326. int flag = 0;
  327. static int status = 1;
  328. for (index = 0; index<(LOCAL_INPUT_BITS_NUMBER + 6 + 7)>> 3; index++)
  329. {
  330. io_outputs[index].bytes[0] = io_inputs[index].bytes[0];
  331. flag += io_inputs[index].bytes[0];
  332. }
  333. if(flag == 0)
  334. {
  335. if (sw_timer_expire(&iotimer)) {
  336. status <<= 1;
  337. sw_timer_start(&iotimer, 0, 500000);
  338. }
  339. if(status==0x2000000) status=1;
  340. io_outputs[0].bytes[0] = status;
  341. io_outputs[1].bytes[0] = status>>8;
  342. io_outputs[2].bytes[0] = status>>16;
  343. io_outputs[3].bytes[0] = status>>24;
  344. }
  345. else {
  346. sw_timer_start(&iotimer, 0, 500000);
  347. }
  348. #else
  349. for (index = 0; index<(LOCAL_OUTPUT_BITS_NUMBER + 7)>> 3; index++) {
  350. if (middle_coils[(200 >> 3) + index].bytes[0] != 0) {
  351. io_outputs[index].bytes[0] ^= middle_coils[(200 >> 3) + index].bytes[0];
  352. middle_coils[(200 >> 3) + index].bytes[0] = 0;
  353. }
  354. }
  355. #endif
  356. }