//******************************************************************************
// LaunchPad Lab4 - Timer Toggle P1.6,
//
// MSP430G2452
// -----------------
// /|\\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.6|-->LED
CALBC1_1MHZ和CALDCO_1MHZ的值是存储在MSP430的FLASH中的,
所以在设置DCO的时候可以直接使用;
FLASH擦除的效果是所有的值全部变为1。
这段代码的作用是检查CALBC1_1MHZ和CALDCO_1MHZ的值是不是全0xFF,
如果是的话说明这段FLASH被擦除了,
当然值就是错误的,这个时候设置的DCO频率也肯定错误了,就让CPU进入死循环,
不再执行代码
//
//******************************************************************************
#include
unsigned int count=0,flag=0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased, trap CPU!!
}
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO
IFG1 &= ~OFIFG; // Clear OSCFault flag
BCSCTL1 |= DIVA_0; // ACLK = VLO/8
BCSCTL2 |= SELM_0 + DIVM_0 + DIVS_0; // MCLK = DCO/8, SMCLK = DCO/8
P1DIR =0xf9 ; // Set P1.1 P1.2to input direction
P1OUT = 0; // LED off
P1IES = 0x00; // P1.1 P1.2Hi/lo edge
P1IFG = 0x00; // P1.1,P1.2 IFG cleared
P1IE = 0x06; // P1.1 P1.2interrupt enabled
// Configure TimerA
TACTL = TASSEL_1 + MC_1 + TAIE; // Source: ACLK, UP mode
CCR0 = 21; //Timer count 21近似2MS
CCR1 = 10; //Timer count 10
CCTL0 = CCIE; //CCR0 interrupt enabled
CCTL1 = CCIE; //CCR1 interrupt enabled
_BIS_SR(GIE);
for(;;);
}
// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{ count++;
if(count>500&&flag==0)
{ count=0;
CCTL0 = 0; //CCR0 关
CCTL1 = 0; //CCR1 关
}
P1OUT |= BIT6; // P1.6 output High
}
// Timer A1 Interrupt Vector (TA0IV) handler
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A1(void)
{
switch( TA0IV )
{
case 2: P1OUT &= ~BIT6; // P1.6 output Low
break;
case 10:
break;
}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
{
if (P1IFG & BIT1)
{
flag=0;
CCTL0 = CCIE; //CCR0 interrupt enabled
CCTL1 = CCIE; //CCR1 interrupt enabled
P1IFG &= ~BIT1; // P1.1 IFG cleared
}
}
if (P1IFG & BIT2)
{
flag=1;
CCTL0 = CCIE; //CCR0 interrupt enabled
CCTL1 = CCIE; //CCR1 interrupt enabled
P1IFG &= ~BIT2; // P1.2 IFG cleared
}
}