📄 i2cmstr.lst
字号:
I2CMSTR PAGE 1
1 ;======================================================================
2 ;
3 ; Author : ADI - Apps www.analog.com/MicroConverter
4 ;
5 ; Date : Oct 2000
6 ;
7 ; File : i2Cmstr.asm
8 ;
9 ; Hardware : ADuC812 (commented out = ADuC824/ADuC816)
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 $MOD812 ; use ADuC812 & 8052 predefined symbols
25 ;$MOD816
26 ;$MOD824
27
28 ;____________________________________________________________________
29 ; DEFINE VARIABLES IN INTERNAL RAM
30
0030 31 BITCNT DATA 30h ; bit counter for I2C routines
0031 32 SLAVEADD DATA 31h ; slave address for I2C routines
0032 33 INPUT DATA 32h ; data recieved from the slave
0033 34 OUTPUT DATA 33h ; data to be transmitted to slave
35
0000 36 NOACK BIT 00h ; I2C no acknowledge flag
0000 37 ERR BIT 00h ; I2C error flag
38
00B4 39 LED EQU P3.4
40
41 ;____________________________________________________________________
42 ; BEGINNING OF CODE
---- 43 CSEG
0000 44 ORG 0000h
0000 020060 45 JMP MAIN
46 ;____________________________________________________________________
47 ; INT0 ISR
0003 48 ORG 0003h
0003 0533 49 INC OUTPUT
0005 32 50 RETI
51 ;____________________________________________________________________
52 ; MAIN PROGRAM
0060 53 ORG 0060h
0060 54 MAIN:
55
56 ; configure the UART ADuC812
0060 759852 57 MOV SCON,#52h ; configure UART for 9600baud..
0063 758920 58 MOV TMOD,#20h ; ..assuming 11.0592MHz crystal
I2CMSTR PAGE 2
0066 758DFD 59 MOV TH1,#-3
0069 D28E 60 SETB TR1
61
62 ; configure the UART ADuC824/ADuC816
63 ; MOV RCAP2H,#0FFh ; config UART for 9830baud
64 ; MOV RCAP2L,#-5 ; (close enough to 9600baud)
65 ; MOV TH2,#0FFh
66 ; MOV TL2,#-5
67 ; MOV SCON,#52h
68 ; MOV T2CON,#34h
69
70 ; configure & enable interrupts
006B D2A8 71 SETB EX0 ; enable INT0
006D D288 72 SETB IT0 ; INT0 edge triggered
006F D2AF 73 SETB EA ; allow all the interrupts
74
75 ; initialise settings
0071 753188 76 MOV SLAVEADD,#88H ; clear RW bit
0074 75E8A8 77 MOV I2CCON,#0A8h ; sets SDATA & SCLOCK, and
78 ; selects master mode
0077 753300 79 MOV OUTPUT,#0 ; TX 0 as default
007A C200 80 CLR NOACK
007C C200 81 CLR ERR
82
83
007E 84 RXTXLOOP:
85 ; code for a read mode ( master recieves one byte from slave )
007E 1200CA 86 CALL RCVDATA ; sends start bit
87 ; sends address byte
88 ; checks acknowledge
89 ; receives byte into ACC
90 ; checks ACK
91 ; sends stop bit
92
93 ; code for write mode ( master transmits one byte to slave )
0081 1200AF 94 CALL SENDDATA ; sends start bit
95 ; sends address byte
96 ; checks acknowledge
97 ; transmits ACC
98 ; checks ACK
99 ; sends stop bit
100
101 ; Check for Error message
0084 200008 102 JB ERR,SENDERR ; if error, send error message
103
104 ; Transmit received byte (INPUT) up UART to PC (hyperterminal)
0087 E532 105 MOV A,INPUT ; put value recieved into ACC
0089 120168 106 CALL SENDVAL ; send value recieved out the UART
008C 020094 107 JMP SKIP
108
008F 109 SENDERR:
008F 12014F 110 CALL ERROR ; send error message out the UART
0092 C200 111 CLR ERR ; clear error flag
112
0094 113 SKIP:
0094 740A 114 MOV A,#10 ; send LF+CR
0096 120154 115 CALL SENDCHAR
0099 740D 116 MOV A,#13
I2CMSTR PAGE 3
009B 120154 117 CALL SENDCHAR
118
119 ; Toggle LED (1s delay so that LED can be seen toggle)
009E 740A 120 MOV A, #10
00A0 120143 121 CALL DELAY
00A3 B2B4 122 CPL LED
123
124 ; Check for new OUTPUT
00A5 3098D6 125 JNB RI, RXTXLOOP ; repeat (unless UART data received)
126
127
128 ; If UART data received, then save to OUTPUT
00A8 859933 129 MOV OUTPUT,SBUF ; update OUTPUT byte to new value
00AB C298 130 CLR RI ; must clear RI
00AD 80CF 131 JMP RXTXLOOP ; back to main loop
132
133
134
135 ;====================================================================
136 ; SUBROUTINES
137 ;====================================================================
138
139 ;____________________________________________________________________
140 ; SENDDATA
141 ; Send all the sequence to the slave (slave address + data (OUTPUT))
142
00AF 143 SENDDATA:
144 ; send start bit
00AF 1200EC 145 CALL STARTBIT ; acquire bus and send slave address
146
147 ; send slave address
00B2 E531 148 MOV A, SLAVEADD
00B4 120104 149 CALL SENDBYTE ; sets NOACK if NACK received
150
00B7 200005 151 JB NOACK, STOPSEND ; if no acknowledge send stop
152
153 ; send OUTPUT byte
00BA E533 154 MOV A, OUTPUT
00BC 120104 155 CALL SENDBYTE ; sets NOACK if NACK received
156
00BF 157 STOPSEND:
00BF 1200F8 158 CALL STOPBIT ; sends stop bit
00C2 300004 159 JNB NOACK, SENDRET ; if slave sends no-acknowedge send error
00C5 D200 160 SETB ERR ; sets the error flag
00C7 D2EA 161 SETB I2CRS ; this resets the I2C interface
00C9 162 SENDRET:
00C9 22 163 RET
164
165
166 ;____________________________________________________________________
167 ; RCVDATA
168 ; receives one or more bytes of data from an I2C slave device.
169
00CA 170 RCVDATA:
00CA 0531 171 INC SLAVEADD ; Set RW for reception
172
173 ; send start bit
00CC 1200EC 174 CALL STARTBIT ; acquire bus and send slave address
I2CMSTR PAGE 4
175
176 ; send slave address
00CF E531 177 MOV A, SLAVEADD
00D1 120104 178 CALL SENDBYTE ; sets NOACK if NACK received
179
00D4 1531 180 DEC SLAVEADD ; put slave back in transmit mode
181
00D6 200008 182 JB NOACK, STOPRCV ; Check for slave not responding.
00D9 120141 183 CALL DELAY5 ; this lets slave get data ready
00DC 120124 184 CALL RCVBYTE ; Receive next data byte.
00DF F532 185 MOV INPUT,A ; Save data byte in buffer.
186
00E1 187 STOPRCV:
00E1 1200F8 188 CALL STOPBIT
00E4 300004 189 JNB NOACK, RCVRET ; if slave sends NACK send error
00E7 D200 190 SETB ERR ; sets the error flag
00E9 D2EA 191 SETB I2CRS ; this resets the I2C interface
00EB 192 RCVRET:
00EB 22 193 RET
194
195
196 ;____________________________________________________________________
197 ; STARTBIT
198 ; Sends the start bit to initiate an I2C communication
199
00EC 200 STARTBIT:
201
00EC D2EE 202 SETB MDE ; enable SDATA pin as an output
00EE C200 203 CLR NOACK
00F0 C2EF 204 CLR MDO ; low O/P on SDATA
00F2 120141 205 CALL DELAY5 ; delay 5 Machine cycles
00F5 C2ED 206 CLR MCO ; start bit
00F7 22 207 RET
208
209
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -