📄 24c02.s
字号:
.dbline 196
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 196
.dbline 196
.dbline 197
; SDA_1;
.dbline 197
sbi 0x15,1
.dbline 197
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 197
.dbline 197
.dbline -2
L29:
.dbline 0 ; func end
ret
.dbend
.dbfunc e IICRecAck _IICRecAck fc
; ack -> R20
.even
_IICRecAck::
xcall push_gset1
.dbline -1
.dbline 205
; //SCL_0;
; }
; //=======================================================================
; //检查应答位,向I2C写数据时,I2C设备(EEPROM)正常接受到数据后在第9周期发出0应答,
; //如果不正常接受到数据,则不以0应答而进入待命状态。
; //写数据后需检查I2C设备是否有0应答。
; unsigned char IICRecAck(void)
; {
.dbline 208
; uint8 ack;
;
; SCL_0;
.dbline 208
cbi 0x15,0
.dbline 208
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 208
.dbline 208
.dbline 209
; SDA_0;
.dbline 209
cbi 0x15,1
.dbline 209
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 209
.dbline 209
.dbline 210
; SCL_1;
.dbline 210
sbi 0x15,0
.dbline 210
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 210
.dbline 210
.dbline 212
;
; i2c_ddr&=~(1<<SDA); //设置SDA为输入
cbi 0x14,1
.dbline 213
; i2c_port|=1<<SDA; //上拉
sbi 0x15,1
.dbline 214
; ack=(i2c_pin & (1<<SDA)); //读SDA应答位
in R20,0x13
andi R20,2
.dbline 215
; i2c_ddr|=(1<<SDA); //恢复为输出
sbi 0x14,1
.dbline 217
;
; SCL_0;
.dbline 217
cbi 0x15,0
.dbline 217
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 217
.dbline 217
.dbline 218
; return(ack);
mov R16,R20
.dbline -2
L30:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r ack 20 c
.dbend
.dbfunc e IICReceiveByte _IICReceiveByte fc
; i -> R20
; receivebyte -> R22
.even
_IICReceiveByte::
xcall push_gset2
.dbline -1
.dbline 223
; }
; //=======================================================================
; //从IIC总线上读数据。
; unsigned char IICReceiveByte(void)
; {
.dbline 224
; uint8 receivebyte=0,i=8;
clr R22
.dbline 224
ldi R20,8
.dbline 226
;
; i2c_ddr&=~(1<<SDA); //设置SDA为输入
cbi 0x14,1
.dbline 227
; i2c_port|=1<<SDA; //上拉
sbi 0x15,1
.dbline 228
; i2c_port&=~(1<<SCL);
cbi 0x15,0
xjmp L33
L32:
.dbline 231
;
; while(i--)
; {
.dbline 232
; SCL_1;
.dbline 232
sbi 0x15,0
.dbline 232
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 232
.dbline 232
.dbline 233
; receivebyte <<= 1;
lsl R22
.dbline 234
; if(i2c_pin & (1<<SDA)) receivebyte |= 1;
sbis 0x13,1
rjmp L35
.dbline 234
ori R22,1
L35:
.dbline 235
.dbline 235
cbi 0x15,0
.dbline 235
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 235
.dbline 235
.dbline 236
L33:
.dbline 230
mov R2,R20
clr R3
subi R20,1
tst R2
brne L32
.dbline 238
; SCL_0;
; }
;
; i2c_ddr|=(1<<SDA); //恢复SDA为输出
sbi 0x14,1
.dbline 239
; return(receivebyte);
mov R16,R22
.dbline -2
L31:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r i 20 c
.dbsym r receivebyte 22 c
.dbend
.dbfunc e IICACK _IICACK fV
.even
_IICACK::
.dbline -1
.dbline 246
; }
;
;
; //=======================================================================
; //对IIC总线产生应答。读I2C设备时,如果下一步还要读则产生0应答。
; void IICACK(void)
; {
.dbline 247
; SDA_0;
.dbline 247
cbi 0x15,1
.dbline 247
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 247
.dbline 247
.dbline 248
; SCL_1;
.dbline 248
sbi 0x15,0
.dbline 248
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 248
.dbline 248
.dbline 249
; SCL_0;
.dbline 249
cbi 0x15,0
.dbline 249
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 249
.dbline 249
.dbline 251
;
; SDA_1;
.dbline 251
sbi 0x15,1
.dbline 251
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 251
.dbline 251
.dbline -2
L37:
.dbline 0 ; func end
ret
.dbend
.dbfunc e IICNoAck _IICNoAck fV
.even
_IICNoAck::
.dbline -1
.dbline 256
; }
; //=======================================================================
; //不对IIC总线产生应答。读I2C设备时,如果下一步不需要读则产生1应答(即不应答)。
; void IICNoAck(void)
; {
.dbline 257
; SDA_1;
.dbline 257
sbi 0x15,1
.dbline 257
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 257
.dbline 257
.dbline 258
; SCL_1;
.dbline 258
sbi 0x15,0
.dbline 258
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 258
.dbline 258
.dbline 259
; SCL_0;
.dbline 259
cbi 0x15,0
.dbline 259
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 259
.dbline 259
.dbline -2
L38:
.dbline 0 ; func end
ret
.dbend
.dbfunc e IICSendByte _IICSendByte fV
; j -> R20
; sendbyte -> R22
.even
_IICSendByte::
xcall push_gset2
mov R22,R16
.dbline -1
.dbline 264
; }
; //=======================================================================
; //向IIC总线写数据。SCL=0时SDA的数据可改变,SCL=1时需保持SDA数据稳定。
; void IICSendByte(unsigned char sendbyte)
; {
.dbline 265
; uint8 j=8;
ldi R20,8
.dbline 266
; for(;j>0;j--)
xjmp L43
L40:
.dbline 267
; {
.dbline 268
; SCL_0;
.dbline 268
cbi 0x15,0
.dbline 268
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 268
.dbline 268
.dbline 270
;
; if(sendbyte&0x80) SDA_1 //SCL=0时写数据到SDA
sbrs R22,7
rjmp L44
.dbline 270
.dbline 270
sbi 0x15,1
.dbline 270
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 270
xjmp L45
L44:
.dbline 271
; else SDA_0
.dbline 271
cbi 0x15,1
.dbline 271
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 271
L45:
.dbline 273
lsl R22
.dbline 275
.dbline 275
sbi 0x15,0
.dbline 275
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 275
.dbline 275
.dbline 276
L41:
.dbline 266
dec R20
L43:
.dbline 266
clr R2
cp R2,R20
brlo L40
.dbline 277
;
; sendbyte<<=1;
;
; SCL_1;
; }
; SCL_0;
.dbline 277
cbi 0x15,0
.dbline 277
ldi R16,100
ldi R17,0
xcall _Delay_us
.dbline 277
.dbline 277
.dbline -2
L39:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r j 20 c
.dbsym r sendbyte 22 c
.dbend
.dbfunc e i2c_Read _i2c_Read fc
; ErrorCount -> R22
; data -> R20
; device -> R10
; subAddr -> R12
.even
_i2c_Read::
xcall push_gset4
mov R10,R18
mov R12,R16
.dbline -1
.dbline 283
; }
;
; //=======================================================================
; //通过I2C读STA013
; unsigned char i2c_Read(unsigned char subAddr,uint8 device)
; {
.dbline 284
; uint8 data=0;
clr R20
.dbline 285
; uint8 ErrorCount=10;
ldi R22,10
xjmp L48
L47:
.dbline 289
; //Delay_us(1200);
;
; while(ErrorCount--)
; {
.dbline 290
; IICStart();
xcall _IICStart
.dbline 291
; IICSendByte(device);
mov R16,R10
xcall _IICSendByte
.dbline 293
;
; if(IICRecAck()) continue; //如果为非0应答,重复写
xcall _IICRecAck
tst R16
breq L50
.dbline 293
xjmp L48
L50:
.dbline 295
;
; IICSendByte(subAddr);
mov R16,R12
xcall _IICSendByte
.dbline 296
; if(IICRecAck()) continue; //如果为非0应答,重复写
xcall _IICRecAck
tst R16
breq L52
.dbline 296
xjmp L48
L52:
.dbline 297
; IICStart();
xcall _IICStart
.dbline 298
; IICSendByte(device | 0x01);
mov R16,R10
ori R16,1
xcall _IICSendByte
.dbline 299
; if(IICRecAck()) continue;
xcall _IICRecAck
tst R16
breq L54
.dbline 299
xjmp L48
L54:
.dbline 302
xcall _IICReceiveByte
mov R20,R16
.dbline 303
xcall _IICNoAck
.dbline 304
xjmp L49
L48:
.dbline 288
mov R2,R22
clr R3
subi R22,1
tst R2
brne L47
L49:
.dbline 306
;
;
; data=IICReceiveByte(); //读数据,并发出1应答,表示不再读。
; IICNoAck();
; break;
; }
; IICStop();
xcall _IICStop
.dbline 307
; return data;
mov R16,R20
.dbline -2
L46:
xcall pop_gset4
.dbline 0 ; func end
ret
.dbsym r ErrorCount 22 c
.dbsym r data 20 c
.dbsym r device 10 c
.dbsym r subAddr 12 c
.dbend
.dbfunc e i2c_Write _i2c_Write fV
; ErrorCount -> R20
; data -> R22
; device -> R10
; subAddr -> R12
.even
_i2c_Write::
xcall push_gset4
mov R10,R18
mov R12,R16
ldd R22,y+8
.dbline -1
.dbline 312
; }
;
; //=======================================================================
; void i2c_Write(unsigned char subAddr,unsigned char device,unsigned char data)
; {
.dbline 313
; uint8 ErrorCount=10;
ldi R20,10
xjmp L58
L57:
.dbline 316
;
; while(ErrorCount--)
; {
.dbline 317
; IICStart();
xcall _IICStart
.dbline 318
; IICSendByte(device);
mov R16,R10
xcall _IICSendByte
.dbline 319
; if(IICRecAck()) continue; //如果为非0应答,重复写
xcall _IICRecAck
tst R16
breq L60
.dbline 319
xjmp L58
L60:
.dbline 320
; IICSendByte(subAddr);
mov R16,R12
xcall _IICSendByte
.dbline 321
; if(IICRecAck()) continue; //如果为非0应答,重复写
xcall _IICRecAck
tst R16
breq L62
.dbline 321
xjmp L58
L62:
.dbline 323
mov R16,R22
xcall _IICSendByte
.dbline 324
xcall _IICACK
.dbline 326
xjmp L59
L58:
.dbline 315
mov R2,R20
clr R3
subi R20,1
tst R2
brne L57
L59:
.dbline 329
;
; IICSendByte(data);
; IICACK();
;
; break;
; }
;
; IICStop();
xcall _IICStop
.dbline -2
L56:
xcall pop_gset4
.dbline 0 ; func end
ret
.dbsym r ErrorCount 20 c
.dbsym r data 22 c
.dbsym r device 10 c
.dbsym r subAddr 12 c
.dbend
.area bss(ram, con, rel)
.dbfile E:\MYWORK~1\开发设计\EasyAVR\程序范例\Mega16例程\24C02T~1\24c02.c
_DISP_NUM::
.blkb 1
.dbsym e DISP_NUM _DISP_NUM c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -