UART.c 3.8 KB

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