Skip to content

Commit

Permalink
更新 pxt.json, basic.ts, _locales/zh-CN/pxt-PlanetX-strings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
elecfreaks1 committed Oct 8, 2023
1 parent f607f9d commit b502b1e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
3 changes: 3 additions & 0 deletions _locales/zh-CN/pxt-PlanetX-strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@
"PlanetX_Basic.ultrasoundSensor|block": "超声波传感器 %Rjpin 距离 %distance_unit",
"PlanetX_Basic.waterLevel|block": "水位传感器 %Rjpin 水位值(0~100)",
"PlanetX_Basic.writeData|block": "RFID 传感器 IIC接口 将数据 %data 写入卡",
"PlanetX_Basic.ValType.DS18B20_temperature_C|block": "摄氏温度(℃)",
"PlanetX_Basic.ValType.DS18B20_temperature_F|block": "华氏温度(℉)",
"PlanetX_Basic.Ds18b20Temp|block": "DS18B20 温度传感器 %state 连接至 %Rjpin",
"PlanetX_Basic|block": "行星X_基础",
"PlanetX_Display.AnalogRJPin.J1|block": "J1",
"PlanetX_Display.AnalogRJPin.J2|block": "J2",
Expand Down
86 changes: 86 additions & 0 deletions basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2529,4 +2529,90 @@ namespace PlanetX_Basic {
re_value = re_value * 4;
return re_value
}

let sc_byte = 0
let dat = 0
let low = 0
let high = 0
let temp = 0
let temperature = 0
let ack = 0
let lastTemp = 0

export enum ValType {
//% block="temperature(℃)" enumval=0
DS18B20_temperature_C,

//% block="temperature(℉)" enumval=1
DS18B20_temperature_F
}
function init_18b20(mpin: DigitalPin) {
pins.digitalWritePin(mpin, 0)
control.waitMicros(600)
pins.digitalWritePin(mpin, 1)
control.waitMicros(30)
ack = pins.digitalReadPin(mpin)
control.waitMicros(600)
return ack
}
function write_18b20(mpin: DigitalPin, data: number) {
sc_byte = 0x01
for (let index = 0; index < 8; index++) {
pins.digitalWritePin(mpin, 0)
if (data & sc_byte) {
pins.digitalWritePin(mpin, 1)
control.waitMicros(60)
} else {
pins.digitalWritePin(mpin, 0)
control.waitMicros(60)
}
pins.digitalWritePin(mpin, 1)
data = data >> 1
}
}
function read_18b20(mpin: DigitalPin) {
dat = 0x00
sc_byte = 0x01
for (let index = 0; index < 8; index++) {
pins.digitalWritePin(mpin, 0)
pins.digitalWritePin(mpin, 1)
if (pins.digitalReadPin(mpin)) {
dat = dat + sc_byte
}
sc_byte = sc_byte << 1
control.waitMicros(60)
}
return dat
}

//% subcategory=Sensor group="Digital" color=#EA5532
//% block="value of DS18B20 %state at pin %Rjpin"
export function Ds18b20Temp(Rjpin: DigitalRJPin, state: ValType): number {
let pin = RJpin_to_digital(Rjpin)
init_18b20(pin)
write_18b20(pin, 0xCC)
write_18b20(pin, 0x44)
basic.pause(10)
init_18b20(pin)
write_18b20(pin, 0xCC)
write_18b20(pin, 0xBE)
low = read_18b20(pin)
high = read_18b20(pin)
temperature = high << 8 | low
temperature = temperature / 16
if (temperature > 130) {
temperature = lastTemp
}
lastTemp = temperature
switch (state) {
case ValType.DS18B20_temperature_C:
return temperature
case ValType.DS18B20_temperature_F:
temperature = (temperature * 1.8) + 32
return temperature
default:
return 0
}

}
}
2 changes: 1 addition & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"public": false,
"targetVersions": {
"target": "4.0.18",
"target": "6.0.18",
"targetId": "microbit"
},
"supportedTargets": [
Expand Down

0 comments on commit b502b1e

Please sign in to comment.