main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Curr_Offset_t xdata currOffset;
  12. Motor_Control_t motorControl;
  13. CurrentVarible xdata mcCurVarible;
  14. FOCCTRL xdata mcFocCtrl;
  15. void HardwareInit(void)
  16. {
  17. // 上电等待
  18. uint16 PowerUpCnt = 0;
  19. while (PowerUpCnt++ < 10000);
  20. // 参考电压初始化
  21. VREF_Config_Init();
  22. // 外部中断初始化 预驱故障中断保护
  23. PreDriver_Falut_Init();
  24. // IO初始化
  25. GPIO_Init();
  26. // ADC端口初始化
  27. ADC_Init();
  28. // AMP初始化
  29. AMP_Init();
  30. // 驱动初始化
  31. Driver_Init();
  32. // 串口初始化
  33. UART1_Init();
  34. Set_IRQ_DMA(DMA_IRQ_L1);
  35. // 系统定时器初始化
  36. Sys_Tick();
  37. _nop_(); _nop_();
  38. EA = 1;
  39. }
  40. /* ---------------------------------------------------------------------------------
  41. Function Name : void SoftwareInit(void)
  42. Description : 软件初始化,初始化所有定义变量,按键初始化扫描
  43. Input : 无
  44. Output : 无
  45. ----------------------------------------------------------------------------------*/
  46. void SoftwareInit(void)
  47. {
  48. // FaultVarible变量清零
  49. memset(&mcFaultDect, 0, sizeof(FaultVarible));
  50. // ProtectVarible保护次数清零
  51. memset(&mcProtectTime, 0, sizeof(ProtectVarible));
  52. /// 电流保护的变量清零
  53. memset(&mcCurVarible, 0, sizeof(CurrentVarible));
  54. McStaSet.SetMode = 0;
  55. mcState = mcReady;
  56. mcFaultSource = 0;
  57. }
  58. /**
  59. @function main
  60. @brief 主函数 电机驱动主循环 任务循环
  61. @date 2025-10-31
  62. */
  63. void main(void)
  64. {
  65. HardwareInit();
  66. SoftwareInit();
  67. _nop_(); _nop_(); _nop_();
  68. WatchDogConfig(600, 1);
  69. while (1)
  70. {
  71. if (IsTick)
  72. {
  73. Get_ADC_Value();
  74. // 目标转速设置
  75. Get_Target_Ref();
  76. // 环路响应
  77. Loop_Control();
  78. // 上电控制
  79. Power_In_Control();
  80. // LED灯故障显示
  81. LED_State_Display(mcFaultSource);
  82. // 故障保护函数功能
  83. Fault_Detection();
  84. Tick_Task();
  85. // 调试信息上载
  86. Dabug_Data_Update();
  87. // 喂狗
  88. SetBit(WDT_CR, WDTRF);
  89. IsTick = false;
  90. }
  91. Motor_Control_State();
  92. }
  93. }