main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. @copyright None
  3. @file main.c
  4. @author Comment Vivre
  5. @date 2025-10-31
  6. @brief 主函数 初始化 主循环
  7. */
  8. #include <Myproject.h>
  9. bool data IsTick = false;
  10. bool data isCtrlPowOn = false;
  11. Motor_Control_t motorControl;
  12. void HardwareInit(void)
  13. {
  14. // 上电等待
  15. uint16 PowerUpCnt = 0;
  16. while (PowerUpCnt++ < 10000);
  17. // 参考电压初始化
  18. VREF_Config_Init();
  19. // 外部中断初始化 预驱故障中断保护
  20. PreDriver_Falut_Init();
  21. // IO初始化
  22. GPIO_Init();
  23. // ADC端口初始化
  24. ADC_Init();
  25. // AMP初始化
  26. AMP_Init();
  27. // 驱动初始化
  28. Driver_Init();
  29. // 串口初始化
  30. UART1_Init();
  31. Set_IRQ_DMA(DMA_IRQ_L1);
  32. // 系统定时器初始化
  33. Sys_Tick();
  34. _nop_(); _nop_();
  35. EA = 1;
  36. }
  37. /* ---------------------------------------------------------------------------------
  38. Function Name : void SoftwareInit(void)
  39. Description : 软件初始化,初始化所有定义变量,按键初始化扫描
  40. Input : 无
  41. Output : 无
  42. ----------------------------------------------------------------------------------*/
  43. void SoftwareInit(void)
  44. {
  45. memset(&mcFaultDect, 0, sizeof(FaultVarible)); // FaultVarible变量清零
  46. /************保护次数*************/
  47. memset(&mcProtectTime, 0, sizeof(ProtectVarible)); // ProtectVarible保护次数清零
  48. /***********过流保护**************/
  49. memset(&mcCurVarible, 0, sizeof(CurrentVarible)); // 电流保护的变量清零
  50. /*****电机状态机时序变量***********/
  51. McStaSet.SetMode = 0;
  52. /*************外部控制环************/
  53. memset(&mcFocCtrl, 0, sizeof(FOCCTRL)); // mcFocCtrl变量清零
  54. mcFocCtrl.mcDcbus_chazhi = 32760;
  55. /**************************电流偏置校准变量**********************/
  56. memset(&mcCurOffset, 0, sizeof(CurrentOffset)); // mcCurOffset变量清零
  57. mcCurOffset.IuOffsetSum = 16383;
  58. mcCurOffset.IvOffsetSum = 16383;
  59. mcCurOffset.Iw_busOffsetSum = 16383;
  60. /*****速度环的响应***/
  61. memset(&mcSpeedRamp, 0, sizeof(MCRAMP)); // mcSpeedRamp变量清零
  62. mcSpeedRamp.IncValue = Motor_Speed_Inc;
  63. mcSpeedRamp.DecValue = Motor_Speed_Dec;
  64. mcState = mcReady;
  65. mcFaultSource = 0;
  66. }
  67. /**
  68. @function main
  69. @brief 主函数 电机驱动主循环 任务循环
  70. @date 2025-10-31
  71. */
  72. void main(void)
  73. {
  74. HardwareInit();
  75. SoftwareInit();
  76. _nop_(); _nop_(); _nop_();
  77. WatchDogConfig(600, 1);
  78. while (1)
  79. {
  80. if (IsTick)
  81. {
  82. Get_ADC_Value();
  83. // 目标转速设置
  84. Get_Target_Ref();
  85. // 环路响应
  86. Speed_response();
  87. // 上电控制
  88. Power_In_Control();
  89. // LED灯故障显示
  90. LED_State_Display(mcFaultSource);
  91. Tick_Task();
  92. // 故障保护函数功能
  93. Fault_Detection();
  94. // 调试信息上载
  95. Dabug_Data_Update();
  96. // 喂狗
  97. SetBit(WDT_CR, WDTRF);
  98. IsTick = false;
  99. }
  100. Motor_Control_State();
  101. Get_Current_Offset();
  102. }
  103. }