当前位置: 首页 > article >正文

H7-TOOL的LUA小程序教程第15期:电压,电流,NTC热敏电阻以及4-20mA输入(2024-10-21,已经发布)

LUA脚本的好处是用户可以根据自己注册的一批API(当前TOOL已经提供了几百个函数供大家使用),实现各种小程序,不再限制Flash里面已经下载的程序,就跟手机安装APP差不多,所以在H7-TOOL里面被广泛使用,支持在线调试运行,支持离线运行。TOOL的LUA教程争取做到大家可以无痛调用各种功能函数,不需要学习成本。


简介

电压,电流,NTC热敏电阻以及4-20mA输入,可以在上位机端设置,也可以显示屏端设置

详细使用说明可以看在线或者离线操作说明手册:H7-TOOL操作说明和客户常见问题汇总贴,含PDF离线版(2024-08-16) - H7-TOOL开发工具 - 硬汉嵌入式论坛 - Powered by Discuz!

建议优先熟悉下,特别是这几个功能对应使用的引脚。


LUA函数说明:

1、启动模拟量采集。

启动模拟量采集仅需用到两个大类配置,一个负载电流测量,还有一个低速多通道。

所以启动模拟信号采集封装了两种配置

(1)负载电流测量,配置代码固定如下:

function start_dso(void)
        write_reg16(0x01FF, 1) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end

(2)低速多通道测量,配置代码固定如下:

--启动模拟量电路
function start_dso(void)
        write_reg16(0x01FF, 2) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end

2、测量函数,读取模拟值

测量函数比较简单,周期调用即可,建议100ms以上读取一次,因为所有数据100ms更新一轮

read_analog(9) --9 - 读取4-20mA


(1)电压读取

read_analog(0) -- 0 - CH1电压
read_analog(1) -- 1 - CH2电压

举例:每500ms读取一次CH1和CH2通道电压

实现代码如下:

--启动模拟量电路
function start_dso(void)
        write_reg16(0x01FF, 2) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end
 
print("启动电压测量")
start_dso() -- 调用一次初始化
 
for i = 1, 10, 1 do -- 读取10次
data1 = read_analog(0) -- 0 - CH1电压
data2 = read_analog(1) -- 1 - CH2电压
print(string.format("CH1电压:%f,CH2电压:%f", data1,data2))
delayms(500)
end

实际效果:


(2)高侧负载测量

read_analog(2) --2 - 高侧负载电压
read_analog(3) --3 - 高端负载电流

举例:每500ms读取一次

实现代码如下

--启动模拟量电路
function start_dso(void)
        write_reg16(0x01FF, 1) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end
 
print("启动高侧测量")
start_dso() -- 调用一次初始化
 
for i = 1, 10, 1 do -- 读取10次
data1 = read_analog(2) --2 - 高侧负载电压
data2 = read_analog(3) --3 - 高端负载电流
print(string.format("负载电压:%f,负载电流:%f", data1,data2))
delayms(500)
end

实际效果:


(3)TVCC测量

read_analog(4) --4 - TVCC电压
read_analog(5) --5 - TVCC电流

举例:每500ms读取一次

实现代码如下:

--启动模拟量电路
function start_dso(void)
        write_reg16(0x01FF, 2) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end
 
print("启动TVCC测量")
start_dso() -- 调用一次初始化
 
for i = 1, 10, 1 do -- 读取10次
data1 = read_analog(4) --4 - TVCC电压
data2 = read_analog(5) --5 - TVCC电流
print(string.format("TVCC电压:%f,TVCC电流:%f", data1,data2))
delayms(500)
end

(4)NTC热敏电阻测量

read_analog(6) --6 - NTC热敏电阻阻值

举例:每500ms读取一次

--启动模拟量电路
function start_dso(void)
        write_reg16(0x01FF, 2) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end
 
print("启动NTC热敏电阻测量")
start_dso() -- 调用一次初始化
 
for i = 1, 10, 1 do -- 读取10次
data1 = read_analog(6) --6 - NTC热敏电阻阻值
print(string.format("NTC热敏电阻:%f", data1))
delayms(500)
end

(5)供电电压测量

read_adc(7) --7 - 外部供电电压
read_analog(8) --8 - USB供电电压

举例:每500ms读取一次

--启动模拟量电路
function start_dso(void)
        write_reg16(0x01FF, 2) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end
 
print("启动供电电压测量")
start_dso() -- 调用一次初始化
 
for i = 1, 10, 1 do -- 读取10次
data1 = read_adc(7) --7 - 外部供电电压
data2 = read_analog(8) --8 - USB供电电压
print(string.format("外部供电电压:%f, USB供电电压:%f", data1, data2))
delayms(500)
end

