MLX90614测温

MXL90614

这个MLX90614测温很容易受外界环境的干扰,需要自己写算法。

声明

#include "bsp_adc.h"

#define ADC_BUF_SIZE    10
static __IO uint16_t sg_aADCConvertedBuf[ADC_BUF_SIZE];
static __IO uint8_t g_byFlagConvFinish = 0;
#define uint unsigned int
#define uchar unsigned char

#define ACK           0
#define NACK          1
#define SA            0x00 //Slave address ??MLX90614????0x00,????????0x5a
#define RAM_ACCESS    0x00 //RAM access command
#define EEPROM_ACCESS 0x20 //EEPROM access command
#define RAM_TOBJ1     0x07 //To1 address in the eeprom


#define SDA_L     GPIO_ResetBits(GPIOB, GPIO_Pin_0)
#define SDA_H     GPIO_SetBits(GPIOB, GPIO_Pin_0)  
#define SCL_H     GPIO_SetBits(GPIOB, GPIO_Pin_1)
#define SCL_L     GPIO_ResetBits(GPIOB, GPIO_Pin_1)
#define SMBUS_SDA_PIN    GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) //SDA --1  
void 	SMBus_StartBit(void);
void 	SMBus_StopBit(void);
void  SMBus_SendBit(u8);
u8 	SMBus_SendByte(u8);
u8 	SMBus_ReceiveBit(void);
u8 	SMBus_ReceiveByte(u8);
void 	SMBus_Delay(u16);
void 	SMBus_Init(void);
u16 	SMBus_ReadMemory(u8, u8);
u8 	PEC_Calculation(u8*);
void Mlx96014_Configuration(void);
float SMBus_ReadTemp(void); 


/* 内部函数声明 */
static void ADC1Init(void);
static short GetOneTemp(void);

/* 变量声明 */
T_ADCOpr g_tADCOpr = {
	.isInit = 0,
	.ADCInit = ADC1Init,
	.GetTempVal = GetOneTemp
};

#if 0   //another censer

初始化ADC

/*
*********************************************************************************************************
*	函 数 名: ADC1Init
*	功能说明: 初始化ADC
*	形    参: 无
*	返 回 值: 无
*********************************************************************************************************
*/
static void ADC1Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    ADC_InitTypeDef ADC_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
	
	if(g_tADCOpr.isInit == 1)
	{
		return;
	}
    
    RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE );
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &GPIO_InitStructure);	
    
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    RCC_APB2PeriphClockCmd ( RCC_APB2Periph_ADC1, ENABLE );
    DMA_DeInit(DMA1_Channel1);
    
    // 配置 DMA 初始化结构体
    DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t)0x4001244C);
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)sg_aADCConvertedBuf;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = ADC_BUF_SIZE;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;	
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    DMA_Cmd(DMA1_Channel1 , DISABLE);
    
    // ADC 模式配置
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE ; 
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = 1;	
    ADC_Init(ADC1, &ADC_InitStructure);
    RCC_ADCCLKConfig(RCC_PCLK2_Div8);           //ADC时钟最大值为14MHz,当系统时钟为64MHz时,只能设置成8分频,即ADCLK=8MHz 
    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5);//设置AD转换周期为1.5个采样周期+12.5个转换时间 = 14个采样周期
    ADC_DMACmd(ADC1, ENABLE);
    ADC_Cmd(ADC1, ENABLE);
    ADC_ResetCalibration(ADC1);
    while(ADC_GetResetCalibrationStatus(ADC1));
    ADC_StartCalibration(ADC1);
    while(ADC_GetCalibrationStatus(ADC1));
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
            
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;                //配置DMA1_Channel1中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;               //主优先级             
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;                      //中断优先级分组4,无次优先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
	g_tADCOpr.isInit = 1;
}

void DMA1_Channel1_IRQHandler(void)
{
    if(DMA_GetITStatus(DMA1_IT_TC1) == SET)//获取DMA1_CH1的传输完成标志位
    {
        if (g_byFlagConvFinish == 0)
            g_byFlagConvFinish = 1;
        DMA_ClearITPendingBit(DMA1_IT_TC1);//清除满标志
    }  
}

初始化MLX_IIC用的端口

