1101287021
Newbie level 6
- Joined
- Jul 9, 2014
- Messages
- 14
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,382
THE I2C FRAME
WHEN MICROCONTROLLER SEND NOACK THEN STOP THE FRAME
IN BETWEEN NOACK AND STOP FRAME
A SINGLE BIT IS READ .
SO THAT ALWAYS READ EVEN NUMBER
WHAT CAN I DO SO THAT AFTER NOACK FRAME IS STOP ?
example- RLC A ;accumulator value move to carry from right to left
in that time if carry = 1 then led glow
carry = 0 then same led off
in this way i know 31h store in 00 eeprom memory location.
[COLOR="silver"][SIZE=1]- - - Updated - - -[/SIZE][/COLOR]
Hello sir,
sir this is my byte write code
;*** FOR 7 BIT ADDRESS I2C FRAME IS = START / 7 BIT ADDRESS / R/W / ACK / DATA / ACK /DATA / ACK / STOP
;*** FOR R/W = 0 IT SHOULD BE WRITE CONDITION , IF IT IS = 1 THEN IT IS READ CONDITION
;*** FOR START / STOP CLK SIGNAL CONDITION = HIGH
;*** FOR START DATA SIGNAL CONDITION = HIGH TO LOW
;*** FOR STOP DATA SIGNAL CONDITION = LOW TO HIGH
;*** FOR CLK = P3.6
;*** FOR DAT = P3.7
;*** FOR LED = P0.4
SCL BIT P3.6
SDA BIT P3.7
LED BIT P0.4
ORG 00H
; CLR LED
ACALL DELAY
SETB SCL ;CLK SIGNAL IS HIGH
NOP
SETB SDA ;DATA SIGNAL IS HIGH TO LOW FOR STARTING CONDITION
NOP
CLR SDA ;DATA SIGNAL IS LOW
CLR SCL
; ACALL DELAY ; OFF
; ACALL DELAY
; ACALL DELAY
; CPL LED
;*** 7 BIT ADDRESS SEND IN SDA
;*** AFTER EVERY BIT SEND DATA LINE HIGH TO LOW AND CLK HIGH
MOV B,#8 ; B REGISTER CONTAIN VALUE 8
MOV A,#0A0H ; ACC STORE DEVICE ADDRESS [EEPROM]
LOOP: RLC A ; ACC VALUE MOVE RIGHT TO LEFT IN CARRY
MOV SDA,C ; CARRY VALUE TRANSFER TO DEVICE THROUGH DATA LINE
SETB SCL ; HIGH CLK SIGNAL
NOP ; NO OPERATION
NOP
CLR SCL ; LOW CLK SIGNAL
NOP ; NO OPERATION
DJNZ B,LOOP ; DECREMENT B REGISTER AND JUMP LOOP LEVEL IF B=0 MOVE NEXT INSTRUCTION
CLR C ; CLEAR CARRY
; SETB SDA
NOP
SETB SCL ; HIGH CLK
NOP
LOOP1: JB SDA,LOOP1 ; WHEN SDA = 0 THEN MOVE NEXT INSTRUCTION OTHERWISE JUMP LOOP1
CLR SCL ; CLEAR CLK
; ACALL DELAY
; ACALL DELAY
; ACALL DELAY
; CPL LED ; ON
;*** STORE OFFSET ADDRESS IN EEPROM
MOV B,#8 ; R1 REGISTER CONTAIN THE VALUE 8
MOV A,#00H ; 01H MEMORY LOCATION [EEPROM] DATA START TO STORE ACC=30H
LOOP2: RLC A ; ACCUMULATOR VALUE MOVE CARRY FROM RIGHT TO LEFT
MOV SDA,C ; CARRY VALUE MOVE TO DATA LINE
SETB SCL ; HIGH CLK SIGNAL
NOP ; NOOPERATION OCCURE
NOP
CLR SCL ; LOW CLK SIGNAL
NOP
DJNZ B, LOOP2 ; DECREMENT R1 VALUE JUMP LOOP2 STIL R1=0
CLR C ; CLEAR CARRY
; SETB SDA
NOP
SETB SCL ; HIGH CLK SIGNAL
LOOP3: JB SDA, LOOP3 ; WAIT WHEN SDA=0 THEN MOVE NEXT INSTRUCTION
CLR SCL ; CLEAR CLK SIGNAL
; ACALL DELAY
; ACALL DELAY
; ACALL DELAY
; CPL LED
; OFF
;*** STORE DATA IN EEPROM
CLR A ; ACC = 0
MOV B,#8 ; R1 REGISTER CONTAIN THE VALUE 8
MOV A,#31H ; ACC VALUE STORE IN EEPROM IN 30H MEMORY LOCATION
LOOP4: RLC A ; ACCUMULATOR VALUE MOVE CARRY FROM RIGHT TO LEFT
MOV SDA,C ; CARRY VALUE MOVE TO DATA LINE
SETB SCL ; HIGH CLK SIGNAL
NOP ; NO OPERATION
NOP
CLR SCL ; LOW CLK SIGNAL
NOP
DJNZ B,LOOP4 ; DECREMENT R1 VALUE JUMP LOOP4 STIL R1=0
CLR C ; CARRY = 0
; SETB SDA
NOP
SETB SCL ; HIGH CLK SIGNAL
NOP
LOOP5: JB SDA, LOOP5 ; WAIT WHEN SDA=0 THEN MOVE NEXT INSTRUCTION
NOP
CLR SCL ; LOW CLK SIGNAL
CLR LED
; ACALL DELAY
; ACALL DELAY
; ACALL DELAY
; CPL LED ; ON
;*** STOP I2C BUS
SETB SCL ; HIGH CLK SIGNAL
NOP
;CLR SDA ; LOW DATA LINE
;NOP
SETB SDA ; LOW DATA LINE
NOP
CLR SCL
; ACALL DELAY
; ACALL DELAY
; ACALL DELAY
; CPL LED ; OFF
SJMP $
;*** FOR READ OPERATION
DELAY: mov r6,#150
VL3: mov r2,#143
VL2: mov r3,#20
VL1: djnz r3,VL1
djnz r2,VL2
djnz r6,VL3
ret
END
Code ASM - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 SCL BIT P3.6 SDA BIT P3.7 RS BIT P0.1 EN BIT P0.0 LED BIT P0.4 ORG 00H MOV R0,#30H ; ACALL LCD ACALL DELAY ACALL DELAY ; CPL LED ACALL START ACALL DEVICE_ADDRESS ACALL ACK ACALL WORD_ADDRESS ACALL ACK ACALL REPEAT_START ACALL DEVICE_ADDRESS1 ACALL ACK ACALL VALUE ACALL NOACK ACALL STOP ACALL EVE LU2: SJMP $ ;*** I2C START PROGRAM START: SETB SCL ; NOP SETB SDA ; NOP CLR SDA ; NOP CLR SCL RET ;*** I2C DEVICE ADDRESS PROGRAM DEVICE_ADDRESS: CLR A MOV R3,#8 MOV A,#0A0H LOOP7: CLR SCL NOP RLC A MOV SDA,C SETB SCL NOP NOP DJNZ R3,LOOP7 CLR SCL CLR C RET ;*** I2C ACKNOWLEDGE SLAVE TO MASTER ACK: SETB SCL NOP NOP NOP LOOP8: JB SDA,LOOP8 NOP CLR SCL NOP RET ;*** I2C WORD ADDRESS WORD_ADDRESS: MOV R1,#8 ; R1 REGISTER CONTAIN THE VALUE 8 MOV A,#00H ; 30H MEMORY LOCATION [EEPROM] DATA START TO STORE ACC=30H LOP8: RLC A ; ACCUMULATOR VALUE MOVE CARRY FROM RIGHT TO LEFT MOV SDA,C ; CARRY VALUE MOVE TO DATA LINE SETB SCL ; HIGH CLK SIGNAL NOP NOP CLR SCL ; LOW CLK SIGNAL NOP DJNZ R1, LOP8 ; DECREMENT R1 VALUE JUMP LOOP2 STIL R1=0 CLR C ; CLEAR CARRY RET ;*** I2C REPETED START REPEAT_START: SETB SDA NOP NOP SETB SCL NOP NOP CLR SDA NOP CLR SCL NOP RET ;*** I2C READ DEVICE ADDRESS DEVICE_ADDRESS1: CLR A MOV R2,#8 MOV A,#0A1H NOP LOP7: CLR SCL NOP RLC A MOV SDA,C SETB SCL NOP NOP NOP DJNZ R2,LOP7 CLR SCL NOP CLR C RET ;*** I2C VALUE VALUE: CLR SCL MOV R1,#8 LOOP14: SETB SCL NOP NOP MOV C,SDA NOP NOP CLR SCL NOP NOP RLC A NOP DJNZ R1,LOOP14 NOP CLR C RET ;*** I2C ACK ACK1: CLR SDA SETB SCL ; NOP ; CLR SDA ; NOP RET ;*** I2C NO ACK NOACK: SETB SDA NOP SETB SCL NOP NOP NOP NOP CLR SCL RET ;*** I2C STOP STOP: CLR SDA NOP NOP SETB SCL NOP NOP NOP NOP SETB SDA RET ;*** I2C STOP ONE STOP1: CLR SCL NOP MOV C,SDA NOP SETB SCL NOP SETB SDA NOP CLR SCL MOV ACC.0,C RET ;*** I2C SHOW ODD: MOV ACC.0,C MOV R0,#30H MOV A,@R0 MOV P0,A NOP RET EVE: ;MOV R0,#30H ;MOV A,@R0 MOV P0,A NOP RET STORE: MOV @R0,A MOV B,#2H DIV AB MOV R1,B CJNE R1,#00H,DE MOV R3,#4 CLR SCL RET MH: ACALL SINGLE DE: RET ;*** I2C FOR SINGLE BIT SINGLE: CLR SCL MOV C,SDA ; SETB SCL RET ;*** LCD INNITIALIZE PROGRAM LCD: MOV DPTR,#LU LOP1: MOVC A,@A+DPTR JZ LOP ACALL CMD ACALL DELAY1 INC DPTR CLR A SJMP LOP1 LOP: RET ;*** COMMAND LINE PROGRAM FOR LCD CMD: MOV P2,A CLR RS SETB EN ACALL DELAY1 CLR EN RET ;*** DATA LINE FOR LCD PROGRAM DAT: MOV P2,A SETB EN SETB RS ACALL DELAY1 CLR EN RET ;*** DELAY1 FOR LCD DELAY1: MOV R6,#143 OOP2: MOV R7,#50 OOP3: DJNZ R7,OOP3 DJNZ R6,OOP2 RET ;*** DELAY PROGRAM FOR MAIN PROGRAM DELAY: MOV R5,#150 OOP4: MOV R6,#143 OOP5: MOV R7,#20 OOP6: DJNZ R7,OOP6 DJNZ R6,OOP5 DJNZ R5,OOP4 RET LU: DB 38H,01H,06H,0EH,80H,0 END
FADDR EQU 0a0h ; fixed address for AT24Cxx EEPROMs
PADDR EQU 0 ; programmable address (0..7)
PSIZE EQU 32 ; bytes per page for AT24C64
buffer EQU 30h ; storage for read/write data
; Register definitions.
index EQU r0 ; buffer pointer
kount EQU r1 ; byte count register
zdata EQU r1 ; data register
addr EQU r2 ; address register
; Microcontroller connections to AT24Cxx serial bus lines.
SCL BIT p1.2 ; serial clock
SDA BIT p1.3 ; serial data
write_block:
call start
jc x38 ; abort if bus not available
rl a ; programmable address to bits 3:1
orl a, #FADDR ; add fixed address
clr acc.0 ; specify write operation
call shout ; send device address
jc x37 ; abort if no acknowledge
mov a, addr ; send byte of address
call shout ;
jc x37 ; abort if no acknowledge
mov index, #buffer ; point to buffer
x36: mov a, @index ; get data
call shout ; send data
jc x37 ; abort if no acknowledge
inc index ; advance buffer pointer
djnz kount, x36 ; next byte
clr c ; clear error flag
x37: call stop
x38: ret
read_block:
call start
jc x35 ; abort if bus not available
rl a ; programmable address to bits 3:1
orl a, #FADDR ; add fixed address
mov index, a ; save copy of device address
clr acc.0 ; specify write operation
call shout ; send device address
jc x34 ; abort if no acknowledge
mov a, addr ; send byte of address
call shout ;
jc x34 ; abort if no acknowledge
; Send read command and receive data.
call start ; second start for read
jc x34 ; abort if bus not available
mov a, index ; get device address
setb acc.0 ; specify read operation
call shout ; send device address
jc x34 ; abort if no acknowledge
mov index, #buffer ; point to buffer
x31: call shin ; receive data byte
mov @index, a ; save data
cjne kount, #1, x32 ; jump if not last byte
call NAK ; do not acknowledge last byte
jmp x33 ; done
x32: call ACK ; acknowledge byte
inc index ; advance buffer pointer
djnz kount, x31 ; next byte
x33: clr c ; clear error flag
x34: call stop
x35: ret
write_byte:
call start
jc x49 ; abort if bus not available
rl a ; programmable address to bits 3:1
orl a, #FADDR ; add fixed address
clr acc.0 ; specify write operation
call shout ; send device address
jc x48 ; abort if no acknowledge
mov a, addr ; send byte of address
call shout ;
jc x48 ; abort if no acknowledge
mov a, zdata ; get data
call shout ; send data
jc x48 ; abort if no acknowledge
clr c ; clear error flag
x48: call stop
x49: ret
read_current:
call start
jc x45 ; abort if bus not available
rl a ; programmable address to bits 3:1
orl a, #FADDR ; add fixed address
setb acc.0 ; specify read operation
call shout ; send device address
jc x44 ; abort if no acknowledge
call shin ; receive data byte
call NAK ; do not acknowledge byte
clr c ; clear error flag
x44: call stop
x45: ret
read_random:
push b
mov b, a ; save copy of programmable address
; Send dummy write command to set internal address.
call start
jc x47 ; abort if bus not available
rl a ; programmable address to bits 3:1
orl a, #FADDR ; add fixed address
clr acc.0 ; specify write operation
call shout ; send device address
jc x46 ; abort if no acknowledge
mov a, addr ; send byte of address
call shout ;
jc x46 ; abort if no acknowledge
; Call Current Address Read function.
mov a, b ; get programmable address
call read_current
jmp x47 ; exit
x46: call stop
x47: pop b
ret
start:
setb SDA
setb SCL
jnb SDA, x40 ; jump if not high
jnb SCL, x40 ; jump if not high
nop ; enforce setup delay and cycle delay
clr SDA
nop ; enforce hold delay
nop ;
nop ;
nop ;
nop ;
clr SCL
clr c ; clear error flag
jmp x41
x40: setb c ; set error flag
x41: ret
stop:
clr SDA
nop ; enforce SCL low and data setup
nop
setb SCL
nop ; enforce setup delay
nop ;
nop ;
nop ;
nop ;
setb SDA
ret
shout:
push b
mov b, #8 ; bit counter
x42: rlc a ; move bit into CY
mov SDA, c ; output bit
nop ; enforce SCL low and data setup
setb SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
clr SCL ; drop clock
djnz b, x42 ; next bit
setb SDA ; release SDA for ACK
nop ; enforce SCL low and tAA
nop ;
setb SCL ; raise ACK clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
mov c, SDA ; get ACK bit
clr SCL ; drop ACK clock
pop b
ret
shin:
setb SDA ; make SDA an input
push b
mov b, #8 ; bit count
x43: nop ; enforce SCL low and data setup
nop ;
nop ;
setb SCL ; raise clock
nop ; enforce SCL high
nop ;
mov c, SDA ; input bit
rlc a ; move bit into byte
clr SCL ; drop clock
djnz b, x43 ; next bit
pop b
ret
ACK:
clr SDA ; ACK bit
nop ; enforce SCL low and data setup
nop ;
setb SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
clr SCL ; drop clock
ret
NAK:
setb SDA ; NAK bit
nop ; enforce SCL low and data setup
nop ;
setb SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
clr SCL ; drop clock
ret
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?