定时器PWM周期:
T_timer_a = CLKDIV * (CCR0 + 1) / f_clk
= 48 * (19999 + 1) / 48000000
= 0.02s = 50Hz
CLKDIV=48,CCR0=19999
0度:( 499 + 1) / (19999 + 1) = 500 / 20000 = 2.5%
45度:( 999 + 1) / (19999 + 1) = 1000 / 20000 = 5%
90度:( 1499 + 1) / (19999 + 1) = 1500 / 20000 = 7.5%
135度:( 1999 + 1) / (19999 + 1) = 2000 / 20000 = 10%
180度:( 2499 + 1) / (19999 + 1) = 2500 / 20000 = 12.5%
225度:( 2999 + 1) / (19999 + 1) = 3000 / 20000 = 15%
270度:( 3499 + 1) / (19999 + 1) = 3500 / 20000 = 17.5%
timA.h
#ifndef __RNA_TIMA_H
#define __RNA_TIMA_H
#include
void TimA0_PWM_Init(uint16_t ccr0, uint16_t psc);
#endif
timA.c
#include "timA.h"
void TimA0_PWM_Init(uint16_t ccr0, uint16_t psc)
{
/*初始化引脚*/
MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN4, GPIO_PRIMARY_MODULE_FUNCTION);
Timer_A_PWMConfig TimA0_PWMConfig;
/*定时器PWM初始化*/
TimA0_PWMConfig.clockSource = TIMER_A_CLOCKSOURCE_SMCLK; //时钟源
TimA0_PWMConfig.clockSourceDivider = psc; //时钟分频 范围1-64
TimA0_PWMConfig.timerPeriod = ccr0; //自动重装载值(ARR)
TimA0_PWMConfig.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1; //通道一 (引脚定义)
TimA0_PWMConfig.compareOutputMode = TIMER_A_OUTPUTMODE_TOGGLE_SET; //输出模式
TimA0_PWMConfig.dutyCycle = ccr0; //这里是改变占空比的地方 默认100%
MAP_Timer_A_generatePWM(TIMER_A0_BASE, &TimA0_PWMConfig); /* 初始化比较寄存器以产生 PWM1 */
}
main.c
#include "sysinit.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "tim32.h"
#include "timA.h"
int main(void)
{
SysInit(); // 第3讲 时钟配置
uart_init(115200); // 第7讲 串口配置
delay_init(); // 第4讲 滴答延时
/*开始填充初始化代码*/
TimA0_PWM_Init(19999, 48);
LED_Init();
/*停止填充初始化代码*/
printf("Hello,MSP432!\r\n");
MAP_Interrupt_enableMaster(); // 开启总中断
while (1)
{
/*开始填充用户代码*/
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 499);//0度
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 999);//45
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 1499);//90
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 1999);//135
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 2499);//180
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 2999);//225
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 3499);//270
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 2999);//225
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 2499);//180
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 1999);//135
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 1499);//90
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 999);//45
delay_ms(1500);
MAP_Timer_A_setCompareValue(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 499);//90
delay_ms(1500);
/*停止填充用户代码*/
}
}