void Mlx96014_Configuration()
{
	GPIO_InitTypeDef GPIO; //?????????
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//????????SDAL ??PB.15,SCL??PB.14---??GPIOD??
	// ?????????
	GPIO.GPIO_Pin = (GPIO_Pin_0|GPIO_Pin_1);//????????SDAL ??PD.15,SCL??PD.14
	GPIO.GPIO_Speed = GPIO_Speed_50MHz;//?????50MHZ
	GPIO.GPIO_Mode = GPIO_Mode_Out_OD;//?????
	GPIO_Init(GPIOB,&GPIO);//???GPIOB???

	SDA_H;
	SCL_H; //??SMBus??????
}

IIC协议

/*******************************************************************************
* Function Name  : SMBus_StartBit
* Description    : Generate START condition on SMBus
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_StartBit(void)
{
    SDA_H;               // Set SDA line 
    SMBus_Delay(1);      // Wait a few microseconds 
    SCL_H;               // Set SCK line  
    SMBus_Delay(5);      // Generate bus free time between Stop
    SDA_L;               // Clear SDA line
    SMBus_Delay(10);     // Hold time after (Repeated) Start
                         // Condition. After this period, the first clock is generated.
                         //(Thd:sta=4.0us min)
    SCL_L;               // Clear SCK line
    SMBus_Delay(2);      // Wait a few microseconds
}
/*******************************************************************************
* Function Name  : SMBus_StopBit
* Description    : Generate STOP condition on SMBus
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_StopBit(void)
{
    SCL_L;                // Clear SCK line
    SMBus_Delay(5);       // Wait a few microseconds
    SDA_L;                // Clear SDA line
    SMBus_Delay(5);       // Wait a few microseconds
    SCL_H;                // Set SCK line
    SMBus_Delay(10);      // Stop condition setup time(Tsu:sto=4.0us min)
    SDA_H;                // Set SDA line
}

/*******************************************************************************
* Function Name  : SMBus_SendByte
* Description    : Send a byte on SMBus
* Input          : Tx_buffer
* Output         : None
* Return         : None
*******************************************************************************/
u8 SMBus_SendByte(u8 Tx_buffer)
{
    u8        Bit_counter;
    u8        Ack_bit;
    u8        bit_out;
    for(Bit_counter=8; Bit_counter; Bit_counter--)
    {
        if (Tx_buffer&0x80)
        {
            bit_out=1;       // If the current bit of Tx_buffer is 1 set bit_out
        }
        else
        {
            bit_out=0;      // else clear bit_out
        }
        SMBus_SendBit(bit_out);           // Send the current bit on SDA
        Tx_buffer<<=1;                    // Get next bit for checking
    }
    Ack_bit=SMBus_ReceiveBit();           // Get acknowledgment bit
    return        Ack_bit;
}

/*******************************************************************************
* Function Name  : SMBus_SendBit
* Description    : Send a bit on SMBus
* Input          : bit_out
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
    if(bit_out==0)
    {
      SDA_L;   
    }
    else
    {
    SDA_H;
    }
    SMBus_Delay(2);                            // Tsu:dat = 250ns minimum
    SCL_H;                                     // Set SCK line
    SMBus_Delay(10);                           // High Level of Clock Pulse
    SCL_L;                                     // Clear SCK line
    SMBus_Delay(10);                           // Low Level of Clock Pulse
//        SMBUS_SDA_H();                       // Master release SDA line ,
    return;
}
/*******************************************************************************
* Function Name  : SMBus_ReceiveBit
* Description    : Receive a bit on SMBus
* Input          : None
* Output         : None
* Return         : Ack_bit
*******************************************************************************/
u8 SMBus_ReceiveBit(void)
{
    u8 Ack_bit;
    SDA_H;             //?????????,????
    SCL_H;             // Set SCL line
    SMBus_Delay(2);    // High Level of Clock Pulse
    if (SMBUS_SDA_PIN)
    {
        Ack_bit=1;
    }
    else
    {
        Ack_bit=0;
    }
    SCL_L;                    // Clear SCL line
    SMBus_Delay(4);           // Low Level of Clock Pulse
    return   Ack_bit;
}
/*******************************************************************************
* Function Name  : SMBus_ReceiveByte
* Description    : Receive a byte on SMBus
* Input          : ack_nack
* Output         : None
* Return         : RX_buffer
*******************************************************************************/
u8 SMBus_ReceiveByte(u8 ack_nack)
{
    u8        RX_buffer;
    u8        Bit_Counter;
    for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
    {
        if(SMBus_ReceiveBit())         // Get a bit from the SDA line
        {
            RX_buffer <<= 1;           // If the bit is HIGH save 1  in RX_buffer
            RX_buffer |=0x01;
        }
        else
        {
            RX_buffer <<= 1;           // If the bit is LOW save 0 in RX_buffer
            RX_buffer &=0xfe;
        }
    }
    SMBus_SendBit(ack_nack);           // Sends acknowledgment bit
    return RX_buffer;
}


