“数码管”的版本间的差异
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献) (→概述) |
Tangliufeng(讨论 | 贡献) (→MicroPython示例) |
||
第28行: | 第28行: | ||
from microbit import * | from microbit import * | ||
− | + | _TubeTab = [ | |
− | + | 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, | |
− | + | 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x00, 0x40] | |
− | |||
− | |||
− | |||
− | + | def tm1650Init(): | |
+ | i2c.write(0x24, bytearray([0x01])) | ||
+ | def tm1650DisplayUint(x): | ||
+ | charTemp = [0, 0, 0, 0] | ||
+ | x = (x if x < 10000 else 9999) | ||
+ | charTemp[3] = x%10 | ||
+ | charTemp[2] = (x//10)%10 | ||
+ | charTemp[1] = (x//100)%10 | ||
+ | charTemp[0] = (x//1000)%10 | ||
+ | if x < 1000: | ||
+ | charTemp[0] = 0x10 | ||
+ | if x < 100: | ||
+ | charTemp[1] = 0x10 | ||
+ | if x < 10: | ||
+ | charTemp[2] = 0x10 | ||
+ | for i in range(0, 4): | ||
+ | i2c.write(0x34+i, bytearray([_TubeTab[charTemp[i]]])) | ||
+ | def tm1650DisplayInt(x): | ||
+ | x = round(x) | ||
+ | if x >= 0: | ||
+ | tm1650DisplayUint(x) | ||
+ | else: | ||
+ | temp = (x if x > -999 else -999) | ||
+ | temp = abs(temp) | ||
+ | tm1650DisplayUint(temp) | ||
+ | if temp < 10: | ||
+ | i2c.write(0x36, bytearray([_TubeTab[0x11]])) | ||
+ | elif temp < 100: | ||
+ | i2c.write(0x35, bytearray([_TubeTab[0x11]])) | ||
+ | elif temp < 1000: | ||
+ | i2c.write(0x34, bytearray([_TubeTab[0x11]])) | ||
+ | |||
+ | tm1650Init() | ||
+ | |||
+ | display.off() | ||
while True: | while True: | ||
− | + | tm1650DisplayInt(7777) | |
− | sleep( | + | sleep(1000) |
+ | tm1650DisplayInt(8888) | ||
+ | sleep(1000) | ||
+ | tm1650DisplayInt(9999) | ||
+ | sleep(1000) | ||
+ | tm1650DisplayInt(1111) | ||
+ | sleep(1000) | ||
+ | |||
</pre> | </pre> | ||
=== 图形化示例 === | === 图形化示例 === |
2017年12月19日 (二) 15:32的版本
概述
I2C通讯,显示4位数字和小数点,3.6寸红色,四位7段。
技术参数
- 工作电压:3.3V~5V
- 接口方式:I2C接口
- 超声波测量范围:3cm~300cm
- 误差:±1%
- 模块尺寸:
引脚定义
VCC | 电源 |
SDA | I2C数据 |
SCL | I2C时钟 |
GND | 地 |
使用教程
连接示意图
Arduino示例
MicroPython示例
from microbit import * _TubeTab = [ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x00, 0x40] def tm1650Init(): i2c.write(0x24, bytearray([0x01])) def tm1650DisplayUint(x): charTemp = [0, 0, 0, 0] x = (x if x < 10000 else 9999) charTemp[3] = x%10 charTemp[2] = (x//10)%10 charTemp[1] = (x//100)%10 charTemp[0] = (x//1000)%10 if x < 1000: charTemp[0] = 0x10 if x < 100: charTemp[1] = 0x10 if x < 10: charTemp[2] = 0x10 for i in range(0, 4): i2c.write(0x34+i, bytearray([_TubeTab[charTemp[i]]])) def tm1650DisplayInt(x): x = round(x) if x >= 0: tm1650DisplayUint(x) else: temp = (x if x > -999 else -999) temp = abs(temp) tm1650DisplayUint(temp) if temp < 10: i2c.write(0x36, bytearray([_TubeTab[0x11]])) elif temp < 100: i2c.write(0x35, bytearray([_TubeTab[0x11]])) elif temp < 1000: i2c.write(0x34, bytearray([_TubeTab[0x11]])) tm1650Init() display.off() while True: tm1650DisplayInt(7777) sleep(1000) tm1650DisplayInt(8888) sleep(1000) tm1650DisplayInt(9999) sleep(1000) tm1650DisplayInt(1111) sleep(1000)