| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include <Myproject.h>
- // 1 / 115200 * 640 *1000 ≈ 5.6 ms
- uint8_t xdata DebugDat[64] = {0};
- /**
- @function UART1_Init
- @brief 调试串口配置
- @date 2025-11-05
- */
- void UART1_Init(void)
- {
- // 使能端口 P05 P06 复用为串口
- SetBit(PH_SEL, UART1EN);
- // 模式1 全/半双工模式
- UT_MOD1 = 0;
- UT_MOD0 = 1;
- // 多机通讯关闭
- SM2 = 0;
- // 允许接收
- REN = 1;
- // 关闭中断
- ES0 = 0;
- // 优先级配置为最低
- ClrBit(IP3, PI2C_UT11 | PI2C_UT10);
- // 波特率配置
- // 参考波特率 115200 --> 0x0C 9600 --> 0x9B
- UT_BAUD = 0x0C;
- // DMA发送配置
- Conf_DMA(0, XDATA_UART1, DebugDat, 64);
- ES0 = 1;
- }
- void UART2_Init(void)
- {
- SetBit(PH_SEL, UART2EN);
- /* UART模式 UT2_MOD1,UT2_MOD0为:
- 00 模式0,移位寄存器波特率为 系统时钟 /12
- 01 模式1,8-bit UART 波特率为 fcpu_clk / ( (16 / (1+ UT2_BAUD[BAUD2_SEL]) ) / (UT2_BAUD+1) )
- 10 模式2,9-bit UART 波特率为 fcpu_clk / ( 32 – 16* UT2_BAUD[BAUD2_SEL])
- 11 模式3,9-bit UART 波特率为 fcpu_clk / ( (16 / (1+ UT2_BAUD[BAUD2_SEL])) / (UT2_BAUD+1) ) */
- UT2MOD1 = 0; // 9位模式 01
- UT2MOD0 = 1;
- /* Does not allow multi-thread cpu operation */
- /* UT2_SM2 为 0 - 单机通讯为 1 - 多机通讯 */
- UT2SM2 = 0;
- /* UT2_REN 为 0 - 禁止接收为 1 - 使能接收 */
- UT2REN = 1;
- UT2RI = 0;
- /* Uart2设置中断优先级 */
- SetBit(IP3, PSPI_UT21);
- ClrBit(IP3, PSPI_UT20);
- // UART2CH1, UART2CH0
- // 00:P3.6 为 RXD、P3.7 为 TXD(P3.6 为单线模式的输入输出)
- // 01:P4.7 为 RXD、P1.2 为 TXD(P1.2 为单线模式的输入输出)
- // 1X:P0.1 为 RXD、P0.0 为 TXD(P0.1 为单线模式的输入输出)
- ClrBit(PH_SEL1, UART2CH1);
- ClrBit(PH_SEL1, UART2CH0);
- // 设置波特率 根据 fcpu_clk / ( (16 / (1+ UT2_BAUD[BAUD2_SEL]) ) / (UT2_BAUD+1) )计算
- UT2_BAUD |= 1500000 / 9600 - 1;
- // SetBit(UT2_BAUD,BAUD2_SEL);
- SetBit(UT2_BAUD, UART2IEN);
- }
- /**
- @function Dabug_Data_Update
- @brief 调试信息上载 !高字节在前
- @date 2025-11-05
- */
- void Dabug_Data_Update(void)
- {
- static uint8_t update_delay_cnt = 0;
- uint8_t sumcheck = 0, addcheck = 0;
- uint16_t i = 0, flen = 0, switch_temp = 0;
-
- if (++ update_delay_cnt > 9)
- {
- update_delay_cnt = 0;
- *(_IO uint16_t xdata *)(&DebugDat + 0) = 0xABFF;
- *(_IO uint16_t xdata *)(&DebugDat + 2) = 0xFFF1;
- *(_IO uint16_t xdata *)(&DebugDat + 4) = 0x3800;
- *(_IO uint16_t xdata *)(&DebugDat + 6) = (mcCurOffset.IuOffset >> 8) | (mcCurOffset.IuOffset << 8);
- *(_IO uint16_t xdata *)(&DebugDat + 8) = (mcCurOffset.IvOffset >> 8) | (mcCurOffset.IvOffset << 8);
- #if 0
- switch_temp = (uint16_t)motorControl.uPhaseCurr;
- *(_IO uint16_t xdata *)(&DebugDat + 10) = (switch_temp >> 8) | (switch_temp << 8);
- switch_temp = (uint16_t)motorControl.vPhaseCurr;
- *(_IO uint16_t xdata *)(&DebugDat + 12) = (switch_temp >> 8) | (switch_temp << 8);
- #endif
- *(_IO uint16_t xdata *)(&DebugDat + 14) = (motorControl.DCBus >> 8) | (motorControl.DCBus << 8);
- flen = DebugDat[4] + DebugDat[5] * 256;
-
- // 计算校验
- for (i = 0; i < (flen + 6); i++)
- {
- sumcheck += DebugDat[i];
- addcheck += sumcheck;
- }
-
- //将计算出来的校验数据写入数据帧
- DebugDat[flen + 6] = sumcheck;
- DebugDat[flen + 7] = addcheck;
- Set_DMA_Data_Package(0, DebugDat, 0x40);
- Switch_DMA(0);
- }
- }
|