MLX90640 紅外熱成像儀測(cè)溫模塊開(kāi)發(fā)筆記(二)API 移植-I2C 和關(guān)鍵接口函數(shù)
穩(wěn)控科技MLX90640 紅外熱成像傳感器
API 說(shuō)明文件里面有官方的移植指導(dǎo),但我覺(jué)得可以把重點(diǎn)放在與 MLX90640 具體操作有關(guān)的幾個(gè)函數(shù)上,而與標(biāo)準(zhǔn) I2C 相關(guān)的函數(shù)和文件結(jié)構(gòu)還是按照自己習(xí)慣的套路實(shí)現(xiàn)。這樣更符合我們開(kāi)發(fā)人員的可控性的習(xí)慣。步驟如下:
(1) 建立標(biāo)準(zhǔn) I2C 文件 IIC.h 和 IIC.c
用自己的方法實(shí)現(xiàn)如下幾個(gè)函數(shù)(硬件也好,GPIO 模擬也好),函數(shù)名稱(chēng)用下面建議的。
void IIC_Init(void); //I2C 接口初始化
void IIC_Start(void); //發(fā)送開(kāi)始信號(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 改為#include
(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è)備地址+寫(xiě)命令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{
c1=IIC_RecvData(); IIC_SendACK();
c2=IIC_RecvData();
if (i==(nWordsRead-1)) IIC_SendNAK();
else
IIC_SendACK();;i++)
datas[i]=c1; datas[i]<<=8; datas[i]|=c2;
}
IIC_Stop(); //發(fā)送停止命令
return 0;
}
//向指定地址寫(xiě)入 1 個(gè)字(2 字節(jié))
unsigned char MLX90640_I2CWrite(unsigned int writeAddress, unsigned int word)
{
IIC_Start(); //發(fā)送起始命令
IIC_SendData(0x66); //發(fā)送設(shè)備地址+寫(xiě)命令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;
}
至此移植完成
編譯工程,若沒(méi)有錯(cuò)誤提示則基本上沒(méi)有問(wèn)題了,下一篇開(kāi)始講述如何操作MLX90640。
-
傳感器
+關(guān)注
關(guān)注
2551文章
51099瀏覽量
753606 -
測(cè)溫模塊
+關(guān)注
關(guān)注
0文章
11瀏覽量
2852 -
MLX90640
+關(guān)注
關(guān)注
2文章
22瀏覽量
1232
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論