<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1 概述
软件主程序文件
main.c
软件子程序文件
ds18b20.h DS18B20温度传感器头文件
delay.h 延时头文件
2 各程序模块描述
2.1主程序名main();
主程序通过和温度传感器通讯,计算和判断被测机子内的温度和指定的范围进行比较,
落在范围之内不报警,范围之外报警。其中恢复临界温度值小于报警临界温度值,以
免临界状态下频繁报警和恢复。
流程如下:
2.2:ds18b20.h头文件模块主要任务是读取温度传感器的温度转换数据,并返回调用主程序,流程如下:
2.3 delay.h 主要用于产生μs和ms“延时标准”,由调用程序决定延时系数。
流程简单,但此处用C语言编写的话最好实测其延时时间。可以在主程序中编写一段产生一方波的程序来测量“延时标准”。DS18(B)20单总线时序要求还是挺严格的。
3.源程序
在调试过程当中针对不能直观的看到温度测量数值,在程序中加入了232串口通信程序,方便调试查看(该电路没有设计显示接口),对DS18(B)20的其它应用场合具有一定的参考价值。
下面是Keil C51 下DS18(B)20的C语言头文件,也可以修改为AVR的应用:
///designed by hsz///
///all rights reserved//
#include <reg51.h>
#include <delay.h>
#include <intrins.h>
sbit DQ="P1"^7;
#define set() {DQ=1;}//{DDRB|=(1<<PB0);PORTB|=(1<<PB0);}//PB0éè???aê?3?×′ì?£?ê?3?1
#define clear() {DQ=0;}//{DDRB|=(1<<PB0);PORTB&=~(1<<PB0);}//PB0éè???aê?3?×′ì?£?ê?3?0
#define read() (DQ)//(PINB&(1<<PB0))//DDRB&=~(1<<PB0);//PB0éè???aê?è?×′ì?£??áPB0??
unsigned char ds18_start(void);
void ds18_send(unsigned char);
unsigned char ds18_readchar(void);
//---------------------------------------------------------------------------------------
/*void Init_DS18B20(void)
{
if(ds18_start()==0)//?′??
{ds18_send(0xcc);//ì?1yROM?¥??
//ds18_send(0x4e);//éè??D′?£ê?
//ds18_send(0x64);//éè?????èé??T100?è
//ds18_send(0x8a);//éè?????è???T-10?è
//ds18_send(0x7f);//12?????裨??�?
}
}*/
//----------------------------------------------------------------------------------------
char Read_ds18b20()
{
unsigned char th,tl;
char temp="0";
if(ds18_start())//3?ê??ˉ꧰ü?ò·μ??0
return '\0';
//ds18_start();
ds18_send(0xcc);//ì?1yROM?¥??
ds18_send(0x44);//???ˉ×a??
_delay_ms(200);//?ó3ù1s
ds18_start();//???e
ds18_send(0xcc);//ì?1yROM?¥??
ds18_send(0xbe);//?áè??Y′??÷êy?Y£?9×??ú
tl=ds18_readchar();//?è?áμí8??
th=ds18_readchar();//?ù?á??8??
ds18_start();//?áê??áè?oó??μ?êy?Y
///----------------------------DS18B20------------------------------------//
/*
tl&=0xF0;//è?μí×??úμ???????
tl>>=4;//
temp=th;
temp&=0x07;//è???×??úμ?μí3??
temp<<=4;//
temp|=tl;
if((th&0xF8)==0xF8)//?o???è?μêy
temp=(-temp);
*/
///--------------------------DS1820--------------------------------------//
tl>>=1;//è?μí×??úμ???7??
temp=tl;
if(th==0xFF)//?o???è?μêy//DS1820
temp*=(-1);/////????·?±??ê1?è
///------------------------------------------------------------------//
return temp;
}
//---------------------------------------------------------------------
unsigned char ds18_start(void)
{
unsigned char i,flag;//?¨ò?3?ê??ˉ3é1|±ê??
flag=1;
i=100;
set()
_nop_();
clear()//×ü????à£?3?ê??ˉ?aê?
_delay_us(45);//×ü??±£3?μíμ???480-960us????//45-80???????éò?
set()//×ü??à??
_nop_();//?áó|′e£?15-60usoó
_nop_();
while(i--)
{
if(!read())
{flag=0;//μ?μ?′ó?ú?ìó|
_delay_us(25);
break;}//480us//μèμ?ó|′eμíμ????áê?
}
set()
return(flag);
}
//----------------------------------------------------------------------------
void ds18_send(unsigned char i)
{
unsigned char j="8";
for(;j>0;j--)
{clear()///?è??0£?oí????×?oóμ???1??o?2úéúê±????
_nop_();
_nop_();
_nop_();
_nop_();//10us
if(i&0x01)/// ×?μí??£?è??a1£??ò??1
set()
_delay_us(7);//45us
set()//×?±????ˉ??ò???D′ê±????
i>>=1;
}//end for
set()
}
//-----------------------------------------------------------------------------
unsigned char ds18_readchar(void)
{
unsigned char i="0",j=8;
for(;j>0;j--)
{
i>>=1;
set()
_nop_();//2us
clear()
_nop_();//2us//ê±????′óóú1us
set()
_nop_();//2us
_nop_();//2us
_nop_();//2us
_nop_();//2us
//_nop_();//2us
if(read())
i|=0x80;//?è?óê?μ?ê?μí??
_nop_();//2us
_nop_();//2us
_delay_us(7);//40us//45us
}//end for
return i;
}
//----------------------------------------------------------------------------
以下是本人以前汇编源程序,带232串口通信,对多点温度采集有点参考价值:
;;;;designed by hsz 20050710;;;
;;;;all rights reserved;;
SCLK bit P3.2
SDA bit P3.3
LED bit P3.5
TXPC bit P3.1
RXPC bit P3.0
EEADD EQU 21H
WRITADD EQU 22H
DATANUM EQU 23H
TRUENUM EQU 20H
TEMPER_L EQU 26H
TEMPER_H EQU 25H
TEMPER_NUM EQU 24H
DQ BIT P3.4
ORG 0000H
AJMP BEGIN
ORG 0003H;INT0
RETI
ORG 000BH;T0
RETI
ORG 0013H;INT1
RETI
ORG 001BH;T1
RETI
ORG 0023H;COM
AJMP serialport
;RETI
ORG 002BH;T2
RETI
ORG 0100H
BEGIN:
MOV SP,#5FH
MOV IE,#00H;1??ùóD?D??
CLR LED;??ê?μ?ááò???
ACALL DELAY
MOV A,#0FFH
MOV P1,A
MOV P3,A
;CLR RXIR ;oìía?óêü??3?ê??ˉμíμ???
CLR C
MOV A, #00H
MOV R0,#7FH;PC BUF
MOV R1,#20H;IR BUF
MOV R2,#00H;PC COUNT
MOV R3,#00H
MOV R4,#00H;IR COUNT
mov TMOD,#20H ;T1: 1¤×÷?£ê?2
mov TL1,#0FDH
mov TH1,#0FDH ;3?ê??ˉ2¨ì??꣨2???±í£?
CLR ET1
SETB TR1 ;T1?aê?1¤×÷
mov SCON,#50h ;Standard UART settings
mov PCON,#00h ;9600bps
SETB ES ;?a′??ú?D??
CLR TI
CLR RI
;SETB REN ;?êDí?óê?
SETB EA;?êDí×ü?D??
LCALL TEMP_CONFIG
MOV A,#'H'
MOV SBUF,A
JNB TI,$
CLR TI
MOV A,#'I'
MOV SBUF,A
JNB TI,$
CLR TI
;************************************************
MAIN:
setb es
CLR RI
MOV A, #00H
MOV R0,#7FH;PC BUF
MOV R2,#00H;PC COUNT
MOV R3,#00H
MOV R4,#00H
JNB RI,$
NOP
CLR RI
MOV A,@R0
CJNE A,#0C0H,MAIN;?o3??úá?·¢?í??pc£?·??òí?3??ú?óê?
NEXT1:
DEC R0
JNB RI,$
NOP
CLR RI
MOV A,@R0
CJNE A,#0FFH,MAIN
NEXT2:
DEC R0
JNB RI,$
NOP
CLR RI
MOV A,@R0
CJNE A,#68H,MAIN;;;;??í·ê?±e
RECEIVE:
DEC R0;;′?′¢??????ò?
INC R2;;??êy?óêüμ?μ?óDD§êy?Y??êy
JNB RI,$
NOP
CLR RI
MOV A,@R0
CJNE A,#4FH,RECEIVE
DEC R0
INC R2;;??êy?óêüμ?μ?óDD§êy?Y??êy
JNB RI,$
NOP
CLR RI
MOV A,@R0
CJNE A,#4BH,RECEIVE
DEC R2;;?óê?μ??áê?·??á?à??2′?£???????2
DEC R2
CLR ES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CHECK: MOV A,R2;??′?êy?Yê±D£?é×?oí?áê?·?óé??′?3ìDò±?éí2úéú
MOV TRUENUM,R2
MOV A,R0
MOV R1,A;???òμ1êyμú3??×??ú£??′??êyD£?é×?
INC R1
INC R1
MOV A,@R1
MOV DATANUM,A
CJNE A,TRUENUM,MAIN;?D??′??íà′μ?×??úêyê?·??yè·£?2??yè·?ò?a?ú
NOP
CLR F0
mov 26h,#00h
mov 25h,#00h
LCALL GET_TEMPER
MOV R0,#TEMPER_L
LCALL SEND
LCALL DELAY
LCALL TEMPER_COV
MOV DATANUM,#02H
MOV R0,#TEMPER_NUM
LCALL SEND
LCALL DELAY
MOV 37H,#00H
CLR A
MOV C,F0
RLC A
MOV 37H,A
MOV R0,#37H;#TEMPER_NUM
LCALL SEND
LJMP MAIN
GET_TEMPER:
SETB DQ
BCD:
LCALL INIT_1820
JB F0,S22
LJMP BCD
RET
S22:LCALL DELAY1
MOV A,#0CCH
LCALL WRITE_1820
MOV A,#44H
LCALL WRITE_1820
NOP
LCALL DELAY
LCALL DELAY
CBA:
LCALL INIT_1820
JB F0,ABC
LJMP CBA
ABC:LCALL DELAY1
MOV A,#0CCH
LCALL WRITE_1820
MOV A,#0BEH;;;;;;;;;;?á?ú2???′??÷?üá?
LCALL WRITE_1820
LCALL READ_1820
RET
INIT_1820:
SETB DQ
NOP
CLR DQ
MOV R7,#3;;;;;;;;;;256*2=512us;;òa?ó480--960usμíμ??·
TSR1:MOV R6,#107
DJNZ R6,$
DJNZ R7,TSR1
SETB DQ
NOP
NOP
NOP;;;;;;;;;;;;;;;;;;;;12M ADD A NOP
MOV R7,#25H;#3CH;60US??μ???
TSR2:
JNB DQ,TSR3;;60-240US
DJNZ R7,TSR2
LJMP TSR4
TSR3:;SETB FLAG1
SETB F0
LJMP TSR5
TSR4:;CLR FLAG1
CLR F0
LJMP TSR7
TSR5:MOV R7,#00H;#117;256*2=512us;;;;;ds1820?ìó|?áéùòa480us
TSR6:DJNZ R7,TSR6
TSR7:SETB DQ
RET
WRITE_1820:
MOV R6,#8
CLR C
WR1:CLR DQ
MOV R7,#5;;;12M USE #6
DJNZ R7,$
CLR C
RRC A
MOV DQ,C
MOV R3,#21;;;;;;;12M USE #23
DJNZ R3,$;;;;;;±£3?μíμ????áéù15us
SETB DQ
NOP
DJNZ R6,WR1
SETB DQ
RET
/*TEMPER_COV:
MOV A,#0F0H
ANL A,TEMPER_L
SWAP A
MOV TEMPER_NUM,A
MOV A,TEMPER_L
JNB ACC.3,TEMPER_COV1;??éá??è? °?????±ê??acc.3=0 éá acc.3=1 ??
INC TEMPER_NUM
TEMPER_COV1:
MOV A,TEMPER_H
ANL A,#07H
SWAP A
ORL A,TEMPER_NUM;????μí???′?eà′
MOV TEMPER_NUM,A
LCALL BIN_BCD
RET
*/
TEMPER_COV:
MOV A,TEMPER_H
CJNE A,#0FFH,TMA1
CLR 01H
LJMP TMMA1
TMA1:SETB 01H
LJMP TMMA2
TMMA1:MOV A,TEMPER_L
CPL A
MOV TEMPER_L,A
TMMA2:
MOV A,TEMPER_L
CLR C
RRC A
MOV TEMPER_NUM,A
/*
MOV A,#00H
RLC A
ORL A,#00011000B
MOV TEMPER_XIAO*/
LCALL BIN_BCD
RET
BIN_BCD:
MOV DPTR,#TEMPER_TAB
MOV A,TEMPER_NUM
MOVC A,@A+DPTR
MOV TEMPER_NUM,A
RET
TEMPER_TAB:
DB 00H,01H,02H,03H,04H,05H,06H,07H,08H,09H
DB 10H,11H,12H,13H,14H,15H,16H,17H,18H,19H
DB 20H,21H,22H,23H,24H,25H,26H,27H,28H,29H
DB 30H,31H,32H,33H,34H,35H,36H,37H,38H,39H
DB 40H,41H,42H,43H,44H,45H,46H,47H,48H,49H
DB 50H,51H,52H,53H,54H,55H,56H,57H,58H,59H
DB 60H,61H,62H,63H,64H,65H,66H,67H,68H,69H
DB 70H,71H,72H,73H,74H,75H,76H,77H,78H,79H
DB 80H,81H,82H,83H,84H,85H,86H,87H,88H,89H
DB 90H,91H,92H,93H,94H,95H,96H,97H,98H,99H
READ_1820:
MOV R4,#2
MOV R1,#TEMPER_L;#26H;;;êy?Yμí×??ú·??ú36h£???×??ú·??ú35h
RE00:
MOV R2,#8
RE01:
CLR C
SETB DQ;;;;;;;?áéù±£3?1us;;;BUS CAN BE LOW AFTER READ
NOP
NOP
CLR DQ
NOP
NOP
NOP
SETB DQ;;;;;;;?áéù±£3?1us
MOV R3,#8;;;;;;12M USE #9
DJNZ R3,$;;;;;15us?ú2é?ù×ü??êy?Y
MOV C,DQ;;;;;;;;;;2é?ùêy?Y
MOV R3,#21;;;;45us?ó3ù;;;;12M USE #23
DJNZ R3,$
RRC A
DJNZ R2,RE01;;;;;;;μí8??êy?Y
MOV @R1,A
DEC R1
DJNZ R4,RE00;;;;;;;??8??êy?Y
RET
SEND:
CLR TI
SETB ES
MOV A,#0C0H;??′?êy?Yê±?eê?×?óé??′?3ìDò±?éí2úéú
MOV SBUF,A
JNB TI,$
CLR TI
MOV A,#0FFH
MOV SBUF,A
JNB TI,$
CLR TI
MOV A,#68H
MOV SBUF,A
JNB TI,$
CLR TI
;MOV R0,#7CH;???òêy?Y??
MOV A,@R0
MOV R3,DATANUM;?′?ú??′???·?Déù′?D£?é×?
DEC R3
MOV R4,#00H
CJNE R3,#00H,REPEAT;;;??óDóDD§êy?Y
AJMP TAIL ;;;ì?·¢?í???2
REPEAT: MOV SBUF,A
DEC R0
INC R4
MOV A,@R0;μ?μ?pc?o3???μ?êy?Y
JNB TI,$
NOP
CLR TI
DJNZ R3,REPEAT
TAIL:INC R4;;;;óDD§êy?Y??êy?óD£?é×?1
MOV A,R4;??′?êy?Yê±D£?é×?oí?áê?·?óé??′?3ìDò±?éí2úéú
MOV SBUF,A
JNB TI,$
CLR TI
MOV A,#'O';;?áê?·?
MOV SBUF,A
JNB TI,$
CLR TI
MOV A,#'K'
MOV SBUF,A
JNB TI,$
CLR TI
clr es
RET
serialport:
CLR EA
WAIT: JB TI,SENDIR
JB RI,RECIR;RI==1μ??°??á?RI2¢ì?×a
SETB EA
RETI
RECIR: MOV A,SBUF
MOV @R0,A ;oìíaêy?Y?o3???
CPL LED
SETB EA
RETI
SENDIR: CPL LED
SETB EA
RETI
TEMP_CONFIG:
MOV A,#0CCH
LCALL WRITE_1820
MOV A,#4EH
LCALL WRITE_1820
MOV A,#64H;;;;;;;é??T±¨?ˉ100
LCALL WRITE_1820
MOV A,#8aH;;;;;;;???T±¨?ˉ-10
LCALL WRITE_1820
MOV A,#7FH;;;;;;;;;;;;;;;;;;12sbit #1fh 8BIT
LCALL WRITE_1820
NOP
NOP
RET
DELAY:MOV R7,#00H
MIN:DJNZ R2,YS500
RET
YS500:LCALL YS500US
LJMP MIN
YS500US:MOV R6,#00H
DJNZ R6,$
RET
DELAY1:MOV R7,#20H
DJNZ R7,$
RET
END
用户347384 2011-5-29 15:49