查看: 2507|回复: 0

Micropython TPYBoard读取芯片上的温度传感器

[复制链接]
  • TA的每日心情
    慵懒
    2018-1-6 09:01
  • 签到天数: 7 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2018-3-30 09:06:48 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 酥小小酥 于 2018-3-30 09:55 编辑

    STM32 内部温度传感器概要

    STM32 芯片内部一项独特的功能就是内部集成了一个温度传感器, 因为是内置, 所以测试的是芯片内部的温度, 如果芯片外接负载一定的情况下, 那么芯片的发热也基本稳定, 相对于外界的温度而言, 这个偏差值也是基本稳定的. 也就是说用 STM32 内部传感器来测量外界环境的温度。 在一些恶劣的应用环境下面, 可以通过检测芯片内部而感知设备的工作环境温度, 如果温度过高或者过低了 则马上睡眠或者停止运转. 可以保证您的设备工作的可靠性。

    STM32内部温度传感器参数

    1.STM32内部温度传感器与ADC的通道16相连,与ADC配合使用实现温度测量。

    2.测量范围–40~125℃,精度±1.5℃。

    3.温度传感器产生一个随温度线性变化的电压,转换范围在2V < VDDA < 3.6V之间。转换公式如下图所示: ca0cf13c85f45ee83bf2e3c823af85c.png


    手册中对于公式中的参数说明:

    83b6d45719cf91dd24c4571a6726ba4.png

    读取温度的实现原理

    写代码的时候, 在测量要求不怎么高的情况下, 公式可以简化。简化的公式:

    Temperature= (1.42 - ADC_Value*3.3/4096)*1000/4.35 + 25

    程序编写:

    1.初始化ADC , 初始化DMA

      注意:内部温度传感器是使用了 ADC1 的第 16 通道哦


    2.ADC_TempSensorVrefintCmd(ENABLE);

    使能温度传感器和内部参考电压通道


    3.按照刚才列出的公式计算

    Temperature= (1.42 - ADC_Value*3.3/4096)*1000/4.35 + 25;

    TPYBoard读取温度例程

    7a558b1b106105338ecb818d2d27e64.png

    main:

    1. # main.py -- put your code here!
    2. import pyb
    3. import time
    4. import stm
    5. from pyb import Pin

    6. def adcread(chan):                              # 16 temp 17 vbat 18 vref
    7.         assert chan >= 16 and chan <= 18, 'Invalid ADC channel'
    8.         start = pyb.millis()
    9.         timeout = 100
    10.         stm.mem32[stm.RCC + stm.RCC_APB2ENR] |= 0x100 # enable ADC1 clock.0x4100
    11.         stm.mem32[stm.ADC1 + stm.ADC_CR2] = 1       # Turn on ADC
    12.         stm.mem32[stm.ADC1 + stm.ADC_CR1] = 0       # 12 bit
    13.         if chan == 17:
    14.                 stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x200000 # 15 cycles
    15.                 stm.mem32[stm.ADC + 4] = 1 << 23
    16.         elif chan == 18:
    17.                 stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x1000000
    18.                 stm.mem32[stm.ADC + 4] = 0xc00000
    19.         else:
    20.                 stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x40000
    21.                 stm.mem32[stm.ADC + 4] = 1 << 23
    22.         stm.mem32[stm.ADC1 + stm.ADC_SQR3] = chan
    23.         stm.mem32[stm.ADC1 + stm.ADC_CR2] = 1 | (1 << 30) | (1 << 10) # start conversion
    24.         while not stm.mem32[stm.ADC1 + stm.ADC_SR] & 2: # wait for EOC
    25.                 if pyb.elapsed_millis(start) > timeout:
    26.                         raise OSError('ADC timout')
    27.         data = stm.mem32[stm.ADC1 + stm.ADC_DR]     # clear down EOC
    28.         stm.mem32[stm.ADC1 + stm.ADC_CR2] = 0       # Turn off ADC
    29.         return data

    30. def v33():
    31.         return 4096 * 1.21 / adcread(17)

    32. def vbat():
    33.         return  1.21 * 2 * adcread(18) / adcread(17)  # 2:1 divider on Vbat channel

    34. def vref():
    35.         return 3.3 * adcread(17) / 4096

    36. def temperature():
    37.         return 25 + 400 * (3.3 * adcread(16) / 4096 - 0.76)

    38. adc = pyb.ADCAll(12)
    39. leds = [pyb.LED(i) for i in range(1,5)]

    40. sw=pyb.Switch()
    41. def test():
    42.         pyb.LED(1).on()
    43.         pyb.LED(2).on()
    44.         pyb.LED(3).on()
    45.         pyb.LED(4).on()
    46.         pyb.delay(2000)
    47. sw.callback(test)

    48. for l in leds:
    49.         l.off()

    50. n = 0

    51. try:
    52.    while True:
    53.           n = (n + 1) % 4
    54.           leds[n].toggle()
    55.           pyb.delay(50)
    56.           print('v33:',v33())
    57.           print('vbat:',vbat())
    58.           print('vref:',vref())
    59.           print('temperature:',temperature())
    60. finally:
    61.         for l in leds:
    62.                 l.off()
    63. Next  Previous
    复制代码


    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-5-15 09:40 , Processed in 0.107882 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.