|
回复 板凳yangwang87 的帖子
#include
char s[6];
void clockinit()
{
BCSCTL1=CALBC1_8MHZ; //设置DCO为8MHz,MCLK认为DCO
DCOCTL=CALDCO_8MHZ;
BCSCTL2|=DIVS_3+DIVM_3; //SMCLK为1MHz
}
void adc_init()
{
//P1SEL=BIT4;
//ADC10CTL0=ADC10ON+SREF_2+ADC10SHT_3+MSC+ADC10IE; //打开ADC,选择外部参考电压,采样周期64*4*Tadc10clk
ADC10CTL0=ADC10ON+SREF_1+ADC10SHT_3+MSC+ADC10IE+REFON+REF2_5V;
ADC10CTL1=CONSEQ_2+INCH_5+ADC10SSEL_3+ADC10DIV_0; //单通道多次转换,A5模拟输入,SMclk为AD时钟源
ADC10AE0|=BIT5; //使P1.5允许AD模拟输入信号
}
void usart_init()
{
P1DIR&=~BIT1;
P1DIR|=BIT2;
P1SEL=BIT1+BIT2;
P1SEL2=BIT1+BIT2;
UCA0CTL1|=UCSSEL_2; //波特率源时钟SMCLK1M
UCA0BR0=104; //波特率9600
UCA0BR1=0;
UCA0MCTL=UCBRS0;
UCA0CTL1&=~UCSWRST;
}
void main()
{
WDTCTL=WDTPW+WDTHOLD;
clockinit();
adc_init();
usart_init();
ADC10CTL0|=ENC+ADC10SC;
_bis_SR_register(LPM0_bits+GIE);
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10(void)
{
unsigned int x=ADC10MEM;
int a,b,c;
a=x/100;
b=(x/10)%10;
c=x%10;
s[0]='A';
s[1]=a;
s[2]=b;
s[3]=c;
s[4]='\n';
s[5]='\0';
for(i=0;i<6;i++)
{
while(!(IFG2&UCA0TXIFG)); //判断发送是否完成
UCA0TXBUF = s;
}
}
ADC10CTL0&=~ADC10IFG;
} |
|