UART.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <Myproject.h>
  2. // 1 / 115200 * 640 *1000 ≈ 5.6 ms
  3. uint8_t xdata DebugDat[MAX_FRAME_LEN] = {0};
  4. uint8_t RecvBuffer[MAX_FRAME_LEN] = {0};
  5. bool RecMessageFalg = false;
  6. /**
  7. @function UART1_Init
  8. @brief 调试串口配置
  9. @date 2025-11-05
  10. */
  11. void UART1_Init(void)
  12. {
  13. // 使能端口 P05 P06 复用为串口
  14. SetBit(PH_SEL, UART1EN);
  15. // 模式1 全/半双工模式
  16. UT_MOD1 = 0;
  17. UT_MOD0 = 1;
  18. // 多机通讯关闭
  19. SM2 = 0;
  20. // 允许接收
  21. REN = 1;
  22. // 关闭中断
  23. ES0 = 0;
  24. // 优先级配置为最低
  25. ClrBit(IP3, PI2C_UT11 | PI2C_UT10);
  26. // 波特率配置
  27. // 参考波特率 115200 --> 0x0C 9600 --> 0x9B
  28. UT_BAUD = 0x0C;
  29. // DMA发送配置
  30. Conf_DMA(0, XDATA_UART1, DebugDat, MAX_FRAME_LEN);
  31. ES0 = 1;
  32. }
  33. void UART2_Init(void)
  34. {
  35. SetBit(PH_SEL, UART2EN);
  36. /* UART模式 UT2_MOD1,UT2_MOD0为:
  37. 00 模式0,移位寄存器波特率为 系统时钟 /12
  38. 01 模式1,8-bit UART 波特率为 fcpu_clk / ( (16 / (1+ UT2_BAUD[BAUD2_SEL]) ) / (UT2_BAUD+1) )
  39. 10 模式2,9-bit UART 波特率为 fcpu_clk / ( 32 – 16* UT2_BAUD[BAUD2_SEL])
  40. 11 模式3,9-bit UART 波特率为 fcpu_clk / ( (16 / (1+ UT2_BAUD[BAUD2_SEL])) / (UT2_BAUD+1) ) */
  41. UT2MOD1 = 0; // 9位模式 01
  42. UT2MOD0 = 1;
  43. /* Does not allow multi-thread cpu operation */
  44. /* UT2_SM2 为 0 - 单机通讯为 1 - 多机通讯 */
  45. UT2SM2 = 0;
  46. /* UT2_REN 为 0 - 禁止接收为 1 - 使能接收 */
  47. UT2REN = 1;
  48. UT2RI = 0;
  49. /* Uart2设置中断优先级 */
  50. SetBit(IP3, PSPI_UT21);
  51. ClrBit(IP3, PSPI_UT20);
  52. // UART2CH1, UART2CH0
  53. // 00:P3.6 为 RXD、P3.7 为 TXD(P3.6 为单线模式的输入输出)
  54. // 01:P4.7 为 RXD、P1.2 为 TXD(P1.2 为单线模式的输入输出)
  55. // 1X:P0.1 为 RXD、P0.0 为 TXD(P0.1 为单线模式的输入输出)
  56. ClrBit(PH_SEL1, UART2CH1);
  57. ClrBit(PH_SEL1, UART2CH0);
  58. // 设置波特率 根据 fcpu_clk / ( (16 / (1+ UT2_BAUD[BAUD2_SEL]) ) / (UT2_BAUD+1) )计算
  59. UT2_BAUD |= 1500000 / 9600 - 1;
  60. // SetBit(UT2_BAUD,BAUD2_SEL);
  61. SetBit(UT2_BAUD, UART2IEN);
  62. }
  63. /**
  64. @function Dabug_Data_Update
  65. @brief 调试信息上载
  66. @date 2025-11-05
  67. */
  68. void Dabug_Data_Update(void)
  69. {
  70. static uint8_t update_delay_cnt = 0;
  71. uint8_t sumcheck = 0, addcheck = 0;
  72. uint16_t i = 0, switchTemp = 0;
  73. if (++ update_delay_cnt > 9)
  74. {
  75. update_delay_cnt = 0;
  76. *(_IO uint16_t xdata *)(&DebugDat + 0) = 0xABFF;
  77. *(_IO uint16_t xdata *)(&DebugDat + 2) = 0xFFF1;
  78. *(_IO uint16_t xdata *)(&DebugDat + 4) = 0x3800;
  79. *(_IO uint16_t xdata *)(&DebugDat + 6) = (currOffset.IuOffset >> 8) | (currOffset.IuOffset << 8);
  80. *(_IO uint16_t xdata *)(&DebugDat + 8) = (currOffset.IvOffset >> 8) | (currOffset.IvOffset << 8);
  81. *(_IO uint16_t xdata *)(&DebugDat + 10) = (motorControl.DCBus >> 8) | (motorControl.DCBus << 8);
  82. switchTemp = (uint16_t)FOC__ID;
  83. *(_IO uint16_t xdata *)(&DebugDat + 12) = (switchTemp >> 8) | (switchTemp << 8);
  84. switchTemp = (uint16_t)FOC__IQ;
  85. *(_IO uint16_t xdata *)(&DebugDat + 14) = (switchTemp >> 8) | (switchTemp << 8);
  86. switchTemp = (uint16_t)FOC__IA;
  87. *(_IO uint16_t xdata *)(&DebugDat + 16) = (switchTemp >> 8) | (switchTemp << 8);
  88. switchTemp = (uint16_t)FOC__IB;
  89. *(_IO uint16_t xdata *)(&DebugDat + 18) = (switchTemp >> 8) | (switchTemp << 8);
  90. switchTemp = (uint16_t)FOC__IC;
  91. *(_IO uint16_t xdata *)(&DebugDat + 20) = (switchTemp >> 8) | (switchTemp << 8);
  92. switchTemp = (uint16_t)FOC_IDREF;
  93. *(_IO uint16_t xdata *)(&DebugDat + 22) = (switchTemp >> 8) | (switchTemp << 8);
  94. switchTemp = (uint16_t)FOC_IQREF;
  95. *(_IO uint16_t xdata *)(&DebugDat + 24) = (switchTemp >> 8) | (switchTemp << 8);
  96. switchTemp = (uint16_t)motorControl.ActualSpeed;
  97. *(_IO uint16_t xdata *)(&DebugDat + 26) = (switchTemp >> 8) | (switchTemp << 8);
  98. // 计算校验
  99. for (i = 0; i < 62; i++)
  100. {
  101. sumcheck += DebugDat[i];
  102. addcheck += sumcheck;
  103. }
  104. //将计算出来的校验数据写入数据帧
  105. DebugDat[62] = sumcheck;
  106. DebugDat[63] = addcheck;
  107. Set_DMA_Data_Package(0, DebugDat, 0x40);
  108. Switch_DMA(0);
  109. }
  110. }