io_app.c 6.8 KB

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