1·功能特点
※采用单总线技术,与单片机通讯只要一根IO线
※通过比较系列号可以在一根线上挂多个DS18B20
※低压供电,电源范围从3V~5V,也可以直接从数据线上窃取电源
※测温范围-55~125摄氏度,在-10~85摄氏度范围内误差为±0.5度
※数据位可编程9~12位,转换12位温度时间为750ms(最大)
※用户可自设定预警上下限温度
※报警搜索命令可识别和寻址那个器件的温度至超出预定值
2·与单片机接口
3·时序图
主器件写 0 和 1 时序图
主器件读 0 和 1 时序
复位脉冲时序
3·DS18B20的命令字
| 44H | Convert T |
| BEH | Read Scratchpad |
| 4EH | Write Scratchpad |
| 48H | Copy Scratchpad |
| B8H | Recall E2 |
| B4H | Read Power Supply |
| 数据位 | 转换时间 |
| 9位 | 93.75ms |
| 10位 | 187.5ms |
| 11 位 | 375ms |
| 12 位 | 750 ms |
A·总线上只有一个器件
复位发CCH SKIP ROM命令发44H开始转换延时复位发CCH SKIP ROM命令发读存储器命令连续读出两个字节数据(即温度)->结束
B·总线上挂接多个器件
复位发55H MATCH ROM命令发位地址发44H开始转换命令延时复位发55H MATCH ROM命令发位地址发0BE读存储器命令连续读出两个字节数据(即温度) 复位读下个器件温度
6·接口程序
A:汇编程序,该程序为总线上只有一个器件
;DIN CONNECT TO DS1820'S DA
;读出的温度值在TEMP中
DIN EQU P1.0
TEMP EQU 30H
TEMP1 EQU 31H
TEMP2 EQU 32H
ORG 0000H
SJMP MAIN
ORG 0030H
MAIN: CALL RDTEM
SJMP $
RDTEM: LCALL RESET ;复位
MOV A, #0CCH ;发SKIP ROM 命令
LCALL WRITE
MOV A, #44H ;发开始温度转换命令
LCALL WRITE
LCALL DEL1000
LCALL RESET
MOV A, #0CCH
LCALL WRITE
MOV A, #0BEH ;发读存储器命令
LCALL WRITE
LCALL READ
MOV TEMP1, TEMP
LCALL READ
MOV TEMP2, TEMP
RET
RESET: NOP ;复位子程序
L0: CLR DIN
MOV R2, #200
L1: NOP
DJNZ R2, L1
SETB DIN
MOV R2, #30
L4: DJNZ R2, L4
CLR C
ORL C, DIN
JC L3
MOV R6, #80
L5: ORL C, DIN
JC L3
DJNZ R6, L5
SJMP L0
L3: MOV R2, #250
L2: DJNZ R2, L2
RET
WRITE: MOV R3, #8 ;写字节子程序
WR1: SETB DIN
MOV R4, #8
RRC A
CLR DIN
WR2: DJNZ R4, WR2
MOV DIN, C
MOV R4, #20
WR3: DJNZ R4, WR3
DJNZ R3, WR1
SETB DIN
RET
READ: MOV R6, #8 ;读子程序
RE1: CLR DIN
MOV R4, #6
NOP
SETB DIN
RE2: DJNZ R4, RE2
MOV C, DIN
RRC A
MOV R5, #30
RE3: DJNZ R5, RE3
DJNZ R6, RE1
MOV TEMP, A
SETB DIN
RET
DEL1000:MOV 40H, #18
DEL100: MOV 41H, #0FFH
DEL10: MOV 42H, #0FFH
DEL1: DJNZ 42H, DEL1
DJNZ 41H, DEL10
DJNZ 40H, DEL100
RET
B:C语言程序--该程序为总线上只有一个器件
;AT CONNECT TO DS1820'S DA
;读出的温度值在last中
#include #define uchar unsigned char sbit AT = P3^6; /********** FUNCTION **********/ void dmsec (unsigned int count) { // mSec Delay 11.0592 Mhz unsigned int i; // 1MS 延时 while (count--) { for (i=0;i<125;i++){} } } void tmreset (void) { // Reset TX unsigned int i; AT = 0; i = 103; while (i>0) i--; // Approx 900 uS AT = 1; i = 4; while (i>0) i--; } void tmpre (void) { // Wait for Presence RX unsigned int i; while (AT); while (~AT); i = 4; while (i>0) i--; } bit tmrbit (void) { // read one bit unsigned int i; bit dat; AT = 0; i++; AT = 1; i++; i++; dat = AT; i = 8; while (i>0) i--; return (dat); } unsigned char tmrbyte (void) { // read one byte unsigned char i,j,dat; dat = 0; for (i=1;i<=8;i++) { j = tmrbit (); dat = (j << 7) | (dat >> 1); } return (dat); } void tmwbyte (unsigned char dat) { // write one byte unsigned int i; unsigned char j; bit testb; for (j=1;j<=8;j++) { testb = dat & 0x01; dat = dat >> 1; if (testb) { AT = 0; // Write 1 i++; i++; AT = 1; i = 8; while (i>0) i--; } else { AT = 0; // Write 0 i = 8; while (i>0) i--; AT = 1; i++; i++; } } } void tmstart (void) { // ds1820 start convert tmreset (); tmpre (); dmsec (1); tmwbyte (0xcc); // skip rom tmwbyte (0x44); // convert } unsigned char tmrtemp (void) { // read temp unsigned char a,b,y1,y2,y3; tmreset (); tmpre (); dmsec (1); tmwbyte (0xcc); // skip rom tmwbyte (0xbe); // convert a = tmrbyte (); // LSB b = tmrbyte (); // MSB y1=a>>4; y2=b<<4; y3=y1 | y2; return(y3); } /********** MAIN **********/ void main (void) { unsigned int i; unsigned char last; uchar lsb,msb; dmsec(1); tmstart (); // ds1820 start convert dmsec(1000); last=tmrtemp (); // read temperature msb=last/0x0a+0x30; lsb=last%0x0a+0x30; while(1){} }下载本文