91精品一区二区三区久久久久久_欧美一级特黄大片色_欧美一区二区人人喊爽_精品一区二区三区av

位置:51電子網 » 技術資料 » 單 片 機

pic單片機的模擬I2C通信

發布時間:2008/8/16 0:00:00 訪問次數:945

; copyright (c) 1997 by innovatus
; this code may be distributed and used freely provided that this
; copyright notice stays intact and that any modifications are noted.
; for more information about innovatus:
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; file name: i2c_low.asm
; author: alan g. smith
; purpose: this code is borrowed from microchip with all of the fancy
; stuff taken out.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 initi2cbusmaster

;************************************************************
 txmtstartbit
bsf bus_busy ; on a start condition bus is busy
bsf status, rp0 ; select page 1
bsf _sda ; set sda high
bsf _scl ; clock is high
call delay40usec ; this is necessary for setup time
bcf _sda ; this gives a falling edge on sda while clock is high
call delay47usec ; necessary for start hold time
return
;************************************************************
 txmtstopbit
bsf status, rp0 ; select page 1
bcf _scl ; clock is low
bcf _sda ; set sda low
bsf _scl ; clock is pulled up
call delay40usec ; setup time for stop condition
bsf _sda ; rising edge on sda while clock is high
call delay47usec ; makes sure a start isn‘t sent immediately after a stop
bcf bus_busy ; the bus isn‘t busy anymore
return
;************************************************************
 aborti2c
call txmtstopbit ; send a stop bit
bsf abort ; set the abort bit
return
;************************************************************
 txmtslaveaddr
movf slaveaddr, w ; move slave address to w
bcf ack_error ; reset acknowledge error bit
movwf i2cdata ; move w to i2c data
bcf i2cdata, lsb ; set for write
btfsc slave_rw ; if skip then write operation
bsf i2cdata, lsb ; clear for read
call senddata ; send the address
btfss txmt_success ; skip if successful
goto addrsendfail ; oops, we failed
retlw true ; return true
 addrsendfail
btfss ack_error ; was there an error acknowledging
retlw false ; no, so return 0
call txmtstopbit ; address not acknowleged, so send stop bit
retlw false ; unsuccessful, so return 0

;************************************************************
 senddata
; we might should make a copy of the data here, the example does but i don‘t see why!!!
bsf txmt_progress ; we are in the middle of transmitting
bcf txmt_success ; reset success bit
movlw 0x08
movwf i2cbitcount ; set i2c bit count to 8
bsf status, rp0 ; select page 1
 txmtnextbit:
bcf _scl ; set clock low
rlf i2cdata, f ; msb first, note that i2cdata is destroyed
bcf _sda ; set clock based on what the msb is
btfsc status,c ; was the msb a 1
bsf _sda ; nope set it high
call delay47usec ; guarantee min low time tlow & setup time
bsf _scl ; set clock high
call delay40usec ; guarantee min high time thigh
decfsz i2cbitcount, f ; are we done yet
goto txmtnextbit ; nope, send the next bit
;
; check for acknowledge
;
bcf _scl ; reset clock
bsf _sda ; release sda line for slave to pull down
call delay47usec ; guarantee min low time tlow & setup time
bsf _scl ; clock for slave to ack
call delay40usec ; guarantee min high time thigh
bcf status, rp0 ; select page 0 to test sda pin
btfsc sdapin ; sda should be pulled low by slave if ok
goto txmterrorack ; uh oh, slave isn‘t behaving (or isn‘t there)
bsf status, rp0 ; select page 1
bcf _scl ; reset clock
bcf txmt_progress ; reset progress bit in bus status
bsf txmt_success ; transmission successful
bcf ack_error ; ack ok
return
 txmterrorack
bsf status,rp0 ; select page 1
bsf _sda ; tristate sda
bsf _scl ; t

; copyright (c) 1997 by innovatus
; this code may be distributed and used freely provided that this
; copyright notice stays intact and that any modifications are noted.
; for more information about innovatus:
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; file name: i2c_low.asm
; author: alan g. smith
; purpose: this code is borrowed from microchip with all of the fancy
; stuff taken out.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 initi2cbusmaster

;************************************************************
 txmtstartbit
bsf bus_busy ; on a start condition bus is busy
bsf status, rp0 ; select page 1
bsf _sda ; set sda high
bsf _scl ; clock is high
call delay40usec ; this is necessary for setup time
bcf _sda ; this gives a falling edge on sda while clock is high
call delay47usec ; necessary for start hold time
return
;************************************************************
 txmtstopbit
bsf status, rp0 ; select page 1
bcf _scl ; clock is low
bcf _sda ; set sda low
bsf _scl ; clock is pulled up
call delay40usec ; setup time for stop condition
bsf _sda ; rising edge on sda while clock is high
call delay47usec ; makes sure a start isn‘t sent immediately after a stop
bcf bus_busy ; the bus isn‘t busy anymore
return
;************************************************************
 aborti2c
call txmtstopbit ; send a stop bit
bsf abort ; set the abort bit
return
;************************************************************
 txmtslaveaddr
movf slaveaddr, w ; move slave address to w
bcf ack_error ; reset acknowledge error bit
movwf i2cdata ; move w to i2c data
bcf i2cdata, lsb ; set for write
btfsc slave_rw ; if skip then write operation
bsf i2cdata, lsb ; clear for read
call senddata ; send the address
btfss txmt_success ; skip if successful
goto addrsendfail ; oops, we failed
retlw true ; return true
 addrsendfail
btfss ack_error ; was there an error acknowledging
retlw false ; no, so return 0
call txmtstopbit ; address not acknowleged, so send stop bit
retlw false ; unsuccessful, so return 0

;************************************************************
 senddata
; we might should make a copy of the data here, the example does but i don‘t see why!!!
bsf txmt_progress ; we are in the middle of transmitting
bcf txmt_success ; reset success bit
movlw 0x08
movwf i2cbitcount ; set i2c bit count to 8
bsf status, rp0 ; select page 1
 txmtnextbit:
bcf _scl ; set clock low
rlf i2cdata, f ; msb first, note that i2cdata is destroyed
bcf _sda ; set clock based on what the msb is
btfsc status,c ; was the msb a 1
bsf _sda ; nope set it high
call delay47usec ; guarantee min low time tlow & setup time
bsf _scl ; set clock high
call delay40usec ; guarantee min high time thigh
decfsz i2cbitcount, f ; are we done yet
goto txmtnextbit ; nope, send the next bit
;
; check for acknowledge
;
bcf _scl ; reset clock
bsf _sda ; release sda line for slave to pull down
call delay47usec ; guarantee min low time tlow & setup time
bsf _scl ; clock for slave to ack
call delay40usec ; guarantee min high time thigh
bcf status, rp0 ; select page 0 to test sda pin
btfsc sdapin ; sda should be pulled low by slave if ok
goto txmterrorack ; uh oh, slave isn‘t behaving (or isn‘t there)
bsf status, rp0 ; select page 1
bcf _scl ; reset clock
bcf txmt_progress ; reset progress bit in bus status
bsf txmt_success ; transmission successful
bcf ack_error ; ack ok
return
 txmterrorack
bsf status,rp0 ; select page 1
bsf _sda ; tristate sda
bsf _scl ; t

相關IC型號

熱門點擊

 

推薦技術資料

硬盤式MP3播放器終級改
    一次偶然的機會我結識了NE0 2511,那是一個遠方的... [詳細]
版權所有:51dzw.COM
深圳服務熱線:13751165337  13692101218
粵ICP備09112631號-6(miitbeian.gov.cn)
公網安備44030402000607
深圳市碧威特網絡技術有限公司
付款方式


 復制成功!
逊克县| 图木舒克市| 正宁县| 大理市| 浦城县| 竹山县| 上饶市| 怀宁县| 沁源县| 九龙坡区| 泌阳县| 富裕县| 文山县| 芜湖市| 曲松县| 衢州市| 舞钢市| 万宁市| 耒阳市| 娄底市| 茶陵县| 安福县| 九龙坡区| 龙里县| 高密市| 靖远县| 泽州县| 长武县| 项城市| 利津县| 榆林市| 封丘县| 昌吉市| 德格县| 保德县| 乐山市| 奈曼旗| 平原县| 昭平县| 道真| 东乌珠穆沁旗|