📄 at24c.s
字号:
.module AT24C.c
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\AT24C.c
.dbfunc e AT24C_delay _AT24C_delay fV
; us -> R16
.even
_AT24C_delay::
.dbline -1
.dbline 17
;
; #include"include.h"
;
; #define AT24CWAIT 1
; #define AT24_DEVICEADDR 0xA0
;
; #define SCL_HI() PORTB |= (1<<1)
; #define SCL_LO() PORTB &= ~(1<<1)
;
; #define SDA_HI() PORTB |= (1<<0)
; #define SDA_LO() PORTB &= ~(1<<0)
; #define SDA_OUTPUT() DDRB |= (1<<0)
; #define SDA_INPUT() DDRB &= ~(1<<0)
; #define SDAIN() (PINB&(1<<0))
;
; void AT24C_delay(unsigned char us)
; {
L3:
.dbline 18
; do{
.dbline 19
; asm("nop");
nop
.dbline 20
; asm("nop");
nop
.dbline 21
; }while(us--);
L4:
.dbline 21
mov R2,R16
clr R3
subi R16,1
tst R2
brne L3
X0:
.dbline -2
L2:
.dbline 0 ; func end
ret
.dbsym r us 16 c
.dbend
.dbfunc e I2C_Initial _I2C_Initial fV
.even
_I2C_Initial::
.dbline -1
.dbline 26
; }
;
; //打开I2C
; void I2C_Initial(void)
; {
.dbline 27
; DDRB |= (1<<1);//
sbi 0x17,1
.dbline 28
; DDRB |= (1<<0); //
sbi 0x17,0
.dbline 30
;
; SCL_HI();//SCL
sbi 0x18,1
.dbline 31
; SDA_HI();//SDA
sbi 0x18,0
.dbline -2
L6:
.dbline 0 ; func end
ret
.dbend
.dbfunc e I2C_Close _I2C_Close fV
.even
_I2C_Close::
.dbline -1
.dbline 36
; }
;
; //关闭I2C
; void I2C_Close(void)
; {
.dbline 37
; SCL_HI();//SCL
sbi 0x18,1
.dbline 38
; SDA_HI();//SDA
sbi 0x18,0
.dbline 40
;
; DDRB |= (1<<1);//
sbi 0x17,1
.dbline 41
; DDRB |= (1<<0); //
sbi 0x17,0
.dbline -2
L7:
.dbline 0 ; func end
ret
.dbend
.dbfunc e I2C_SendStart _I2C_SendStart fV
.even
_I2C_SendStart::
.dbline -1
.dbline 46
; }
;
; /*发送起动信号*/
; void I2C_SendStart(void)
; {
.dbline 47
; SCL_HI();
sbi 0x18,1
.dbline 48
; SDA_HI();
sbi 0x18,0
.dbline 49
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 50
; SDA_LO();
cbi 0x18,0
.dbline 51
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 52
; SCL_LO();
cbi 0x18,1
.dbline -2
L8:
.dbline 0 ; func end
ret
.dbend
.dbfunc e I2C_SendStop _I2C_SendStop fV
.even
_I2C_SendStop::
.dbline -1
.dbline 57
; }
;
; /*发送停止信号*/
; void I2C_SendStop(void)
; {
.dbline 58
; SDA_LO();
cbi 0x18,0
.dbline 59
; SCL_HI();
sbi 0x18,1
.dbline 60
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 61
; SDA_HI();
sbi 0x18,0
.dbline -2
L9:
.dbline 0 ; func end
ret
.dbend
.dbfunc e I2C_SendByte _I2C_SendByte fc
; i -> R20
; c -> R10
.even
_I2C_SendByte::
st -y,R10
st -y,R20
mov R10,R16
.dbline -1
.dbline 66
; }
;
; /*发送一字节数据*/
; unsigned char I2C_SendByte(unsigned char c)
; {
.dbline 68
; unsigned char i;
; for(i=0;i<8;i++)
clr R20
xjmp L14
L11:
.dbline 69
; {
.dbline 70
; if((c<<i)&0x80) SDA_HI();
mov R16,R10
mov R17,R20
xcall lsl8
mov R24,R16
andi R24,128
breq L15
X1:
.dbline 70
sbi 0x18,0
xjmp L16
L15:
.dbline 71
; else SDA_LO();
cbi 0x18,0
L16:
.dbline 72
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 73
; SCL_HI();
sbi 0x18,1
.dbline 74
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 75
; SCL_LO();
cbi 0x18,1
.dbline 76
; }
L12:
.dbline 68
inc R20
L14:
.dbline 68
cpi R20,8
brlo L11
X2:
.dbline 77
; SDA_INPUT();
cbi 0x17,0
.dbline 78
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 79
; SCL_HI();
sbi 0x18,1
.dbline 80
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 81
; if(SDAIN()) i=0;
sbis 0x16,0
rjmp L17
X3:
.dbline 81
clr R20
xjmp L18
L17:
.dbline 82
; else i=1;
ldi R20,1
L18:
.dbline 83
; SCL_LO();
cbi 0x18,1
.dbline 84
; SDA_OUTPUT();
sbi 0x17,0
.dbline 85
; return i;
mov R16,R20
.dbline -2
L10:
.dbline 0 ; func end
ld R20,y+
ld R10,y+
ret
.dbsym r i 20 c
.dbsym r c 10 c
.dbend
.dbfunc e I2C_ReceiveByte _I2C_ReceiveByte fc
; retc -> R20
; i -> R22
.even
_I2C_ReceiveByte::
st -y,R20
st -y,R22
.dbline -1
.dbline 90
; }
;
; /*接收一字节数据*/
; unsigned char I2C_ReceiveByte(void)
; {
.dbline 93
; unsigned char i;
; unsigned char retc;
; retc=0;
clr R20
.dbline 94
; SDA_INPUT();
cbi 0x17,0
.dbline 95
; for(i=0;i<8;i++)
clr R22
xjmp L23
L20:
.dbline 96
; {
.dbline 97
; retc <<= 1;
lsl R20
.dbline 98
; SCL_LO();
cbi 0x18,1
.dbline 99
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 100
; SCL_HI();
sbi 0x18,1
.dbline 101
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 102
; if(SDAIN()) retc |= 1;
sbis 0x16,0
rjmp L24
X4:
.dbline 102
ori R20,1
L24:
.dbline 103
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 104
; }
L21:
.dbline 95
inc R22
L23:
.dbline 95
cpi R22,8
brlo L20
X5:
.dbline 105
; SCL_LO();
cbi 0x18,1
.dbline 106
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 107
; SDA_HI();
sbi 0x18,0
.dbline 108
; SDA_OUTPUT();
sbi 0x17,0
.dbline 109
; SCL_HI();
sbi 0x18,1
.dbline 110
; AT24C_delay(AT24CWAIT);
ldi R16,1
xcall _AT24C_delay
.dbline 111
; SCL_LO();
cbi 0x18,1
.dbline 112
; return(retc);
mov R16,R20
.dbline -2
L19:
.dbline 0 ; func end
ld R22,y+
ld R20,y+
ret
.dbsym r retc 20 c
.dbsym r i 22 c
.dbend
.dbfunc e AT24_Read _AT24_Read fc
; i -> R10
; ret -> R22
; num -> y+10
; addr -> R20,R21
; buffer -> R12,R13
.even
_AT24_Read::
xcall push_xgsetF0FC
movw R20,R18
movw R12,R16
.dbline -1
.dbline 117
; }
;
; /*EEPROM数据读出*/
; unsigned char AT24_Read(unsigned char * buffer,unsigned int addr,unsigned char num)
; {
.dbline 119
; unsigned char i,ret;
; for(i=0;i<num;i++)
clr R10
xjmp L30
L27:
.dbline 120
; {
.dbline 121
; I2C_SendStart();
xcall _I2C_SendStart
.dbline 122
; ret = I2C_SendByte(AT24_DEVICEADDR);
ldi R16,160
xcall _I2C_SendByte
mov R22,R16
.dbline 123
; if(ret != 1)
cpi R16,1
breq L31
X6:
.dbline 124
; return 0;
clr R16
xjmp L26
L31:
.dbline 125
; ret = I2C_SendByte(addr>>8);
movw R16,R20
mov R16,R17
clr R17
xcall _I2C_SendByte
mov R22,R16
.dbline 126
; if(ret != 1)
cpi R16,1
breq L33
X7:
.dbline 127
; return 0;
clr R16
xjmp L26
L33:
.dbline 128
; ret = I2C_SendByte(addr);
mov R16,R20
xcall _I2C_SendByte
mov R14,R16
mov R22,R14
.dbline 129
; if(ret != 1)
cpi R16,1
breq L35
X8:
.dbline 130
; return 0;
clr R16
xjmp L26
L35:
.dbline 131
; I2C_SendStart();
xcall _I2C_SendStart
.dbline 132
; ret = I2C_SendByte(AT24_DEVICEADDR|0x01);
ldi R16,161
xcall _I2C_SendByte
mov R14,R16
mov R22,R14
.dbline 133
; if(ret != 1)
cpi R16,1
breq L37
X9:
.dbline 134
; return 0;
clr R16
xjmp L26
L37:
.dbline 135
; buffer[i] = I2C_ReceiveByte();
xcall _I2C_ReceiveByte
mov R14,R16
mov R30,R10
clr R31
add R30,R12
adc R31,R13
std z+0,R14
.dbline 136
; I2C_SendStop();
xcall _I2C_SendStop
.dbline 137
; addr ++;
subi R20,255 ; offset = 1
sbci R21,255
.dbline 138
; AT24C_delay(10);
ldi R16,10
xcall _AT24C_delay
.dbline 139
; }
L28:
.dbline 119
inc R10
L30:
.dbline 119
ldd R0,y+10
cp R10,R0
brsh X11
xjmp L27
X11:
X10:
.dbline 140
; return 1;
ldi R16,1
.dbline -2
L26:
.dbline 0 ; func end
xjmp pop_xgsetF0FC
.dbsym r i 10 c
.dbsym r ret 22 c
.dbsym l num 10 c
.dbsym r addr 20 i
.dbsym r buffer 12 pc
.dbend
.dbfunc e AT24_Write _AT24_Write fc
; ct -> R10
; ret -> R14
; i -> R22
; num -> R12
; addr -> y+12
; buffer -> y+10
.even
_AT24_Write::
xcall push_arg4
xcall push_xgsetF0FC
ldd R12,y+14
.dbline -1
.dbline 145
; }
;
; /*写数据到EEPROM*/
; unsigned char AT24_Write(unsigned char * buffer,unsigned int addr,unsigned char num)
; {
.dbline 147
; unsigned char i,ct,ret;
; ct = 0;
clr R10
L40:
.dbline 149
; nextpage:
; I2C_SendStart();
xcall _I2C_SendStart
.dbline 150
; ret = I2C_SendByte(AT24_DEVICEADDR);
ldi R16,160
xcall _I2C_SendByte
mov R14,R16
.dbline 151
; if(ret != 1)
cpi R16,1
breq L41
X12:
.dbline 152
; return 0;
clr R16
xjmp L39
L41:
.dbline 153
; ret = I2C_SendByte(addr>>8);
ldd R16,y+12
ldd R17,y+13
mov R16,R17
clr R17
xcall _I2C_SendByte
mov R14,R16
.dbline 154
; if(ret != 1)
cpi R16,1
breq L43
X13:
.dbline 155
; return 0;
clr R16
xjmp L39
L43:
.dbline 156
; ret = I2C_SendByte(addr);
ldd R16,y+12
xcall _I2C_SendByte
mov R20,R16
mov R14,R20
.dbline 157
; if(ret != 1)
cpi R16,1
breq L45
X14:
.dbline 158
; return 0;
clr R16
xjmp L39
L45:
.dbline 159
; for(i=0;i<num;i++)
clr R22
xjmp L50
L47:
.dbline 160
; {
.dbline 161
; ret = I2C_SendByte(buffer[ct++]);
mov R20,R10
clr R21
mov R24,R20
subi R24,255 ; addi 1
mov R10,R24
mov R30,R20
clr R31
ldd R0,y+10
ldd R1,y+11
add R30,R0
adc R31,R1
ldd R16,z+0
xcall _I2C_SendByte
mov R20,R16
mov R14,R20
.dbline 162
; if(ret != 1)
cpi R16,1
breq L51
X15:
.dbline 163
; return 0;
clr R16
xjmp L39
L51:
.dbline 164
; addr ++;
ldd R24,y+12
ldd R25,y+13
adiw R24,1
std y+13,R25
std y+12,R24
.dbline 165
; if((((unsigned char)addr)&0x7F) == 0)
andi R24,127
brne L53
X16:
.dbline 166
; {
.dbline 167
; I2C_SendStop();
xcall _I2C_SendStop
.dbline 168
; AT24C_delay(20);
ldi R16,20
xcall _AT24C_delay
.dbline 169
; num -= i+1;
mov R24,R22
subi R24,255 ; addi 1
sub R12,R24
.dbline 170
; goto nextpage;
xjmp L40
L53:
.dbline 172
; }
; }
L48:
.dbline 159
inc R22
L50:
.dbline 159
cp R22,R12
brlo L47
X17:
.dbline 173
; I2C_SendStop();
xcall _I2C_SendStop
.dbline 174
; AT24C_delay(15);
ldi R16,15
xcall _AT24C_delay
.dbline 175
; return 1;
ldi R16,1
.dbline -2
L39:
.dbline 0 ; func end
xcall pop_xgsetF0FC
adiw R28,4
ret
.dbsym r ct 10 c
.dbsym r ret 14 c
.dbsym r i 22 c
.dbsym r num 12 c
.dbsym l addr 12 i
.dbsym l buffer 10 pc
.dbend
; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -