ds1820.s
来自「avr单片机atmage16驱动小灯的一些例程 并且包括一些收发数据的程序利用」· S 代码 · 共 542 行
S
542 行
.module DS1820.C
.area text(rom, con, rel)
.dbfile E:\ICCAVR\project\AVRMEG~2\icc\030-DS18B20-LED4\DS1820.C
.dbfunc e delay_10us _delay_10us fV
; i -> R16
.even
_delay_10us::
.dbline -1
.dbline 51
; /*
; LCD12864液晶屏驱动模块
; 1、可直接嵌入到项目中使用
; 2、晶振频率:1M
; 3、如晶振提高低层驱动延时要作相应修改
; AVR_AFA
; www.iccavr.com
; */
;
; #include <iom16v.h>
; #include <macros.h>
;
; #define DQ_IN DDRA &= ~(1 << PA7) /*设置输入*/
; #define DQ_OUT DDRA |= (1 << PA7) /*设置输出*/
; #define DQ_CLR PORTA &= ~(1 << PA7) /*置低电平*/
; #define DQ_SET PORTA |= (1 << PA7) /*置高电平*/
; #define DQ_R PINA & (1 << PA7) /*读了电平*/
;
; unsigned char flag; /*中断标志缓存*/
;
; /*===================================================================
; // 函数功能: DS18B20数据校验函数
; // 形参: void
; // 返回: unsigned char 校验结果
; // 编写: 2004/8/25
; // 备注: CRC公式为:CRC = X^8 + X^5 + X^4 + 1
; ===================================================================*/
; /*unsigned char crc(unsigned char *p,unsigned char len)
; {
; unsigned char bit0,cbit,r,temp,i,j,byte;
; temp = 0;
; for(j = 0; j < len; j++)
; {
; byte = p[j];
; for(i = 0; i < 8; i++)
; {
; cbit = temp & 0x01;
; bit0 = byte&0x01;
; temp >>= 1;
; r = cbit ^ bit0;
; if(r == 1)
; temp ^= 0x8c;
; byte >>= 1;
; }
; }
; return temp;
; }*/
;
; /*延时函数*/
; /*频率为1MHz,延时:i X 10 + 16uS*/
; void delay_10us(unsigned char i) {
.dbline 53
;
; if (i == 0) {
tst R16
brne L5
.dbline 53
.dbline 54
; return ;
xjmp L1
L4:
.dbline 56
.dbline 57
nop
.dbline 58
nop
.dbline 59
nop
.dbline 60
nop
.dbline 61
L5:
.dbline 56
; }
; while (i--) {
mov R2,R16
clr R3
subi R16,1
tst R2
brne L4
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbsym r i 16 c
.dbend
.dbfunc e ds1820_reset _ds1820_reset fc
; i -> R20
.even
_ds1820_reset::
xcall push_gset1
.dbline -1
.dbline 65
; NOP();
; NOP();
; NOP();
; NOP();
; }
; }
;
; /*DS18B20复位函数*/
; unsigned char ds1820_reset(void) {
.dbline 68
;
; unsigned char i;
; flag = SREG; /*中断保护*/
in R2,0x3f
sts _flag,R2
.dbline 69
; CLI(); /*关中断*/
cli
.dbline 70
; DQ_OUT;
sbi 0x1a,7
.dbline 71
; DQ_CLR;
cbi 0x1b,7
.dbline 72
; delay_10us(49); /*延时500uS(480-960)*/
ldi R16,49
xcall _delay_10us
.dbline 73
; DQ_SET;
sbi 0x1b,7
.dbline 74
; DQ_IN;
cbi 0x1a,7
.dbline 75
; delay_10us(7); /*延时80uS*/
ldi R16,7
xcall _delay_10us
.dbline 76
; i = DQ_R;
in R20,0x19
andi R20,128
.dbline 77
; delay_10us(49); /*延时500uS(保持>480uS)*/
ldi R16,49
xcall _delay_10us
.dbline 78
; if (flag & 0x80) { /*恢复中断状态*/
lds R2,_flag
sbrs R2,7
rjmp L8
.dbline 78
.dbline 79
; SEI();
sei
.dbline 80
; }
L8:
.dbline 81
; if (i) {
tst R20
breq L10
.dbline 81
.dbline 82
; return 0x00;
clr R16
xjmp L7
L10:
.dbline 84
; }
; else {
.dbline 85
; return 0x01;
ldi R16,1
.dbline -2
L7:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r i 20 c
.dbend
.dbfunc e ds1820_read_byte _ds1820_read_byte fc
; value -> R20
; i -> R22
.even
_ds1820_read_byte::
xcall push_gset2
.dbline -1
.dbline 95
; }
; }
;
; /*===================================================================
; // 函数功能: 从 1-wire 总线上读取一个字节
; // 形参: void
; // 返回: unsigned char 读到的值
; // 编写: 2004/8/25
; ===================================================================*/
; unsigned char ds1820_read_byte(void) {
.dbline 98
;
; unsigned char i;
; unsigned char value = 0;
clr R20
.dbline 99
; flag = SREG; /*中断保护*/
in R2,0x3f
sts _flag,R2
.dbline 100
; CLI(); /*关中断*/
cli
.dbline 101
ldi R22,8
xjmp L16
L13:
.dbline 101
; for (i = 8; i != 0; i--) {
.dbline 102
; value >>= 1;
lsr R20
.dbline 103
; DQ_OUT;
sbi 0x1a,7
.dbline 104
; DQ_CLR;
cbi 0x1b,7
.dbline 105
; NOP(); /*延时4uS*/
nop
.dbline 106
; NOP();
nop
.dbline 107
; NOP();
nop
.dbline 108
; NOP();
nop
.dbline 109
; DQ_SET;
sbi 0x1b,7
.dbline 110
; DQ_IN;
cbi 0x1a,7
.dbline 111
; NOP(); /*延时10uS*/
nop
.dbline 112
; NOP();
nop
.dbline 113
; NOP();
nop
.dbline 114
; NOP();
nop
.dbline 115
; NOP();
nop
.dbline 116
; NOP();
nop
.dbline 117
; NOP();
nop
.dbline 118
; NOP();
nop
.dbline 119
; NOP();
nop
.dbline 120
; NOP();
nop
.dbline 121
; if (DQ_R) {
sbis 0x19,7
rjmp L17
.dbline 121
.dbline 122
; value|=0x80;
ori R20,128
.dbline 123
; }
L17:
.dbline 124
ldi R16,5
xcall _delay_10us
.dbline 125
L14:
.dbline 101
dec R22
L16:
.dbline 101
tst R22
brne L13
.dbline 126
; delay_10us(5); /*延时60uS*/
; }
; if (flag & 0x80) { /*恢复中断状态*/
lds R2,_flag
sbrs R2,7
rjmp L19
.dbline 126
.dbline 127
; SEI();
sei
.dbline 128
; }
L19:
.dbline 129
; return(value);
mov R16,R20
.dbline -2
L12:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r value 20 c
.dbsym r i 22 c
.dbend
.dbfunc e ds1820_write_byte _ds1820_write_byte fV
; i -> R20
; value -> R22
.even
_ds1820_write_byte::
xcall push_gset2
mov R22,R16
.dbline -1
.dbline 138
; }
;
; /*===================================================================
; // 函数功能: 向 1-WIRE 总线上写一个字节
; // 形参: value 写到总线上的值
; // 返回: void
; // 编写: 2004/8/25
; ===================================================================*/
; void ds1820_write_byte(unsigned char value) {
.dbline 141
;
; unsigned char i;
; flag = SREG; /*中断保护*/
in R2,0x3f
sts _flag,R2
.dbline 142
; CLI(); /*关中断*/
cli
.dbline 143
ldi R20,8
xjmp L25
L22:
.dbline 143
; for (i = 8; i != 0; i--) {
.dbline 144
; DQ_OUT;
sbi 0x1a,7
.dbline 145
; DQ_CLR;
cbi 0x1b,7
.dbline 146
; NOP(); /*延时4uS*/
nop
.dbline 147
; NOP();
nop
.dbline 148
; NOP();
nop
.dbline 149
; NOP();
nop
.dbline 150
; if (value & 0x01) {
sbrc R22,0
.dbline 150
.dbline 151
; DQ_SET;
sbi 0x1b,7
.dbline 152
; }
L26:
.dbline 153
ldi R16,7
xcall _delay_10us
.dbline 154
sbi 0x1b,7
.dbline 155
lsr R22
.dbline 156
L23:
.dbline 143
dec R20
L25:
.dbline 143
tst R20
brne L22
.dbline 157
lds R2,_flag
sbrs R2,7
rjmp L28
.dbline 157
.dbline 158
sei
.dbline 159
L28:
.dbline -2
L21:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r i 20 c
.dbsym r value 22 c
.dbend
.dbfunc e ds1820_start _ds1820_start fV
.even
_ds1820_start::
.dbline -1
.dbline 163
; delay_10us(7); /*延时80uS*/
; DQ_SET; /*位结束*/
; value >>= 1;
; }
; if (flag & 0x80) { /*恢复中断状态*/
; SEI();
; }
; }
;
; /*启动ds1820转换*/
; void ds1820_start(void) {
.dbline 165
;
; ds1820_reset();
xcall _ds1820_reset
.dbline 166
; ds1820_write_byte(0xCC); /*勿略地址*/
ldi R16,204
xcall _ds1820_write_byte
.dbline 167
; ds1820_write_byte(0x44); /*启动转换*/
ldi R16,68
xcall _ds1820_write_byte
.dbline -2
L30:
.dbline 0 ; func end
ret
.dbend
.dbfunc e ds1820_read_temp _ds1820_read_temp fi
; buf -> y+0
; i -> R20,R21
.even
_ds1820_read_temp::
xcall push_gset1
sbiw R28,9
.dbline -1
.dbline 176
; }
;
; /*===================================================================
; // 函数功能: 读取温度
; // 形参: *temperature 温度存储空间
; // 返回: unsigned char true为有效
; // 编写: 2004/8/25
; ===================================================================*/
; unsigned int ds1820_read_temp(void) {
.dbline 181
;
; unsigned int i;
; unsigned char buf[9];
;
; ds1820_reset();
xcall _ds1820_reset
.dbline 182
; ds1820_write_byte(0xCC); /*勿略地址*/
ldi R16,204
xcall _ds1820_write_byte
.dbline 183
; ds1820_write_byte(0xBE); /*读取温度*/
ldi R16,190
xcall _ds1820_write_byte
.dbline 184
clr R20
clr R21
xjmp L35
L32:
.dbline 184
.dbline 185
xcall _ds1820_read_byte
movw R24,R28
movw R30,R20
add R30,R24
adc R31,R25
std z+0,R16
.dbline 186
L33:
.dbline 184
subi R20,255 ; offset = 1
sbci R21,255
L35:
.dbline 184
; for (i = 0; i < 9; i++) {
cpi R20,9
ldi R30,0
cpc R21,R30
brlo L32
.dbline 187
; buf[i] = ds1820_read_byte();
; }
; i = buf[1];
ldd R20,y+1
clr R21
.dbline 188
; i <<= 8;
mov R21,R20
clr R20
.dbline 189
; i |= buf[0];
ldd R2,y+0
clr R3
or R20,R2
or R21,R3
.dbline 191
;
; return i;
movw R16,R20
.dbline -2
L31:
adiw R28,9
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym l buf 0 A[9:9]c
.dbsym r i 20 i
.dbend
.area bss(ram, con, rel)
.dbfile E:\ICCAVR\project\AVRMEG~2\icc\030-DS18B20-LED4\DS1820.C
_flag::
.blkb 1
.dbsym e flag _flag c
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?