//各個(gè)指示燈的說明
P2.0->LED8 ------ 輸出20ms的方波信號(hào)禾进,輸出正確說明AD的采樣間隔正確
P2.1->LED7 ------ P6.0的輸入小于1.6V就滅煎谍,大于1.6V就亮
P2.2->LED6 ------ 檢測一組數(shù)據(jù)的狀態(tài):是1就滅危彩,是0就亮
P2.3->LED5 ------ 保留位瞳脓,指示TIMERA OUT1的輸出缺亮,相當(dāng)于采樣的時(shí)鐘
P2.4->LED4 ------
<pre>#include <msp430x14x.h>
#include <stdlib.h>
#define Num_of_Results 200 //測試用的LED8的周期大概是20ms
const int DATA[6]={1,1,0,0,1,1}; //標(biāo)準(zhǔn)狀態(tài)數(shù)組 表示20ms(高電平),20ms(低電平),20ms(高電平)
int nADC_BUF_Temp[Num_of_Results] = {0};
int nADC_BUF[Num_of_Results] = {0};
int nBuf_Flow_Flag = 0;
int nCount = 0;
int nStatus = 0;
int number = 0;
void Init_clk(void);
void Init_timer(void);
void Init_ADC(void);
void WeakUp(void);
void Status(void);
//********************主函數(shù)***************************
void main(void)
{
WDTCTL =WDTPW + WDTHOLD; //關(guān)閉看門狗
//P1SEL |= (BIT2+BIT6); //測試TAOUT的輸出
//P1DIR |= (BIT2+BIT6);
//P2OUT |= 0xf0; //顯示LED
//P5SEL |= BIT5+BIT4; //測試系統(tǒng)時(shí)鐘
//P5DIR |= BIT5+BIT4;
P2DIR = 0xFF;
P2OUT = 0xFF; //最初所有LED都滅
Init_clk();
Init_ADC();
Init_timer();
ADC12CTL0 |= ENC; //使能轉(zhuǎn)換
_BIS_SR(GIE); //使能全局中斷
while(1)
{
if(1 == nBuf_Flow_Flag)
{
nBuf_Flow_Flag = 0;
Status();//當(dāng)前這組數(shù)據(jù)狀態(tài)為1悔政,即有超過一半的數(shù)據(jù)大于3V晚吞,LED6亮
WeakUp();//喚醒判斷
if(6 == number)
{
number = 0;
P2OUT ^= 0xF0;
}
}
}//while
}//main
void Init_timer(void)
{
P2SEL |= BIT3;
P2DIR |= BIT3;
//CCTL1 = CCIE;
CCR0 = 50;
CCR1 = 25;
TACCTL1 = OUTMOD_3;
TACTL = TASSEL_2 | MC_1 | TACLR; //SMCLK
}
void Init_clk(void)
{
unsigned int i;
BCSCTL1=0X00; //將寄存器BCSCTL1的內(nèi)容清零
//XT2開啟,LFTX1工作在低頻,ACLK的分頻因子為1
do
{
IFG1 &=~OFIFG; //清除OSCFault標(biāo)志位
for(i=0x20;i>0;i--); //IFG1中斷標(biāo)志寄存器1
}
while((IFG1 & OFIFG)== OFIFG);//如果OSCFault=1
BCSCTL2=0X00;
BCSCTL2 += SELM_2; //MCLK的時(shí)鐘源為TX2CLK,分頻因子為1 8MHZ
BCSCTL2 += SELS +DIVS_3; //SMCLK的時(shí)鐘源為TX2CLK,分頻因子為8 1MHZ
return;
}
void Init_ADC(void)
{
P6SEL |= 0x01; // 使能ADC通道P6.0即A0
ADC12CTL0 = ADC12ON+SHT0_1; // 打開ADC,設(shè)置采樣時(shí)間
ADC12CTL1 = SHP+CONSEQ_2+SHS_1; // 使用采樣定時(shí)器
//ADC12CTL1 = ADC12SSEL_3+ADC12DIV_0; //ADC內(nèi)核時(shí)鐘SMCLK 1MHZ
//之前一直出錯(cuò)是因?yàn)檫@一句 為什么啊 仍然沒想通 T_T 淚奔....
ADC12MCTL0 = INCH_0;
ADC12IE = 0x0001; // 使能中斷
}
void Status(void)
{
nCount = 0;
for(int i=0; i<Num_of_Results; i++)
{
if(nADC_BUF[i]>3722)
nCount ++;
}
if(nCount > Num_of_Results/2)
{
nStatus = 1;
P2OUT |= BIT2;//LED 滅
}
else
{
P2OUT &= ~BIT2;
nStatus = 0;
}
}
void WeakUp(void)
{
switch (number)
{
case 0:
if(nStatus == DATA[0])
number = 1;
else
number = 0;
break;
case 1:
if(nStatus == DATA[1])
number = 2;
else
number = 0;
break;
case 2:
if(nStatus == DATA[2])
number = 3;
else
number = 2;
break;
case 3:
if(nStatus == DATA[3])
number = 4;
else
number = 1;
break;
case 4:
if(nStatus == DATA[4])
number = 5;
else
number = 0;
break;
case 5:
if(nStatus == DATA[5])
number = 6;
else
number = 1;
break;
}
}
pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
//************看當(dāng)前的值是否大于1.6V***********
if (ADC12MEM0 < 0x7FF)
P2OUT |= BIT1; //小于1.6V就滅谋国,順時(shí)針
else
P2OUT &= ~BIT1;
//****************轉(zhuǎn)移AD數(shù)據(jù)到BUF*************
static unsigned int index = 0;
nADC_BUF_Temp[index++] = ADC12MEM0;
if (index == Num_of_Results)
{
index = 0;
nBuf_Flow_Flag = 1;
for(int i = 0; i< Num_of_Results; i++)
nADC_BUF[i] = nADC_BUF_Temp[i];
P2OUT ^= BIT0;
}
}</pre>