1 Star 0 Fork 0

ischen.x/mcp3021-driver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MCP3021.c 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
ischen.x 提交于 2024-08-23 16:55 . 编写readme
/*
* @Author: ischen.x ischen.x@foxmail.com
* @Date: 2024-08-22 13:13:11
* @LastEditors: ischen.x ischen.x@foxmail.com
* @LastEditTime: 2024-08-23 16:46:16
*
* Copyright (c) 2024 by fhchengz, All Rights Reserved.
*/
#include "MCP3021.h"
#define MCP3021_REF_VOLTAGE (3.3)
#define MCP3021_IIC_ADDR (0x4D<<1)
/**
* @description: I2C write interface
* @param data: write data
* @param length: write length
* @return 0: successful
*/
static mcp3021_i2c_write_fun mcp3021_i2c_write;
/**
* @description: I2C read interface
* @param data: read data
* @param length: read length
* @return 0: successful
*/
static mcp3021_i2c_read_fun mcp3021_i2c_read;
int mcp3021_set_i2c_read_write_intf(mcp3021_i2c_read_fun read, mcp3021_i2c_write_fun write)
{
if (read && write) {
mcp3021_i2c_read = read;
mcp3021_i2c_write = write;
return 0;
} else {
return -1;
}
}
int mcp3021_get_voltage(double *voltage)
{
uint8_t temp[2];
uint16_t temp_16bit = 0;
if (mcp3021_i2c_read(MCP3021_IIC_ADDR, temp, 2)) {
return -1; // I2C read error
}
temp_16bit = ((temp[0] << 8) | temp[1]);
temp_16bit >>= 2;
*voltage = (double)temp_16bit * (MCP3021_REF_VOLTAGE / 1024.0);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ischen-x/mcp3021-driver.git
git@gitee.com:ischen-x/mcp3021-driver.git
ischen-x
mcp3021-driver
mcp3021-driver
master

搜索帮助