当前位置:网站首页>cubemx stm32 afm3000模块 气体流量传感器 驱动代码
cubemx stm32 afm3000模块 气体流量传感器 驱动代码
2022-08-04 10:22:00 【嵌入一下?】
afm3000
- AFM3000 传感器是奥松电子的数字流量计,专为呼吸机应用而设计。它以高超的精确度测量空气,氧气和其他非侵蚀性气体的流量。风道内采用特殊设计,使得通过传感器的流动体的压损非常低,使其性能适用于各种苛刻的应用场景,例如医疗通风和呼吸应用。
- AFM3000 采用 5V 电源电压工作,具有数字 I2C 接口。输出测量结果经过内部校准和温度补偿。
- 这款传感器的卓越性能基于奥松电子的传感器技术,内部由一个热式传感芯片和一个高性集成 24 位 AD 采集的 CMOS 微处理器相连接。气体流量由热传感器芯片测量,确保了非常快的信号处理时间和相比同类产品有最佳精度且具备双向测量功能。
cubemx配置
- 开个I2C

- 串口配置

硬件连接


代码
afm3000
afm3000.c
#include "afm3000.h"
/* 设备i2c地址 */
#define AFM_WAdd 0x80 // 写地址
#define AFM_RAdd 0x81 // 读地址
/* 设备命令 */
//#define AFM_FlowCollect 0x1000 // 流量采集
#define AFM_FlowRead 0x1000 // 读取流量:返回2个字节
#define AFM_IDRead 0x31AE // 读取ID号:返回4个字节
#define AFM_Reset 0x2000 // 复位指令
/* 内部函数声明 */
static HAL_StatusTypeDef afm3000_write(uint16_t afmCmd, uint8_t * afmData, uint16_t afmDataSize);
static HAL_StatusTypeDef afm3000_read(uint16_t afmCmd, uint8_t * afmData, uint16_t afmDataSize);
/* 内部函数实现 */
static HAL_StatusTypeDef afm3000_write(uint16_t afmCmd, uint8_t * afmData, uint16_t afmDataSize)
{
return HAL_I2C_Mem_Write(&AFM_hi2c, AFM_WAdd, afmCmd, I2C_MEMADD_SIZE_16BIT, afmData, afmDataSize, 0xffff);
}
static HAL_StatusTypeDef afm3000_read(uint16_t afmCmd, uint8_t * afmData, uint16_t afmDataSize)
{
return HAL_I2C_Mem_Read(&AFM_hi2c, AFM_RAdd, afmCmd, I2C_MEMADD_SIZE_16BIT, afmData, afmDataSize, 0xffff);
}
/* 外部函数 */
/* 复位AFM3000 参数 : 无 返回值 : 来自 HAL_StatusTypeDef 枚举中 */
HAL_StatusTypeDef afm3000_Reset(void)
{
uint8_t *afmData = NULL;
return afm3000_write(AFM_Reset, afmData, 0);
}
/* 获取AFM3000的值 参数 : afmGetVal 通过指针获取数据 返回值 : 来自 HAL_StatusTypeDef 枚举中 */
HAL_StatusTypeDef afm3000_GetVal(uint16_t * afmGetVal)
{
uint8_t afmVal[3] = {
0}; // 2字节的数据 + 1字节的CRC
HAL_StatusTypeDef afmState = HAL_ERROR;
HAL_Delay(20); // 至少等待10ms一次数据,保险起见延时翻倍
afmState = afm3000_read(AFM_FlowRead, afmVal, 3);
*afmGetVal = (afmVal[0]<<8 | afmVal[1]); //不知道先发过来的是高位还是低位,先测试一波
*afmGetVal = (*afmGetVal - AFM_Offset) / AFM_FlowCoeff; // 计算数据,根据公式 ((测试量 - 偏移量)/ 流量系数)
return afmState;
}
/* 初始化AFM3000 参数 : 无 返回值 : 来自 HAL_StatusTypeDef 枚举中 */
HAL_StatusTypeDef afm3000_Init(void)
{
uint16_t afmGetVal = 0;
HAL_StatusTypeDef afmState = HAL_ERROR;
afm3000_Reset(); // 先复位一波
// 前面几次获取的数据不准确,去掉
for(uint8_t i=0; i<5; i++)
{
afmState = afm3000_GetVal(&afmGetVal);
}
return afmState;
}
afm3000.h
#ifndef _AFM3000_H_
#define _AFM3000_H_
#include "i2c.h"
/* i2c句柄 */
#define AFM_hi2c hi2c1
/* AFM偏移量和空气的流量系数 (不知道可不可以修改,也不知道原始值是不是这个)*/
#define AFM_Offset 32000 // 偏移量
#define AFM_FlowCoeff 140 // 空气的流量系数
/* 外部函数区 */
HAL_StatusTypeDef afm3000_Reset(void);
HAL_StatusTypeDef afm3000_GetVal(uint16_t * afmGetVal);
HAL_StatusTypeDef afm3000_Init(void);
/* // 使用示例 // 初始化 if(afm3000_Init() == HAL_OK) { printf("afm3000_Init OK !"); } else { printf("afm3000_Init failed ..."); } // 获取数据 uint16_t afmGetVal = 0; afm3000_GetVal(&afmGetVal); printf("Flow Val : %d slm", afmGetVal); */
#endif /* _AFM3000_H_ */
main
头文件
#include "stdio.h"
#include "afm3000.h"
初始化
uint16_t afmGetVal = 0;
if(afm3000_Init() == HAL_OK)
{
printf("afm3000_Init OK !");
}
else
{
printf("afm3000_Init failed ...");
}
循环中
afm3000_GetVal(&afmGetVal);
printf("Flow Val : %d slm", afmGetVal);
HAL_Delay(500);
边栏推荐
猜你喜欢

HCIP 交换实验

MySQL: Integrity Constraints and Table Design Principles

参数优化。

双指针方法

8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!

onlyoffice设置跟踪变化trackChanges默认为对自己启动

线程必备内容

航企纠缠A350安全问题 空客主动取消飞机订单

无线Mesh自组网方案,CV5200无线模组应用,支持高清数据远距离传输

Multimedia and Internet of Things technology make the version "live" 129 vinyl records "Centennial Voice"
随机推荐
华为开源:聚焦开源基础软件,共建健康繁荣生态
【C补充】指针相关知识点收集01
Techwiz OLED:OLED器件的发光效率
HTB-Nibbles
LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss
数据万象内容审核 — 共建安全互联网,专项开展“清朗”直播整治行动
CompletableFuture接口核心方法介绍
mae,mse,rmse分别利用sklearn和numpy实现
Introduction to Mysql storage engine
XCTF-reverse-signin
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三
八、MFC对话框
请问下Flink SQL如何写hologres分区表?我想要每天一个分区
There are 12 balls, including 11 weight, only one, don't know is light or heavy. Three times in balance scales, find out the ball.
转转测试环境的标签域名实践
Acwing 3208. Z字形扫描 偏移量+扩展图
OD-Model【5】:YOLOv1
Servlet基础详细版
参数优化。