根据下面的代码、目标是根据计时器配置函数串联产生蜂鸣声。 第一个值是计时器周期、然后是占空比、第三个值是要产生的蜂鸣声数量。
因此、根据现有函数、将计时器周期设置为32768、占空比为16384、蜂鸣次数为4。 但代码在我的 MSP430F5529 LP 上没有按预期运行。
#include <driverlib.h>
//Tested on MSP430F5529LP
int tCount; dutyCycle;
int beepCount; int counter; int newDutyCycle;
int countval = 0; int
void init();
int timer_config(int newPeriod, int newDutyCycle, int beep);
void stop_timer();
void main(){
init();
timer_config(32768,16384, 4); //Configure timer
while(1){
}
__bis_SR_register(LPM0 + GIE); //Enter Low Power Mode
__no_operation(); //Enter Debugger Mode
}
void init() {
WDT_A_hold(WDT_A_BASE); //Stop Watchdog Timer
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN0); //Set Pin 2.0 as output pin
//Configure Timer
Timer_A_initUpModeParam param = {0};
param.clockSource = TIMER_A_CLOCKSOURCE_ACLK; // ~1045000Hz
param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1; //~18660Hz
param.timerPeriod = 32768;
param.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
param.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;
param.timerClear = TIMER_A_DO_CLEAR;
param.startTimer = false;
Timer_A_initUpMode(TIMER_A1_BASE, ¶m);
//Configure Capture Compare Register
Timer_A_initCompareModeParam paramCCR1 = {0};
paramCCR1.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
paramCCR1.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
paramCCR1.compareOutputMode = TIMER_A_OUTPUTMODE_RESET_SET;
paramCCR1.compareValue = 16384;
Timer_A_initCompareMode(TIMER_A1_BASE, ¶mCCR1);
}
int timer_config(int newPeriod, int newDutyCycle, int beep){
Timer_A_setCompareValue(TIMER_A1_BASE,
TIMER_A_CAPTURECOMPARE_REGISTER_0,
newPeriod);
Timer_A_setCompareValue(TIMER_A1_BASE,
TIMER_A_CAPTURECOMPARE_REGISTER_1,
newDutyCycle);
Timer_A_clear(TIMER_A1_BASE);
Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
if (countval==beep)
{
countval==0;
stop_timer();
}
}
void stop_timer(){
Timer_A_stop(TIMER_A1_BASE);
Timer_A_disableInterrupt(TIMER_A1_BASE);
}
#pragma vector = TIMER1_A0_VECTOR
__interrupt void timer1_ISR(void) {
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
Timer_A_clearTimerInterrupt(TIMER_A1_BASE);
}
#pragma vector = TIMER1_A1_VECTOR
__interrupt void timer0_ISR(void) {
switch(__even_in_range(TA0IV, TA0IV_TAIFG)) {
case TA0IV_NONE: break;
case TA0IV_TACCR1:
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0);
countval++;
Timer_A_clearTimerInterrupt(TIMER_A1_BASE);
break;
case TA0IV_TACCR2: break;
case TA0IV_TACCR3: break;
case TA0IV_TACCR4: break;
case TA0IV_5: break;
case TA0IV_6: break;
case TA0IV_TAIFG: break;
default: _never_executed();
}
}
