首頁 > 評測 > 【凌鷗創(chuàng)芯電機(jī)開發(fā)板LKS32MC071CBT8評測報(bào)告】6.用MCPWM讓電機(jī)轉(zhuǎn)起來
【凌鷗創(chuàng)芯電機(jī)開發(fā)板LKS32MC071CBT8評測報(bào)告】6.用MCPWM讓電機(jī)轉(zhuǎn)起來
- [導(dǎo)讀]
- 本帖最后由 yuyy1989 于 2023-6-24 19:01 編輯 #申請?jiān)瓌?chuàng)# 6.用MCPWM讓電機(jī)轉(zhuǎn)起來 之前用手動(dòng)控制IO的方法成功讓電機(jī)轉(zhuǎn)了起來,不過LKS32MC071有電機(jī)控制專用的MCPWM模塊,是一個(gè)精確控制電機(jī)驅(qū)動(dòng)波形輸出的
本帖最后由 yuyy1989 于 2023-6-24 19:01 編輯
#申請?jiān)瓌?chuàng)# 6.用MCPWM讓電機(jī)轉(zhuǎn)起來
之前用手動(dòng)控制IO的方法成功讓電機(jī)轉(zhuǎn)了起來,不過LKS32MC071有電機(jī)控制專用的MCPWM模塊,是一個(gè)精確控制電機(jī)驅(qū)動(dòng)波形輸出的模塊。
首先對上一貼的控制序列做個(gè)處理,UP UN VP VN WP WN序列
1 1 0 0 0 1
0 1 1 1 0 0
0 0 0 1 1 1
停止時(shí)
0 1 0 1 0 1
轉(zhuǎn)換為波形是這樣的
從手冊里看MCPWM的介紹,貌似找不到合適的波形輸出,改一下這個(gè)序列,因?yàn)殡姍C(jī)引腳有個(gè)下拉電阻
我想是不是不用的引腳直接拉低也行,所以變成
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
再轉(zhuǎn)成波形
這次就很容易找到對應(yīng)的了
中心對齊輸出,但是CH<n>N要翻轉(zhuǎn)一下
先用上一貼的手動(dòng)控制IO的方法試了一下,這個(gè)序列是可以轉(zhuǎn)起來的,接下來編寫MCPWM的代碼
為了保證能轉(zhuǎn)起來,IO的切換速度盡量靠近手動(dòng)控制IO的速度,手動(dòng)控制IO時(shí)是每10ms轉(zhuǎn)動(dòng)三分之一,換成一個(gè)PWM周期是30ms,頻率在30Hz左右,用12M的系統(tǒng)時(shí)鐘可以正好得到這個(gè)頻率,但是在實(shí)際使用時(shí)發(fā)現(xiàn)設(shè)置成12M時(shí)鐘后MCU不工作了,嘗試24M時(shí)鐘可以正常工作,因此最終頻率設(shè)置成了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模塊運(yùn)行主頻 */
- #define PWM_MCLK ((u32)MCU_MCLK) /**< PWM模塊運(yùn)行主頻 */
- #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模塊運(yùn)行預(yù)分頻器 */
- #endif
- #define PWM_FREQ0 ((u16)50) /**< *< MCPWM_CNT0PWM斬波頻率 */
- /**< 電機(jī)控制PWM 周期計(jì)數(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ū)時(shí)間 */
- #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; /* 模塊時(shí)鐘開啟 */
- MCPWM_InitStructure.CLK_DIV = PWM_MCLK_DIV; /* MCPWM時(shí)鐘分頻設(shè)置 */
- MCPWM_InitStructure.IO_FLT_CLKDIV = 12; /* 急停事件(來自IO口信號(hào))數(shù)字濾波器時(shí)間設(shè)置 */
- MCPWM_InitStructure.CMP_FLT_CLKDIV = 12; /* 急停事件(來自比較器信號(hào))數(shù)字濾波器時(shí)間設(shè)置 */
- MCPWM_InitStructure.AUEN = MCPWM0_ALL_AUPDAT; /*自動(dòng)加載使能*/
- /* MCPWM0_CNT0 */
- MCPWM_InitStructure.BASE_CNT0_EN = ENABLE; /* 主計(jì)數(shù)器開始計(jì)數(shù)使能開關(guān) */
- MCPWM_InitStructure.TH0 = MCPWM_PERIOD0; /* 計(jì)數(shù)周期設(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; /* 通道工作模式設(shè)置,中心對齊或邊沿對齊 */
- MCPWM_InitStructure.MCPWM_WorkModeCH2 = MCPWM0_CENTRAL_PWM_MODE;
- MCPWM_InitStructure.DeadTimeCH012N = DEADTIME;/* 死區(qū)時(shí)間設(shè)置 */
- 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更新事件 中斷關(guān)閉 */
- MCPWM_InitStructure.T1_Update0_INT_EN = DISABLE ; /* T1更新事件 中斷使能 */
- MCPWM_InitStructure.Update1_INT_EN = DISABLE; /* CNT0 更新事件 中斷使能 */
- MCPWM_InitStructure.CH0N_Polarity_INV = ENABLE; /* CH0N通道輸出極性設(shè)置 | 正常輸出或取反輸出*/
- MCPWM_InitStructure.CH0P_Polarity_INV = DISABLE; /* CH0P通道輸出極性設(shè)置 | 正常輸出或取反輸出 */
- 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; /* 通道交換選擇設(shè)置 | CH0P和CH0N是否選擇信號(hào)交換 */
- MCPWM_InitStructure.Switch_CH1N_CH1P = DISABLE; /* 通道交換選擇設(shè)置 */
- MCPWM_InitStructure.Switch_CH2N_CH2P = DISABLE; /* 通道交換選擇設(shè)置 */
- /* 默認(rèn)電平設(shè)置 默認(rèn)電平輸出不受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對應(yīng)引腳在空閑狀態(tài)輸出低電平 */
- MCPWM_InitStructure.CH1N_default_output = MCPWM0_HIGH_LEVEL; /* CH1N對應(yīng)引腳在空閑狀態(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(); /* 關(guān)閉中斷 中斷總開關(guān) */
- SYS_WR_PROTECT = 0x7a83; /*使能系統(tǒng)寄存器寫操作*/
- FLASH_CFG |= 0x00080000; /* enable prefetch ,F(xiàn)LASH預(yù)取加速使能*/
- IWDG_DISABLE(); /*關(guān)閉清零看門狗*/
- SYS_MclkChoice(SYS_MCLK_24M_RC); /*選擇當(dāng)前主時(shí)鐘頻率*/
- led_init();
- key_init();
- uvw_testinit();
- mcpwm_init();
- yuyy_delay_us(100); /* 延時(shí)等待硬件初始化穩(wěn)定 */
- SYS_WR_PROTECT = 0x0; /*關(guān)閉系統(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);
- }
- }
正式接電機(jī)使用前,如果有示波器最好用示波器看一下波形是否正常,以免燒壞電機(jī)
運(yùn)行效果
這只是一個(gè)剛?cè)腴T無刷電機(jī)的人做的一個(gè)簡單的用MCPWM驅(qū)動(dòng)電機(jī)的示例,后面會(huì)繼續(xù)研究無刷電機(jī)的控制方法
- 本文系21ic原創(chuàng),未經(jīng)許可禁止轉(zhuǎn)載!
網(wǎng)友評論
- 聯(lián)系人:巧克力娃娃
- 郵箱:board@21ic.com
- 我要投稿
-
歡迎入駐,開放投稿
-
人均百萬?英偉達(dá)中國員工收入曝光! 2024-08-29
-
《黑神話:悟空》玩家硬盤升級攻略:提升游戲體驗(yàn),暢享3A大作 2024-08-29
-
數(shù)睿數(shù)據(jù)參加《系統(tǒng)與軟件工程 低代碼開發(fā)平臺(tái)通用技術(shù)要求》國家標(biāo)準(zhǔn)編制 2024-08-29
- NRF52810藍(lán)牙數(shù)字耳機(jī)找人定制
預(yù)算:¥30005天前
- 125KW模塊式PCS軟硬件外包開發(fā)
預(yù)算:¥1100000015小時(shí)前
- 12V汽車啟動(dòng)電源項(xiàng)目BMS設(shè)計(jì)
預(yù)算:¥50000023小時(shí)前
- 數(shù)據(jù)可視化軟件 開發(fā)
預(yù)算:¥5000023小時(shí)前
- PLC項(xiàng)目調(diào)試修改
預(yù)算:¥100001天前
- 起動(dòng)電機(jī)控制器開發(fā)
預(yù)算:¥1100001天前