Page 25 - The Journal of the Royal Society of Thailand
P. 25
วารสารราชบััณฑิิตยสภา
ปีีท่� ๔๙ ฉบัับัท่� ๑ มกิราคุม-เมษาย์น ๒๕๖๗
ศาสตราจารย์์กิิตติคุุณ ดร.มงคุล เดชนคุรินทร์ 15
๔. กิระบวนคุำส่�ง i2c_rd (อ่านขี�อม้ล) : อ�างอิงวงจรในภาพื่ที� ๔ และภาพื่คล่�นสัญญาณในภาพื่ที� ๖
i2c_rd proc ;(receive 8 bits in to al, send ah.0=ACK/NACK=0/1)
;ACK=0 => acknowledge, NACK=1 => Not ACK
push bx ;save bx on stack
push cx ;save cx on stack
mov al,01h ;al=01h=00000001b
out ppi_port2c,al ;make PC1=SCL=0, PC0=SDA=1
jmp $+2 ;I/O delay
mov bl,0 ;use bl for storage
mov cx,8 ;count for 8 bits
lop1:
shl bl,1 ;prepare for next bit
mov al,03h ;al=03h=00000011b
out ppi_port2c,al ;set PC1=PC0=1 => release SCL and SDA lines
jmp $+2 ;I/O delay
call wtc5hi ;wait for C5=SCL=1 during clock stretching (optional)
call delay0 ;short extension of clock pulse (optional)
in al,ppi_port2c ;read Port 2C in to al
jmp $+2 ;I/O delay
and al,10h ;test bit PC4=SDA
jz nt10h ;al=00h => no change to bl
inc bl ;al=10h => add LSb=1 to bl
nt10h: mov al,01h ;PC0=SDA=1 => release SDA line
out ppi_port2c,al ;PC1=SCL=1->0, finish clock pulse
jmp $+2 ;I/O delay
th
loop lop1 ;repeat until finishing 8 bit
th
;//send the 9 bit in ah.0 as ACK=0 or NACK=1
th
mov al,ah ;for 9 bit, ah.0=ACK/NACK
and al,01h ;choose LSb only
out ppi_port2c,al ;send PC1=SCL=0, PC0=SDA=0/1
jmp $+2 ;I/O delay