首頁 > 評測 > 【凌鷗創(chuàng)芯電機開發(fā)板LKS32MC071CBT8評測報告】6.用MCPWM讓電機轉起來
【凌鷗創(chuàng)芯電機開發(fā)板LKS32MC071CBT8評測報告】6.用MCPWM讓電機轉起來
- [導讀]
- 本帖最后由 yuyy1989 于 2023-6-24 19:01 編輯 #申請原創(chuàng)# 6.用MCPWM讓電機轉起來 之前用手動控制IO的方法成功讓電機轉了起來,不過LKS32MC071有電機控制專用的MCPWM模塊,是一個精確控制電機驅動波形輸出的
本帖最后由 yuyy1989 于 2023-6-24 19:01 編輯
#申請原創(chuàng)# 6.用MCPWM讓電機轉起來
之前用手動控制IO的方法成功讓電機轉了起來,不過LKS32MC071有電機控制專用的MCPWM模塊,是一個精確控制電機驅動波形輸出的模塊。
首先對上一貼的控制序列做個處理,UP UN VP VN WP WN序列
1 1 0 0 0 1
0 1 1 1 0 0
0 0 0 1 1 1
停止時
0 1 0 1 0 1
轉換為波形是這樣的
從手冊里看MCPWM的介紹,貌似找不到合適的波形輸出,改一下這個序列,因為電機引腳有個下拉電阻
我想是不是不用的引腳直接拉低也行,所以變成
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
再轉成波形
這次就很容易找到對應的了
中心對齊輸出,但是CH<n>N要翻轉一下
先用上一貼的手動控制IO的方法試了一下,這個序列是可以轉起來的,接下來編寫MCPWM的代碼
為了保證能轉起來,IO的切換速度盡量靠近手動控制IO的速度,手動控制IO時是每10ms轉動三分之一,換成一個PWM周期是30ms,頻率在30Hz左右,用12M的系統(tǒng)時鐘可以正好得到這個頻率,但是在實際使用時發(fā)現(xiàn)設置成12M時鐘后MCU不工作了,嘗試24M時鐘可以正常工作,因此最終頻率設置成了50Hz
工程需添加庫文件lks32mc07x_mcpwm.c
代碼示例
- #include "lks32mc07x.h"
- #include "yuyy_delay.h"
- void led_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_Pin = LED_PINS;
- GPIO_Init(LED_GPIO, &GPIO_InitStruct);
- GPIO_ResetBits(LED_GPIO, LED1_PIN|LED2_PIN);
- GPIO_SetBits(LED_GPIO, LED3_PIN);
- }
- void key_init()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStruct.GPIO_Pin = KEY_PINS;
- GPIO_Init(KEY_GPIO, &GPIO_InitStruct);
- }
- void uvw_testinit()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
- GPIO_Init(GPIO1, &GPIO_InitStruct);
- GPIO_PinAFConfig(GPIO1, GPIO_PinSource_4, AF3_MCPWM);
- GPIO_PinAFConfig(GPIO1, GPIO_PinSource_5, AF3_MCPWM);
- GPIO_PinAFConfig(GPIO1, GPIO_PinSource_6, AF3_MCPWM);
- GPIO_PinAFConfig(GPIO1, GPIO_PinSource_7, AF3_MCPWM);
- GPIO_PinAFConfig(GPIO1, GPIO_PinSource_8, AF3_MCPWM);
- GPIO_PinAFConfig(GPIO1, GPIO_PinSource_9, AF3_MCPWM);
- }
- #define MCU_MCLK (24000000LL) /**< PWM模塊運行主頻 */
- #define PWM_MCLK ((u32)MCU_MCLK) /**< PWM模塊運行主頻 */
- #define PWM_MCLK_DIV 3
- #if(PWM_MCLK_DIV == 0)
- #define PWM_PRSC
- #elif(PWM_MCLK_DIV == 1)
- #define PWM_PRSC 2
- #elif(PWM_MCLK_DIV == 2)
- #define PWM_PRSC 4
- #elif(PWM_MCLK_DIV == 3)
- #define PWM_PRSC 8 /**< *< *< PWM模塊運行預分頻器 */
- #endif
- #define PWM_FREQ0 ((u16)50) /**< *< MCPWM_CNT0PWM斬波頻率 */
- /**< 電機控制PWM 周期計數(shù)器值 */
- #define MCPWM_PERIOD0 ((u16) (PWM_MCLK / (u32)(2 * PWM_FREQ0 *(PWM_PRSC)))) //15位 最大32767
- #define MCPWM_WIDTH (MCPWM_PERIOD0*2/3)
- #define DEADTIME_NS ((u16)1200) /**< 死區(qū)時間 */
- #define DEADTIME (u16)(((unsigned long long)PWM_MCLK * (unsigned long long)DEADTIME_NS)/1000000000uL)
- void mcpwm_init()
- {
- MCPWM_InitTypeDef MCPWM_InitStructure;
- MCPWM_StructInit(&MCPWM_InitStructure);
- MCPWM_InitStructure.MCLK_EN = ENABLE; /* 模塊時鐘開啟 */
- MCPWM_InitStructure.CLK_DIV = PWM_MCLK_DIV; /* MCPWM時鐘分頻設置 */
- MCPWM_InitStructure.IO_FLT_CLKDIV = 12; /* 急停事件(來自IO口信號)數(shù)字濾波器時間設置 */
- MCPWM_InitStructure.CMP_FLT_CLKDIV = 12; /* 急停事件(來自比較器信號)數(shù)字濾波器時間設置 */
- MCPWM_InitStructure.AUEN = MCPWM0_ALL_AUPDAT; /*自動加載使能*/
- /* MCPWM0_CNT0 */
- MCPWM_InitStructure.BASE_CNT0_EN = ENABLE; /* 主計數(shù)器開始計數(shù)使能開關 */
- MCPWM_InitStructure.TH0 = MCPWM_PERIOD0; /* 計數(shù)周期設置即MCPWM輸出周期*/
- MCPWM_InitStructure.TH00 = (u16)(-MCPWM_PERIOD0+5);
- MCPWM_InitStructure.TH01 = (u16)(-MCPWM_PERIOD0+MCPWM_WIDTH-5);
- MCPWM_InitStructure.TH10 = (u16)(-MCPWM_PERIOD0+MCPWM_WIDTH+5);
- MCPWM_InitStructure.TH11 = (u16)(MCPWM_PERIOD0-MCPWM_WIDTH-5);
- MCPWM_InitStructure.TH20 = (u16)(MCPWM_PERIOD0-MCPWM_WIDTH+5);
- MCPWM_InitStructure.TH21 = (u16)(MCPWM_PERIOD0-5);
- MCPWM_InitStructure.MCPWM_WorkModeCH0 = MCPWM0_CENTRAL_PWM_MODE; /* MCPWM CH0工作模式:中心對齊PWM模式 */
- MCPWM_InitStructure.MCPWM_WorkModeCH1 = MCPWM0_CENTRAL_PWM_MODE; /* 通道工作模式設置,中心對齊或邊沿對齊 */
- MCPWM_InitStructure.MCPWM_WorkModeCH2 = MCPWM0_CENTRAL_PWM_MODE;
- MCPWM_InitStructure.DeadTimeCH012N = DEADTIME;/* 死區(qū)時間設置 */
- MCPWM_InitStructure.DeadTimeCH012P = DEADTIME;
- MCPWM_InitStructure.CMP_CTRL_CNT0 = DISABLE ;/* CMP控制CNT0失能 */
- MCPWM_InitStructure.EVT_CNT0_EN = DISABLE ;/* MCPWM_CNT1外部觸發(fā)失能 */
- MCPWM_InitStructure.EVT0 = DISABLE ;
- MCPWM_InitStructure.TR0_UP_INTV = DISABLE;
- MCPWM_InitStructure.TR0_T0_UpdateEN = ENABLE ;
- MCPWM_InitStructure.TR0_T1_UpdateEN = DISABLE ;
- MCPWM_InitStructure.TR0_AEC = DISABLE;
- MCPWM_InitStructure.T0_Update0_INT_EN = DISABLE; /* T0更新事件 中斷關閉 */
- MCPWM_InitStructure.T1_Update0_INT_EN = DISABLE ; /* T1更新事件 中斷使能 */
- MCPWM_InitStructure.Update1_INT_EN = DISABLE; /* CNT0 更新事件 中斷使能 */
- MCPWM_InitStructure.CH0N_Polarity_INV = ENABLE; /* CH0N通道輸出極性設置 | 正常輸出或取反輸出*/
- MCPWM_InitStructure.CH0P_Polarity_INV = DISABLE; /* CH0P通道輸出極性設置 | 正常輸出或取反輸出 */
- MCPWM_InitStructure.CH1N_Polarity_INV = ENABLE;
- MCPWM_InitStructure.CH1P_Polarity_INV = DISABLE;
- MCPWM_InitStructure.CH2N_Polarity_INV = ENABLE;
- MCPWM_InitStructure.CH2P_Polarity_INV = DISABLE;
- MCPWM_InitStructure.Switch_CH0N_CH0P = DISABLE; /* 通道交換選擇設置 | CH0P和CH0N是否選擇信號交換 */
- MCPWM_InitStructure.Switch_CH1N_CH1P = DISABLE; /* 通道交換選擇設置 */
- MCPWM_InitStructure.Switch_CH2N_CH2P = DISABLE; /* 通道交換選擇設置 */
- /* 默認電平設置 默認電平輸出不受MCPWM_IO01和MCPWM_IO23的 BIT0、BIT1、BIT8、BIT9、BIT6、BIT14通道交換和極性控制的影響,直接控制通道輸出 */
- MCPWM_InitStructure.CH0P_default_output = MCPWM0_LOW_LEVEL;
- MCPWM_InitStructure.CH0N_default_output = MCPWM0_HIGH_LEVEL;
- MCPWM_InitStructure.CH1P_default_output = MCPWM0_LOW_LEVEL; /* CH1P對應引腳在空閑狀態(tài)輸出低電平 */
- MCPWM_InitStructure.CH1N_default_output = MCPWM0_HIGH_LEVEL; /* CH1N對應引腳在空閑狀態(tài)輸出高電平 */
- MCPWM_InitStructure.CH2P_default_output = MCPWM0_LOW_LEVEL;
- MCPWM_InitStructure.CH2N_default_output = MCPWM0_HIGH_LEVEL;
- MCPWM_InitStructure.CH0N_FAIL_EN = ENABLE ;
- MCPWM_InitStructure.CH0P_FAIL_EN = ENABLE ;
- MCPWM_InitStructure.CH1N_FAIL_EN = ENABLE ;
- MCPWM_InitStructure.CH1P_FAIL_EN = ENABLE ;
- MCPWM_InitStructure.CH2N_FAIL_EN = ENABLE ;
- MCPWM_InitStructure.CH2P_FAIL_EN = ENABLE ;
- MCPWM_InitStructure.FAIL0_INPUT_EN = DISABLE ;//FAIL_0CAP
- MCPWM_InitStructure.FAIL0_INT_EN = DISABLE;
- MCPWM_InitStructure.FAIL0_Signal_Sel = MCPWM0_FAIL_SEL_CMP ;//FAIL_0CAP
- MCPWM_InitStructure.FAIL0_Polarity = MCPWM0_HIGH_LEVEL_ACTIVE ;
- MCPWM_InitStructure.FAIL1_INPUT_EN = DISABLE ;//FAIL_0CAP
- MCPWM_InitStructure.FAIL1_INT_EN = DISABLE;
- MCPWM_InitStructure.FAIL1_Signal_Sel = MCPWM0_FAIL_SEL_IO ;//FAIL_0CAP
- MCPWM_InitStructure.FAIL1_Polarity = MCPWM0_HIGH_LEVEL_ACTIVE ;
- MCPWM_InitStructure.HALT_PRT0 = DISABLE ;
- MCPWM_InitStructure.FAIL_0CAP = DISABLE ;
- MCPWM_Init(&MCPWM_InitStructure);
- NVIC_SetPriority(MCPWM0_IRQn, 1);
- NVIC_EnableIRQ(MCPWM0_IRQn);
- }
- void MCPWM0_IRQHandler(void)
- {
- }
- void mcu_init(void)
- {
- __disable_irq(); /* 關閉中斷 中斷總開關 */
- SYS_WR_PROTECT = 0x7a83; /*使能系統(tǒng)寄存器寫操作*/
- FLASH_CFG |= 0x00080000; /* enable prefetch ,F(xiàn)LASH預取加速使能*/
- IWDG_DISABLE(); /*關閉清零看門狗*/
- SYS_MclkChoice(SYS_MCLK_24M_RC); /*選擇當前主時鐘頻率*/
- led_init();
- key_init();
- uvw_testinit();
- mcpwm_init();
- yuyy_delay_us(100); /* 延時等待硬件初始化穩(wěn)定 */
- SYS_WR_PROTECT = 0x0; /*關閉系統(tǒng)寄存器寫操作*/
- __enable_irq(); /* 開啟總中斷 */
- }
- int main(void)
- {
- uint8_t run = 0;
- uint8_t key_start_downcount = 0;
- uint8_t key_stop_downcount = 0;
- uint8_t runstep = 0;
- mcu_init();
- while (1)
- {
- if(Bit_RESET == GPIO_ReadInputDataBit(KEY_GPIO,KEY_START_PIN))
- {
- if(key_start_downcount < 0xFF)
- key_start_downcount++;
- }
- else
- {
- if(key_start_downcount > 3 && run == 0)
- {
- run = 1;
- runstep = 0;
- PWMOutputs(ENABLE);
- }
- key_start_downcount = 0;
- }
- if(Bit_RESET == GPIO_ReadInputDataBit(KEY_GPIO,KEY_STOP_PIN))
- {
- if(key_stop_downcount < 0xFF)
- key_stop_downcount++;
- }
- else
- {
- if(key_stop_downcount > 3 && run > 0)
- {
- run = 0;
- runstep = 0;
- PWMOutputs(DISABLE);
- }
- key_stop_downcount = 0;
- }
- if(runstep > 0)
- runstep--;
- else
- {
- runstep = 20;
- switch(run)
- {
- case 1:
- GPIO_SetBits(LED_GPIO, LED1_PIN);
- GPIO_ResetBits(LED_GPIO, LED2_PIN);
- GPIO_SetBits(LED_GPIO, LED3_PIN);
- run = 2;
- break;
- case 2:
- GPIO_ResetBits(LED_GPIO, LED1_PIN);
- GPIO_SetBits(LED_GPIO, LED2_PIN);
- GPIO_SetBits(LED_GPIO, LED3_PIN);
- run = 3;
- break;
- case 3:
- GPIO_ResetBits(LED_GPIO, LED1_PIN);
- GPIO_ResetBits(LED_GPIO, LED2_PIN);
- GPIO_ResetBits(LED_GPIO, LED3_PIN);
- run = 1;
- break;
- default:
- GPIO_ResetBits(LED_GPIO, LED1_PIN);
- GPIO_ResetBits(LED_GPIO, LED2_PIN);
- GPIO_SetBits(LED_GPIO, LED3_PIN);
- run = 0;
- break;
- }
- }
- yuyy_delay_ms(10);
- }
- }
正式接電機使用前,如果有示波器最好用示波器看一下波形是否正常,以免燒壞電機
運行效果
這只是一個剛入門無刷電機的人做的一個簡單的用MCPWM驅動電機的示例,后面會繼續(xù)研究無刷電機的控制方法
- 本文系21ic原創(chuàng),未經(jīng)許可禁止轉載!
網(wǎng)友評論
- 聯(lián)系人:巧克力娃娃
- 郵箱:board@21ic.com
- 我要投稿
-
歡迎入駐,開放投稿
-
人均百萬?英偉達中國員工收入曝光! 2024-08-29
-
《黑神話:悟空》玩家硬盤升級攻略:提升游戲體驗,暢享3A大作 2024-08-29
-
數(shù)睿數(shù)據(jù)參加《系統(tǒng)與軟件工程 低代碼開發(fā)平臺通用技術要求》國家標準編制 2024-08-29
- NRF52810藍牙數(shù)字耳機找人定制
預算:¥30005天前
- 125KW模塊式PCS軟硬件外包開發(fā)
預算:¥1100000015小時前
- 12V汽車啟動電源項目BMS設計
預算:¥50000023小時前
- 數(shù)據(jù)可視化軟件 開發(fā)
預算:¥5000023小時前
- PLC項目調(diào)試修改
預算:¥100001天前
- 起動電機控制器開發(fā)
預算:¥1100001天前