📄 i2cmstr.lst
字号:
I2CMSTR PAGE 1
1 ;======================================================================
2 ;
3 ; Author : ADI - Apps www.analog.com/MicroConverter
4 ;
5 ; Date : May 2002
6 ;
7 ; File : i2Cmstr.asm
8 ;
9 ; Hardware : ADuC831
10 ;
11 ; Description : Code for a master in an I2C system. This code will
12 ; continuously receive and transmit a byte over the I2C
13 ; interface, then send the received byte out the UART,
14 ; then check if a character had been entered in the UART,
15 ; if so, it will send the ASCII value of the character
16 ; entered to the slave, the next time it transmits a byte.
17 ;
18 ; Reference : Tech Note, uC001: "MicroConverter I2C Compatible
19 ; Interface" find it at www.analog.com/microconverter
20
21 ;
22 ;======================================================================
23
24 $MOD831
25
26 ;____________________________________________________________________
27 ; DEFINE VARIABLES IN INTERNAL RAM
28
0030 29 BITCNT DATA 30h ; bit counter for I2C routines
0031 30 SLAVEADD DATA 31h ; slave address for I2C routines
0032 31 INPUT DATA 32h ; data recieved from the slave
0033 32 OUTPUT DATA 33h ; data to be transmitted to slave
33
0000 34 NOACK BIT 00h ; I2C no acknowledge flag
0000 35 ERR BIT 00h ; I2C error flag
36
00B4 37 LED EQU P3.4
38
39
40
41 ;____________________________________________________________________
42 ; BEGINNING OF CODE
---- 43 CSEG
0000 44 ORG 0000h
0000 020060 45 JMP MAIN
46
47
48 ;____________________________________________________________________
49 ; INT0 ISR
0003 50 ORG 0003h
0003 0533 51 INC OUTPUT
0005 32 52 RETI
53
54 ;____________________________________________________________________
55 ; MAIN PROGRAM
0060 56 ORG 0060h
0060 57 MAIN:
58
I2CMSTR PAGE 2
0060 759E85 59 MOV T3CON,#085h
0063 759D08 60 MOV T3FD,#08h
0066 759852 61 MOV SCON,#52h
62
63 ; configure & enable interrupts
0069 D2A8 64 SETB EX0 ; enable INT0
006B D288 65 SETB IT0 ; INT0 edge triggered
006D D2AF 66 SETB EA ; allow all the interrupts
67
68 ; initialise settings
006F 753188 69 MOV SLAVEADD,#88H ; clear RW bit
0072 75E8A8 70 MOV I2CCON,#0A8h ; sets SDATA & SCLOCK, and
71 ; selects master mode
0075 753300 72 MOV OUTPUT,#0 ; TX 0 as default
0078 C200 73 CLR NOACK
007A C200 74 CLR ERR
75
007C 76 RXTXLOOP:
77 ; code for a read mode ( master recieves one byte from slave )
007C 1200C6 78 CALL RCVDATA ; sends start bit
79 ; sends address byte
80 ; checks acknowledge
81 ; receives byte into ACC
82 ; checks ACK
83 ; sends stop bit
84
85 ; code for write mode ( master transmits one byte to slave )
007F 1200AD 86 CALL SENDDATA ; sends start bit
87 ; sends address byte
88 ; checks acknowledge
89 ; transmits ACC
90 ; checks ACK
91 ; sends stop bit
92
93 ; Check for Error message
0082 200008 94 JB ERR,SENDERR ; if error, send error message
95
96 ; Transmit received byte (INPUT) up UART to PC (hyperterminal)
0085 E532 97 MOV A,INPUT ; put value received into ACC
0087 120152 98 CALL SENDVAL ; send value received out the UART
008A 020092 99 JMP SKIP
100
008D 101 SENDERR:
008D 120138 102 CALL ERROR ; send error message out the UART
0090 C200 103 CLR ERR ; clear error flag
104
0092 105 SKIP:
0092 740A 106 MOV A,#10 ; send LF+CR
0094 12013E 107 CALL SENDCHAR
0097 740D 108 MOV A,#13
0099 12013E 109 CALL SENDCHAR
110
111 ; Toggle LED (1s delay so that LED can be seen toggle)
009C 740A 112 MOV A, #10
009E 12012C 113 CALL DELAY
00A1 B2B4 114 CPL LED
115
116 ; Check for new OUTPUT
I2CMSTR PAGE 3
00A3 3098D6 117 JNB RI, RXTXLOOP ; repeat (unless UART data received)
118
119 ; If UART data received, then save to OUTPUT
00A6 859933 120 MOV OUTPUT,SBUF ; update OUTPUT byte to new value
00A9 C298 121 CLR RI ; must clear RI
00AB 80CF 122 JMP RXTXLOOP ; back to main loop
123
124
125 ;====================================================================
126 ; SUBROUTINES
127 ;====================================================================
128
129 ;____________________________________________________________________
130 ; SENDDATA
131 ; Send all the sequence to the slave (slave address + data (OUTPUT))
132
00AD 133 SENDDATA:
134 ; send start bit
00AD 1200E3 135 CALL STARTBIT ; acquire bus and send slave address
136
137 ; send slave address
00B0 E531 138 MOV A, SLAVEADD
00B2 1200F5 139 CALL SENDBYTE ; sets NOACK if NACK received
140
00B5 200005 141 JB NOACK, STOPSEND ; if no acknowledge send stop
142
143 ; send OUTPUT byte
00B8 E533 144 MOV A, OUTPUT
00BA 1200F5 145 CALL SENDBYTE ; sets NOACK if NACK received
146
00BD 147 STOPSEND:
00BD 1200EC 148 CALL STOPBIT ; sends stop bit
00C0 300002 149 JNB NOACK, SENDRET ; if slave sends NACK send error
00C3 D200 150 SETB ERR ; sets the error flag
00C5 151 SENDRET:
00C5 22 152 RET
153
154 ;____________________________________________________________________
155 ; RCVDATA
156 ; receives one or more bytes of data from an I2C slave device.
157
00C6 158 RCVDATA:
00C6 0531 159 INC SLAVEADD ; Set RW for reception
160
161 ; send start bit
00C8 1200E3 162 CALL STARTBIT ; acquire bus and send slave address
163
164 ; send slave address
00CB E531 165 MOV A, SLAVEADD
00CD 1200F5 166 CALL SENDBYTE ; sets NOACK if NACK received
167
00D0 1531 168 DEC SLAVEADD ; returns SLAVEADD to 88h (after INC)
169
00D2 200005 170 JB NOACK, STOPRCV ; Check for slave not responding.
00D5 120112 171 CALL RCVBYTE ; Receive next data byte.
00D8 F532 172 MOV INPUT,A ; Save data byte in buffer.
173
00DA 174 STOPRCV:
I2CMSTR PAGE 4
00DA 1200EC 175 CALL STOPBIT
00DD 300002 176 JNB NOACK, RCVRET ; if slave sends NACK send error
00E0 D200 177 SETB ERR ; sets the error flag
00E2 178 RCVRET:
00E2 22 179 RET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -