main.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "string.h"
  5. #include "uart.h"
  6. #include "led.h"
  7. uint8_t recive_buff[256] = {0};
  8. uint16_t recive_count = 0;
  9. uint8_t sendbuf[256] = {0};
  10. #define AC_DC 1
  11. #define AC3 2
  12. uint16_t flag = 0;
  13. extern uint16_t recive_flag;
  14. unsigned short calculateModbusCRC16(uint8_t *data,uint16_t lenth)
  15. {
  16. uint16_t crc = 0xFFFF; // Initial value
  17. unsigned short swappedCRC = 0xFFFF;
  18. int i = 0;
  19. int j = 0;
  20. for (; i < lenth; i++)
  21. {
  22. crc = crc ^ data[i]; // XOR byte into the least significant byte of CRC
  23. j = 0;
  24. for (; j < 8; j++)
  25. {
  26. if (crc & 1)
  27. {
  28. crc >>= 1;
  29. crc ^= 0xA001;
  30. }
  31. else
  32. {
  33. crc >>= 1;
  34. }
  35. }
  36. }
  37. // Swap the bytes of the CRC to match Modbus convention
  38. swappedCRC = ((crc >> 8) & 0xFF) | ((crc & 0xFF) << 8);
  39. return crc;
  40. }
  41. int check_ac_dc()
  42. {
  43. int t = -1;
  44. uint16_t data = 0;
  45. recive_count = 0;
  46. memset(sendbuf,0,sizeof(sendbuf));
  47. sendbuf[0] = 0x01;
  48. sendbuf[1] = 0x03;
  49. sendbuf[2] = 0x0f;
  50. sendbuf[3] = 0xA0;
  51. sendbuf[4] = 0x00;
  52. sendbuf[5] = 0x20;
  53. data = calculateModbusCRC16(sendbuf,6);
  54. sendbuf[6] = data;
  55. sendbuf[7] = data >> 8;
  56. recive_count = (sendbuf[5]) * 2 +3+2;
  57. uart3_send(&sendbuf[0],8);
  58. delay_ms(300);
  59. if(recive_flag)
  60. {
  61. flag = AC_DC;
  62. t = 0;
  63. }else
  64. flag = 0;
  65. return t;
  66. }
  67. int check_ac3()
  68. {
  69. int t = -1;
  70. #if 1
  71. uint16_t data = 0;
  72. recive_count = 0;
  73. memset(sendbuf,0,sizeof(sendbuf));
  74. sendbuf[0] = 0x01;
  75. sendbuf[1] = 0x03;
  76. sendbuf[2] = 0x17;
  77. sendbuf[3] = 0x70;
  78. sendbuf[4] = 0x00;
  79. sendbuf[5] = 0x20;
  80. data = calculateModbusCRC16(sendbuf,6);
  81. sendbuf[6] = data;
  82. sendbuf[7] = data >> 8;
  83. recive_count = (sendbuf[5]) * 2 + 2 + 4;
  84. uart3_send(&sendbuf[0],8);
  85. delay_ms(300);
  86. if(recive_flag)
  87. {
  88. flag = AC3;
  89. t = 0;
  90. }else
  91. flag = 0;
  92. #endif
  93. return t;
  94. }
  95. int main(void)
  96. {
  97. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  98. delay_init();
  99. uart_init(115200);
  100. led_init();
  101. uart3_init();
  102. while(1)
  103. {
  104. if(flag == 0)
  105. {
  106. if( -1 ==check_ac_dc())
  107. check_ac3();
  108. }else if(flag == 1)
  109. {
  110. check_ac_dc();
  111. }else if(flag == 2)
  112. {
  113. check_ac3();
  114. }
  115. if(flag)
  116. {
  117. led_flash();
  118. led_status();
  119. }else
  120. {
  121. led_clear();
  122. }
  123. delay_ms(500);
  124. }
  125. }