📄 main.s
字号:
.module main.c
.area text(rom, con, rel)
.dbfile E:\atmega16子函数\TWI_slaver0\main.c
.dbfunc e Start _Start fV
.even
_Start::
.dbline -1
.dbline 22
; /*
; TWI 从机的查询和中断接收
; ICCAVR6.31A
; */
; #include"iom16v.h"
; #include"UART.h"
; #include"delay.h"
; #define uchar unsigned char
; #define STATUS (TWSR&0xf8)
; #define SLA_W 0x32
; #define SLA_R 0x33
; #define TWI_ADDRESS 0x32
; #define START 0x08
; #define RE_START 0x10
; #define MT_SLA_WRITE_ACK 0x18
; #define MT_SLA_READ_ACK 0x40
; #define MT_DATA_ACK 0x28
; #define MT_READ_ACK 0x58
; #define TW_SR_DATA_ACK 0x80
;
; void Start(void)
; {
.dbline 23
; TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
ldi R24,164
out 0x36,R24
L2:
.dbline 24
L3:
.dbline 24
; while(!(TWCR&(1<<TWINT)));
in R2,0x36
sbrs R2,7
rjmp L2
.dbline -2
.dbline 25
; }
L1:
.dbline 0 ; func end
ret
.dbend
.dbfunc e Stop _Stop fV
.even
_Stop::
.dbline -1
.dbline 27
; void Stop(void)
; {
.dbline 28
; TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
ldi R24,148
out 0x36,R24
.dbline -2
.dbline 29
; }
L5:
.dbline 0 ; func end
ret
.dbend
.dbfunc e Load _Load fV
; i -> R16
.even
_Load::
.dbline -1
.dbline 31
; void Load(uchar i)
; {
.dbline 32
; TWDR=i;
out 0x3,R16
.dbline 33
; TWCR=(1<<TWINT)|(1<<TWEN);
ldi R24,132
out 0x36,R24
L7:
.dbline 34
L8:
.dbline 34
; while(!(TWCR&(1<<TWINT)));
in R2,0x36
sbrs R2,7
rjmp L7
.dbline -2
.dbline 36
;
; }
L6:
.dbline 0 ; func end
ret
.dbsym r i 16 c
.dbend
.dbfunc e Get _Get fV
; d -> R16,R17
.even
_Get::
.dbline -1
.dbline 39
;
; void Get(unsigned char *d)
; {
.dbline 40
; TWCR=(1<<TWINT)|(1<<TWEN);
ldi R24,132
out 0x36,R24
L11:
.dbline 41
L12:
.dbline 41
; while(!(TWCR&(1<<TWINT)));
in R2,0x36
sbrs R2,7
rjmp L11
.dbline 42
; *d= TWDR;
in R2,0x3
movw R30,R16
std z+0,R2
.dbline -2
.dbline 43
; }/**/
L10:
.dbline 0 ; func end
ret
.dbsym r d 16 pc
.dbend
.dbfunc e ERROR _ERROR fV
; state -> R20
.even
_ERROR::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 45
; void ERROR(uchar state)
; {Stop();
.dbline 45
xcall _Stop
.dbline -2
.dbline 46
; }
L14:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r state 20 c
.dbend
.dbfunc e WriteByte _WriteByte fc
; data -> R20
.even
_WriteByte::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 51
;
; /*写一个字节data,成功则返回1,否则
; 返回0*/
; uchar WriteByte(uchar data)
; {
.dbline 53
; //启动I2C
; Start();
xcall _Start
.dbline 54
; if(STATUS!=START) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,8
breq L16
.dbline 54
.dbline 54
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 54
clr R16
xjmp L15
L16:
.dbline 57
;
; //发送从机地址(写),等待回应, 错误检测
; Load(SLA_W);
ldi R16,50
xcall _Load
.dbline 58
; if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,24
breq L18
.dbline 58
.dbline 58
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 58
clr R16
xjmp L15
L18:
.dbline 65
; /*
; //发送EEPROM地址,等待回应,错误检测
; Load(addr);
; if(STATUS!=MT_DATA_ACK) {ERROR(STATUS);return 0;}
; */
; //发送数据,等待回应,错误检测
; Load(data);
mov R16,R20
xcall _Load
.dbline 66
; if(STATUS!=MT_DATA_ACK) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,40
breq L20
.dbline 66
.dbline 66
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 66
clr R16
xjmp L15
L20:
.dbline 68
;
; Stop();
xcall _Stop
.dbline 69
; return 1;//发送成功,返回1;
ldi R16,1
.dbline -2
L15:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r data 20 c
.dbend
.dbfunc e ReadByte _ReadByte fc
; p_data -> R20,R21
; addr -> R22
.even
_ReadByte::
xcall push_gset2
movw R20,R18
mov R22,R16
.dbline -1
.dbline 77
;
;
;
; }
;
; unsigned char ReadByte(unsigned char addr,
; unsigned char *p_data)
; {
.dbline 79
; //启动I2C
; Start();
xcall _Start
.dbline 80
; if(STATUS!=START) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,8
breq L23
.dbline 80
.dbline 80
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 80
clr R16
xjmp L22
L23:
.dbline 83
;
; //发送从机地址(写),等待回应, 错误检测
; Load(SLA_W);
ldi R16,50
xcall _Load
.dbline 84
; if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,24
breq L25
.dbline 84
.dbline 84
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 84
clr R16
xjmp L22
L25:
.dbline 87
;
; //发送EEPROM地址,等待回应,错误检测
; Load(addr);
mov R16,R22
xcall _Load
.dbline 88
; if(STATUS!=MT_DATA_ACK) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,40
breq L27
.dbline 88
.dbline 88
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 88
clr R16
xjmp L22
L27:
.dbline 91
;
; //再次启动I2C
; Start();
xcall _Start
.dbline 92
; if(STATUS!=RE_START) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,16
breq L29
.dbline 92
.dbline 92
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 92
clr R16
xjmp L22
L29:
.dbline 95
;
; //发送从机地址(读),等待回应, 错误检测
; Load(SLA_R);
ldi R16,51
xcall _Load
.dbline 96
; if(STATUS!=MT_SLA_READ_ACK) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,64
breq L31
.dbline 96
.dbline 96
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 96
clr R16
xjmp L22
L31:
.dbline 99
;
; //读取I2C数据
; Get(p_data);
movw R16,R20
xcall _Get
.dbline 100
; if(STATUS!=MT_READ_ACK) {ERROR(STATUS);return 0;}
in R24,0x1
andi R24,248
cpi R24,88
breq L33
.dbline 100
.dbline 100
in R16,0x1
andi R16,248
xcall _ERROR
.dbline 100
clr R16
xjmp L22
L33:
.dbline 103
;
; //发送Stop,结束读I2C操作。
; Stop();
xcall _Stop
.dbline 104
; return 1;
ldi R16,1
.dbline -2
L22:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r p_data 20 pc
.dbsym r addr 22 c
.dbend
.dbfunc e Init_Io _Init_Io fV
.even
_Init_Io::
.dbline -1
.dbline 110
;
;
;
; }/**/
; void Init_Io(void)
; {
.dbline 111
; DDRC&=~((1<<PC0)&(1<<PC0)&(1<<PC2));
in R24,0x14
andi R24,255
out 0x14,R24
.dbline 112
; PORTC|= (1<<PC0)|(1<<PC0)|(1<<PC2);
in R24,0x15
ori R24,5
out 0x15,R24
.dbline 115
;
; //TWI接口初始化,从器件模式
; TWAR=TWI_ADDRESS | (1<<TWGCE);
ldi R24,51
out 0x2,R24
.dbline 116
; TWCR=(1<<TWEA) | (1<<TWEN)|(1<<TWIE);
ldi R24,69
out 0x36,R24
.dbline 117
; asm("SEI");
SEI
.dbline -2
.dbline 118
; }
L35:
.dbline 0 ; func end
ret
.dbend
.dbfunc e main _main fV
; temp2 -> R20
; temp1 -> R22
.even
_main::
.dbline -1
.dbline 120
; void main()
; {
.dbline 122
; uchar temp1,temp2;
; Init_Io();
xcall _Init_Io
.dbline 123
; uart_init();
xcall _uart_init
xjmp L38
L37:
.dbline 126
; // puts("串口正常工作");
; while(1)
; {
.dbline 127
; TWCR=(1<<TWINT)|(1<<TWEA) | (1<<TWEN); //启动一次接收
ldi R24,196
out 0x36,R24
L40:
.dbline 128
L41:
.dbline 128
; while((TWCR&(1<<TWINT))==0); //等待有数据被接收
in R2,0x36
sbrs R2,7
rjmp L40
.dbline 130
; //Delay_nms(100);
; temp1=STATUS;
in R22,0x1
andi R22,248
.dbline 132
;
; temp2=TWDR;
in R20,0x3
.dbline 135
; //UDR = temp2;
; //putchar(temp1);
; if(temp1==0x90)
cpi R22,144
brne L43
.dbline 136
; {
.dbline 138
; // puts("接受到:");
; putchar(temp2);
mov R16,R20
xcall _putchar
.dbline 139
; }
L43:
.dbline 142
L38:
.dbline 125
xjmp L37
X0:
.dbline -2
.dbline 144
;
;
; }
;
; }
L36:
.dbline 0 ; func end
ret
.dbsym r temp2 20 c
.dbsym r temp1 22 c
.dbend
.area vector(rom, abs)
.org 68
jmp _TWI_ISR
.area text(rom, con, rel)
.dbfile E:\atmega16子函数\TWI_slaver0\main.c
.dbfunc e TWI_ISR _TWI_ISR fV
; temp2 -> R16
; temp1 -> R16
.even
_TWI_ISR::
st -y,R16
st -y,R24
in R16,0x3f
st -y,R16
.dbline -1
.dbline 147
; #pragma interrupt_handler TWI_ISR:18
; void TWI_ISR(void)
; {
.dbline 149
; uchar temp1,temp2;
; temp1=STATUS;
in R16,0x1
andi R16,248
.dbline 150
; temp2=TWDR;
in R16,0x3
.dbline 156
; /* if(temp1==0x90)
; {
; puts("接受到:");
; putchar(temp2);
; }*/
; TWCR=(1<<TWINT)|(1<<TWEA) | (1<<TWEN)|(1<<TWIE); //启动一次接收
ldi R24,197
out 0x36,R24
.dbline -2
.dbline 158
;
; }
L45:
ld R16,y+
out 0x3f,R16
ld R24,y+
ld R16,y+
.dbline 0 ; func end
reti
.dbsym r temp2 16 c
.dbsym r temp1 16 c
.dbend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -