---恢復內容開始---
16位定時器 Timer_A 輸出精密PWM
1.模式操作
Timer_A 支持 :多路捕獲/比較 、PWM輸出 、 定時
同時支持 :中斷
Timer_A 特點: 異步16位具有四種模式的定時器/計數器
可選擇、可配置的時鍾資源
多達7個可配置的 捕獲/比較 寄存器
具有配置PWM的功能
異步輸入和輸出自鎖
中斷向量寄存器快速解碼定時器中斷
2.基本操作模式
三種模式: 連續計數模式
增計數模式
增減計數模式
TimerA_initCompare() 初始化比較模式 /*需要必要的參數*/
TimerA_generatePWM() 生成PWM /*需要必要的參數*/
/*配置TimerA PWM參數*/
Timer_A_PWMConfig pwmConfig =
{
TIMER_A_CLOCKSOURCE_SMCLK,
TIMER_A_CLOCKSOURCE_DIVDER_1,
32000,
TIMER_A_CAPTURECOMPARE_REGISTER_0,
TIMER_A_OUTPUTMODE_TOGGLE,
3200
};
參數詳情:
typedef struct _Timer_A_PWMConfig
{
uint_fast16_t clockSource; //時鍾資源,選取哪個時鍾做定時器的來源,同430中的TASSELx
uint_fast16_t clockSourceDivider;//分頻率
uint_fast16_t timerPeriod;//周期
uint_fast16_t compareRegister;//選取比較寄存器
uint_fast16_t compareOutputMode;//比較模塊的輸出模式
uint_fast16_t dutyCycle;//高電平 占
} Timer_A_PWMConfig;
/*配置TimerA PWM參數*/
/*程序實例*/
/* 配置 MCLK to REFO at 128Khz for LF mode
* 配置 SMCLK 為 64Khz */
MAP_CS_setReferenceOscillatorFrequency(CS_REFO_128KHZ);
MAP_CS_initClockSignal(CS_MCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);
MAP_CS_initClockSignal(CS_SMCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_2);
MAP_PCM_setPowerState(PCM_AM_LF_VCORE0);
/* 將GPIO7.3設置為PWM輸出引腳,P1.1位按鍵
* 中斷 */
MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P7, GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN1);
MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN1);
MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN1);
/* 配置定時器A為周期為500ms,初始占空比為10% (3200 ticks)*/
MAP_Timer_A_generatePWM(TIMER_A0_MODULE, &pwmConfig);
/*程序實例*/
3.相關庫函數的說明
(1)void Timer_A_clearCapureCompareInterrupt( uint32_t timer,
uint_fast16_t captureCompareRegister) //L1047 允許定時中斷
timer:TIMER_A0_MODULE
TIMER_A1_MODULE
TIMER_A2_MODULE
TIMER_A3_MODULE
CaptureCompareRegister:TIMER_A_CAPTURECOMPARE_REGISTER_n (n=0,1,2,3,4,5,6)
Returns None
(2)void Timer_A_clearInterruptFlag(uint32_t timer) //清除定時器TAIFG中斷標志位
timer:略
Returns None
(3)void Timer_A_clearTimer(uint32_t timer)//重啟/清除 定時器的分頻,計數方向等
timer:略
Returns None
(4)void Timer_A_configureContinuousMode(uint32_t timer,
const Timer_A_ContinuousModeConfig *config)//配置定時器A為連續模式
timer:略
Returns None
config: 數據結構如下
/********************************************
typedef struct _Timer_A_ContinuousModeConfig
{
uint_fast16_t clockSource;//時鍾資源,選取哪個時鍾做定時器的來源,同430中的TASSELx
uint_fast16_t clockSourceDivider;//分頻
uint_fast16_t timerInterruptEnable_TAIE;//定時器中斷允許位 0禁1允
uint_fast16_t timerClear;//清除位
}Timer_A_ContinuousModeConfig;
********************************************/
(5)void Timer_A_configureUpDownConfig(uint32_t timer,
const Timer_A_UpModeConfig *config)//增減計數模式的配置
timer:略
Returns None
config:數據結構如下
/********************************************
typedef struct _Timer_A_UpDownModeConfig
{
uint_fast16_t clockSource;//同上
uint_fast16_t clockSourceDivider;
uint_fast16_t timerPeriod;
uint_fast16_t timerInterruptEnable_TAIE;
uint_fast16_t captureCompareInterruptEnable_CCR0_CCIE;
uint_fast16_t timerClear;
}Timer_A_UpDownModeConfig;
*********************************************/
(6)void Timer_A_configureUpMode(uint32_t timer,
const Timer_A_UpModeConfig *config)//增計數模式的配置
timer:略
Returns None
config:數據結構如下
/********************************************
typedef struct _Timer_A_UpModeConfig
{
uint_fast16_t clockSource;//同上
uint_fast16_t clockSourceDivider;
uint_fast16_t timerPeriod;
uint_fast16_t timerInterruptEnable_TAIE;
uint_fast16_t captureCompareInterruptEnable_CCR0_CCIE;
uint_fast16_t timerClear;
}Timer_A_UpModeConfig;
**********************************************/
(7)void Timer_A_disableCaptureCompareInterrupt(uint32_t timer,
uint_fast16_t captureCompareRegister)//
函數部分未完待續---
下面是對官方例程的一個修改,主要產生周期為5ms,P2.4輸出的可調PWM。
例程中有注釋錯誤,具體在應用中也需要注意到很多問題。
例如中斷函數 port1_isr()
官方例程中沒有聲明外部文件使用,而中斷正常工作,需要將中斷函數的接口
引入到msp432_startup_ewarm.c文件中的 __root const uVectorEntry __vector_table[] @ ".intvec" ={};
中。因此在實際應用時會出現一些問題,可私信共同探討。
#include "driverlib.h" #include "delay.h" #include <stdint.h> #include <stdbool.h> int32_t test=0; Timer_A_PWMConfig pwmConfig = { TIMER_A_CLOCKSOURCE_SMCLK, TIMER_A_CLOCKSOURCE_DIVIDER_1, 320, TIMER_A_CAPTURECOMPARE_REGISTER_1, TIMER_A_OUTPUTMODE_RESET_SET, 288 }; //配置PWM輸出參數 int main(void) { MAP_WDT_A_holdTimer(); MAP_CS_setReferenceOscillatorFrequency(CS_REFO_128KHZ); MAP_CS_initClockSignal(CS_MCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1); MAP_CS_initClockSignal(CS_SMCLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_2); MAP_PCM_setPowerState(PCM_AM_LF_VCORE0); MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN4, GPIO_PRIMARY_MODULE_FUNCTION);//配置P2.4為輸出PWM MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN1);//P1.1為按鍵 MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN1); MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN1); MAP_Interrupt_enableInterrupt(INT_PORT1); MAP_Interrupt_enableSleepOnIsrExit(); MAP_Interrupt_enableMaster(); while (1) { MAP_PCM_gotoLPM0(); } } extern void port1_isr(void)//中斷函數 { uint32_t status = MAP_GPIO_getEnabledInterruptStatus(GPIO_PORT_P1); MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, status); if(status & GPIO_PIN1) { MAP_Timer_A_generatePWM(TIMER_A0_MODULE, &pwmConfig); if(pwmConfig.dutyCycle == 288) pwmConfig.dutyCycle = 32; else pwmConfig.dutyCycle += 32; test=pwmConfig.dutyCycle; } }
---恢復內容結束---