.module ds18b20.c
.area text(rom, con, rel)
.dbfile ..\..\..\..\PROTEU~2\5.avr\19.温度测量ds18b20\3.开搞ds18b20\ds18b20.c
.dbfile D:\教程及心得\Proteus\PROTEU~2\5.avr\19.温度测量ds18b20\3.开搞ds18b20\ds18b20.c
.dbfunc e reset _reset fV
.even
_reset::
.dbline -1
.dbline 11
; /*
; 我自己写的ds18b20的驱动,暂时固定PORTC0口当总线口好了
; */
; #include
; #include
; #include "mydelay.h"
; #include "ds18b20.h"
; //一。单点测量只要有presense应答就可以了,多点测量以后再搞
; //1.reset
; void reset(void)
; {
.dbline 12
; DDRC=DDRC|0x01;//输出
sbi 0x14,0
.dbline 13
; PORTC=PORTC&0xfe;//置0
in R24,0x15
andi R24,254
out 0x15,R24
.dbline 14
; delay0ms500us();
.dbline -2
L1:
.dbline 0 ; func end
rjmp _delay0ms500us
.dbend
.dbfunc e ReadPresense _ReadPresense fV
.even
_ReadPresense::
.dbline -1
.dbline 18
; }
; //2.读取presense应答信号
; void ReadPresense(void)
; {
.dbline 19
; DDRC=DDRC&0xfe;//输入
in R24,0x14
andi R24,254
out 0x14,R24
.dbline 20
; delay0ms60us();
rcall _delay0ms60us
L3:
.dbline 21
; while((PINC&0x01)==0x00) ;//检测到了应答信号
L4:
.dbline 21
sbis 0x13,0
rjmp L3
X0:
.dbline -2
L2:
.dbline 0 ; func end
ret
.dbend
.dbfunc e rdbit _rdbit fc
; data -> R20
.even
_rdbit::
st -y,R20
.dbline -1
.dbline 25
; //delay0ms60us();
; }
; unsigned char rdbit(void)
; {
.dbline 27
; unsigned char data;
; DDRC=DDRC|0x01;//输出
sbi 0x14,0
.dbline 28
; PORTC=PORTC&0xfe;//置0
in R24,0x15
andi R24,254
out 0x15,R24
.dbline 29
; delay0ms5us();//等待1us即可,为了照顾晶振,这里给5us
rcall _delay0ms5us
.dbline 31
; //PORTC=PORTC|0x01;//置1
; DDRC=DDRC&0xfe;//输入
in R24,0x14
andi R24,254
out 0x14,R24
.dbline 32
; data=PINC&0x01;
in R20,0x13
andi R20,1
.dbline 33
; delay0ms15us();
rcall _delay0ms15us
.dbline 34
; delay0ms60us();//读时隙不少于60us
rcall _delay0ms60us
.dbline 35
; return (data);
mov R16,R20
.dbline -2
L6:
.dbline 0 ; func end
ld R20,y+
ret
.dbsym r data 20 c
.dbend
.dbfunc e rdbyte _rdbyte fc
; j -> R12
; i -> R20
; data -> R10
.even
_rdbyte::
rcall push_xgset303C
.dbline -1
.dbline 38
; }
; unsigned char rdbyte(void)
; {
.dbline 41
; unsigned char data;
; unsigned char i,j;
; for(i=8;i>0;i--)
ldi R20,8
rjmp L11
L8:
.dbline 42
; {
.dbline 43
; j=rdbit();
rcall _rdbit
mov R12,R16
.dbline 44
; data=(data>>1)|(j mov R2,R12
lsl R2
lsl R2
lsl R2
lsl R2
lsl R2
lsl R2
lsl R2
mov R3,R10
lsr R3
or R3,R2
mov R10,R3
.dbline 45
; }
L9:
.dbline 41
dec R20
L11:
.dbline 41
ldi R24,0
cp R24,R20
brlo L8
X1:
.dbline 46
; return (data);
mov R16,R10
.dbline -2
L7:
.dbline 0 ; func end
rjmp pop_xgset303C
.dbsym r j 12 c
.dbsym r i 20 c
.dbsym r data 10 c
.dbend
.dbfunc e wtbit _wtbit fV
; data -> R20
.even
_wtbit::
st -y,R20
mov R20,R16
.dbline -1
.dbline 49
; }
; void wtbit(unsigned char data)
; {
.dbline 50
; DDRC=DDRC|0x01;//输出
sbi 0x14,0
.dbline 51
; PORTC=PORTC&0xfe;//置0
in R24,0x15
andi R24,254
out 0x15,R24
.dbline 52
; delay0ms5us();//等待1us即可,为了照顾晶振,这里给5us
rcall _delay0ms5us
.dbline 53
; if (data==0x01) PORTC=PORTC|0x01;//置1
cpi R20,1
brne L13
X2:
.dbline 53
sbi 0x15,0
L13:
.dbline 54
; delay0ms60us();
rcall _delay0ms60us
.dbline 55
; PORTC=PORTC|0x01;//拉高回来
sbi 0x15,0
.dbline 56
; delay0ms60us();//时隙不少于60us
rcall _delay0ms60us
.dbline -2
L12:
.dbline 0 ; func end
ld R20,y+
ret
.dbsym r data 20 c
.dbend
.dbfunc e wtbyte _wtbyte fV
; j -> R22
; i -> R20
; data -> R10
.even
_wtbyte::
rcall push_xgsetF00C
mov R10,R16
.dbline -1
.dbline 59
; }
; void wtbyte(unsigned char data)
; {
.dbline 61
; unsigned char i,j;
; for (i=8;i>0;i--)
ldi R20,8
rjmp L19
L16:
.dbline 62
; {
.dbline 63
; j=(data>>i)&0x01;
mov R16,R10
mov R17,R20
rcall lsr8
mov R22,R16
andi R22,1
.dbline 64
; wtbit(j);
mov R16,R22
rcall _wtbit
.dbline 65
; }
L17:
.dbline 61
dec R20
L19:
.dbline 61
ldi R24,0
cp R24,R20
brlo L16
X3:
.dbline -2
L15:
.dbline 0 ; func end
rjmp pop_xgsetF00C
.dbsym r j 22 c
.dbsym r i 20 c
.dbsym r data 10 c
.dbend
.dbfunc e convert _convert fV
.even
_convert::
.dbline -1
.dbline 69
; }
; //启动温度转换
; void convert(void)
; {
.dbline 70
; reset();
rcall _reset
.dbline 71
; ReadPresense();
rcall _ReadPresense
.dbline 72
; wtbyte(0xcc);
ldi R16,204
rcall _wtbyte
.dbline 73
; wtbyte(0x44);
ldi R16,68
.dbline -2
L20:
.dbline 0 ; func end
rjmp _wtbyte
.dbend
.dbfunc e rdtemp _rdtemp fi
; tmp ->
; temp -> R10,R11
.even
_rdtemp::
st -y,R10
st -y,R11
.dbline -1
.dbline 77
; }
; //读温度值
; unsigned int rdtemp(void)
; {
.dbline 79
; unsigned int temp,tmp;
; reset();
rcall _reset
.dbline 80
; ReadPresense();
rcall _ReadPresense
.dbline 81
; wtbyte(0xcc);
ldi R16,204
rcall _wtbyte
.dbline 82
; wtbyte(0xbe);
ldi R16,190
rcall _wtbyte
.dbline 87
; //temp=rdbyte();
; //tmp=temp;
; //temp=rdbyte();
; //temp=(temp ; PORTB=rdbyte();
rcall _rdbyte
out 0x18,R16
.dbline 88
; PORTD=rdbyte();
rcall _rdbyte
out 0x12,R16
.dbline 89
; return temp;
movw R16,R10
.dbline -2
L21:
.dbline 0 ; func end
ld R11,y+
ld R10,y+
ret
.dbsym l tmp 1 i
.dbsym r temp 10 i
.dbend
; }