近日在做一个用按键来控制pwm的程序,结果发现,一旦把中断按键扫描程序模块加进去,按键就失灵了。而单独编译中断按键扫描程序却是有效的。以下为我的程序。
#include <msp430.h>
#include <driverlib.h>
void PWM();
void TIMER_INIT();
void GPIO_INIT();
void key_scan();
int v1=10;
int v0=2626;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
GPIO_INIT();
TIMER_INIT();
PWM();
__bis_SR_register( GIE);
__no_operation();
}
// Timer1_A3 Interrupt Vector (TAIV) handler
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMER1_A1_VECTOR
__interrupt void TIMER1_A1_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(TIMER1_A1_VECTOR))) TIMER1_A1_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(TA1IV,14))
{
case 0: break; // No interrupt
case 2: break; // CCR1 not used
case 4: break; // CCR2 not used
case 6: break; // reserved
case 8: break; // reserved
case 10: break; // reserved
case 12: break; // reserved
case 14: key_scan(); // overflow
break;
default: break;
}
}
// Timer B0 interrupt service routine
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMERB0_VECTOR
__interrupt void TIMERB0_ISR (void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(TIMERB0_VECTOR))) TIMERB0_ISR (void)
#else
#error Compiler not supported!
#endif
{
// Toggle P1.0 using exclusive-OR
}
void PWM(void)
{
P2DIR |= BIT0+BIT1; // P2.0 and P2.1 output
P2SEL |= BIT0+BIT1; // P2.0 and P2.1 options select
TA1CCR0 = 128; // PWM Period/2
TA1CCTL1 = OUTMOD_7; // CCR1 toggle/set
TA1CCR1 = 32; // CCR1 PWM duty cycle
TA1CCTL2 = OUTMOD_7; // CCR2 toggle/set
TA1CCR2 = 96; // CCR2 PWM duty cycle
TA1CTL = TASSEL_2 + MC_3 + TACLR; // SMCLK, up-down mode, clear TAR
}
void TIMER_INIT(void)
{
TA1CCR0 =50000;
TA1CTL = TASSEL_2 + MC_1 + TACLR + TAIE;
TBCCTL0 = CCIE; // TBCCR0 interrupt enabled
TBCCR0 = 10551;
TBCTL = TBSSEL_2 + MC_1 + TBCLR;
}
void GPIO_INIT(void)
{
GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN0);
GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN2);
GPIO_setAsInputPin(GPIO_PORT_P6,GPIO_PIN0||GPIO_PIN1||GPIO_PIN2||GPIO_PIN3||GPIO_PIN4);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1,GPIO_PIN1);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P2,GPIO_PIN1);
}
#define S1 GPIO_getInputPinValue(GPIO_PORT_P2,GPIO_PIN1)
#define S2 GPIO_getInputPinValue(GPIO_PORT_P1,GPIO_PIN1)
uint8_t work_mode;//0,1
void key_scan(void)
{
static uint8_t i=0;
static uint8_t keycnt=0;
i=i>>2;
i=i+((1^S1)<<2)+((1^S2)<<3);
switch(i&0x03){
case 0x01:if(keycnt==0||keycnt==15){work_mode^=1;PWM();}else if(keycnt>=24)keycnt=14;keycnt++;break;
case 0x02:if(keycnt==0||keycnt==15){work_mode^=1;GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);}else if(keycnt>=24)keycnt=14;keycnt++;break;
case 0x00:keycnt=0;break;
}
}