feat: onewire and ds18b20 driver
This commit is contained in:
parent
31482c9a70
commit
8edaf36a1a
3
.idea/editor.xml
generated
3
.idea/editor.xml
generated
@ -147,7 +147,7 @@
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppAbstractClassWithoutSpecifier/@EntryIndexedValue" value="WARNING" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppIncompleteSwitchStatement/@EntryIndexedValue" value="WARNING" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppDefaultCaseNotHandledInSwitchStatement/@EntryIndexedValue" value="WARNING" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppPossiblyErroneousEmptyStatements/@EntryIndexedValue" value="WARNING" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppPossiblyErroneousEmptyStatements/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppExpressionWithoutSideEffects/@EntryIndexedValue" value="WARNING" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNoDiscardExpression/@EntryIndexedValue" value="WARNING" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppFunctionResultShouldBeUsed/@EntryIndexedValue" value="HINT" type="string" />
|
||||
@ -338,5 +338,6 @@
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CommentTypo/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=IdentifierTypo/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppCompileTimeConstantCanBeReplacedWithBooleanConstant/@EntryIndexRemoved" />
|
||||
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppPossiblyErroneousEmptyStatements/@EntryIndexRemoved" />
|
||||
</component>
|
||||
</project>
|
7
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
7
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="CppCompileTimeConstantCanBeReplacedWithBooleanConstant" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="CppPossiblyErroneousEmptyStatements" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
18
lib/ds18b20/ds18b20.c
Normal file
18
lib/ds18b20/ds18b20.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include "ds18b20.h"
|
||||
#include "onewire.h"
|
||||
|
||||
void DS18B20_startConvert(void) {
|
||||
OneWire_init();
|
||||
OneWire_writeByte(DS18B20_SKIP_ROM);
|
||||
OneWire_writeByte(DS18B20_CONVERT_TEMP);
|
||||
}
|
||||
|
||||
int DS18B20_readTemperature(void) {
|
||||
OneWire_init();
|
||||
OneWire_writeByte(DS18B20_SKIP_ROM);
|
||||
OneWire_writeByte(DS18B20_READ_SCRATCHPAD);
|
||||
uchar T_LSB = OneWire_readByte();
|
||||
uchar T_MSB = OneWire_readByte();
|
||||
int temp = T_MSB << 8 | T_LSB;
|
||||
return temp;
|
||||
}
|
20
lib/ds18b20/ds18b20.h
Normal file
20
lib/ds18b20/ds18b20.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef __TEMP_H
|
||||
#define __TEMP_H
|
||||
|
||||
#ifndef uchar
|
||||
#define uchar unsigned char
|
||||
#endif
|
||||
|
||||
#ifndef uint
|
||||
#define uint unsigned int
|
||||
#endif
|
||||
|
||||
#define DS18B20_SKIP_ROM 0xCC
|
||||
#define DS18B20_CONVERT_TEMP 0x44
|
||||
#define DS18B20_READ_SCRATCHPAD 0xBE
|
||||
|
||||
void DS18B20_startConvert(void);
|
||||
|
||||
int DS18B20_readTemperature(void);
|
||||
|
||||
#endif
|
58
lib/onewire/onewire.c
Normal file
58
lib/onewire/onewire.c
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by 5ANK41 on 2024/11/28.
|
||||
|
||||
#include "onewire.h"
|
||||
|
||||
uchar OneWire_init(void) {
|
||||
uchar i;
|
||||
ONE_WIRE_P = 1;
|
||||
ONE_WIRE_P = 0;
|
||||
i = 247;
|
||||
while (--i);
|
||||
ONE_WIRE_P = 1;
|
||||
i = 32;
|
||||
while (--i);
|
||||
uchar ack = ONE_WIRE_P;
|
||||
i = 247;
|
||||
while (--i);
|
||||
return ack;
|
||||
}
|
||||
|
||||
void OneWire_writeBit(uchar _bit) {
|
||||
uchar i;
|
||||
ONE_WIRE_P = 0;
|
||||
i = 4;
|
||||
while (--i);
|
||||
ONE_WIRE_P = _bit;
|
||||
i = 24;
|
||||
while (--i);
|
||||
ONE_WIRE_P = 1;
|
||||
}
|
||||
|
||||
void OneWire_writeByte(uchar _byte) {
|
||||
for (uchar i = 0; i < 8; i++) {
|
||||
OneWire_writeBit(_byte & 0x01 << i);
|
||||
}
|
||||
}
|
||||
|
||||
uchar OneWire_readBit(void) {
|
||||
uchar i, _bit;
|
||||
ONE_WIRE_P = 0;
|
||||
i = 2;
|
||||
while (--i);
|
||||
ONE_WIRE_P = 1;
|
||||
i = 2;
|
||||
while (--i);
|
||||
_bit = ONE_WIRE_P;
|
||||
i = 24;
|
||||
while (--i);
|
||||
return _bit;
|
||||
}
|
||||
|
||||
uchar OneWire_readByte(void) {
|
||||
uchar _byte = 0x00;
|
||||
for (uchar i = 0; i < 8; i++) {
|
||||
if (OneWire_readBit()) { _byte |= 0x01 << i; }
|
||||
}
|
||||
return _byte;
|
||||
}
|
30
lib/onewire/onewire.h
Normal file
30
lib/onewire/onewire.h
Normal file
@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by 5ANK41 on 2024/11/28.
|
||||
//
|
||||
|
||||
#ifndef ONEWIRE_H
|
||||
#define ONEWIRE_H
|
||||
|
||||
#include <8052.h>
|
||||
|
||||
#ifndef uchar
|
||||
#define uchar unsigned char
|
||||
#endif
|
||||
|
||||
#ifndef uint
|
||||
#define uint unsigned int
|
||||
#endif
|
||||
|
||||
#define ONE_WIRE_P P3_7
|
||||
|
||||
uchar OneWire_init(void);
|
||||
|
||||
void OneWire_writeBit(uchar _bit);
|
||||
|
||||
void OneWire_writeByte(uchar _byte);
|
||||
|
||||
uchar OneWire_readBit(void);
|
||||
|
||||
uchar OneWire_readByte(void);
|
||||
|
||||
#endif //ONEWIRE_H
|
@ -1,78 +0,0 @@
|
||||
#include "temp.h"
|
||||
|
||||
void Delay1ms(uint y) {
|
||||
uint x;
|
||||
for(; y > 0; y--) {
|
||||
for(x = 110; x > 0; x--);
|
||||
}
|
||||
}
|
||||
|
||||
uchar Ds18b20Init(void) {
|
||||
uchar i;
|
||||
DSPORT = 0; // Pull down bus
|
||||
i = 70;
|
||||
while(i--) {}
|
||||
DSPORT = 1; // Pull up bus
|
||||
i = 0;
|
||||
while(DSPORT) {
|
||||
Delay1ms(1);
|
||||
i++;
|
||||
if(i > 5) return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Ds18b20WriteByte(uchar payload) {
|
||||
uint i, j;
|
||||
for(j = 0; j < 8; j++) {
|
||||
DSPORT = 0;
|
||||
i++;
|
||||
DSPORT = payload & 0x01;
|
||||
i = 6;
|
||||
while(i--){}
|
||||
DSPORT = 1;
|
||||
payload >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
uchar Ds18b20ReadByte(void) {
|
||||
uchar byte, bi;
|
||||
uint i, j;
|
||||
for(j = 8; j > 0; j--) {
|
||||
DSPORT = 0;
|
||||
i++;
|
||||
DSPORT = 1;
|
||||
i++;
|
||||
i++;
|
||||
bi = DSPORT;
|
||||
byte = (byte >> 1) | (bi << 7);
|
||||
i = 4;
|
||||
while(i--);
|
||||
}
|
||||
return byte;
|
||||
}
|
||||
|
||||
void Ds18b20ChangTemp(void) {
|
||||
Ds18b20Init();
|
||||
Delay1ms(1);
|
||||
Ds18b20WriteByte(0xCC);
|
||||
Ds18b20WriteByte(0x44);
|
||||
}
|
||||
|
||||
void Ds18b20ReadTempCom() {
|
||||
Ds18b20Init();
|
||||
Delay1ms(1);
|
||||
Ds18b20WriteByte(0xCC);
|
||||
Ds18b20WriteByte(0xBE);
|
||||
}
|
||||
|
||||
int Ds18b20ReadTemp() {
|
||||
int temp = 0;
|
||||
uchar th, tl;
|
||||
Ds18b20ChangTemp();
|
||||
Ds18b20ReadTempCom();
|
||||
temp = th;
|
||||
temp <<= 8;
|
||||
temp |= tl;
|
||||
return temp;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
#ifndef __TEMP_H
|
||||
#define __TEMP_H
|
||||
|
||||
#include <8052.h>
|
||||
|
||||
#ifndef uchar
|
||||
#define uchar unsigned char
|
||||
#endif
|
||||
|
||||
#ifndef uint
|
||||
#define uint unsigned int
|
||||
#endif
|
||||
|
||||
#define DSPORT P3_7
|
||||
|
||||
void Delay1ms(uint y);
|
||||
uchar Ds18b20Init(void);
|
||||
void Ds18b20WriteByte(uchar payload);
|
||||
uchar Ds18b20ReadByte(void);
|
||||
void Ds18b20ChangTemp(void);
|
||||
void Ds18b20ReadTempCom(void);
|
||||
int Ds18b20ReadTemp(void);
|
||||
|
||||
#endif
|
@ -1,7 +1,8 @@
|
||||
#include <8052.h>
|
||||
#include "serial_command.h"
|
||||
|
||||
#include "temp.h"
|
||||
#include "onewire.h"
|
||||
#include "ds18b20.h"
|
||||
|
||||
typedef unsigned int u16;
|
||||
typedef unsigned char u8;
|
||||
@ -199,7 +200,8 @@ void ProcessTempCmd(void) {
|
||||
#endif
|
||||
if (tempCmdCode == CTRL_CODE_TEMP_Enable) {
|
||||
EA = 0;
|
||||
temp = Ds18b20ReadTemp();
|
||||
DS18B20_startConvert();
|
||||
temp = DS18B20_readTemperature();
|
||||
EA = 1;
|
||||
tc = MCU_CMD_TEMP << 12;
|
||||
td = (u16) temp & 0xFFF;
|
||||
@ -222,6 +224,8 @@ int ProcessGasCmd(void) {
|
||||
|
||||
void main(void) {
|
||||
int gasFlag, bodyFlag;
|
||||
DS18B20_startConvert();
|
||||
delay(100);
|
||||
UsartInit();
|
||||
while (1) {
|
||||
ProcessCommand();
|
||||
|
Loading…
Reference in New Issue
Block a user