(6)4-20mA测量
read_analog(9) -- 4-20mA测量

举例:每500ms读取一次

--启动模拟量电路
function start_dso(void)
        write_reg16(0x01FF, 2) -- 测量模式 0:示波器 1:负载电流 2:多路低速扫描
        write_reg16(0x0200, 1) -- CH1耦合,0:AC 1:DC
        write_reg16(0x0201, 1) -- CH2耦合,0:AC 1:DC
 
        --量程取值 0:±13.8V 1:±6.4V 2:±3.2V 3:±1.6V 4:±800mV 5:±400mV 6:±200mV 7:±100mV
        write_reg16(0x0202, 0) -- CH1量程
        write_reg16(0x0203, 0) -- CH2量程
        write_reg16(0x0204, 0) -- CH1通道直流偏值,未用
        write_reg16(0x0205, 0) -- CH2通道直流偏值,未用
        write_reg16(0x0206, 12) --采样频率 0:100 1:200 2:500 3:1K 4:2K 5:5K 6:10K 7:20K
                                                        --8:50K 9:100K 10:200K 11:500K 12:1M 13:2M 14:5M
        write_reg16(0x0207, 0) --采样深度 0:1K 1:2K 3:4K 4:8K 5:16K 6:32K
        write_reg16(0x0208, 32768) --触发电平ADC 0-65535
        write_reg16(0x0209, 50) --触发位置百分比 0-100
        write_reg16(0x020A, 0) --触发模式 0:自动 1:普通 2:单次
        write_reg16(0x020B, 0) --触发通道 0:CH1 1:CH2
        write_reg16(0x020C, 0) --触发边沿 0:下降沿 1:上升沿 
        write_reg16(0x020D, 0x03) --通道使能控制 bit0 = CH1  bit1 = CH2
        write_reg16(0x020E, 1) --采集控制 0:停止 1:启动
  end
 
print("启动4-20mA测量")
start_dso() -- 调用一次初始化
 
for i = 1, 10, 1 do -- 读取10次
data1 = read_analog(9) -- 4-20mA测量
print(string.format("4-20mA读取:%f", data1, data2))
delayms(500)
end

测量的10mA,精度还是非常不错的


3、测量函数,直接读取ADC值

这个用法和第2步读取模拟值是完全一样的。只是这里获取的是ADC支持。

read_adc(0) --0 - CH1电压
read_adc(1) --1 - CH2电压
read_adc(2) --2 - 高侧负载电压
read_adc(3) --3 - 高端负载电流
read_adc(4) --4 - TVCC电压
read_adc(5) --5 - TVCC电流
read_adc(6) --6 - NTC热敏电阻阻值
read_adc(7) --7 - 外部供电电压
read_adc(8) --8 - USB供电电压
read_adc(9) -- 9 - 4-20mA输入
 

4、使用上位机同时展示这些数值


http://www.kler.cn/news/364932.html

相关文章:

  • 基于SpringBoot的“高校校园点餐系统”的设计与实现(源码+数据库+文档+PPT)
  • Lua for循环语句
  • 3D图片动画效果组件封装
  • 【WebSocket实战】——创建项目初始架构
  • 汽车级DC-DC转换器英飞凌TLF35584
  • 【c++ arx选项板】
  • 【Linux】命令行参数环境变量
  • vue 对象拷贝,解决引用赋值内容变化会修改原对象的方式
  • 【K8S】快速入门Kubernetes
  • Lua 语言中的注释详解
  • 本地原生多IPseo建站
  • 阿里云的 ALB (Application Load Balancer) 然后到 nginx 和具体服务时,如果超过 60 秒请求失败
  • 【真题笔记】09-12年系统架构设计师要点总结
  • tp8框架中有那些主要异常
  • 【更新】2024年国家自然科学基金立项名单(经管类)
  • 使用LangChain进行LLM应用开发(2)——理解模型I/O (模型、提示词、输出)
  • 自己写一个Markdown解析器的最简单的html页面,方便gpt回复内容解析成你喜欢的样子
  • 二百七十、Kettle——ClickHouse中增量导入清洗数据错误表
  • yolov9目标检测/分割预测报错AttributeError: ‘list‘ object has no attribute ‘device‘常见汇总
  • HarmonyOS NEXT原生重榜发布-安利一款鸿蒙可视化代码生成器
  • 爬虫python=豆瓣Top250电影
  • 基于大型语言模型的智能网页抓取
  • 【spring】从spring是如何避免并发下获取不完整的bean引发的思考 什么是双重检查锁 什么是java内存模型
  • WeThinkIn | 从图像到视频:浅谈Video Diffusion Models背后的底层原理
  • Servlet实现博客系统
  • 谷歌新政来袭!涉及社交、通信、实用工具等类型应用