main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(&PFCFaultDect, 0, sizeof(PFCFaultVarible));
  48. memset(&ConTrolCmd, 0, sizeof(CONTROLCMDD));
  49. /*****电机状态机时序变量***********/
  50. McStaSet.SetMode = 0;
  51. /*************外部控制环************/
  52. memset(&mcFocCtrl, 0, sizeof(FOCCTRL)); // mcFocCtrl变量清零
  53. mcFocCtrl.mcDcbus_chazhi = 32760;
  54. /******ADC采样滤波值*********/
  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. // 喂狗
  92. SetBit(WDT_CR, WDTRF);
  93. IsTick = false;
  94. Tick_Task();
  95. // 故障保护函数功能
  96. Fault_Detection();
  97. }
  98. Motor_Control_State();
  99. Get_Current_Offset();
  100. }
  101. }