使用的是数码管显示相应的电压
#include "delay.h"
#define Num_of_Results 32
uint results[Num_of_Results];
uchar d[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
uchar d1[11]={0xbf,0x86,0xdb,0xcf,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
uchar a[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
void main()
{
// Stop watchdog timer to prevent time out reset
uchar j;
WDTCTL = WDTPW + WDTHOLD;
//使用外部高频晶体振荡器
BCSCTL1&=~XT2OFF;
do{
IFG1&=~OFIFG;
for(j=0XFF;j>0;j--);
}while((IFG1&OFIFG));
BCSCTL2|=SELM_2;
P6DIR&=~(BIT0+BIT1);
P6SEL |= BIT0+BIT1; // 使能ADC通道
ADC12CTL0 = ADC12ON+SHT0_8+MSC;
// ADC12ON打开ADC,
//SHT0_8设置采样时间,合适即可
//MSC设置多次采样
ADC12CTL1 = CSTARTADD_0+CSTARTADD_1+SHP+CONSEQ_3;
// CSTARTADD_3让采样结果保存在ADC12MEM3中
//CONSEQ_2单通道多次转换
//SHP使用采样定时器
ADC12IE = BIT0+BIT1;// 中断允许,IFG置位后会进入中断服务程序
ADC12MCTL0=INCH_0;//通道A1 默认SERF为0,即参考0-AVCC
ADC12MCTL1=INCH_1;//通道A1 默认SERF为0,即参考0-AVCC
ADC12CTL0 |= ENC+ADC12SC; // 使能转换并且开始转换
ADC12IFG=0;
_EINT();//打开中断
while(1);
}
//ADC中断服务函数,在这里用多次平均的
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
static uint index = 0;
uint i,j;
uchar ptr[4];
P4DIR=0XFF;//p4作为输出
P5DIR=0XFF;
P6DIR=0XFF;
results[index++] = ADC12MEM0+ADC12MEM1; // 必须和CSTARTADD_3一样
if(index == Num_of_Results)
{
unsigned long sum = 0,real;
index = 0;
for(i = 0; i < Num_of_Results; i++)
{
sum += results;
}
sum >>= 5; //除以32求得平均值
real=3300*sum/4095;//扩大1000倍
ptr[3] = real / 1000;
ptr[2] = (real - ptr[3]*1000)/100;
ptr[1] = (real - ptr[3]*1000 - ptr[2]*100)/10;
ptr[0] = (real - ptr[3]*1000 - ptr[2]*100 - ptr[1]*10);
for(i=0;i<4;i++)
{
P4OUT=d[10];
P5OUT|=BIT5;//设置p5.5为高电平
P4OUT=a;
P5OUT^=BIT5;//设置p5.5为低电平
P6OUT|=BIT6;
if(i==3)
P4OUT=d1[ptr];
else
P4OUT=d[ptr];
for(j=0;j<1000;j++);
P4OUT=d[10];
P6OUT^=BIT6;
}
}
ADC12IFG=0;
}