.module ds18b20.c
.area text(rom, con, rel)
.dbfile D:\NEW\ECHO\ds18b20.c
.dbfunc e resetDS18B20 _resetDS18B20 fc
; errTime -> R20
.even
_resetDS18B20::
xcall push_gset1
.dbline -1
.dbline 13
; #include
; #include
; #include "ds18b20.h"
; #include "delay.h"
;
;
; unsigned char temp_buf[4];
; /**********************************************************************
; functionName: unsigned char resetDS18B20(void)
; description :DS18B20初始化
; **********************************************************************/
; unsigned char resetDS18B20(void)
; {
.dbline 14
; unsigned char errTime=0;
clr R20
.dbline 15
; RLS_DS18B20; //释放总线
cbi 0x14,1
.dbline 16
; _NOP();
nop
.dbline 17
; HLD_DS18B20; //Maga16控制总线
sbi 0x14,1
.dbline 18
; CLR_DS18B20; //强制拉低
cbi 0x15,1
.dbline 19
; ds_delay(254); //209.42us
ldi R16,254
xcall _ds_delay
.dbline 20
; ds_delay(254);
ldi R16,254
xcall _ds_delay
.dbline 21
; ds_delay(254);
ldi R16,254
xcall _ds_delay
.dbline 23
; //以上延时大于480us
; RLS_DS18B20; //释放总线,总线自动上拉
cbi 0x14,1
.dbline 24
; _NOP();
nop
xjmp L3
L2:
.dbline 26
; while(STU_DS18B20)
; {
.dbline 27
; ds_delay(10);//5-15us
ldi R16,10
xcall _ds_delay
.dbline 28
; errTime++;
inc R20
.dbline 29
; if(errTime>4)
ldi R24,4
cp R24,R20
brsh L5
.dbline 30
; return(0x00); //如果等带大于约 5.15us*20就返回0x00,报告复位失败(实际上只要等待15-60us)
clr R16
xjmp L1
L5:
.dbline 31
L3:
.dbline 25
sbic 0x13,1
rjmp L2
.dbline 32
; }
; errTime=0;
clr R20
xjmp L8
L7:
.dbline 34
; while(!(STU_DS18B20))
; {
.dbline 35
; ds_delay(10);//5.15us
ldi R16,10
xcall _ds_delay
.dbline 36
; errTime++;
inc R20
.dbline 37
; if(errTime>20)
ldi R24,20
cp R24,R20
brsh L10
.dbline 38
; return(0x00); //如果等带大于约 5.15us*50就返回0x00,报告复位失败(实际上只要等待60-240us)
clr R16
xjmp L1
L10:
.dbline 39
L8:
.dbline 33
sbis 0x13,1
rjmp L7
.dbline 40
; }
; return(0xff);
ldi R16,255
.dbline -2
L1:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r errTime 20 c
.dbend
.dbfunc e readByteDS18B20 _readByteDS18B20 fc
; retVal -> R20
; i -> R22
.even
_readByteDS18B20::
xcall push_gset2
.dbline -1
.dbline 48
; }
;
; /**********************************************************************
; functionName: unsigned char readByteDS18B20(void)
; description :读DS18B20一个字节
; **********************************************************************/
; unsigned char readByteDS18B20(void)
; {
.dbline 50
; unsigned char i;
; unsigned char retVal=0;
clr R20
.dbline 51
; RLS_DS18B20; //释放总线
cbi 0x14,1
.dbline 52
; for(i=8;i>0;i--)
ldi R22,8
xjmp L16
L13:
.dbline 53
; {
.dbline 54
; retVal>>=1;
lsr R20
.dbline 55
; HLD_DS18B20; //Maga16控制总线
sbi 0x14,1
.dbline 56
; CLR_DS18B20; //强制拉低
cbi 0x15,1
.dbline 57
; ds_delay(2); //延时大于1us
ldi R16,2
xcall _ds_delay
.dbline 58
; SET_DS18B20; //释放总线,DS18B20会将总线强制拉低
sbi 0x15,1
.dbline 60
; //delay_nus(8);
; RLS_DS18B20; //释放总线
cbi 0x14,1
.dbline 61
; if(STU_DS18B20)
sbis 0x13,1
rjmp L17
.dbline 62
; retVal|=0x80;
ori R20,128
L17:
.dbline 63
ldi R16,50
xcall _ds_delay
.dbline 64
sbi 0x14,1
.dbline 65
sbi 0x15,1
.dbline 67
L14:
.dbline 52
dec R22
L16:
.dbline 52
clr R2
cp R2,R22
brlo L13
.dbline 68
; ds_delay(50); //31us
; HLD_DS18B20; //释放总线
; SET_DS18B20; //释放总线,DS18B20会将总线强制拉低
; //delay_nus(30); //30.38us
; }
; ds_delay(5); //2.71us(大于1us就行了)
ldi R16,5
xcall _ds_delay
.dbline 69
; return(retVal);
mov R16,R20
.dbline -2
L12:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r retVal 20 c
.dbsym r i 22 c
.dbend
.dbfunc e writeByteDS18B20 _writeByteDS18B20 fV
; temp -> R20
; i -> R22
; wb -> R10
.even
_writeByteDS18B20::
xcall push_gset3
mov R10,R16
.dbline -1
.dbline 77
; }
;
; /**********************************************************************
; functionName: unsigned char readByteDS18B20(void)
; description :写DS18B20一个字节
; **********************************************************************/
; void writeByteDS18B20(unsigned char wb)
; {
.dbline 80
; unsigned char i;
; unsigned char temp;
; RLS_DS18B20; //释放总线
cbi 0x14,1
.dbline 81
; for(i=0;i clr R22
xjmp L23
L20:
.dbline 82
; {
.dbline 83
; HLD_DS18B20; //Maga16控制总线
sbi 0x14,1
.dbline 84
; CLR_DS18B20; //强制拉低
cbi 0x15,1
.dbline 85
; ds_delay(6); //14.92us
ldi R16,6
xcall _ds_delay
.dbline 88
; //SET_DS18B20; //释放总线
; //delay_nus(16); //14.92us
; temp=wb>>i;
mov R16,R10
mov R17,R22
xcall lsr8
mov R20,R16
.dbline 89
; temp&=0x01;
andi R20,1
.dbline 90
; if(temp)
breq L24
.dbline 91
; SET_DS18B20; //释放总线
sbi 0x15,1
xjmp L25
L24:
.dbline 93
; else
; CLR_DS18B20; //强制拉低
cbi 0x15,1
L25:
.dbline 94
ldi R16,50
xcall _ds_delay
.dbline 96
sbi 0x15,1
.dbline 97
ldi R16,5
xcall _ds_delay
.dbline 98
L21:
.dbline 81
inc R22
L23:
.dbline 81
cpi R22,8
brlo L20
.dbline -2
.dbline 99
; ds_delay(50); //30.38us
; //RLS_DS18B20; //释放总线
; SET_DS18B20; //释放总线
; ds_delay(5); //2.71us(大于1us就行了)
; }
; }
L19:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r temp 20 c
.dbsym r i 22 c
.dbsym r wb 10 c
.dbend
.dbfunc e readTempDS18B20 _readTempDS18B20 fi
; x -> R20,R21
; tempH -> R20
; tempL -> R22
.even
_readTempDS18B20::
xcall push_gset2
.dbline -1
.dbline 106
;
; /**********************************************************************
; functionName: unsigned int readTempDS18B20(void)
; description :读DS18B20温度
; **********************************************************************/
; unsigned int readTempDS18B20(void)
; {
.dbline 112
; unsigned char tempL,tempH;
; unsigned int x;
; //resetDS18B20();
; //writeByteDS18B20(0xcc); //跳过ROM
; //writeByteDS18B20(0x44); //启动温度转换
; delay_nms(1); //等待1ms
ldi R16,1
ldi R17,0
xcall _delay_nms
.dbline 113
; resetDS18B20();
xcall _resetDS18B20
.dbline 114
; writeByteDS18B20(0xcc); //跳过ROM
ldi R16,204
xcall _writeByteDS18B20
.dbline 115
; writeByteDS18B20(0xbe); //读数据
ldi R16,190
xcall _writeByteDS18B20
.dbline 116
; tempL=readByteDS18B20();
xcall _readByteDS18B20
mov R22,R16
.dbline 117
; tempH=readByteDS18B20();
xcall _readByteDS18B20
mov R20,R16
.dbline 118
; x=(tempH mov R21,R20
mov R20,R22
.dbline 119
; resetDS18B20();
xcall _resetDS18B20
.dbline 120
; writeByteDS18B20(0xcc); //跳过ROM
ldi R16,204
xcall _writeByteDS18B20
.dbline 121
; writeByteDS18B20(0x44); //启动温度转换
ldi R16,68
xcall _writeByteDS18B20
.dbline 122
; return(x);
movw R16,R20
.dbline -2
L26:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r x 20 i
.dbsym r tempH 20 c
.dbsym r tempL 22 c
.dbend
.dbfunc e BCDtoASCII _BCDtoASCII fV
; j -> R20
; i -> R22
; pstr -> R18,R19
; cBCD -> R16
.even
_BCDtoASCII::
xcall push_gset2
.dbline -1
.dbline 126
; }
; //压缩BCD码转换成ASCII码
; void BCDtoASCII(unsigned char cBCD,unsigned char *pstr)
; {
.dbline 128
; unsigned char i,j;
; i=cBCD>>4;
mov R22,R16
swap R22
andi R22,#0x0F
.dbline 129
; j=cBCD&0x0F;
mov R20,R16
andi R20,15
.dbline 130
; *pstr++=i+0x30;
mov R24,R22
subi R24,208 ; addi 48
movw R30,R18
st Z+,R24
movw R18,R30
.dbline 131
; *pstr=j+0x30;
mov R24,R20
subi R24,208 ; addi 48
std z+0,R24
.dbline -2
.dbline 132
; }
L27:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r j 20 c
.dbsym r i 22 c
.dbsym r pstr 18 pc
.dbsym r cBCD 16 c
.dbend
.dbfunc e Read_Temp _Read_Temp fV
; temp -> R20,R21
.even
_Read_Temp::
xcall push_gset1
.dbline -1
.dbline 135
; //读取当前温度并解释
; void Read_Temp(void)
; {
.dbline 136
; unsigned int temp=0;
clr R20
clr R21
.dbline 137
; temp=readTempDS18B20();
xcall _readTempDS18B20
movw R20,R16
.dbline 138
; BCDtoASCII((temp>>8)&0x0F,&temp_buf[0]);
ldi R18, ldi R19,>_temp_buf
mov R16,R17
clr R17
andi R16,15
andi R17,0
xcall _BCDtoASCII
.dbline 139
; BCDtoASCII((temp>>8)&0x0F,&temp_buf[1]);
ldi R18, ldi R19,>_temp_buf+1
movw R16,R20
mov R16,R17
clr R17
andi R16,15
andi R17,0
xcall _BCDtoASCII
.dbline 140
; BCDtoASCII((temp>>4)&0x0F,&temp_buf[2]);
ldi R18, ldi R19,>_temp_buf+2
movw R16,R20
lsr R17
ror R16
lsr R17
ror R16
lsr R17
ror R16
lsr R17
ror R16
andi R16,15
andi R17,0
xcall _BCDtoASCII
.dbline 141
; BCDtoASCII(temp&0x0F,&temp_buf[3]);
ldi R18, ldi R19,>_temp_buf+3
mov R16,R20
andi R16,15
andi R17,0
xcall _BCDtoASCII
.dbline -2
.dbline 142
; }
L28:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r temp 20 i
.dbend
.area bss(ram, con, rel)
.dbfile D:\NEW\ECHO\ds18b20.c
_temp_buf::
.blkb 4
.dbsym e temp_buf _temp_buf A[4:4]c