📄 18b20.s
字号:
.module _18b20.c
.area text(rom, con, rel)
.dbfile F:\单片机资料\AVR单片机\制作ds18b20\18B20\18b20.c
.dbfunc e init_1820 _init_1820 fV
.even
_init_1820::
.dbline -1
.dbline 12
; /**************************************
; ** Filename: 18b20.c
; ** Describe: 18b20 温度传感器
; ** Author : 古欣 www.avrvi.com
; ** Time : 2007-2-15
; **************************************/
;
; #include "config.h"
;
; unsigned char wmh,wml,flag,count; //flag温度为负标志,count为实际温度
; void init_1820(void)
; {
.dbline 13
; SET_DIR_1WIRE; //设置PC2 为输出
sbi 0x17,4
.dbline 14
; SET_OP_1WIRE;
sbi 0x18,4
.dbline 15
; CLR_OP_1WIRE;
cbi 0x18,4
.dbline 16
; delay_us(480); //480us以上
ldi R16,480
ldi R17,1
xcall _delay_us
.dbline 17
; SET_OP_1WIRE;
sbi 0x18,4
.dbline 18
; CLR_DIR_1WIRE;
cbi 0x17,4
.dbline 19
; delay_us(20); //15~60us
ldi R16,20
ldi R17,0
xcall _delay_us
L2:
.dbline 20
; while(CHECK_IP_1WIRE);
L3:
.dbline 20
sbic 0x16,4
rjmp L2
X0:
.dbline 21
; SET_DIR_1WIRE;
sbi 0x17,4
.dbline 22
; SET_OP_1WIRE;
sbi 0x18,4
.dbline 23
; delay_us(140); //60~240us
ldi R16,140
ldi R17,0
.dbline -2
L1:
.dbline 0 ; func end
xjmp _delay_us
.dbend
.dbfunc e write_1820 _write_1820 fV
; m -> R20
; x -> R10
.even
_write_1820::
st -y,R10
st -y,R20
mov R10,R16
.dbline -1
.dbline 26
; }
; void write_1820(unsigned char x)
; {
.dbline 28
; unsigned char m;
; for(m=0;m<8;m++)
clr R20
xjmp L9
L6:
.dbline 29
; {
.dbline 30
; CLR_OP_1WIRE;
cbi 0x18,4
.dbline 31
; if(x&(1<<m)) //写数据了,先写低位的!
ldi R16,1
mov R17,R20
xcall lsl8
mov R2,R10
and R2,R16
breq L10
X1:
.dbline 32
; SET_OP_1WIRE;
sbi 0x18,4
xjmp L11
L10:
.dbline 34
; else
; {CLR_OP_1WIRE;}
.dbline 34
cbi 0x18,4
.dbline 34
L11:
.dbline 35
; delay_us(40); //15~60us
ldi R16,40
ldi R17,0
xcall _delay_us
.dbline 36
; SET_OP_1WIRE;
sbi 0x18,4
.dbline 37
; }
L7:
.dbline 28
inc R20
L9:
.dbline 28
cpi R20,8
brlo L6
X2:
.dbline 38
; SET_OP_1WIRE;
sbi 0x18,4
.dbline -2
L5:
.dbline 0 ; func end
ld R20,y+
ld R10,y+
ret
.dbsym r m 20 c
.dbsym r x 10 c
.dbend
.dbfunc e read_1820 _read_1820 fc
; temp -> R10
; k -> R22
; n -> R20
.even
_read_1820::
xcall push_xgsetF00C
.dbline -1
.dbline 41
; }
; unsigned char read_1820(void)
; {
.dbline 43
; unsigned char temp,k,n;
; temp=0;
clr R10
.dbline 44
; for(n=0;n<8;n++)
clr R20
xjmp L16
L13:
.dbline 45
; {
.dbline 46
; CLR_OP_1WIRE;
cbi 0x18,4
.dbline 47
; SET_OP_1WIRE;
sbi 0x18,4
.dbline 48
; CLR_DIR_1WIRE;
cbi 0x17,4
.dbline 49
; k=(CHECK_IP_1WIRE); //读数据,从低位开始
in R22,0x16
andi R22,16
.dbline 50
; if(k)
breq L17
X3:
.dbline 51
; temp|=(1<<n);
ldi R16,1
mov R17,R20
xcall lsl8
or R10,R16
xjmp L18
L17:
.dbline 53
; else
; temp&=~(1<<n);
ldi R16,1
mov R17,R20
xcall lsl8
mov R2,R16
com R2
and R10,R2
L18:
.dbline 54
; delay_us(50); //60~120us
ldi R16,50
ldi R17,0
xcall _delay_us
.dbline 55
; SET_DIR_1WIRE;
sbi 0x17,4
.dbline 56
; }
L14:
.dbline 44
inc R20
L16:
.dbline 44
cpi R20,8
brlo L13
X4:
.dbline 57
; return (temp);
mov R16,R10
.dbline -2
L12:
.dbline 0 ; func end
xjmp pop_xgsetF00C
.dbsym r temp 10 c
.dbsym r k 22 c
.dbsym r n 20 c
.dbend
.dbfunc e gettemp _gettemp fV
; wm3 -> R12
; wm1 -> R10
; wm0 -> R20
; teml -> R12
; wm2 -> R10
; temh -> R22
.even
_gettemp::
xcall push_xgsetF03C
.dbline -1
.dbline 61
; }
;
; void gettemp(void) //读取温度值
; {
.dbline 63
; unsigned char temh,teml,wm0,wm1,wm2,wm3;
; init_1820(); //复位18b20
xcall _init_1820
.dbline 64
; write_1820(0xcc); // 发出转换命令
ldi R16,204
xcall _write_1820
.dbline 65
; write_1820(0x44);
ldi R16,68
xcall _write_1820
.dbline 67
; // delay_nms(800); //不延时也好使,不知道怎么回事!
; init_1820();
xcall _init_1820
.dbline 68
; write_1820(0xcc); //发出读命令
ldi R16,204
xcall _write_1820
.dbline 69
; write_1820(0xbe);
ldi R16,190
xcall _write_1820
.dbline 70
; teml=read_1820(); //读数据
xcall _read_1820
mov R12,R16
.dbline 71
; temh=read_1820();
xcall _read_1820
mov R22,R16
.dbline 72
; wm0=teml>>4; //只要高8位的低四位和低8位的高四位,温度范围0~99啦!
mov R20,R12
swap R20
andi R20,#0x0F
.dbline 73
; wm1=temh<<4;
mov R24,R22
andi R24,#0x0F
swap R24
mov R10,R24
.dbline 76
;
; //count=(temh*256+teml)*6.25; //计算具体温度
; if((temh&0xF8) == 0xF8)
mov R24,R22
andi R24,248
cpi R24,248
brne L20
X5:
.dbline 77
; {
.dbline 78
; flag=1;
ldi R24,1
sts _flag,R24
.dbline 79
; count=((0xFF-temh)*256+(0xFF-teml))*6.25;
ldi R16,<L22
ldi R17,>L22
xcall lpm32
movw R2,R16
movw R4,R18
mov R6,R22
clr R7
ldi R18,255
ldi R19,0
sub R18,R6
sbc R19,R7
ldi R16,256
ldi R17,1
xcall empy16s
movw R6,R16
mov R8,R12
clr R9
ldi R24,255
ldi R25,0
sub R24,R8
sbc R25,R9
add R6,R24
adc R7,R25
movw R16,R6
xcall int2fp
st -y,R19
st -y,R18
st -y,R17
st -y,R16
movw R16,R2
movw R18,R4
xcall fpmule2
xcall fpint
sts _count,R16
.dbline 81
; //count=((0xFF-temh)*256+(0xFF-teml))*625;
; }
xjmp L21
L20:
.dbline 83
; else
; {
.dbline 84
; flag=0;
clr R2
sts _flag,R2
.dbline 85
; count=(temh*256+teml)*6.25;
ldi R16,<L22
ldi R17,>L22
xcall lpm32
movw R2,R16
movw R4,R18
mov R18,R22
clr R19
ldi R16,256
ldi R17,1
xcall empy16s
movw R6,R16
mov R8,R12
clr R9
add R6,R8
adc R7,R9
movw R16,R6
xcall int2fp
st -y,R19
st -y,R18
st -y,R17
st -y,R16
movw R16,R2
movw R18,R4
xcall fpmule2
xcall fpint
sts _count,R16
.dbline 87
; //count=(temh*256+teml)*625; //计算具体温度
; }
L21:
.dbline 88
; wm2=wm1+wm0; //16进制转10进制
add R10,R20
.dbline 89
; wm3=wm2/100;
ldi R17,100
mov R16,R10
xcall div8u
mov R12,R16
.dbline 90
; wmh=(wm2%100)/10; //出口参数了!wmh是显示的高位,wml是显示的低位
ldi R17,100
mov R16,R10
xcall mod8u
ldi R17,10
xcall div8u
sts _wmh,R16
.dbline 91
; wml=(wm2%100)%10;
ldi R17,100
mov R16,R10
xcall mod8u
ldi R17,10
xcall mod8u
sts _wml,R16
.dbline -2
L19:
.dbline 0 ; func end
xjmp pop_xgsetF03C
.dbsym r wm3 12 c
.dbsym r wm1 10 c
.dbsym r wm0 20 c
.dbsym r teml 12 c
.dbsym r wm2 10 c
.dbsym r temh 22 c
.dbend
.area bss(ram, con, rel)
.dbfile F:\单片机资料\AVR单片机\制作ds18b20\18B20\18b20.c
_count::
.blkb 1
.dbsym e count _count c
_flag::
.blkb 1
.dbsym e flag _flag c
_wml::
.blkb 1
.dbsym e wml _wml c
_wmh::
.blkb 1
.dbsym e wmh _wmh c
.area lit(rom, con, rel)
L22:
.word 0x0,0x40c8
;
;
; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -