pic單片機的模擬I2C通信
發(fā)布時間:2008/8/16 0:00:00 訪問次數(shù):978
; 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
熱門點擊
- 單片機驅(qū)動蜂鳴器原理與設(shè)計
- 51單片機8*8點陣LED顯示原理及程序
- 80C51原始IP核內(nèi)部RAM的擴展方案
- 單片機的圖形化編程方法探討
- 用AT89S51單片機來制作的手動計數(shù)器
- Proteus在單片機系統(tǒng)設(shè)計中的應(yīng)用
- TEA5767收音機模塊的C51控制程序
- pic單片機的模擬I2C通信
- ST推出基于STM8內(nèi)核的STM8S系列8位
- 單片機相關(guān)常用名詞解釋
推薦技術(shù)資料
- 硬盤式MP3播放器終級改
- 一次偶然的機會我結(jié)識了NE0 2511,那是一個遠方的... [詳細]
- MOSFET 電感單片降壓開關(guān)模式變換器優(yōu)勢
- SiC MOSFET 和 IG
- 新型 電隔離無芯線性霍爾效應(yīng)電
- 業(yè)界超小絕對位置編碼器技術(shù)參數(shù)設(shè)計
- 高帶寬、更高分辨率磁角度傳感技術(shù)應(yīng)用探究
- MagAlpha 角度位置傳感
- 多媒體協(xié)處理器SM501在嵌入式系統(tǒng)中的應(yīng)用
- 基于IEEE802.11b的EPA溫度變送器
- QUICCEngine新引擎推動IP網(wǎng)絡(luò)革新
- SoC面世八年后的產(chǎn)業(yè)機遇
- MPC8xx系列處理器的嵌入式系統(tǒng)電源設(shè)計
- dsPIC及其在交流變頻調(diào)速中的應(yīng)用研究