io_app.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "io_app.h"
  2. #include "board.h"
  3. //#include "user_app.h"
  4. #include "variable.h"
  5. #include <string.h>
  6. #define INPUT_BITS_NUMBER 12
  7. #define OUTPUT_BITS_NUMBER 12
  8. #if (INPUT_BITS_NUMBER < IO_PIN_INPUT_NUMBER)
  9. #undef INPUT_BITS_NUMBER
  10. #define INPUT_BITS_NUMBER IO_PIN_INPUT_NUMBER
  11. #endif
  12. #if (OUTPUT_BITS_NUMBER < IO_PIN_OUTPUT_NUMBER)
  13. #undef OUTPUT_BITS_NUMBER
  14. #define OUTPUT_BITS_NUMBER IO_PIN_OUTPUT_NUMBER
  15. #endif
  16. #define IO_FILTER_H 2 //IO皛斗郭擃㗛�朞恣�?
  17. #define IO_FILTER_L 0 //IO皛斗郭雿𡡞�朞恣�?
  18. #define IO_FILTER_JIFTTER 1000 //IO皛斗郭�湧�嚗��雿齿糓敺桃�
  19. int io_inputs_buffer[IO_PIN_INPUT_NUMBER];
  20. byte_bits_t io_inputs[(INPUT_BITS_NUMBER + 7)>> 3];
  21. byte_bits_t io_inputs_old[(INPUT_BITS_NUMBER + 7)>> 3];
  22. byte_bits_t io_outputs[(OUTPUT_BITS_NUMBER + 7)>> 3];
  23. byte_bits_t io_outputsen;
  24. static sw_timer_t filter_timer;
  25. /**
  26. * IO摨𠉛鍂�嘥��硋遆�?
  27. *
  28. * @author lxz (2019/5/29/�其�)
  29. */
  30. void io_app_init(void) {
  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_enable();
  36. }
  37. /**
  38. * IO皛斗郭颲枏�
  39. *
  40. * @author lxz (2019/5/29/�其�)
  41. */
  42. void io_app_read_input(void) {
  43. int index = 0;
  44. memcpy(io_inputs_old, io_inputs, sizeof(io_inputs));
  45. if (bSoftwareInputEnable) { //頧臭辣颲枏�嚗諹��箔縑�瑞眏M0160撘�憪𧢲㺭�格𤜯隞?
  46. //IO�唳旿����朞悖�㗛�嚗��摰噼�銝芯��臭誑銝滩�銋�僕
  47. memcpy(io_inputs, &middle_coils[160 >> 3], sizeof(io_inputs));
  48. }
  49. else {
  50. //霂餃�颲枏�
  51. if (sw_timer_expire(&filter_timer)) {
  52. sw_timer_start(&filter_timer, 0, IO_FILTER_JIFTTER);
  53. //靽嘥�銝𠹺�甈∠�IO�嗆�?
  54. for (index = 0; index < IO_PIN_INPUT_NUMBER; index++) {
  55. if (hw_io_pin_input(index)) {
  56. if (io_inputs_buffer[index] < IO_FILTER_H) {
  57. io_inputs_buffer[index]++;
  58. }
  59. else {
  60. io_inputs[index >> 3].value |= 1 << (index & 0x07);
  61. }
  62. }
  63. else if (io_inputs_buffer[index] > IO_FILTER_L) {
  64. io_inputs_buffer[index]--;
  65. }
  66. else {
  67. io_inputs[index >> 3].value &= ~(1 << (index & 0x07));
  68. }
  69. }
  70. }
  71. //IO�唳旿����朞悖�㗛�嚗��摰噼�銝芯��臭誑銝滩�銋�僕
  72. memcpy(&middle_coils[(160 + 7) >> 3], io_inputs, sizeof(io_inputs));
  73. }
  74. }
  75. /**
  76. * 銝餃𢆡颲枏枂
  77. *
  78. * @author lxz (2019/5/29/�其�)
  79. */
  80. void io_app_write_ouput(void) {
  81. int index = 0;
  82. if (bSoftwareOutputEnable) { //頧臭辣颲枏枂嚗諹��箔縑�瑞眏M0240撘�憪𧢲㺭�格𤜯隞?
  83. for (index = 0; index < IO_PIN_OUTPUT_NUMBER; index++) {
  84. hw_io_pin_output(index, (middle_coils[(240 + index) >> 3].value & (1 << (index & 0x07))) != 0);
  85. }
  86. }
  87. else {
  88. for (index = 0; index < IO_PIN_OUTPUT_NUMBER; index++) {
  89. hw_io_pin_output(index, (io_outputs[index >> 3].bytes[0] & (1 << (index & 0x07))) != 0);
  90. }
  91. //IO�唳旿����朞悖�㗛�嚗��摰噼�銝芯��臭誑銝滩�銋�僕
  92. memcpy(&middle_coils[(240 + 7) >> 3], io_outputs, sizeof(io_outputs));
  93. }
  94. //memcpy(&middle_coils[(256 + 7) >> 3], &io_outputsen, 1);
  95. }
  96. /**
  97. * �喲𡡒颲枏枂嚗𣬚眏step�亦&摰𡁻�閬���凋�銋�甅����亥��?
  98. *
  99. * @author lxz (2019/6/16/�冽𠯫)
  100. *
  101. * @param void
  102. */
  103. void io_app_close_output(char step) {
  104. if (step == 1) {
  105. io_outputs[0].value = 0;
  106. io_outputs[1].value = 0;
  107. }
  108. else {
  109. io_outputs[0].value = 0;
  110. io_outputs[1].value = 0;
  111. }
  112. }
  113. /**
  114. * IO摨𠉛鍂銵�遆�唳�
  115. *
  116. * @author lxz (2019/5/29/�其�)
  117. */
  118. #define TEST_APP
  119. void io_app_run(void) {
  120. int index = 0;
  121. #ifdef TEST_APP
  122. int flag = 0;
  123. static int status = 1;
  124. static sw_timer_t timer;
  125. // for (index = 0; index<(OUTPUT_BITS_NUMBER + 7)>> 3; index++) {
  126. //
  127. // io_outputs[index].bytes[0] = io_inputs[index].bytes[0];
  128. //
  129. // flag += io_inputs[index].bytes[0];
  130. // }
  131. if (flag == 0)
  132. {
  133. if (sw_timer_expire(&timer)) {
  134. status <<= 1;
  135. sw_timer_start(&timer, 0, 500000);
  136. }
  137. io_outputs[0].bytes[0] = status;
  138. io_outputs[1].bytes[0] = status>>8;
  139. if(status==0x1000) status=1;
  140. }
  141. else {
  142. sw_timer_start(&timer, 0, 500000);
  143. }
  144. #else
  145. for (index = 0; index<(OUTPUT_BITS_NUMBER + 7)>> 3; index++) {
  146. if (middle_coils[(200 >> 3) + index].bytes[0] != 0) {
  147. io_outputs[index].bytes[0] ^= middle_coils[(200 >> 3) + index].bytes[0];
  148. middle_coils[(200 >> 3) + index].bytes[0] = 0;
  149. }
  150. }
  151. #endif
  152. }