那天写了一个ADC转换的问题,通过A0通道采样。把P1.0设置为输入,但ADC10CTL0 |= ADC10SC;这条语句一执行。P1.0就有电压了,本来应该是0伏特。如果把那条语句屏蔽了,P1.0就是0V。给P1.0配置下拉电阻,不采集的时候是0,但采集时误差太大了。各位大神能帮我看下问题出在哪儿了吗?谢谢!程序代码如下:
#include <msp430g2553.h>
#define uint unsigned int
#define uchar unsigned char
#define Num_of_Results 32
static uint results[Num_of_Results]; /*宏定义各端口,RS(CS)串行通信片选,SID(R/W)串行数据口,SCLK(E)串行同步时钟信号*/
#define RS_0 P2OUT &= ~BIT0 //RS = 0
#define RS_1 P2OUT |= BIT0 //RS = 1
#define SID_0 P2OUT &= ~BIT1 //SID = 0
#define SID_1 P2OUT |= BIT1 //SID = 1
#define SCLK_0 P2OUT &= ~BIT2 //SCLK = 0
#define SCLK_1 P2OUT |= BIT2 //SCLK = 1
uchar m;
unsigned long temp, IntDegF, IntDeg;
void ADC10_init()
{
ADC10CTL0 &= ~ENC;
ADC10CTL0 = ADC10IE+ADC10ON+ADC10SHT_2+MSC+SREF_2; //ADC10ON开启10ADC
ADC10CTL1 = CONSEQ_2+INCH_0
; ADC10AE0 |= 0X03;
ADC10SA = 0X200;
__delay_cycles(30000);
ADC10CTL0 |= ENC;
ADC10CTL0 |= ADC10SC;
ADC10CTL0 &= ~ADC10IFG;
_EINT();
//LPM0;
}
/*--------------------------------------------
delay_1ms(): 延时函数
输入参数:无
编写日期:20140722
---------------------------------------------*/
void delay_1ms()
{
uint k;
for(k=0; k<195; k++);
}
/*--------------------------------------------
delay(uint n): 延时函数,延时大约1ms
输入参数:n 延时nms
编写日期:20140722
---------------------------------------------*/
void delay1ms(uint n)
{ uint j; for(j=0; j<n; j++) delay_1ms(); }
/*写指令函数*/
void write_cmd(uchar cmd)
{
uchar j;
j = 0xF8;
RS_1; //RS=1
SCLK_0; //SCLK=0(E)
for(m=0; m<8; m++)
{
if(j&0x80) //判断最高位是否是为1,如果是则给SDIN赋1即写入1,否则写0
SID_1; //SID(RW)
else SID_0; //最高位不为1,写0
SCLK_0; //让SCLK产生下降沿,使液晶采样
delay1ms(1);
SCLK_1; //将SCLK拉高,为一位数据采样做准备
j=j<<1; //因为先发送高位,所以每次向左移动一位
}
j = cmd;
j &= 0xF0;
for(m=0; m<8; m++)
{
if(j&0x80) //判断最高位是否是为1,如果是则给SDIN赋1即写入1,否则写0
SID_1;
else
SID_0; //最高位不为1,写0
SCLK_0; //让SCLK产生下降沿,使液晶采样
delay1ms(1);
SCLK_1; //将SCLK拉高,为一位数据采样做准备
j=j<<1; //因为先发送高位,所以每次向左移动一位
}
j = cmd;
j <<= 4;
for(m=0; m<8; m++)
{
if(j&0x80) //判断最高位是否是为1,如果是则给SDIN赋1即写入1,否则写0
SID_1;
else
SID_0; //最高位不为1,写0
SCLK_0; //让SCLK产生下降沿,使液晶采样
delay1ms(1);
SCLK_1; //将SCLK拉高,为一位数据采样做准备
j=j<<1; //因为先发送高位,所以每次向左移动一位
}
RS_0;
delay1ms(5);
}
/*写显示数据函数*/
void write_data(uchar dat)
{
uchar j;
j = 0xFA;
RS_1;
for(m=0; m<8; m++)
{
if(j&0x80) //判断最高位是否是为1,如果是则给SDIN赋1即写入1,否则写0
SID_1;
else
SID_0; //最高位不为1,写0
SCLK_0; //让SCLK产生下降沿,使液晶采样
delay1ms(1);
SCLK_1; //将SCLK拉高,为一位数据采样做准备
j=j<<1; //因为先发送高位,所以每次向左移动一位
}
j = dat;
j &= 0xF0;
for(m=0; m<8; m++)
{
if(j&0x80) //判断最高位是否是为1,如果是则给SDIN赋1即写入1,否则写0
SID_1;
else
SID_0; //最高位不为1,写0
SCLK_0; //让SCLK产生下降沿,使液晶采样
delay1ms(1);
SCLK_1; //将SCLK拉高,为一位数据采样做准备
j=j<<1; //因为先发送高位,所以每次向左移动一位
}
j = dat;
j <<= 4;
for(m=0; m<8; m++)
{
if(j&0x80) //判断最高位是否是为1,如果是则给SDIN赋1即写入1,否则写0
SID_1;
else
SID_0; //最高位不为1,写0
SCLK_0; //让SCLK产生下降沿,使液晶采样
delay1ms(1);
SCLK_1; //将SCLK拉高,为一位数据采样做准备
j=j<<1; //因为先发送高位,所以每次向左移动一位
}
RS_0;
delay1ms(5); }
/*设定显示位置*/
void lcd_pos(uchar X, uchar Y)
{
uchar pos;
if(X == 0)
X = 0x80;
else if(X == 1)
X = 0X90;
else if(X == 2)
X = 0X88;
else if(X == 3)
X = 0X98;
pos = X+Y;
write_cmd(pos); //显示地址
}
/*LCD初始化设定*/
void lcd_init()
{
//P1OUT &= ~BIT3;
//PSB = 1;并口方式
write_cmd(0x30); //基本指令操作
delay1ms(5);
write_cmd(0x0c); //显示开,关光标
delay1ms(5);
write_cmd(0x01); //清除LCD的显示类容
delay1ms(5); }
/*--------------------------------------------
Init_Port(): MSP430G2553端口初始化函数
输入参数:无
编写日期:20140722
---------------------------------------------*/
void Init_Port()
{
P1DIR = 0; //将P1口所有的管脚在初始化的时候设置为输入方式
P2DIR = 0; //将P2口所有的管脚在初始化的时候设置为输入方式
//P1REN |= BIT0;
//P1OUT &= ~BIT0
; P1SEL |= BIT0; //将P1口所有的管脚设置为一般I/O口
P1DIR &= ~BIT0;
P1DIR |= BIT4;
P2SEL = 0X00;
P2IE = 0X00;
P2DIR = 0XFF; //将P2口所有的管脚在初始化的时候设置为输入方式
}
void display(long date)
{
uchar ge, shi, bai, qian;
qian=date/1000;
bai=date%1000/100;
shi=date%1000%100/10;
ge=date%1000%100%10;
lcd_pos(0,2);
write_data(0x30+qian);
write_data(0x30+bai);
lcd_pos(0,3);
write_data(0x30+shi);
write_data(0x30+ge);
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ;
Init_Port();
lcd_init();
ADC10_init();
while(1);
}
#pragma vector = ADC10_VECTOR
__interrupt void ADC10ISR()
{
uchar i;
P1DIR |= BIT4;
P2DIR |= BIT0+BIT1+BIT2;
static uint index = 0;
results[index++] = ADC10MEM;
if(index >= Num_of_Results)
{
unsigned long sum = 0;
index = 0;
for(i = 0; i < Num_of_Results; i++)
{
sum += results[i];
}
sum >>= 5; //除以32
temp = 3300*sum/1023;
display(temp);
}
}