📄 18b20.s
字号:
.module _18b20.c
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\administrator\桌面\教程\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
L3:
.dbline 20
; while(CHECK_IP_1WIRE);
sbic 0x16,4
rjmp L2
.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
xcall _delay_us
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.dbfunc e write_1820 _write_1820 fV
; m -> R20
; x -> R22
.even
_write_1820::
xcall push_gset2
mov R22,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,R22
and R2,R16
breq L10
.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
ldi R16,40
ldi R17,0
xcall _delay_us
.dbline 36
sbi 0x18,4
.dbline 37
L7:
.dbline 28
inc R20
L9:
.dbline 28
cpi R20,8
brlo L6
.dbline 38
; delay_us(40); //15~60us
; SET_OP_1WIRE;
; }
; SET_OP_1WIRE;
sbi 0x18,4
.dbline -2
L5:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r m 20 c
.dbsym r x 22 c
.dbend
.dbfunc e read_1820 _read_1820 fc
; temp -> R20
; k -> R22
; n -> R10
.even
_read_1820::
xcall push_gset3
.dbline -1
.dbline 41
; }
; unsigned char read_1820(void)
; {
.dbline 43
; unsigned char temp,k,n;
; temp=0;
clr R20
.dbline 44
; for(n=0;n<8;n++)
clr R10
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
.dbline 51
; temp|=(1<<n);
ldi R16,1
mov R17,R10
xcall lsl8
or R20,R16
xjmp L18
L17:
.dbline 53
; else
; temp&=~(1<<n);
ldi R16,1
mov R17,R10
xcall lsl8
mov R2,R16
com R2
and R20,R2
L18:
.dbline 54
ldi R16,50
ldi R17,0
xcall _delay_us
.dbline 55
sbi 0x17,4
.dbline 56
L14:
.dbline 44
inc R10
L16:
.dbline 44
mov R24,R10
cpi R24,8
brlo L13
.dbline 57
; delay_us(50); //60~120us
; SET_DIR_1WIRE;
; }
; return (temp);
mov R16,R20
.dbline -2
L12:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r temp 20 c
.dbsym r k 22 c
.dbsym r n 10 c
.dbend
.dbfunc e gettemp _gettemp fV
; wm3 -> R22
; wm1 -> R22
; wm0 -> R20
; teml -> R10
; wm2 -> R20
; temh -> R12
.even
_gettemp::
xcall push_gset5
sbiw R28,4
.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 R10,R16
.dbline 71
; temh=read_1820();
xcall _read_1820
mov R12,R16
.dbline 72
; wm0=teml>>4; //只要高8位的低四位和低8位的高四位,温度范围0~99啦!
mov R20,R10
swap R20
andi R20,#0x0F
.dbline 73
; wm1=temh<<4;
mov R22,R12
andi R22,#0x0F
swap R22
.dbline 76
;
; //count=(temh*256+teml)*6.25; //计算具体温度
; if((temh&0xF8) == 0xF8)
mov R24,R12
andi R24,248
cpi R24,248
breq X0
xjmp L20
X0:
.dbline 77
; {
.dbline 78
; flag=1;
ldi R24,1
sts _flag,R24
.dbline 79
; count=((0xFF-temh)*256+(0xFF-teml))*6.25;
ldi R16,<L24
ldi R17,>L24
xcall lpm32
st -y,R19
st -y,R18
st -y,R17
st -y,R16
mov R2,R12
clr R3
ldi R18,255
ldi R19,0
sub R18,R2
sbc R19,R3
ldi R16,256
ldi R17,1
xcall empy16s
movw R2,R16
mov R4,R10
clr R5
ldi R24,255
ldi R25,0
sub R24,R4
sbc R25,R5
add R2,R24
adc R3,R25
movw R16,R2
xcall int2fp
st -y,R19
st -y,R18
st -y,R17
st -y,R16
xcall empy32f
movw R30,R28
std z+0,R16
std z+1,R17
std z+2,R18
std z+3,R19
movw R30,R28
ldd R2,z+0
ldd R3,z+1
ldd R4,z+2
ldd R5,z+3
st -y,R5
st -y,R4
st -y,R3
st -y,R2
ldi R16,<L25
ldi R17,>L25
xcall lpm32
st -y,R19
st -y,R18
st -y,R17
st -y,R16
xcall cmp32f
brlt L22
movw R30,R28
ldd R2,z+0
ldd R3,z+1
ldd R4,z+2
ldd R5,z+3
st -y,R5
st -y,R4
st -y,R3
st -y,R2
ldi R16,<L25
ldi R17,>L25
xcall lpm32
st -y,R19
st -y,R18
st -y,R17
st -y,R16
xcall sub32f
xcall fp2int
movw R24,R16
subi R24,0 ; offset = 32768
sbci R25,128
movw R14,R24
xjmp L23
L22:
movw R30,R28
ldd R16,z+0
ldd R17,z+1
ldd R18,z+2
ldd R19,z+3
xcall fp2int
movw R14,R16
L23:
sts _count,R14
.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,<L24
ldi R17,>L24
xcall lpm32
st -y,R19
st -y,R18
st -y,R17
st -y,R16
mov R18,R12
clr R19
ldi R16,256
ldi R17,1
xcall empy16s
movw R2,R16
mov R4,R10
clr R5
add R2,R4
adc R3,R5
movw R16,R2
xcall int2fp
st -y,R19
st -y,R18
st -y,R17
st -y,R16
xcall empy32f
movw R30,R28
std z+0,R16
std z+1,R17
std z+2,R18
std z+3,R19
movw R30,R28
ldd R2,z+0
ldd R3,z+1
ldd R4,z+2
ldd R5,z+3
st -y,R5
st -y,R4
st -y,R3
st -y,R2
ldi R16,<L25
ldi R17,>L25
xcall lpm32
st -y,R19
st -y,R18
st -y,R17
st -y,R16
xcall cmp32f
brlt L26
movw R30,R28
ldd R2,z+0
ldd R3,z+1
ldd R4,z+2
ldd R5,z+3
st -y,R5
st -y,R4
st -y,R3
st -y,R2
ldi R16,<L25
ldi R17,>L25
xcall lpm32
st -y,R19
st -y,R18
st -y,R17
st -y,R16
xcall sub32f
xcall fp2int
movw R24,R16
subi R24,0 ; offset = 32768
sbci R25,128
movw R10,R24
xjmp L27
L26:
movw R30,R28
ldd R16,z+0
ldd R17,z+1
ldd R18,z+2
ldd R19,z+3
xcall fp2int
movw R10,R16
L27:
sts _count,R10
.dbline 87
; //count=(temh*256+teml)*625; //计算具体温度
; }
L21:
.dbline 88
; wm2=wm1+wm0; //16进制转10进制
mov R2,R22
add R2,R20
mov R20,R2
.dbline 89
; wm3=wm2/100;
ldi R17,100
mov R16,R2
xcall div8u
mov R22,R16
.dbline 90
; wmh=(wm2%100)/10; //出口参数了!wmh是显示的高位,wml是显示的低位
ldi R17,100
mov R16,R20
xcall mod8u
ldi R17,10
xcall div8u
sts _wmh,R16
.dbline 91
; wml=(wm2%100)%10;
ldi R17,100
mov R16,R20
xcall mod8u
ldi R17,10
xcall mod8u
sts _wml,R16
.dbline -2
L19:
adiw R28,4
xcall pop_gset5
.dbline 0 ; func end
ret
.dbsym r wm3 22 c
.dbsym r wm1 22 c
.dbsym r wm0 20 c
.dbsym r teml 10 c
.dbsym r wm2 20 c
.dbsym r temh 12 c
.dbend
.area bss(ram, con, rel)
.dbfile C:\DOCUME~1\administrator\桌面\教程\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)
L25:
.word 0x0,0x4700
L24:
.word 0x0,0x40c8
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -