当前位置:网站首页>cubemx stm32 afm3000 module gas flow sensor driver code
cubemx stm32 afm3000 module gas flow sensor driver code
2022-08-04 10:31:00 【Embed it?】
afm3000
- AFM3000 The sensor is a digital flow meter from Osun Electronics,Specifically designed for ventilator applications.It measures air with great precision,Flow of oxygen and other non-aggressive gases.Special design is adopted in the air duct,This results in a very low pressure loss of the fluid passing through the sensor,Make its performance suitable for a variety of demanding application scenarios,Such as medical ventilation and respiratory applications.
- AFM3000 采用 5V supply voltage operation,具有数字 I2C 接口.Output measurements are internally calibrated and temperature compensated.
- The superior performance of this sensor is based on Osun Electronics' sensor technology,The interior consists of a thermal sensor chip and a high-performance integration 24 位 AD 采集的 CMOS connected to the microprocessor.Gas flow is measured by thermal sensor chips,This ensures very fast signal processing times and best-in-class accuracy with bidirectional measurement capabilities.
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 Get data by pointer 返回值 : 来自 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一次数据,Double the delay to be on the safe side
afmState = afm3000_read(AFM_FlowRead, afmVal, 3);
*afmGetVal = (afmVal[0]<<8 | afmVal[1]); //I don't know if it's high or low,先测试一波
*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(); // reset first
// The data obtained the previous few times was inaccurate,去掉
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
/* AFMOffset and flow coefficient of air (不知道可不可以修改,I don't know if the original value is this or not)*/
#define AFM_Offset 32000 // 偏移量
#define AFM_FlowCoeff 140 // Air flow coefficient
/* 外部函数区 */
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);
边栏推荐
猜你喜欢
随机推荐
无代码平台附件上传入门教程
学习在php中将特大数字转成带有千/万/亿为单位的字符串
Camunda整体架构和相关概念
MySQL核心SQL:结构化查询语句SQL、库操作、表操作、CRUD
onlyoffice设置跟踪变化trackChanges默认为对自己启动
MySQL之my.cnf配置文件
华为开源:聚焦开源基础软件,共建健康繁荣生态
Introduction to the core methods of the CompletableFuture interface
ThreadLocal详细分析
2万字50张图玩转Flink面试体系
无代码平台多行文字入门教程
Win11文件资源管理器找不到选项卡怎么办?
航企纠缠A350安全问题 空客主动取消飞机订单
STM32入门开发 制作红外线遥控器(智能居家-万能遥控器)
Person.class.getInterfaces() 注意使用方法
XCTF-easy_Maze
语音社交app源码——具备哪些开发优势?
Mysql 存储引擎简介
LeetCode中等题之旋转图像
移动端 开源低代码工具 beeware 和 kivy