MLX90640 紅外熱成像傳感器測(cè)溫模塊開發(fā)筆記(二)
MLX90640 紅外熱成像儀測(cè)溫模塊開發(fā)筆記(二)API 移植-I2C 和關(guān)鍵接口函數(shù)
API 說明文件里面有官方的移植指導(dǎo),但我覺得可以把重點(diǎn)放在與 MLX90640 具體操作有關(guān)的幾個(gè)函數(shù)上,而與標(biāo)準(zhǔn) I2C 相關(guān)的函數(shù)和文件結(jié)構(gòu)還是按照自己習(xí)慣的套路實(shí)現(xiàn)。這樣更符合我們開發(fā)人員的可控性的習(xí)慣。步驟如下:
(1)建立標(biāo)準(zhǔn) I2C 文件 IIC.h 和 IIC.c
用自己的方法實(shí)現(xiàn)如下幾個(gè)函數(shù)(硬件也好,GPIO 模擬也好),函數(shù)名稱用下面建議的。
void IIC_Init(void);//I2C 接口初始化
void IIC_Start(void);//發(fā)送開始信號(hào)
void IIC_Stop(void);//發(fā)送結(jié)束信號(hào)
void IIC_SendACK(void);//發(fā)送應(yīng)答信號(hào)
void IIC_SendNAK(void);//發(fā)送非應(yīng)答信號(hào)unsigned char IIC_RecvACK(void);//讀取應(yīng)答信號(hào)unsigned char IIC_RecvData(void);//讀取 1 個(gè)字節(jié)void IIC_SendData(char dat);//發(fā)送 1 個(gè)字節(jié)
(2)在工程中引入 MLX90640_API.c
并做如下幾處修改
第一行#include <MLX90640_I2C_Driver.h>改為#include <IIC.h>
(3)添加 3 個(gè)函數(shù)
void MLX90640_I2CInit(void)
unsigned char MLX90640_I2CRead (unsigned short startAddress, unsigned short nWordsRead, unsigned short *datas)
unsigned char MLX90640_I2CWrite (unsigned short writeAddress, unsigned short word)
void MLX90640_I2CInit(void)
{
IIC_Stop();
}
//從指定地址讀取 n 個(gè)字(每個(gè)字占用 2 個(gè)字節(jié))
unsigned char MLX90640_I2CRead(unsigned short startAddress, unsigned short nWordsRead, unsigned short *datas)
{
unsigned char c1,c2; unsigned short i; unsigned char Msb,Lsb;
Msb=(unsigned char)(startAddress>>8); Lsb=(unsigned char)(startAddress&0x00FF);
IIC_Start();//發(fā)送起始命令
IIC_SendData(0x66);//發(fā)送設(shè)備地址+寫命令I(lǐng)IC_RecvACK();
IIC_SendData(Msb);//發(fā)送要操作的地址值 2 字節(jié)
IIC_RecvACK();
IIC_SendData(Lsb);
IIC_RecvACK();
IIC_Start();//發(fā)送起始命令
IIC_SendData(0x67);//發(fā)送設(shè)備地址+讀命令I(lǐng)IC_RecvACK();
for (i=0;i<nWordsRead;i++)
{
c1=IIC_RecvData(); IIC_SendACK();
c2=IIC_RecvData();
if (i==(nWordsRead-1)) IIC_SendNAK();
else
IIC_SendACK();
datas[i]=c1; datas[i]<<=8; datas[i]|=c2;
}
IIC_Stop();//發(fā)送停止命令
return 0;
}
//向指定地址寫入 1 個(gè)字(2 字節(jié))
unsigned char MLX90640_I2CWrite(unsigned int writeAddress, unsigned int word)
{
IIC_Start();//發(fā)送起始命令
IIC_SendData(0x66);//發(fā)送設(shè)備地址+寫命令I(lǐng)IC_RecvACK();
IIC_SendData(writeAddress>>8);//發(fā)送要操作的地址值 2 字節(jié)
IIC_RecvACK();
IIC_SendData(writeAddress&0x00FF); IIC_RecvACK();
IIC_SendData(word>>8); IIC_RecvACK();
IIC_SendData(word&0x00FF); IIC_RecvACK();
IIC_Stop(); return 0;
(4)修改 2 個(gè)函數(shù)
unsigned char MLX90640_DumpEE(unsigned short *eeData)
{
return MLX90640_I2CRead(0x2400, 832, eeData);
}
unsigned char MLX90640_GetFrameData(unsigned short *frameData)
{
unsigned short statusRegister,controlRegister1;
MLX90640_I2CRead(0x8000, 1, &statusRegister); if (statusRegister&0x0008)//有測(cè)量完成的 Frame
{
MLX90640_I2CRead(0x800D, 1, &controlRegister1); MLX90640_I2CWrite(0x8000, statusRegister&(~0x0018)); MLX90640_I2CRead(0x0400, 832, frameData); frameData[832] = controlRegister1;
frameData[833] = statusRegister & 0x0001; return 0;
}
Return -1;
}
至此移植完成
編譯工程,若沒有錯(cuò)誤提示則基本上沒有問題了,下一篇開始講述如何操作MLX90640。
*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。