/*******************************************************************************
* Function Name  : SMBus_Delay
* Description    : ??  ?????1us
* Input          : time
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{
    u16 i, j;
    for (i=0; i<4; i++)
    {
        for (j=0; j<time; j++);
    }
}

/*******************************************************************************
 * Function Name  : SMBus_ReadMemory
 * Description    : READ DATA FROM RAM/EEPROM
 * Input          : slaveAddress, command
 * Output         : None
 * Return         : Data
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
    u16 data;               // Data storage (DataH:DataL)
    u8 Pec;                 // PEC byte storage
    u8 DataL=0;             // Low data byte storage
    u8 DataH=0;             // High data byte storage
    u8 arr[6];              // Buffer for the sent bytes
    u8 PecReg;              // Calculated PEC byte storage
    u8 ErrorCounter;        // Defines the number of the attempts for communication with MLX90614
    ErrorCounter=0x00;                                // Initialising of ErrorCounter
        slaveAddress <<= 1;        //2-7???????

    do
    {
repeat:
        SMBus_StopBit();                //If slave send NACK stop comunication
        --ErrorCounter;                 //Pre-decrement ErrorCounter
        if(!ErrorCounter)               //ErrorCounter=0?
        {
            break;                      //Yes,go out from do-while{}
        }

        SMBus_StartBit();               //Start condition
        if(SMBus_SendByte(slaveAddress))//Send SlaveAddress ???Wr=0????????
        {
            goto  repeat;               //Repeat comunication again
        }
        if(SMBus_SendByte(command))     //Send command
        {
            goto    repeat;             //Repeat comunication again
        }

        SMBus_StartBit();                //Repeated Start condition
        if(SMBus_SendByte(slaveAddress+1))  //Send SlaveAddress ???Rd=1????????
        {
            goto        repeat;           //Repeat comunication again
        }
        DataL = SMBus_ReceiveByte(ACK);   //Read low data,master must send ACK
        DataH = SMBus_ReceiveByte(ACK);   //Read high data,master must send ACK
        Pec = SMBus_ReceiveByte(NACK);    //Read PEC byte, master must send NACK
        SMBus_StopBit();                  //Stop condition

        arr[5] = slaveAddress;        
        arr[4] = command;
        arr[3] = slaveAddress+1;         //Load array arr
        arr[2] = DataL;                 
        arr[1] = DataH;                
        arr[0] = 0;                   
        PecReg=PEC_Calculation(arr);     //Calculate CRC
    }
    while(PecReg != Pec);                //If received and calculated CRC are equal go out from do-while{}
        data = (DataH<<8) | DataL;       //data=DataH:DataL
    return data;
}

/*******************************************************************************
* Function Name  : PEC_calculation
* Description    : Calculates the PEC of received bytes
* Input          : pec[]
* Output         : None
* Return         : pec[0]-this byte contains calculated crc value
*******************************************************************************/
u8 PEC_Calculation(u8 pec[])
{
    u8         crc[6];
    u8        BitPosition=47;
    u8        shift;
    u8        i;
    u8        j;
    u8        temp;
    do
    {
        /*Load pattern value 0x000000000107*/
        crc[5]=0;
        crc[4]=0;
        crc[3]=0;
        crc[2]=0;
        crc[1]=0x01;
        crc[0]=0x07;
        /*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
        BitPosition=47;
        /*Set shift position at 0*/
        shift=0;
        /*Find first "1" in the transmited message beginning from the MSByte byte5*/
        i=5;
        j=0;
        while((pec[i]&(0x80>>j))==0 && i>0)
        {
            BitPosition--;
            if(j<7)
            {
                j++;
            }
            else
            {
                j=0x00;
                i--;
            }
        }/*End of while */
        /*Get shift value for pattern value*/
        shift=BitPosition-8;
        /*Shift pattern value */
        while(shift)
        {
            for(i=5; i<0xFF; i--)
            {
                if((crc[i-1]&0x80) && (i>0))
                {
                    temp=1;
                }
                else
                {
                    temp=0;
                }
                crc[i]<<=1;
                crc[i]+=temp;
            }/*End of for*/
            shift--;
        }/*End of while*/
        /*Exclusive OR between pec and crc*/
        for(i=0; i<=5; i++)
        {
            pec[i] ^=crc[i];
        }/*End of for*/
    }
    while(BitPosition>8); /*End of do-while*/

    return pec[0];
}

 /*******************************************************************************
 * Function Name  : SMBus_ReadTemp
 * Description    : Calculate and return the temperature
 * Input          : None
 * Output         : None
 * Return         : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
*******************************************************************************/
float SMBus_ReadTemp(void)
{   
    return SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
}
#endif 

