Ds18b20 c program
//DS1820 C51 subroutine
//This is an example of a 11.0592M crystal. Different crystal speeds may need to adjust the delay time.
//sbit DQ = P2^1; / / define the port according to the actual situation
Typedef unsigned char byte;
Typedef unsigned int word;
//delay
Void delay(word useconds)
{
For(;useconds>0;useconds--);
}
/ / Reset
Byte ow_reset(void)
{
Byte presence;
DQ = 0; //pull DQ line low
Delay(29); // leave it low for 480us
DQ = 1; // allow line to return high
Delay(3); // wait for presence
Presence = DQ; // get presence signal
Delay(25); // wait for end of timeslot
Return(presence); // presence signal returned
} // 0=presence, 1 = no part
//Read a byte from the 1-wire bus
Byte read_byte(void)
{
Byte i;
Byte value = 0;
For (i=8;i>0;i--)
{
Value>>=1;
DQ = 0; // pull DQ low to start timeslot
DQ = 1; // then return high
Delay(1); //for (i=0; i<3; i++);
If(DQ)value|=0 x80;
Delay(6); // wait for rest of timeslot
}
Return(value);
}
//Write a byte to the 1-WIRE bus
Void write_byte(char val)
{
Byte i;
For (i=8; i>0; i--) // writes byte, one bit at a time
{
DQ = 0; // pull DQ low to start timeslot
DQ = val&0 x01;
Delay(5); // hold value for remainder of timeslot
DQ = 1;
Val=val/2;
}
Delay(5);
}
/ / Read the temperature
Char Read_Temperature(void)
{
Unin n{
Byte c[2];
Int x;
}temp;
Ow_reset();
Write_byte(0 xCC); // Skip ROM
Write_byte(0 xBE); // Read Scratch Pad
Temp.c[1]=read_byte();
Temp.c[0]=read_byte();
Ow_reset();
Write_byte(0 xCC); //Skip ROM
Write_byte(0 x44); // Start Conversion
Return temp.x/2;
}
Ds18b20 assembler :
This assembler is only suitable for the connection of a single DS18B20 and 51 single-chip microcomputer. The crystal oscillator is about 12MHz.
DQ: Data bus pin of DS18B20
FLAG1: Flag bit, when it is "1", it indicates that DS18B20 is detected.
TEMPER_NUM: save the read temperature data
TEMPER_LEQU36H
TEMPER_HEQU35H
DQBITP1.7
; DS18B20 initialization assembler
;//
INIT_1820:
SETBDQ
NOP
CLRDQ
MOVR0, #06BH
TSR1:
DJNZR0, TSR1; delay
SETBDQ
MOVR0, #25H
TSR2:
JNBDQ, TSR3
DJNZR0, TSR2
LJMPTSR4; delay
TSR3:
SETBFLAG1; Set flag to indicate the existence of DS1820
LJMPTSR5
TSR4:
CLRFLAG1; Clear flag, indicating that DS1820 does not exist.
LJMPTSR7
TSR5:
MOVR0, #06BH
TSR6:
DJNZR0, TSR6; delay
TSR7:
SETBDQ
RET
;//
; Rewrite DS18B20 temporary memory setting
;//
RE_CONFIG:
JBFLAG1, RE_CONFIG1; If DS18B20 exists, turn RE_CONFIG1
RET
RE_CONFIG1:
MOVA, #0CCH; Send SKIP ROM command
LCALLWRITE_1820
MOVA, #4EH; Write temporary memory command
LCALLWRITE_1820
MOVA, #00H; TH (alarm upper limit) is written to 00H
LCALLWRITE_1820
MOVA, #00H; 00 (lower alarm limit) is written to 00H
LCALLWRITE_1820
MOVA, #1FH; Select 9-bit temperature resolution
LCALLWRITE_1820
RET
;/
; read the converted temperature value
;//
GET_TEMPER:
SETBDQ; timed entry
LCALLINIT_1820
JBFLAG1, TSS2
RET; return if DS18B20 does not exist
TSS2:
MOVA, #0CCH; Skip ROM match
LCALLWRITE_1820
MOVA, #44H; Issue temperature conversion command
LCALLWRITE_1820
LCALLINIT_1820
MOVA, #0CCH; Skip ROM match
LCALLWRITE_1820
MOVA, #0BEH; Issue read temperature command
LCALLWRITE_1820
LCALLREAD_1820
MOVTEMPER_NUM, A; Save the read temperature data
RET
;//
Read the DS18B20 program and read one byte of data from the DS18B20
;//
READ_1820:
MOVR2, #8
RE1:
CLRC
SETBDQ
NOP
NOP
CLRDQ
NOP
NOP
NOP
SETBDQ
MOVR3, #7
DJNZR3, $
MOVC, DQ
MOVR3, #23
DJNZR3, $
RRCA
DJNZR2, RE1
RET
;//
; Write the program of DS18B20
;//
WRITE_1820:
MOVR2, #8
CLRC
WR1:
CLRDQ
MOVR3, #6
DJNZR3, $
RRCA
MOVDQ, C
MOVR3, #23
DJNZR3, $
SETBDQ
NOP
DJNZR2, WR1
SETBDQ
RET
;//
Read the DS18B20 program and read two bytes of temperature data from the DS18B20
;//
READ_18200:
MOVR4, #2; read the high and low temperatures from the DS18B20
MOVR1, #36H; low level stored in 36H (TEMPER_L), high level stored in 35H (TEMPER_H)
RE00:
MOVR2, #8
RE01:
CLRC
SETBDQ
NOP
NOP
CLRDQ
NOP
NOP
NOP
SETBDQ
MOVR3, #7
DJNZR3, $
MOVC, DQ
MOVR3, #23
DJNZR3, $
RRCA
DJNZR2, RE01
MOV@R1, A
DECR1
DJNZR4, RE00
RET
;//
; Convert temperature data read from the DS18B20
;//
TEMPER_COV:
MOVA, #0F0H
ANLA, TEMPER_L; round off the four-digit temperature value after the decimal point in the low temperature
SWAPA
MOVTEMPER_NUM, A
MOVA, TEMPER_L
JNBACC.3, TEMPER_COV1; rounding off the temperature value
INCTEMPER_NUM
TEMPER_COV1:
MOVA, TEMPER_H
ANLA, #07H
SWAPA
ORLA, TEMPER_NUM
MOVTEMPER_NUM,A; Save the transformed temperature data
LCALLBIN_BCD
RET
;//
; Convert hexadecimal temperature data to compressed BCD code
;//
BIN_BCD:
MOVDPTR, #TEMP_TAB
MOVA, TEMPER_NUM
MOVCA, @A+DPTR
MOVTEMPER_NUM, A
RET
TEMP_TAB:
DB00H, 01H, 02H, 03H, 04H, 05H, 06H, 07H
DB08H, 09H, 10H, 11H, 12H, 13H, 14H, 15H
DB16H, 17H, 18H, 19H, 20H, 21H, 22H, 23H
DB24H, 25H, 26H, 27H, 28H, 29H, 30H, 31H
DB32H, 33H, 34H, 35H, 36H, 37H, 38H, 39H
DB40H, 41H, 42H, 43H, 44H, 45H, 46H, 47H
DB48H, 49H, 50H, 51H, 52H, 53H, 54H, 55H
DB56H, 57H, 58H, 59H, 60H, 61H, 62H, 63H
DB64H, 65H, 66H, 67H, 68H, 69H, 70H
;//
The following also introduces a ds18b20 assembler
;**********************************
FLAG1 BIT F0 ; DS18B20 exists flag DQ BIT P1.7
TEMPER_L EQU 29H
TEMPER_H EQU 28H
A_BIT EQU 35H
B_BIT EQU 36H
;************ds18b20 assembler start **********************
ORG 0000H
AJMP MAIN
ORG 0100H
;************** The main program starts **************
MAIN:
LCALL INIT_18B20
;LCALL RE_CONFIG
LCALL GET_TEMPER
AJMP CHANGE
;**********DS18B20 reset procedure*****************
INIT_18B20: SETB DQ
NOP
CLR DQ
MOV R0, #0FBH
TSR1: DJNZ R0, TSR1; Delay SETB DQ
MOV R0, #25H
TSR2: JNB DQ, TSR3
DJNZ R0, TSR2
TSR3: SETB FLAG1 ; Set flag to indicate that DS18B20 has CLR P2.0; diode indicates AJMP TSR5
TSR4: CLR FLAG1
LJMP TSR7
TSR5: MOV R0, #06BH
TSR6: DJNZ R0, TSR6
TSR7: SETB DQ; indicates that RET does not exist
;********************Set DS18B20 register settings *************
;RE_CONFIG:
;JB FLAG1, RE_CONFIG1
;RET
;RE_CONFIG1: MOV A, #0CCH ; put skip ROM command; LCALL WRITE_18B20
;MOV A,#4EH
;LCALL WRITE_18B20 ; Write register command; MOV A, #00H ; Write 00H in the upper alarm limit
;LCALL WRITE_18B20
;MOV A,#00H ;Write 00H in the lower alarm limit
; LCALL WRITE_18B20
Open Type Diesel Generator,Diesel Engine Generator,Diesel Power Generator,Mobile Diesel Generator
Jiangsu Vantek Power Machinery Co., Ltd , https://www.vantekpower.com