?? temperature.c
字號:
/********************************************************************/
//模塊功能:溫度檢測
//使用資源:
//調用的庫文件:<MSP430X14X.h>
//作者:
//版權:
/*******************************************************************/
#include <msp430x14x.h>
#define Num_of_Results 12
static unsigned int Ch1results[Num_of_Results]; // Needs to be global in this
// example. Otherwise, the
// compiler removes it because it
// is not used for anything.
const unsigned int add_temp[11];
/******************************************************************************/
//模塊功能:把采樣到的AD轉換成溫度
//設計一個堆棧,進行兩階濾波器
/******************************************************************************/
void temp_main(void) //AD轉換數據處理程序
{
unsigned char j;
unsigned int a,c,d,i;
long int b;
for(j=0;j<11;j++) //0通道五次結果排序
{
i = j + 1 ;
for(;i<12;i++)
{
if(Ch1results[j] > Ch1results[i])
{
a = Ch1results[j];
Ch1results[j] = Ch1results[i];
Ch1results[i] = a;
}
}
}
i = Ch1results[2]%8;
Ch1results[2] /=8;
j = i;
i = Ch1results[3]%8;
Ch1results[3] /=8;
j = i + j;
i = Ch1results[4]%8;
Ch1results[4] /=8; //求取平均值
j = i + j;
i = Ch1results[5]%8;
Ch1results[5] /=8;
j = i + j;
i = Ch1results[6]%8;
Ch1results[6] /=8; //求取平均值
j = i + j;
i = Ch1results[7]%8;
Ch1results[7] /=8;
j = i + j;
i = Ch1results[8]%8;
Ch1results[8] /=8; //求取平均值
j = i + j;
i = Ch1results[9]%8;
Ch1results[9] /=8;
j = i + j;
i = j/8;
Ch1results[0] = Ch1results[2] + Ch1results[3] + Ch1results[4] + Ch1results[5] + Ch1results[6] + Ch1results[7] + Ch1results[8] + Ch1results[9] + i;
i = Ch1results[0];
b = (long int)i*0x190*0x64;//1d6,470K;186,390k;190,400k;
i = Ch1results[0];
a = 0xfffe - i;//Rx = (400*i*100)/(0Xfffe-i)
c = b/a; //c是需要的電阻值
for(i=1;i<11;i++)
{
if(c>add_temp[i])
{
b = (long int)(add_temp[i-1]-c)*0x64;//計算溫度
a = slop[i-1]; //y = (b-x)/k
c = (int)(b/a);
d = 5*(i-1);
c = c + 100*d;
pass_temp = c;
a = c%100; //溫度小數部分
b = c/100;
i = 100;
}
}
c = b/10;
b = b%10;
i = a%10;
a = a/10;
if(i>5)
{
a +=1;
}
if(a>9)
{
b +=1;
a = 0;
}
if(b>9)
{
c +=1;
b = 0;
}
// while(1);
Ch1results[0] = c;
Ch1results[1] = b;
Ch1results[2] = a;
i = a;
a = c;
c = i;
LCDM10 &= 0xef;
LCDM13 &= 0xef;
LCDM11 &= 0x10; //顯示相應的BCD碼
if(a!=0)
{
LCDM11 = LCDNUM2[a];
}
LCDM10 &= 0x10;
if((b!=0)||(a!=0))
{
LCDM10 = LCDNUM2[b];
}
LCDM9 &= 0x10;
LCDM9 = LCDNUM2[c];
LCDM9 |= 0x10;
}
/******************************************************************************/
//模塊功能:初始化AD控制寄存器
/******************************************************************************/
void init_AD(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
P6SEL |= 0x01; // Enable A/D channel A0
ADC12CTL0 = ADC12ON+SHT0_8+MSC; // Turn on ADC12, set sampling time
ADC12CTL1 = SHP+CONSEQ_2; // Use sampling timer, set mode
ADC12IE = 0x01; // Enable ADC12IFG.0
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start conversion
//_BIS_SR(LPM0_bits + GIE); // Enter LPM0,Enable interrupts
}
/*****************************************************************************/
//模塊功能:采集AD轉換值
/*****************************************************************************/
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
static unsigned int index = 0;
Ch1results[index] = ADC12MEM0; // Move Ch1results
index = (index+1)%Num_of_Results; // Increment Ch1results index, modulo
}
const unsigned int add_temp[11] = {0x82aa,0x654a,0x4f10,0x3e26,0x3124,0x2710,0x1f57,0x194d,0x148c,
0x10c3,0xdc8}; //室溫對應的溫度 470K
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -