|
楼主
查看: 5371回复: 0
发表于 2021-6-4 11:59:34
只看该作者
本帖最后由 黄福均 于 2021-6-5 00:45 编辑
/*******************************************************************************
* MSP432 Timer_A - 4路 PWM 输出
* 在P2.4 ,P2.5 ,P2.6 ,P2.7 引脚输出四路PWM
* 此程序只是测试PWM能否输出,在程序运行中PWM占空比不可变
* 可以在开始的宏定义中修改周期值和占空比。
* Timer_A TA0.1--PWM1 , TA0.2--PWM2 , TA0.3--PWM3 , TA0.4--PWM4
* SMCLK = MCLK = TACLK = default DCO 3MHz
* DUTY_CYCLE1的值越小,高电平时间越长,占空比越高。
* MSP432P401
* ------------------
* /|\| |
* | | |
* --|RST |
* | |
* | |
* | P2.4 |--> Output PWM左前
* | P2.5 |--> Output PWM右前
* | P2.6 |--> Output PWM左后
* | P2.7 |--> Output PWM右后
*******************************************************************************/
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
/* Application Defines */
#define TIMER_PERIOD 128
#define DUTY_CYCLE1 32
#define DUTY_CYCLE2 32
#define DUTY_CYCLE3 32
#define DUTY_CYCLE4 32
/* Timer_A UpDown Configuration Parameter */ //定时器A增减计数模式参数配置
const Timer_A_UpDownModeConfig upDownConfig =
{
TIMER_A_CLOCKSOURCE_SMCLK, // SMCLK Clock SOurce
TIMER_A_CLOCKSOURCE_DIVIDER_1, // SMCLK/1 = 3MHz
TIMER_PERIOD, // 127 tick period 计数周期点数为127
TIMER_A_TAIE_INTERRUPT_DISABLE, // Disable Timer interrupt
TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE, // Disable CCR0 interrupt
TIMER_A_DO_CLEAR // Clear value
};
/* Timer_A Compare Configuration Parameter (PWM1) */ //TA0.1--PWM1参数配置
const Timer_A_CompareModeConfig compareConfig_PWM1 =
{
TIMER_A_CAPTURECOMPARE_REGISTER_1, // Use CCR1
TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, // Disable CCR interrupt
TIMER_A_OUTPUTMODE_TOGGLE_SET, // 计数值未到DUTY_CYCLE1时为低电平,计数值到了DUTY_CYCLE1后为高电平
DUTY_CYCLE1 // 32 Duty Cycle
};
/* Timer_A Compare Configuration Parameter (PWM2) */ //TA0.2--PWM2参数配置
const Timer_A_CompareModeConfig compareConfig_PWM2 =
{
TIMER_A_CAPTURECOMPARE_REGISTER_2, // Use CCR2
TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, // Disable CCR interrupt
TIMER_A_OUTPUTMODE_TOGGLE_SET, // Toggle output but
DUTY_CYCLE2 // 32 Duty Cycle
};
/* Timer_A Compare Configuration Parameter (PWM3) */ //TA0.3--PWM3参数配置
const Timer_A_CompareModeConfig compareConfig_PWM3 =
{
TIMER_A_CAPTURECOMPARE_REGISTER_3, // Use CCR3
TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, // Disable CCR interrupt
TIMER_A_OUTPUTMODE_TOGGLE_SET, // Toggle output but
DUTY_CYCLE3 // 32 Duty Cycle
};
/* Timer_A Compare Configuration Parameter (PWM4) */ //TA0.4--PWM4参数配置
const Timer_A_CompareModeConfig compareConfig_PWM4 =
{
TIMER_A_CAPTURECOMPARE_REGISTER_4, // Use CCR4
TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, // Disable CCR interrupt
TIMER_A_OUTPUTMODE_TOGGLE_SET, // Toggle output but
DUTY_CYCLE4 // 32 Duty Cycle
};
int main(void)
{
/* Stop WDT */
MAP_WDT_A_holdTimer();
MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN6 + GPIO_PIN7);
MAP_GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN3);
MAP_GPIO_setAsOutputPin(GPIO_PORT_P3, GPIO_PIN0);
MAP_GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN6 + GPIO_PIN7);
MAP_GPIO_setAsOutputPin(GPIO_PORT_P6, GPIO_PIN6 + GPIO_PIN7);
MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P6, GPIO_PIN7);
MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN6);
MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN6);
MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P3, GPIO_PIN0);
MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN3);
MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN6);
MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN7);
MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN7);//定义电机正转时的引脚输出
/* 设置 P2.4,P2.5, P2.6 and P2.7 为PWM功能输出 */
MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2,
GPIO_PIN4 + GPIO_PIN5 + GPIO_PIN6 + GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
/* 配置Timer_A0 为增减计数模式并启动 */
MAP_Timer_A_configureUpDownMode(TIMER_A0_BASE, &upDownConfig);
MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UPDOWN_MODE);
/* 初始化比较寄存器并产生PWM1 */
MAP_Timer_A_initCompare(TIMER_A0_BASE, &compareConfig_PWM1);
/* 初始化比较寄存器并产生 PWM2 */
MAP_Timer_A_initCompare(TIMER_A0_BASE, &compareConfig_PWM2);
/* 初始化比较寄存器并产生 PWM3 */
MAP_Timer_A_initCompare(TIMER_A0_BASE, &compareConfig_PWM3);
/* 初始化比较寄存器并产生 PWM4 */
MAP_Timer_A_initCompare(TIMER_A0_BASE, &compareConfig_PWM4);
/* Sleeping when not in use */
while (1)
{
MAP_PCM_gotoLPM0();
}
}
请下载程序的同学投币
1_PWM_test_MSP_EXP432P401R.zip
(122.24 KB, 下载次数: 68)
|
打赏
-
查看全部打赏
|