单片机研发入门:一款简单的温湿度仪表的软硬件设计

in #steempress5 years ago (edited)

温湿度在人们日常生活中应用非常广泛,因此,本文就简单地说明一下如何设计一款基本的温湿度测量仪。

硬件设计:

仪表的的硬件可分为八个部分。它们是:
【A】单片机控制器、
【B】传感器、
【C】频率型A/D、
【D】段码LCD、
【E】电源和复位、
【F】键盘、
【G】JTAG和引导接口、
【H】UART。
image.png
图一

描述:

【A】单片机控制器:
选择低压低功耗MCS,MSP430F413。

【B】传感器:
湿度传感器为电容性传感器,温度传感器为NTC热敏电阻。

【C】频率型A/D:
将被测信号转为频率信号,方便测量,见图2和图3。
image.png
图2

在图2中,使用555将C19(湿度传感器)的电容转换为约20kHz的频率,因为555振荡器的稳定性约为0.1%,为了提高测量精度,有一个参考通道C18(150P)。

image.png
图3

图3的理论与图2相同,但在图3中,将NTC的电阻转换为25℃时约为50赫兹的频率,此外电路中还有一个外部传感器通道,用于测量其他温度。

【D】段码LCD:
LCD由MSP430F413内置的驱动模块驱动,驱动程序的能力为96段。显示内容由三个四位数的值和一些标志组成。

【E】电源和复位:
仪表由两个PCS AAA电池供电,因此所有电路的电源直接由两个PCS AAA电池供电。
当电池电压低于2.6V时,仪表将显示“电池电量低”。当电源电压低于1.8V时,仪表将复位。

【F】键盘:
小键盘上有六个键,分别是“开/关”、“保持”、“摄氏度/华氏度”、“重置”、“模式”和“mn/mx”。按键连接到p6,特别是“开/关”按键同时连接到p1,因为p6没有中断能力。

【G】JTAG和引导接口:
此接口主要用于调试和编程。

【H】UART:
UART在P1.0传输数据,并从P1.1接收数据。

电路中有一个4.000MHz的外部振荡器,它是TA的时钟源,因为XT1太慢,另一方面LCD驱动器需要这个慢的振荡器。见图4。
image.png
图4


软件设计:

该仪表的软件设计主要有八个模块,分别是:
main.c that primarily includes main( ), state_process( ) and so on.

measurement.c that measures the cycle of the oscillator generated from RH and Temp sensors circuits.

dataprocess.c that processes the original data from measure.c.

calibration.c that involves calibration( ) just as its name implies.

display.c that controls the contents of LCD.

info_flash.c that includes flashErase( ) and flashWriteInt( ).

newkeypad.c that scans the keypad and throws out pre_keycode properly.

uart9600.c that transmits RS232 data of RH and Temp one time per second.

描述:

main.c 模块:
其基本功能是初始化MSP430的所有外围设备、仪表硬件等,处理主回路,监控仪表状态。在主循环中有如下操作:

a)调用 dataprocess( ).

b)生成 key_code。from pre_keycode, pre_keycode must be transferred to key_code to ensure the key code is not variable in the whole loop especially in state_process( ), because pre_keycode is thrown out in interrupt of TA at any moment.

c)State_process( ) 检测状态,依照State_machine_chart控制流程

d)Communication( ) 利用uart传输温湿度测量值.

e)DisplayUpdate( ), 用于LCD显示。

State_machine_chart
image.png

measurement.c 模块:
将CCR0和CCR1设置为捕获模式,以测量相对湿度和温度的振荡器的周期。每一秒分为八个阶段,具体如下:

a)阶段0,开始测量外部湿度传感器和内部温度传感器。

b)第1阶段,结束对Rh传感器的测量,将Rh控制针交替到参考通道,并请求dataprocess.c进行计算。

c)第2阶段,开始测量参考电容器。

d)第3阶段,结束对基准电容器和温度传感器的测量,将相对湿度控制针切换到传感器通道,温度控制切换到基准通道,并请求dataprocess.c进行计算。

e)第4阶段,开始测量参考电阻,82.5K。

f)第5阶段,结束参考电阻测量,并将交流温度控制针连接到外部传感器通道,并请求dataprocess.c进行计算。

g)第6阶段,开始测量外部温度传感器。

h)第7阶段,结束外部温度传感器的测量,将温度控制针交替插入内部传感器通道,并请求dataprocess.c进行计算。

顺便说一下,在TA中断中处理所有计时器。

dataprocess.c 模块:
利用第7阶段测量值的原始数据,每秒计算各种数值。

a)内外温度计算:首先根据传感器和参考电阻的循环值得到传感器的电阻,然后查找温度传感器表(电阻与温度),得到最终的温度值。

