#include "msp430f149.h" #include"1602.h" #include"ADC12.h" #define num_of_results 8 static unsigned int results[num_of_results]; /*适用于单通道多次采集*/ uchar adc_flag=1; uint AD_Temp; uchar ADBuffer[]={0}; //AD采集显示缓冲区 void ADC12_int()//单通道单次初始化 { P6SEL|=BIT1;//选择采样通道1 ADC12CTL0&=~ENC;//关断ADC ADC12CTL0|=ADC12ON+REF2_5V+REFON+SHT0_8+MSC; //打开AD模块,参考电压2.5V。 ADC12CTL1=CONSEQ_2+SHS_0+ADC12SSEL_0+CSTARTADD0+SHP;//单通道多次模式 ADC12MCTL0=INCH_1+SREF_1;//A1通道 ADC12IE|=0X02;//使能中断 ADC12CTL0|=ENC;//使能ADC }
void Disp_ADResult(void);
void main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; P2DIR=0XFF; //Sysclk_int(); ADC12_int(); //单通道单次初始化 LcdReset(); Delay5ms(); _EINT(); DispStr(0,0,"Voltage:"); ADC12CTL0|=ADC12SC; //启动采样 P2OUT^=BIT0; while(1) { ADC12CTL0|=ADC12SC; //启动采样 } //{Disp_ADResult();} } /**************采集LCD显示函数***************/ void Disp_ADResult(void) { float analog=0.00; analog=AD_Temp*2.5/4095; //analog=2.50;//test ADBuffer[0]=(int)analog*10/10+'0'; ADBuffer[1]='.'; ADBuffer[2]=(int)(analog*10)%10+'0';//Note:此处模运算优先级高于乘,需加括号,if not 会error ADBuffer[3]=(int)(analog*100)%10+'0'; ADBuffer[4]='V'; ADBuffer[5]='\0'; DispStr(9,0,ADBuffer); //Disp1Char(9,0,ADBuffer[2]); }
#pragma vector=ADC_VECTOR __interrupt void ADC12_ISR(void) { uint i; static unsigned int index=0; unsigned long sum = 0; adc_flag=0; results[index]=ADC12MEM0; index=(index+1)%num_of_results; if(index==0) { P2OUT^=BIT2; for(i=0;i<8;i++) { sum+=results; } AD_Temp=sum/8; Disp_ADResult(); // ADC12CTL0&=~ENC;//关断ADC } }
1602模块这里没有给出!
我的这个程序一下载下去显示1.84V.另一块板子显示1.64V,(此时我的P6.1引脚没有给输入值,用万用表测得电压大概0.9v),这里面很少不解,引脚不应该0v嘛,,测出来还这么大差距。另外不能实时的显示电压值,,,望大家给指点指点,新手!
|