#include <msp430.h>
#include <stdlib.h>
#define uchar unsigned char
#define uint unsigned int
long uint ADC_CH0;
uint VOL[41]={0x0000,0xFF78,0xFE89,0xFD9D,0xFCB0,0xFBC9,0xFADD,0xF9F7,0xF96B,0xF87F,0xF79A,
0xF6AE,0xEFCA,0xEEDF,0xEDFB,0xED77,0xEC8D,0xEBA9,0xEAC0,0xE9DD,0xE8FA,
0xE878,0xE78F,0xE6AD,0xDFCC,0xDEEA,0xDE69,0xDD88,0xDCA7,0xDBC7,0xDAE7,
0xDA67,0xD987,0xD8A7,0xD7C8,0xD6E9,0xD66A,0xCF8C,0xCEAE,0xCDD0,0xCCF8};//0~41度温度对应的AD值
/*long int VOL[41]={0X000,-0X57.E7,-0xB1.2F,-0x106.D,-0x15D.E,-0x1B4.B,-0x20B.3,-0x261.6,
-0x2B7.5,-0x30D.1,-0x362.7,-0x3B7.9,-0x40C.7,-0x461.0,-0x4B5.6,-0x509.7,-0x55D.4,-0x5B0.C,-0x604.1,
-0x657.1,-0x6A9.D,-0x6FC.5,-0x74E.9,-0x7A0.8,-0x7F2.4,-0x843.C,-0x894.F,-0x8E5.F,-0x936.A,
-0x987.2,-0x9D7.5,-0xA27.5,-0xA77.0,-0xAC6.8,-0xB15.C,-0xB64.C,-0xBB3.8,-0xC02.1,-0xC50.5,-0xC9E.5,-0xCEC.2,
};*/
void delay(long uint y)
{
while(y!= 0x00)
{
y--;
}
}
void WriteCommandLcd(unsigned char wdata)//向1602液晶写入命令子程序;
{
P5OUT&=~0x10;//RS清零,代表命令;
P5OUT&=~0x08;//RW清零;
P5OUT&=~0x04;//使能E清零;
delay(1000); //短暂延时,代替检测忙状态
P5OUT|= 0x04;//使能E置1;
P2OUT=wdata;//送出命令或数据
delay(1000);//短暂延时,代替检测忙状态
P5OUT&=~0x04;//使能E清零,下降沿将命令或数据送入1602;
}
void WriteDataLcd(unsigned char wdata)//向1602液晶写入数据子程序;
{
P5OUT|= 0x10;//RS置1,代表数据;
P5OUT&=~0x08;//RW清零;
P5OUT&=~0x04;//使能E清零;
delay(1000); //短暂延时,代替检测忙状态
P5OUT|= 0x04;//使能E置1;
P2OUT=wdata;//送出命令或数据
delay(1000);//短暂延时,代替检测忙状态
P5OUT&=~0x04;//使能E清零,下降沿将命令或数据送入1602;
}
void lcd_init(void)
{
P2OUT = 0;
delay(1000);
WriteCommandLcd(0x38);//显示模式设置,设置为16X2显示,5X7点阵,8位数据接口
delay(5000);
WriteCommandLcd(0x38); //显示模式设置,设置为16X2显示,5X7点阵,8位数据接口
delay(5000);
WriteCommandLcd(0x38); //显示模式设置,设置为16X2显示,5X7点阵,8位数据接口
delay(5000);
WriteCommandLcd(0x01); //显示清屏
WriteCommandLcd(0x0f); //开显示,显示光标,光标闪烁;
WriteCommandLcd(0x06); //读写操作时地址自动加1
}
int main(void)
{
volatile unsigned int i,j,flag=0; // Use volatile to prevent removal
// by compiler optimization
uint result,temp1;
P1DIR = 0Xff;//设置P1口为输出状态;
P2DIR = 0xff; //设置P2口为输出状态;
P5DIR = 0xff; //设置P5口为输出状态;
P6DIR = 0xff; //设置P5口为输出状态;
P6SEL= 0X03;//选择A0+,A0-通道
P5OUT &= ~0x20;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
FLL_CTL0 |= XCAP14PF; // Configure load caps
for (i =0; i < 10000; i++); // Delay for 32 kHz crystal to
// stabilize
P2OUT = 0xff; //向P2输出数据;
lcd_init(); //初始化子程序;
SD16CTL = SD16REFON+SD16SSEL0; // 1.2V ref, SMCLK,
SD16CCTL0 |= SD16SNGL+SD16IE+SD16IFG ; // Single conv, enable interrupt,256次采样率
SD16INCTL0 |= SD16INTDLY_0+SD16INCH_0; // Interrupt on 4th sample
//SD16AE|=SD16AE0; //analog enable
for (i = 0X0000; i < 0x3600; i++); // Delay for 1.2V ref startup
__enable_interrupt(); // Enable general interrupts
while (1)
{
SD16CCTL0 |= SD16SC; // SET BREAKPOINT HERE
// Set bit to start conversion
__bis_SR_register(LPM0_bits); // Enter LPM0
result=ADC_CH0;
for(j=0;j<41;j++)
{
temp1=abs(result-VOL[j]);
if(temp1==0)
{
flag=j;
}
}
delay(10000);
WriteCommandLcd(0x80);//从第一行第一个位置开始显示数据,第一行第一个位置的地址是80H;
WriteDataLcd(0x30+flag%10000/1000);//显示千位;
WriteDataLcd(0x30+flag%1000/100);//显示百位;
WriteDataLcd(0x30+flag%100/10);//显示十位;
WriteDataLcd(0x30+flag%10);//显示个位;
delay(100000); delay(100000);
}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=SD16_VECTOR
__interrupt void SD16ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(SD16_VECTOR))) SD16ISR (void)
#else
#error Compiler not supported!
#endif
{
switch (SD16IV)
{
case 2: // SD16MEM Overflow
break;
case 4: // SD16MEM0 IFG
ADC_CH0 = SD16MEM0; // Save CH0 results (clears IFG)
break;
}
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
|