📄 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 : ADuC814
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 $MOD814
25 ;____________________________________________________________________
26 ; DEFINE VARIABLES IN INTERNAL RAM
27
0030 28 BITCNT DATA 30h ; bit counter for I2C routines
0031 29 SLAVEADD DATA 31h ; slave address for I2C routines
0032 30 INPUT DATA 32h ; data recieved from the slave
0033 31 OUTPUT DATA 33h ; data to be transmitted to slave
32
0000 33 NOACK BIT 00h ; I2C no acknowledge flag
0000 34 ERR BIT 00h ; I2C error flag
35
00B4 36 LED EQU P3.4
37
38
39
40 ;____________________________________________________________________
41 ; BEGINNING OF CODE
---- 42 CSEG
0000 43 ORG 0000h
0000 020060 44 JMP MAIN
45
46
47 ;____________________________________________________________________
48 ; INT0 ISR
0003 49 ORG 0003h
0003 0533 50 INC OUTPUT
0005 32 51 RETI
52
53 ;____________________________________________________________________
54 ; MAIN PROGRAM
0060 55 ORG 0060h
0060 56 MAIN:
57
58
I2CMSTR PAGE 2
59 ; configure the UART ADuC812s
0060 75CBFF 60 MOV RCAP2H,#0FFh ; config UART for 9830baud
0063 75CAF9 61 MOV RCAP2L,#-7 ; (close enough to 9600baud)
0066 75CDFF 62 MOV TH2,#0FFh
0069 75CCF9 63 MOV TL2,#-7
006C 759852 64 MOV SCON,#52h
006F 75C834 65 MOV T2CON,#34h
66
67 ; enable i2c pins on 812s
0072 759C01 68 MOV CFG814,#01H
69
70 ; configure & enable interrupts
0075 D2A8 71 SETB EX0 ; enable INT0
0077 D288 72 SETB IT0 ; INT0 edge triggered
0079 D2AF 73 SETB EA ; allow all the interrupts
74
75 ; initialise settings
007B 753188 76 MOV SLAVEADD,#88H ; clear RW bit
007E 75E8A8 77 MOV I2CCON,#0A8h ; sets SDATA & SCLOCK, and
78 ; selects master mode
0081 753300 79 MOV OUTPUT,#0 ; TX 0 as default
0084 C200 80 CLR NOACK
0086 C200 81 CLR ERR
82
0088 83 RXTXLOOP:
84 ; code for a read mode ( master recieves one byte from slave )
0088 1200D2 85 CALL RCVDATA ; sends start bit
86 ; sends address byte
87 ; checks acknowledge
88 ; receives byte into ACC
89 ; checks ACK
90 ; sends stop bit
91
92 ; code for write mode ( master transmits one byte to slave )
008B 1200B9 93 CALL SENDDATA ; sends start bit
94 ; sends address byte
95 ; checks acknowledge
96 ; transmits ACC
97 ; checks ACK
98 ; sends stop bit
99
100 ; Check for Error message
008E 200008 101 JB ERR,SENDERR ; if error, send error message
102
103 ; Transmit received byte (INPUT) up UART to PC (hyperterminal)
0091 E532 104 MOV A,INPUT ; put value received into ACC
0093 12015E 105 CALL SENDVAL ; send value received out the UART
0096 02009E 106 JMP SKIP
107
0099 108 SENDERR:
0099 120144 109 CALL ERROR ; send error message out the UART
009C C200 110 CLR ERR ; clear error flag
111
009E 112 SKIP:
009E 740A 113 MOV A,#10 ; send LF+CR
00A0 12014A 114 CALL SENDCHAR
00A3 740D 115 MOV A,#13
00A5 12014A 116 CALL SENDCHAR
I2CMSTR PAGE 3
117
118 ; Toggle LED (1s delay so that LED can be seen toggle)
00A8 740A 119 MOV A, #10
00AA 120138 120 CALL DELAY
00AD B2B4 121 CPL LED
122
123 ; Check for new OUTPUT
00AF 3098D6 124 JNB RI, RXTXLOOP ; repeat (unless UART data received)
125
126 ; If UART data received, then save to OUTPUT
00B2 859933 127 MOV OUTPUT,SBUF ; update OUTPUT byte to new value
00B5 C298 128 CLR RI ; must clear RI
00B7 80CF 129 JMP RXTXLOOP ; back to main loop
130
131
132 ;====================================================================
133 ; SUBROUTINES
134 ;====================================================================
135
136 ;____________________________________________________________________
137 ; SENDDATA
138 ; Send all the sequence to the slave (slave address + data (OUTPUT))
139
00B9 140 SENDDATA:
141 ; send start bit
00B9 1200EF 142 CALL STARTBIT ; acquire bus and send slave address
143
144 ; send slave address
00BC E531 145 MOV A, SLAVEADD
00BE 120101 146 CALL SENDBYTE ; sets NOACK if NACK received
147
00C1 200005 148 JB NOACK, STOPSEND ; if no acknowledge send stop
149
150 ; send OUTPUT byte
00C4 E533 151 MOV A, OUTPUT
00C6 120101 152 CALL SENDBYTE ; sets NOACK if NACK received
153
00C9 154 STOPSEND:
00C9 1200F8 155 CALL STOPBIT ; sends stop bit
00CC 300002 156 JNB NOACK, SENDRET ; if slave sends NACK send error
00CF D200 157 SETB ERR ; sets the error flag
00D1 158 SENDRET:
00D1 22 159 RET
160
161 ;____________________________________________________________________
162 ; RCVDATA
163 ; receives one or more bytes of data from an I2C slave device.
164
00D2 165 RCVDATA:
00D2 0531 166 INC SLAVEADD ; Set RW for reception
167
168 ; send start bit
00D4 1200EF 169 CALL STARTBIT ; acquire bus and send slave address
170
171 ; send slave address
00D7 E531 172 MOV A, SLAVEADD
00D9 120101 173 CALL SENDBYTE ; sets NOACK if NACK received
174
I2CMSTR PAGE 4
00DC 1531 175 DEC SLAVEADD ; returns SLAVEADD to 88h (after INC)
176
00DE 200005 177 JB NOACK, STOPRCV ; Check for slave not responding.
00E1 12011E 178 CALL RCVBYTE ; Receive next data byte.
00E4 F532 179 MOV INPUT,A ; Save data byte in buffer.
180
00E6 181 STOPRCV:
00E6 1200F8 182 CALL STOPBIT
00E9 300002 183 JNB NOACK, RCVRET ; if slave sends NACK send error
00EC D200 184 SETB ERR ; sets the error flag
00EE 185 RCVRET:
00EE 22 186 RET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -