/*------------------------------------------
MSP430单片机温度测量源程序
介绍:源程序代码主要包含初始化子程序InitSys()、ADC数据读取子程序Read_ADC()、ADC数据转换为温度子程序ADC_TO_TEM()。
来源:晖创电子网www.schuitron.comMSP430源程序例程
源程序调试OK
--------------------------------------------*/
#include<MSP430x12x2.h>
#defineTEM_ADC_CHL0x0a
voidInitSys(void);
voidRead_ADC(unsignedintINCH_channel);
voidADC_TO_TEM(void);
unsignedintADC_sample_val;
unsignedintlongtemp1,temp2;
unsignedintlongIntDegC;
unsignedintlongIntDegF;
voidmain(void)
{
InitSys();
while(1)
{
Read_ADC(TEM_ADC_CHL);
ADC_TO_TEM();
}
}
voidInitSys(void)
{
//INITSYSTEMCLKS(ACLKANDDCO/MCLK)
WDTCTL=WDTPW+WDTHOLD;//关闭看门狗
BCSCTL1=0x87;//ACLK分频系数是1,内部电阻选7
DCOCTL=0xe0;//设置DCO输出时钟频率5MHz
BCSCTL2=SELM_0;//选择MCLK为DCOCLK,内部电阻
ADC10CTL1|=0x08;//转换时钟为ACLK
ADC10CTL0=SREF_1+ADC10SHT_0+REFOUT+REFON+ADC10ON;
//选择内部电压1.5V,采样时间4XADC10CLKS
}
voidRead_ADC(unsignedintINCH_channel)
{
unsignedinttmp;
ADC10CTL0&=~ENC;
tmp=ADC10CTL1;
tmp&=0x0fff;
tmp|=(INCH_channel<<12);
ADC10CTL1=tmp;
ADC10CTL0|=ENC+ADC10SC;
while(ADC10CTL1&ADC10BUSY)
{}
ADC_sample_val=ADC10MEM;
ADC10CTL0&=~ENC;
}
voidADC_TO_TEM(void)
{
//oC=((A10/1024)*1500mV)-986mV)*1/3.55mV=A10*423/1024-278
temp1=ADC10MEM;
IntDegC=((temp1-673)*423)/1024;
//oF=((A10/1024)*1500mV)-923mV)*1/1.97mV=x*761/1024-468
temp2=ADC10MEM;
IntDegF=((temp2-630)*761)/1024;
}