本帖最后由 lixiang656 于 2015-10-14 11:16 编辑
在STC51单片机上尝试控制步进电机后,考虑使用M453进行驱动,体会如何使用新唐M4的PWM。
硬件环境:Nutiny-EVB-M451-LQFP100 L298N电机驱动模块
软件环境:M451SeriesBSP_CMSIS_v3.00.005
主要代码如下:
/******************************************************************************
* @file main.c
* @version V1.00
* $Revision: 3 $
* $Date: 15/10/13 22:50p $
* @brief Çý¶¯²½½øµç»ú.
* @note
* Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "M451Series.h"
#include "LQ12864.h"
#include "SYS.H"
//¶¨Òåµç»úתÏò¿ØÖƵÄÒý½Å
#define M_CON1 PB1
#define M_CON2 PB2
#define M_CON3 PB3
#define M_CON4 PB4
//¶¨Òåµç»úתÏò
#define CAR_BACKWARD M_CON1=1;M_CON2=0;M_CON3=1;M_CON4=0
#define CAR_FORWARD M_CON1=0;M_CON2=1;M_CON3=0;M_CON4=1
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable PWM0 module clock */
CLK_EnableModuleClock(PWM0_MODULE);
/* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0);
/* Reset PWM0 module */
SYS_ResetModule(PWM0_RST);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and cyclesPerUs automatically. */
SystemCoreClockUpdate();
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PD multi-function pins for UART0 RXD and TXD */
SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC1MFP_Msk));
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC1MFP_PWM0_CH1;//ÅäÖÃPWM¹Ü½Å
SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC2MFP_Msk));
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC2MFP_PWM0_CH2;//ÅäÖÃPWM¹Ü½Å
}
void delay(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<500;jj++);
}
int main()
{
/* Unlock protected registers */
SYS_UnlockReg();
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 to 115200-8n1 for print message */
UART_Open(UART0, 115200);
printf("\n");
printf("Simple Demo Code\n\n");
printf("Please Input Any Key\n\n");
//ÉèÖÃPD0-3ΪÊä³öģʽ
GPIO_SetMode(PB, BIT1, GPIO_MODE_OUTPUT);
GPIO_SetMode(PB, BIT2, GPIO_MODE_OUTPUT);
GPIO_SetMode(PB, BIT3, GPIO_MODE_OUTPUT);
GPIO_SetMode(PB, BIT4, GPIO_MODE_OUTPUT);
//¿ØÖÆС³µµÄת¶¯·½Ïò
PB1=1;
PB2=0;
PB3=1;
PB4=0;
//³õʼ»¯PWM
SYS->GPC_MFPL &= ~SYS_GPC_MFPL_PC1MFP_Msk;
SYS->GPC_MFPL &= ~SYS_GPC_MFPL_PC2MFP_Msk;
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC1MFP_PWM0_CH1;//ÅäÖÃPWM¹Ü½Å
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC2MFP_PWM0_CH2;//ÅäÖÃPWM¹Ü½Å
PWM_ConfigOutputChannel(PWM0, 1, 400, 70); //PWM0_CH2ƵÂÊ100Hz£¬Õ¼¿Õ±È50%
PWM_ConfigOutputChannel(PWM0, 2, 400, 70); //PWM0_CH2ƵÂÊ100Hz£¬Õ¼¿Õ±È50%
/* Enable PWM Output path for PWM0 channel 2 */
PWM_EnableOutput(PWM0, 0x06);
// Start
PWM_Start(PWM0, 0x06);
//lcd³õʼ»¯
//LCD_Init();
while(1);
}
/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
备注:以一个步进电机为例,由ENA、IN1,IN2三个引脚控制,ENA负责输出PWM信号,IN1,IN2负责控制步进电机的转向,例如,IN1=0,IN2=1是正向转动,IN1=1,IN2=0是反向转动。
那么PWM的初始化主要是在系统初始化的时候进行的,总结如下:
1、使能PWM的模块时钟:CLK_EnableModuleClock(PWM0_MODULE);
2、设置PWM所使用的时钟源:CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0);
3、复位PWM:SYS_ResetModule(PWM0_RST);
4、设定哪些IO口为PWM功能:
SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC1MFP_Msk));
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC1MFP_PWM0_CH1;//ÅäÖÃPWM¹Ü½Å
SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC2MFP_Msk));
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC2MFP_PWM0_CH2;//ÅäÖÃPWM¹Ü½Å
遗留问题:
1、是什么控制步进电机的转速?
理解不透,表达不善,敬请赐教! |