feat: onewire and ds18b20 driver
This commit is contained in:
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
|
Reference in New Issue
Block a user