标签:javascript 开源硬件 espruino stm32 arduino
http://blog.csdn.net/qwert1213131/article/details/28845147
本文属于个人理解,能力有限,纰漏在所难免,还望指正!
【小鱼有点电】
var ow = new OneWire(pin); var device = ow.search()[0]; // 获取器件ID if (device===undefined) print("No OneWire devices found"); ow.reset(); ow.select(device); ow.write(42); var result = ow.read()对于DS18B20,官方已经提供了相关模块,可以直接调用,非常方便,具体操作如下:
var ow = new OneWire(B1); var sensor = require("DS18B20").connect(ow); setInterval(function() { console.log(sensor.getTemp());//1s打印一次温度值 }, 1000);需要注意的是第一次获取的温度值是不正确的需要舍弃。如下图红框所示:
sensor.getRes();若要设置分辨率为9,则使用函数setResearch(res),其中res的值必须为9、10、11、12当中的一个;
sensor.setRes(9);我们除了可以让测量值显示在终端上,还能让其在LCD上显示,代码如下:
//DS18B20 var ow = new OneWire(B1); var sensor = require("DS18B20").connect(ow); //Sharp Memory LCD SPI1.setup({ sck:B3, mosi:B5, order: "lsb", baud: 1000000 }); var g = require("MemoryLCD").connect(SPI1, B4/*SCS*/, C12/*EXTCOMIN*/,128/*width*/, 128/*height*/); //在LCD上显示温度 function showTemp(){ g.clear(); g.setFontVector(32); g.drawString("Temp.",0,0); g.drawLine(0,40,g.getWidth(),40); g.drawLine(0,41,g.getWidth(),41); g.drawLine(0,42,g.getWidth(),42); g.setFontVector(20); g.drawString(sensor.getTemp(),20,45); g.flip(); } //1s更新频率 setInterval(showTemp, 1000);
var ow = new OneWire(B1); var temp = require("DS18B20").connect(ow); clearInterval(); SPI1.setup({ sck:B3, mosi:B5, order: "lsb", baud: 1000000 }); var g = require("MemoryLCD").connect(SPI1, B4/*SCS*/, C12/*EXTCOMIN*/,128/*width*/, 128/*height*/); var history = new Int8Array(g.getWidth()); function onTimer() { // 获取温度 var t = temp.getTemp(); for (var i=1;i<history.length;i++) history[i-1] = history[i]; history[history.length-1] = t; // 函数,温度转化为lcd的y坐标 var tempToY = function(t) { return 50-t; }; // 画图 g.clear(); g.moveTo(0,tempToY(history[0])); for (i=1;i<history.length;i++) g.lineTo(i,tempToY(history[i])); g.setFontVector(10); g.drawString("Temp:",0,64); g.setFontVector(16); g.drawString(t,40,64); // 显示到LCD上 g.flip(); } setInterval(onTimer, 500);//500ms更新一次数据
【Espruino】NO.10 数字温度传感器DS18B20,布布扣,bubuko.com
【Espruino】NO.10 数字温度传感器DS18B20
标签:javascript 开源硬件 espruino stm32 arduino
原文地址:http://blog.csdn.net/qwert1213131/article/details/28845147