unsigned int sample1(int channel)

{

switch(channel)

{ case 0: P6SEL |= BIT0; break; //A0

case 1: P6SEL |= BIT1; break; //A1

case 2: P6SEL |= BIT2; break;

case 3: P6SEL |= BIT3; break;

case 4: P6SEL |= BIT4; break;

case 5: P6SEL |= BIT5; break;

case 6: P6SEL |= BIT6; break;

case 7: P6SEL |= BIT7; break; //A7

default:P6SEL |= BIT0;

}

ADC12CTL0 = ADC12ON + SHT0_2 +REF2_5V;//2.5V内部参考电压

ADC12CTL1 = SHP; //采样信号来源采样定时器

ADC12MCTL0 =(unsigned char)channel; //VR+=VREF+,VR-=AVss

ADC12CTL0 |= ENC; //使能

ADC12CTL0 |= ADC12SC; //开始采样转换

while((ADC12IFG & BIT0)==0);

return (unsigned int)(ADC12MEM0);

}

unsigned int sample2(int channel)

{

switch(channel)

{ case 0: P6SEL |= BIT0; break; //A0

case 1: P6SEL |= BIT1; break; //A1

case 2: P6SEL |= BIT2; break;

case 3: P6SEL |= BIT3; break;

case 4: P6SEL |= BIT4; break;

case 5: P6SEL |= BIT5; break;

case 6: P6SEL |= BIT6; break;

case 7: P6SEL |= BIT7; break; //A7

default:P6SEL |= BIT0;

}

ADC12CTL0 = ADC12ON + SHT0_2 +REF2_5V;//2.5V内部参考电压

ADC12CTL1 = SHP; //采样信号来源采样定时器

ADC12MCTL0 =SREF_2+(unsigned char)channel; //Vr+=VeEF+,Vr-=AVss

ADC12CTL0 |= ENC; //使能

ADC12CTL0 |= ADC12SC; //开始采样转换

while((ADC12IFG & BIT0)==0);

return (unsigned int)(ADC12MEM0);

}

#include

#include "ADC12.c"

void main(void)

{

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

P5DIR |= 0x02; // P5.1 output

for (;;)

{

if(sample1(1)<120)

P5OUT &= ~0x02;

else P5OUT |= 0x02;

}

}

// ADC12 interrupt service routine

#pragma vector=ADC12_VECTOR

__interrupt void ADC12_ISR (void)

{

if (ADC12MEM0 < 120)

P5OUT &= ~0x02; // Clear P5.1 LED off

else

P5OUT |= 0x02; // Set P5.1 LED on

_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR)

}

这么久了,还请采纳,俺可是付出的啊

上一篇:

下一篇:

猜你喜欢

mip模板