main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. for (PowerUpCnt = 0; PowerUpCnt < 10000; PowerUpCnt++) {};
  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. SysTick();
  31. _nop_(); _nop_();
  32. EA = 1;
  33. }
  34. /* ---------------------------------------------------------------------------------
  35. Function Name : void SoftwareInit(void)
  36. Description : 软件初始化,初始化所有定义变量,按键初始化扫描
  37. Input : 无
  38. Output : 无
  39. ----------------------------------------------------------------------------------*/
  40. void SoftwareInit(void)
  41. {
  42. memset(&mcFaultDect, 0, sizeof(FaultVarible)); // FaultVarible变量清零
  43. /************保护次数*************/
  44. memset(&mcProtectTime, 0, sizeof(ProtectVarible)); // ProtectVarible保护次数清零
  45. /***********过流保护**************/
  46. memset(&mcCurVarible, 0, sizeof(CurrentVarible)); // 电流保护的变量清零
  47. memset(&ConTrolCmd, 0, sizeof(CONTROLCMDD));
  48. /*****电机状态机时序变量***********/
  49. McStaSet.SetMode = 0;
  50. /*************外部控制环************/
  51. memset(&mcFocCtrl, 0, sizeof(FOCCTRL)); // mcFocCtrl变量清零
  52. mcFocCtrl.mcDcbus_chazhi = 32760;
  53. /******ADC采样滤波值*********/
  54. /**************************电流偏置校准变量**********************/
  55. memset(&mcCurOffset, 0, sizeof(CurrentOffset)); // mcCurOffset变量清零
  56. mcCurOffset.IuOffsetSum = 16383;
  57. mcCurOffset.IvOffsetSum = 16383;
  58. mcCurOffset.Iw_busOffsetSum = 16383;
  59. /*****速度环的响应***/
  60. memset(&mcSpeedRamp, 0, sizeof(MCRAMP)); // mcSpeedRamp变量清零
  61. mcSpeedRamp.IncValue = Motor_Speed_Inc;
  62. mcSpeedRamp.DecValue = Motor_Speed_Dec;
  63. mcState = mcReady;
  64. mcFaultSource = 0;
  65. }
  66. /**
  67. @function main
  68. @brief 主函数 电机驱动主循环 任务循环
  69. @date 2025-10-31
  70. */
  71. void main(void)
  72. {
  73. HardwareInit();
  74. SoftwareInit();
  75. _nop_(); _nop_(); _nop_();
  76. WatchDogConfig(600, 1);
  77. while (1)
  78. {
  79. if (IsTick)
  80. {
  81. Get_ADC_Value();
  82. // 目标转速设置
  83. Get_Target_Ref();
  84. // 环路响应
  85. Speed_response();
  86. // 上电控制
  87. Power_In_Control();
  88. // LED灯故障显示
  89. LED_State_Display(mcFaultSource);
  90. // 喂狗
  91. SetBit(WDT_CR, WDTRF);
  92. IsTick = false;
  93. Tick_Task();
  94. // 故障保护函数功能
  95. Fault_Detection();
  96. }
  97. Motor_Control_State();
  98. Get_Current_Offset();
  99. }
  100. }