代码一:
1 #include <msp430x14x.h> 2 int flag = 0; 3 void int_clk() 4 { 5 BCSCTL1 &= ~XT2OFF; 6 BCSCTL2 |= SELM_2 + SELS; 7 do 8 { 9 IFG1 &= ~OFIFG; 10 for(int i=0;i<100;i++) 11 _NOP(); 12 }while((IFG1 & OFIFG)!=0); 13 IFG1 &= ~OFIFG; 14 } 15 16 void int_COMPARATORA() 17 { 18 P2SEL |= BIT3 + BIT4; //P2.3,P2.4分别作为比较器的输出和输入 19 P2DIR |= BIT3; //P2.3作为输出 20 21 CACTL1 &= ~CAEX; 22 CACTL1 |= CARSEL + CAREF_2 + CAON + CAIES + CAIE; 23 CACTL2 |= CAF + P2CA1; //使能输出滤波器,CA1作为比较器的输入功能 24 } 25 #pragma vector=COMPARATORA_VECTOR 26 __interrupt void COMPARATORA(void) 27 { 28 flag = 1; 29 } 30 31 void main() 32 { 33 WDTCTL = WDTPW + WDTHOLD; 34 P6DIR = 0XFF; 35 P6OUT = 0X00; 36 int_clk(); 37 int_COMPARATORA(); 38 _EINT(); 39 while(1) 40 { 41 while(flag) 42 { 43 P6OUT ^= 0XFF; 44 flag = 0; 45 } 46 } 47 }
代码二:
1 #include <msp430x14x.h> 2 void main() 3 { 4 WDTCTL = WDTPW + WDTHOLD; 5 P2SEL |= BIT3; 6 P1DIR |= BIT2; 7 P1OUT &= ~BIT2; 8 9 CACTL1 = CAEX + CARSEL + CAREF_2 + CAON; 10 CACTL2 = CAF + P2CA1; 11 12 while(1) 13 { 14 if(CAOUT==0) 15 P6OUT = BIT0; 16 else 17 P6OUT = BIT7; 18 } 19 }
时间: 2024-10-05 05:50:33