单片机ADC采集NTC温度

通常会将100kNTC串联一个100K电阻用于测量温度,温度变化时NTC阻值也发生变化,反应到ADC上就是电压变化,只需用ADC时时的采样NTC上的电压计算出电阻,即可根据阻值表计算出NTC的温度。

项目合作,吹牛扯淡,交朋友请联系:18665321219


static const char adclab[111][2] = {{0x03,0xbf},{0x03,0xbc},{0x03,0xb9},{0x03,0xb6},{0x03,0xb2},{0x03,0xab},{0x03,0xa7},{0x03,0xa3},{0x03,0x9e},
{0x03,0x9a},{0x03,0x96},{0x03,0x91},{0x03,0x8c},{0x03,0x87},{0x03,0x82},{0x03,0x7d},{0x03,0x78},{0x03,0x72},{0x03,0x6d},{0x03,0x67},{0x03,0x61},
{0x03,0x5a},{0x03,0x54},{0x03,0x4e},{0x03,0x47},{0x03,0x41},{0x03,0x3a},{0x03,0x33},{0x03,0x2c},{0x03,0x24},{0x03,0x1d},{0x03,0x15},{0x03,0x0e},
{0x03,0x06},{0x02,0xfe},{0x02,0xf6},{0x02,0xee},{0x02,0xe5},{0x02,0xdd},{0x02,0xd4},{0x02,0xcc},{0x02,0xc3},{0x02,0xba},{0x02,0xb1},{0x02,0xa8},
{0x02,0x9f},{0x02,0x96},{0x02,0x8d},{0x02,0x84},{0x02,0x7b},{0x02,0x72},{0x02,0x68},{0x02,0x5f},{0x02,0x56},{0x02,0x4c},{0x02,0x43},{0x02,0x3a},
{0x02,0x30},{0x02,0x27},{0x02,0x1e},{0x02,0x15},{0x02,0x0b},{0x02,0x02},{0x01,0xf9},{0x01,0xf0},{0x01,0xf0},{0x01,0xe7},{0x01,0xde},{0x01,0xd5},
{0x01,0xcc},{0x01,0xc3},{0x01,0xbb},{0x01,0xb2},{0x01,0xaa},{0x01,0xa1},{0x01,0x99},{0x01,0x91},{0x01,0x89},{0x01,0x81},{0x01,0x79},{0x01,0x71},
{0x01,0x69},{0x01,0x61},{0x01,0x5a},{0x01,0x53},{0x01,0x4b},{0x01,0x44},{0x01,0x3d},{0x01,0x36},{0x01,0x30},{0x01,0x29},{0x01,0x22},{0x01,0x1c},
{0x01,0x16},{0x01,0x0f},{0x01,0x09},{0x01,0x03},{0x00,0xfd},{0x00,0xf8},{0x00,0xf2},{0x00,0xed},{0x00,0xe7},{0x00,0xe2},{0x00,0xdd},{0x00,0xd8},
{0x00,0xd3},{0x00,0xce},{0x00,0xc9},{0x00,0xc4},{0x00,0xc0},{0x00,0xba},
};
unsigned char culTemp(unsigned int adTmp)
{
    unsigned char numLab = 0;
    unsigned char tempVal = 0;
    unsigned int cntlab = 0;
    unsigned int minlab = 0;
    unsigned int maxlab = 0;
    
    minlab = (unsigned int)((adclab[0][0]<<8)|adclab[0][1]);
    cntlab = minlab;
    maxlab = (unsigned int)((adclab[100][0]<<8)|adclab[100][1]);
    
    if(minlab < adTmp)
    {
        tempVal = 0;    
    }
    else if(maxlab > adTmp)
    {
        tempVal = 100;
    }
    else
    {
        while( cntlab >= adTmp)
        {
            if(numLab < 101)
            {
                numLab++;
                cntlab = (unsigned int)((adclab[numLab][0]<<8)|adclab[numLab][1]);
            }
            else
            {
                break;
            }
        }
        
        if(numLab == 101)
        {
            tempVal = 100;
        }
        else
        {
            tempVal = numLab;
        }
    }
    return tempVal;
}
if(swAinBuz == 0)
        {
            if(adcStp == 0)
            {
                ADMD  = 0x90 | C_ADC_PA1;            // Select AIN0(PA0) pad as ADC input
                //ADMD  = 0x90 | C_ADC_PA0;    
                ADMDbits.START = 1;                    // Start a ADC conversion session
                while(ADMDbits.EOC);
                adcStp = 1;
            }
            else if(adcStp == 1)
            {
                if(ADMDbits.EOC == 1)
                {
                    adcStp = 0;
                    adcCnt++;
                    R_AIN0_DATA_LB += ( 0x0F & ADR); 
                      R_AIN0_DATA += ADD; 
                      
                      if(adcCnt == 8)
                      {
                          adcCnt = 0;
                          R_AIN0_DATA <<= 4;                    // R_AIN0_DATA shift left 4 bit
                        //R_AIN0_DATA_LB &= 0xF0;                // Only get Bit7~4
                        R_AIN0_DATA += R_AIN0_DATA_LB;        // R_AIN0_DATA + R_AIN0_DATA_LB
                        R_AIN0_DATA >>=5;                    // R_AIN0_DATA divided 8  
                        adcVal = R_AIN0_DATA;                // output val 0~1024
                        R_AIN0_DATA = 0;
                        R_AIN0_DATA_LB = 0;
                        
                        if(buzEn)
                        {
                            swAinBuz = 1;
                            PACON = 0x00;
                            IOSTA &= 0xfd; 
                        }    
                      }
                }
            }
        }
posted @ 2022-01-12 18:40  steven_lg  阅读(1510)  评论(0编辑  收藏  举报