银杏科技有限公司旗下技术文档发布平台 | |||
技术支持电话 | 0379-69926675-801 | ||
技术支持邮件 | Gingko@vip.163.com | ||
版本 | 日期 | 作者 | 修改内容 |
---|---|---|---|
V1.0 | 2020-03-04 | zh. | 初次建立 |
/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2020-02-17 zh. first version */ #include <board.h> #include <drv_qspi.h> #include <rtdevice.h> #include <rthw.h> #include <finsh.h> #ifdef BSP_USING_I2C_LM75A #define LM75A_ADDR (0x90 >> 1) /* For lm75a i2c bus*/ #define LM75A_I2CBUS_NAME "i2c1" static struct rt_i2c_bus_device *lm75a_i2c_bus = RT_NULL; static rt_err_t i2c_read_nbyte(rt_uint8_t slave_addr, rt_uint8_t cmd, rt_uint8_t *buf, rt_uint32_t len) { rt_i2c_master_send(lm75a_i2c_bus, slave_addr, RT_I2C_WR, &cmd, 1); rt_i2c_master_recv(lm75a_i2c_bus, slave_addr, RT_I2C_RD, buf, len); return RT_EOK; } float lm75a_read(void) { union { unsigned char buf[2]; short int value; }temp; float f; unsigned char c; lm75a_i2c_bus = rt_i2c_bus_device_find(LM75A_I2CBUS_NAME); if(lm75a_i2c_bus == RT_NULL) { rt_kprintf("i2c_bus %s for lm75a not found!\n", lm75a_i2c_bus); return -RT_ERROR; } i2c_read_nbyte(LM75A_ADDR,0,temp.buf,2); c = temp.buf[0]; temp.buf[0] = temp.buf[1]; temp.buf[1] = c; f = temp.value; f /= (float)32.0; f *= (float)0.125; return f; } static int lm75a(int argc, char **argv) { float tempture; int temp; tempture = lm75a_read(); temp = tempture * 100; rt_kprintf("The current temperature is: %d.%02d°\n",temp/100,temp%100); return RT_EOK; } MSH_CMD_EXPORT(lm75a, show current temperature); #endif /* BSP_USING_I2C_LM75A */
源代码可以移步这里下载:
链接:https://pan.baidu.com/s/1fcLU4WaRDlgr0mNYwZj1Yg 提取码:zstq