我用的是msp430f149型,想要用A5通道测外部电压,并在1602上显示出来,但程序烧进去后A5通道还未连接任何待测器件时1602上就显示了1.87V的电压。当我依次测十二个电阻串联时的电压时 前两个还是准的可约到后面测的越不准了,电压变化越来越小。这是为什么啊?请教高手。下面是源程序。
#include"msp430x14x.h" #include"1602.h" #define Num_of_Results 32
uchar shuzi[] = {"0123456789."}; uchar dianya[] = {"volt : V"}; uchar gaodu[] = {"Height : cm"};
static uint results[Num_of_Results]; //保存ADC转换结果的数组 // is not used for anything. void Trans_val(uint Hex_Val);
/************************主函数****************************/ void main(void) { unsigned char i; WDTCTL = WDTPW+WDTHOLD; //关闭看门狗 lcd_rest(); //初始化1602液晶 dispnchar(1,0,dianya); //显示电压信息 dispnchar(1,1,gaodu); //显示高度 P6DIR = 0Xff; P6SEL |= BIT5; // 使能ADC通道 P5DIR =0Xe0; P5OUT=0Xff; ADC12CTL0 = ADC12ON+SHT0_8+MSC; // 打开ADC,设置采样时间,自动采样 ADC12CTL1 = SHP+CONSEQ_2; // 使用采样定时器 ADC12MCTL0 = INCH_5; ADC12IE = 0X01; // 使能ADC中断 ADC12CTL0 |= ENC; // 使能转换 ADC12CTL0 |= ADC12SC; // 开始 do { IFG1 &=~OFIFG; for(i=0xff;i>0;i--) ; } while((IFG1&OFIFG)!=0); BCSCTL2 |= SELM_3 + DIVA_3; _BIS_SR(LPM3_bits+GIE);
}
/******************************************* 函数名称:ADC12ISR 功 能:ADC中断服务函数,在这里用多次平均的 计算P6.5口的模拟电压数值 参 数:无 返回值 :无 ********************************************/ #pragma vector=ADC_VECTOR __interrupt void ADC12ISR (void) { static uint index = 0;
results[index++] = ADC12MEM0; // Move results if(index == Num_of_Results) { uchar i; unsigned long sum = 0;
index = 0; for(i = 0; i < Num_of_Results; i++) { sum += results; } sum >>= 5; //除以32 Trans_val(sum); } }
/******************************************* 函数名称:Trans_val 功 能:将16进制ADC转换数据变换成三位10进制 真实的模拟电压数据,并在液晶上显示 参 数:Hex_Val--16进制数据 n--变换时的分母等于2的n次方 返回值 :无 ************************************************/ void Trans_val(uint Hex_Val) { unsigned long caltmp; uint Curr_Volt; static uint t1,i,y; uchar ptr[4]; caltmp = Hex_Val; caltmp = (caltmp << 5) + Hex_Val; //caltmp = Hex_Val * 33 caltmp = (caltmp << 3) + (caltmp << 1); //caltmp = caltmp * 10 Curr_Volt = caltmp >> 12; //Curr_Volt = caltmp / 2^n ptr[0] = Curr_Volt / 100; //Hex->Dec变换 t1 = Curr_Volt - (ptr[0] * 100); ptr[2] = t1 / 10; ptr[3] = t1 - (ptr[2] * 10); ptr[1] = 10; //shuzi表中第10位对应符号"." //在液晶上显示变换后的结果 for(i = 0;i < 4;i++) displchar((9+ i),0,shuzi[ptr]); if(Curr_Volt>0) y=0; if(Curr_Volt>24) y=1; if(Curr_Volt>48) y=2; if(Curr_Volt>72) y=4; if(Curr_Volt>96) y=5; if(Curr_Volt>120) y=6; if(Curr_Volt>144) y=7; if(Curr_Volt>168) y=8; if(Curr_Volt>192) y=9; if(Curr_Volt>216) y=10; if(Curr_Volt>240) y=11; if(Curr_Volt>264) y=12; switch (y) { case 0: {displchar(10,1,'0') ;}/*displchar(11,1,'0');};*/ break; case 1: {displchar(10,1,'1') ;}/*displchar(11,1,'5');};*/ break; case 2: {displchar(10,1,'2') ;}/*displchar(11,1,'0');};*/ break; case 3: {displchar(10,1,'3') ;}/*displchar(11,1,'5');};*/ break; case 4: {displchar(10,1,'4') ;}/*displchar(11,1,'0');};*/ break; case 5: {displchar(10,1,'5') ;}/*displchar(11,1,'5');};*/ break; case 6: {displchar(10,1,'6') ;}/*displchar(11,1,'0');};*/ break; case 7: {displchar(10,1,'7') ;}/*displchar(11,1,'5');};*/ break; case 8: {displchar(10,1,'8') ;}/*displchar(11,1,'0');};*/ break; case 9: {displchar(10,1,'9') ;}/*displchar(11,1,'5');};*/ break; case 10:{displchar(10,1,'1') ; displchar(11,1,'0');}; break; case 11:{displchar(10,1,'1') ;displchar(11,1,'1');}; break; case 12:{displchar(10,1,'1') ;displchar(11,1,'2');}; break; } }
[ 本帖最后由 卖力的小苦工 于 2012-2-2 13:05 编辑 ]
|