用的是TI的launchpad,ADC10用使用ADC10双通道采样,P1.0(A0)处采样和实际电压差别不大,不过P1.1(A1)处的却十分不准确,请问是什么原因?找半天没找出所以然来
代码如下:
#include "MSP430G2553.h"
#include "lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
uint a[11]={0},num; //定义数组用于储存采样值
uint ge=0,shifen=0,baifen=0,ge1=0,shifen1=0,baifen1=0;
long P10=0,P11=0,P10_V=0,P11_V=0; //定义变量
uchar table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //0~9
void ADC10_init(); //声明ADC10初始化函数
void display(); //显示函数
void main()
{ WDTCTL = WDTPW + WDTHOLD; //关狗
IO_Init(); //lcd1602相关IO口初始化
LCD_Init(); //lcd1602初始化
ADC10_init(); //初始化ADC10
while(1)
{
ADC10CTL0 &=~ ENC; //禁用ADC10的Conversion
while(ADC10CTL1 & ADC10BUSY); //测忙
ADC10SA= (unsigned int)a; //获取a[]的首地址
ADC10CTL0 |= ADC10SC + ENC; //启动ADC10
_bis_SR_register(CPUOFF+GIE); //低功耗模式0,开中断
P10=a[1]+a[3]+a[5]+a[7]; //将A0采样值相加
P11=a[0]+a[2]+a[4]+a[6]; //将A1采样值相加
P10 = P10/4.0; //求平均值
P11 = P11/4.0;
P10_V = P10*250/1023; //转化为电压并乘以100
P11_V = P11*250/1023;
display(); //显示
}
}
void display()
{ uint P10_int,P11_int;
P10_int=(uint)P10_V;
P11_int=(uint)P11_V;
/*求A0采样的各个位*/
ge = P10_int/100; //求个位
shifen =(P10_int%100)/10; //求十分位
baifen =(P10_int%100)%10; //求百分位
/*求A1采样的各个位*/
ge1 =P11_int/100;
shifen1 =(P11_int%100)/10;
baifen1 =(P11_int%100)%10;
/*显示A0处的值*/
LCD_Write_Command(0x82);
LCD_Write_Data(table[ge]);
LCD_Write_Data('.');
LCD_Write_Data(table[shifen]);
LCD_Write_Data(table[baifen]);
LCD_Write_Data('V');
/*显示A1处的值*/
LCD_Write_Command(0xC2);
LCD_Write_Data(table[ge1]);
LCD_Write_Data('.');
LCD_Write_Data(table[shifen1]);
LCD_Write_Data(table[baifen1]);
LCD_Write_Data('V');
}
void ADC10_init()
{
/* ADC中断使能+打开ADC10+打开基准生成电压+采样保持时间16*ADC10CLK+(VREF+ and VR-=AVS)+ADC10 multi+2.5V */
ADC10CTL0 = ADC10IE + ADC10ON + REFON + ADC10SHT_2 + SREF_1 + MSC + REF2_5V;
/* 重复序列转换+时钟源选择ADC10OSC+0分频+选择channel1 */
ADC10CTL1 = CONSEQ_3 + + ADC10DIV_0 + INCH_1;
ADC10AE0 |=BIT0+BIT1; //打开模拟输入通道
ADC10DTC1 |= 0X08; //采样数为8
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); //唤醒CPU
}