Microblaze的MLX90614驱动成功

MLX90614温感
上个月,导师突然通知我项目的温度传感器不能用原来的DHT11,原因就是DHT11是表面温度传感器,对温度的变化反应太慢,因为我要参加的是一个比赛,这个东西也不好怎么演示。但是当时的我对MLX90614所用的smbus的协议还不是很了解,还一昧想自己开发一个IP,用来集成这个模块呢,现在想起来,确实感觉有点搞。最后,我在NI的官方上看到了有人用IIC成功驱动MLX90614以后,我就知道这个东西也一直可以。
具体的就直接贴代码了,注释的话我就直接用E文写了,还望大家见谅啊。
值得注意的是,我发现XILINX的IIC驱动当中,首先是发送从器件地址的,应该是一定要接收到从器件的回应才能发送数据的。另外,关于IIC的频率,我用了80K,因为smbus是10K到100K的,所以,我选择了一个折中的速率。而在一个repeated start的状态的生成以及取消当中,它描述的是接下来的读写操作完成以后的状态。而关于IIC和smbus协议,建议参考官方的协议说明文档,而时序波形什么的,我也不说了,网上有一堆。
其实这个东西我弄了一个多星期,而原因是比较让人感到恼火的,是我因为看错了MLX90614的官方说明文档的管脚定义从而导致的焊接错误,在那个过程当中,是无比痛苦的,甚至大大打击了我的自信心,在我阅读了无数的文档中,我发现我写的程序是绝对没有问题了以后一度怀疑是Xilinx的IP出了问题,直到最后我重新焊接了这个传感器,才解决了这个问题。
u8 temprec[]={0x07};
//Please reference the document of mlx90614, this is the address what I need to read.
u8 temp[4];
int gettemp(XIic* InstancePtr,u32 *temp100)
{
int status;
 
    status = XIic_SetAddress(InstancePtr,XII_ADDR_TO_SEND_TYPE,irtpaddr);
//about irtpaddr, is a constant in my code which is the slave address of mlx90614, 0x5A
if (status != XST_SUCCESS)
{
print("setadd fail\n");
return XST_FAILURE;
}
 
InstancePtr->Options=XII_REPEATED_START_OPTION;
//More about this sentence, since between send command and receive data is need the controller // in a  repeated start state. 
status=XIic_Start(InstancePtr);
if (status != XST_SUCCESS)
{
print("start fail\n");
return XST_FAILURE;
}
sended=1;//this parameter in my sendhandler of interrupt will be set to 0. 
status=XIic_MasterSend(InstancePtr,temprec,1);
if (status != XST_SUCCESS)
{
print("send fail, bus is busy wait for not busy\n");
return XST_FAILURE;
}
 
while(sended){}
recved=1;
InstancePtr->Options=0x00;
//cancel the repeated start state.
status=XIic_MasterRecv(InstancePtr,temp,3);
 
if (status != XST_SUCCESS)
{
print("recv fail\n");
return XST_FAILURE;
}
 
while(recved){}
(*temp100)= temp[1];
(*temp100)=(*temp100)*256+temp[0];
(*temp100)=(*temp100)*2-27315;
xil_printf("temp is : %d cel ",*temp100);
while(XIic_IsIicBusy(InstancePtr));
int stoptry=0;
stop:status=XIic_Stop(InstancePtr);
if (status != XST_SUCCESS)
{
busbusy=1;
stoptry++;
if(stoptry>5)return XST_FAILURE;
print("stop fail,retry\n");
while(busbusy);
goto stop;
}
 
return XST_SUCCESS;
}


 青春就应该这样绽放  游戏测试:三国时期谁是你最好的兄弟!!  你不得不信的星座秘密

转载于:https://www.cnblogs.com/zhumai/archive/2012/04/13/2778381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值