滑动滤波器

/*
*********************************************************************************************************
*   函 数 名: SlidingFilter
*   功能说明: 滑动滤波器:相当于一个特殊的FIR低通滤波器(各级滤波系数都为:1/N)
*   形    参: wIn:传入滑动窗口的数据
*   返 回 值: 窗口平均值
*********************************************************************************************************
*/
#define ADC_VALUE_WINDOWS_WIDE  8
static uint16_t SlidingFilter(uint16_t wIn)
{
    static uint16_t s_wWriteArray[ADC_VALUE_WINDOWS_WIDE];
    static uint8_t s_byWriteIndex = 0;
    uint32_t dwFilterValue = 0;
    uint8_t i;
    //可以优化保存上一次输出,改计算方式为Y(N) = Y(N-1) + X(N)/N - X(N - 2)
    //写入偏移值
    s_byWriteIndex++;
    if (s_byWriteIndex > (ADC_VALUE_WINDOWS_WIDE - 1)){
        s_byWriteIndex = 0;
    }
    s_wWriteArray[s_byWriteIndex] = wIn;
    //求均值
    for (i = 0;i < ADC_VALUE_WINDOWS_WIDE; i++){
        dwFilterValue += s_wWriteArray[i];
    }
    dwFilterValue /= ADC_VALUE_WINDOWS_WIDE;
    
    return dwFilterValue;
}

中位值滤波器

/*
*********************************************************************************************************
*   函 数 名: MedianFilter
*   功能说明: 中位值滤波器
*   形    参: wBuf:数据缓冲区
*             wLen:缓冲区大小
*   返 回 值: 中位值
*********************************************************************************************************
*/
static uint16_t MedianFilter(__IO uint16_t wBuf[], uint8_t byLen)
{
    uint8_t i,j;
    uint16_t temp;
    //优化为查找第byLen/2小的数,减小一半查找时间
    for(i = 0; i < ((byLen + 1)>>1); i++)//排序
    {
        for(j = i + 1; j < byLen; j++)
        {
            if(wBuf[i] > wBuf[j])//升序排列
            {
                temp = wBuf[i];
                wBuf[i] = wBuf[j];
                wBuf[j] = temp;
            }
        }
    }
    return wBuf[((byLen + 1) >> 1) - 1];
}

获取一次温度传感器数值

/*
*********************************************************************************************************
*   函 数 名: GetOneTemp
*   功能说明: 获取一次温度传感器数值
*   形    参: psTemp:指向转换结果
*   返 回 值: 无
*********************************************************************************************************
*/


static short GetOneTemp(void)
{
    uint16_t wTimeout=0,wFilterDat2;
    
    //触发一次AD获取
    g_byFlagConvFinish = 0;
    DMA_Cmd(DMA1_Channel1, DISABLE);
    DMA_SetCurrDataCounter(DMA1_Channel1,ADC_BUF_SIZE);
    DMA_Cmd(DMA1_Channel1, ENABLE);
    
    //等待转换完成
    while(g_byFlagConvFinish == 0){
        if ((wTimeout++) > 1000){return 0;}delay_us(10);//10
    }g_byFlagConvFinish = 0;
    
	
	//插入软件滤波算法:中位值滤波(缓冲区大小:10)+滑动滤波(缓冲区大小:8)
	wFilterDat2 = SlidingFilter(MedianFilter(sg_aADCConvertedBuf,ADC_BUF_SIZE));//sg_aADCConvertedBuf,ADC_BUF_SIZE
	
	//温度回归方程:AD转换为温度值,
	//return (short)(0.1373f * wFilterDat2 - 5.5026f);
	return (short)(0.1557f * wFilterDat2 - 43.488f);
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值