值班电路 脉冲方波检测

//各个指示灯的说明
P2.0->LED8 ------ 输出20ms的方波信号,输出正确说明AD的采样间隔正确
P2.1->LED7 ------ P6.0的输入小于1.6V就灭,大于1.6V就亮
P2.2->LED6 ------ 检测一组数据的状态:是1就灭,是0就亮
P2.3->LED5 ------ 保留位,指示TIMERA OUT1的输出,相当于采样的时钟
P2.4->LED4 ------

#include 
#include
#define Num_of_Results 200 //测试用的LED8的周期大概是20ms
const int DATA[6]={1,1,0,0,1,1}; //标准状态数组 表示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);

//********************主函数***************************
void main(void)
{
WDTCTL =WDTPW + WDTHOLD; //关闭看门狗

//P1SEL |= (BIT2+BIT6); //测试TAOUT的输出
//P1DIR |= (BIT2+BIT6);
//P2OUT |= 0xf0; //显示LED
//P5SEL |= BIT5+BIT4; //测试系统时钟
//P5DIR |= BIT5+BIT4;
P2DIR = 0xFF;
P2OUT = 0xFF; //最初所有LED都灭

Init_clk();
Init_ADC();
Init_timer();
ADC12CTL0 |= ENC;            //使能转换
_BIS_SR(GIE);                //使能全局中断

while(1)
{
if(1 == nBuf_Flow_Flag)
{
nBuf_Flow_Flag = 0;
Status();//当前这组数据状态为1,即有超过一半的数据大于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的内容清零
//XT2开启,LFTX1工作在低频,ACLK的分频因子为1
do
{
IFG1 &=~OFIFG; //清除OSCFault标志位
for(i=0x20;i>0;i--); //IFG1中断标志寄存器1
}
while((IFG1 & OFIFG)== OFIFG);//如果OSCFault=1

BCSCTL2=0X00;
BCSCTL2 += SELM_2;            //MCLK的时钟源为TX2CLK,分频因子为1  8MHZ
BCSCTL2 += SELS +DIVS_3;      //SMCLK的时钟源为TX2CLK,分频因子为8  1MHZ
return;

}

void Init_ADC(void)
{
P6SEL |= 0x01; // 使能ADC通道P6.0即A0
ADC12CTL0 = ADC12ON+SHT0_1; // 打开ADC,设置采样时间
ADC12CTL1 = SHP+CONSEQ_2+SHS_1; // 使用采样定时器
//ADC12CTL1 = ADC12SSEL_3+ADC12DIV_0; //ADC内核时钟SMCLK 1MHZ
//之前一直出错是因为这一句 为什么啊 仍然没想通 T_T 泪奔....
ADC12MCTL0 = INCH_0;
ADC12IE = 0x0001; // 使能中断
}

void Status(void)
{
nCount = 0;
for(int i=0; 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)
{
//************看当前的值是否大于1.6V***********
if (ADC12MEM0 < 0x7FF)
P2OUT |= BIT1; //小于1.6V就灭,顺时针
else
P2OUT &= ~BIT1;
//****************转移AD数据到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;
}
}

你可能感兴趣的:(值班电路 脉冲方波检测)