micro:bit是否能连外接的温度测量模块测水温?
如题,想知道micro:bit是否能连外接的温度测量模块测水温,如果可以的话代码怎么设置。。用热敏电阻试了试好像都不行。。。尴尬有的,https://www.dfrobot.com.cn/goods-799.html防水温度传感器套件 rzyzzxw 发表于 2018-1-8 19:20
有的,https://www.dfrobot.com.cn/goods-799.html防水温度传感器套件
那代码直接读pin的analog吗?不读temp? ye.jin2018 发表于 2018-1-9 10:10
那代码直接读pin的analog吗?不读temp?
应该是的。后面我下单买个也用用。:) 可能不行,18B20是单线的传感器,需要库的支持。 本帖最后由 Rockets 于 2018-1-10 13:02 编辑
有个链接,可以给你们参考下
https://www.iot-programmer.com/index.php/books/27-micro-bit-iot-in-c/chapters-micro-bit-iot-in-c/24-micro-bit-and-ds18b20-1-wire-temperature-sensor
连线图
#include "MicroBit.h"
int init();
void sendskip();
void writeBit(int);
void sendOne();
void sendZero();
void writeByte(int);
int readBit();
int convert();
int readByte();MicroBit uBit;
MicroBitPin P0 = uBit.io.P0;
MicroBitPin P1 = uBit.io.P1;
uBit.init();
P0.setDigitalValue(1);
P0.setPull(PullUp);
P1.getDigitalValue();
uBit.sleep(1);
while (1) {
init();
writeByte(0xCC);
int r = convert();
init();
writeByte(0xCC);
writeByte(0xBE);
int b1 = readByte();
int b2 = readByte();
int16_t temp = (b2 << 8 | b1);
temp = temp * 100 / 16;char buff;
sprintf(buff, "%d.%d", temp / 100, abs(temp % 100));
uBit.display.scroll(buff, 200);
uBit.sleep(1000);
}
release_fiber();
}
int init() {
volatile int i;P0.setDigitalValue(0);
for (i = 0; i < 600; i++) {
};
P0.setDigitalValue(1);
for (i = 0; i < 30; i++) {
};
int b = P1.getDigitalValue();
for (i = 0; i < 600; i++) {
};
return b;
}
void sendZero() {
volatile int i;
P0.setDigitalValue(0);
for (i = 1; i < 75; i++) {
};
P0.setDigitalValue(1);
for (i = 1; i < 6; i++) {
};
}
void sendOne() {
volatile int i;
P0.setDigitalValue(0);
for (i = 1; i < 1; i++) {
};
P0.setDigitalValue(1);
for (i = 1; i < 80; i++) {
};
}
void writeBit(int b) {
volatile int i;
int delay1, delay2;
if (b == 1) {
delay1 = 1;
delay2 = 80;
} else {
delay1 = 75;
delay2 = 6;
}
P0.setDigitalValue(0);
for (i = 1; i < delay1; i++) {
};
P0.setDigitalValue(1);
for (i = 1; i < delay2; i++) {
};
}
void sendskip() {
writeBit(0);
writeBit(0);
writeBit(1);
writeBit(1);
writeBit(0);
writeBit(0);
writeBit(1);
writeBit(1);
}
void writeByte(int byte) {
int i;
for (i = 0; i < 8; i++) {
if (byte & 1) {
writeBit(1);
} else {
writeBit(0);
}
byte = byte >> 1;
}
}
int readBit() {
volatile int i;
P0.setDigitalValue(0);
P0.setDigitalValue(1);
for (i = 1; i < 20; i++) {
};
int b = P1.getDigitalValue();
for (i = 1; i < 60; i++) {
};
return b;
}
int convert() {
volatile int i;
int j;
writeByte(0x44);
for (j = 1; j < 1000; j++) {
for (i = 1; i < 900; i++) {
};
if (readBit() == 1)break;
};
return (j);
}
int readByte() {
int byte = 0;
int i;
for (i = 0; i < 8; i++) {
byte = byte | readBit() << i;
};
return byte;
}
Rockets 发表于 2018-1-10 12:45
可能不行,18B20是单线的传感器,需要库的支持。
啊好的谢谢! rzyzzxw 发表于 2018-1-9 19:46
应该是的。后面我下单买个也用用。
好的!谢谢! ye.jin2018 发表于 2018-1-10 15:36
啊好的谢谢!
用不了啊。用不了用到arduino. Rockets 发表于 2018-1-10 12:49
有个链接,可以给你们参考下
https://www.iot-programmer.com/index.php/books/27-micro-bit-iot-in-c/chap ...
啊好的谢谢!
页:
[1]