b)Rh计算:首先根据传感器和参考电容的周期值得到传感器的电容,然后利用Rh传感器的经验公式计算Rh值,最后进行补偿得到Rh值。

c)计算dp和wbt。

calibration.c 模块:
整个校准有四个步骤,参见下面的图表。

a)32.8%校准,将Rh传感器放入氯化镁饱和盐溶液瓶中,等待半小时。

b)32.8%校准完成,暂时保存部分参数。

c)75.2%校准,将Rh传感器放入氯化钠饱和盐溶液瓶中,等待半小时。

d)75.2%校准完成,最后计算校准参数并保存到信息闪存中,直到现在整个校准确实完成。
image.png

display.c 模块:
显示内容分为两种类型,一种是图标,另一种是数字。

a)图标显示:
构造一个常量,其元素是显示项的代码,代码的高半部分表示LCDMEM的缓冲区编号,低半部分表示特定LCDMEM缓冲区的位编号。在dispupdate()中,根据仪表和标志的状态标识亮起和关闭的项目,然后调用dispitem()完成项目显示。

b)数字显示:
整个LCD上有三个数字,第一步处理数字缓冲区(tmpbuf[3]),根据状态,数字值为整数,因为转换成asc字符串比较方便,第二步调用dispint()实现数字显示,在dispint()中先将数字转换成asc字符串,然后显示每个asc字符。通过查找ascii[13]执行。

c)闪烁:
如果某些项目或数字需要闪烁,则要做的只是在每秒钟的前半部分显示它们,而不是在下半部分显示它们。

Info_flash.c 模块:
该模块中只有两个功能,即flash erase()和flash write uint(),第一个功能是擦除整个闪存段,第二个功能是写入一个uint到特定地址,请注意,从大小浮动的角度来看,由两个uint组合而成。

newkeypad.c 模块:
该模块的功能是每32.7675毫秒扫描一次键盘,得到按键的按键代码,按键扫描流程图如下图所示:
image.png

小键盘的计时分为两种状态和两个临界点,两种状态分别是静态(噪声定时器=0)和动态(噪声定时器!=0)状态,两个临界点是噪声定时器=4和噪声定时器=1。

当noise_timer=0时,根据key_timer得到短key_code、长key_code和多key_code。

当noise_timer=1时,判断此变化是由噪声引起的还是由正常操作引起的,如果以后得到释放短/长键的短键代码。

综上所述,键可以()将短键\代码、长键\代码、多键\代码和释放短键\代码用于任何按键,不管是单键还是复合键。

Uart9600.c 模块:
TA的CCR0用作软UART,这里波特率为9600,如果该UART将发送一个字节,则提前添加标记停止位和末尾的空格开始位,然后在每次中断中启用CCR0的中断,将CCR0增加一位时间,并根据发送的字节位设置或重置TX管脚。

UART的格式为:“hxx x.x%:txx.xc:txx.xclrcrlf”或“hxx.x%:txx.xf:txx.xflrcrlf”,在通信()中计算整个消息(tmpmsg[27]),然后逐字节发送。


作为仪表设计人员,类似的温湿度测量是最基本的要求。能够准确测量温湿度,说明对单片机内部的模块以及底层逻辑都比较清楚了,对硬件线路也有了比较直观的认识。然后通过人机界面等程序的编写,熟悉了编译器,烧录器的使用。

因此,这样的小项目作为入职新人的能力检测是非常有效的。如果你对电子研发有兴趣的话,可以自己尝试一下类似的项目,有什么问题,欢迎交流。


以上内容均由 @patriciaphilip原创,如无授权,请勿引用,谢谢!


Posted from my blog with SteemPress : http://cnstm.org/blog/2019/06/17/rd-basic/

Sort:  

Congratulations @patriciaphilip! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 250 as payout for your posts. Your next target is to reach a total payout of 500

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!



This post has been voted on by the SteemSTEM curation team and voting trail. It is elligible for support from @curie and @utopian-io.

If you appreciate the work we are doing, then consider supporting our witness stem.witness. Additional witness support to the curie witness and utopian-io witness would be appreciated as well.

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Please consider setting @steemstem as a beneficiary to your post to get a stronger support.

Please consider using the steemstem.io app to get a stronger support.

Hi @patriciaphilip!

Your post was upvoted by Utopian.io in cooperation with @steemstem - supporting knowledge, innovation and technological advancement on the Steem Blockchain.

Contribute to Open Source with utopian.io

Learn how to contribute on our website and join the new open source economy.

Want to chat? Join the Utopian Community on Discord https://discord.gg/h52nFrV

Coin Marketplace

STEEM 0.35
TRX 0.12
JST 0.040
BTC 70541.68
ETH 3582.21
USDT 1.00
SBD 4.74