《電子技術(shù)應(yīng)用》
您所在的位置:首頁 > 嵌入式技術(shù) > 解決方案 > PIC單片機(jī)的IIC接口程序

PIC單片機(jī)的IIC接口程序

2009-01-16
關(guān)鍵詞: 接口程序 PIC

PIC單片機(jī)的IIC接口程序

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                           ; tristate SCL
   bcf    Txmt_Progress                  ; reset progress bit in Bus Status
   bcf    Txmt_Success                   ; Transmission NOT successful
   bsf    ACK_Error                      ; No ACK From Slave
   return

;************************************************************
GetData
   bsf    Rcv_Progress                   ; set Bus status for txmt progress
   bcf    Rcv_Success                    ; reset status bit
   movlw  0x08
   movwf  I2CBitCount
RcvNextBit
   bsf    STATUS, RP0                    ; page 1 for TRIS manipulation
   bcf    _SCL                           ; lower clock
   bcf    _SDA                           ; lower data line
   call   Delay47uSec                    ; guarantee min LOW TIME tLOW & setup time
   bsf    _SCL                           ; clock high, data sent by slave
   call   Delay40uSec                    ; guarantee min HIGH TIME tHIGH
   bcf    STATUS, RP0                    ; select page 0 to read Ports
   bcf    STATUS, C                      ; 0 out Status
   btfsc  SdaPin                         ; Check state of pin
   bsf    STATUS, C                      ; Pin was high, set status
   rlf    I2CData, F                     ; left Shift data (MSB first)
   decfsz I2CBitCount, F                 ; Are we done yet
   goto   RcvNextBit                     ; Nope, go get the next one
;
; Generate ACK bit if not last byte to be read,
; if last byte Gennerate NACK ; do not send ACK on last byte, main routine will send a STOP bit
;
   bsf    STATUS, RP0                    ; Page 1 for TRIS manipulation
   bcf    _SCL                           ; pull SCL low
   bcf    _SDA                           ; ACK by pulling SDA low
   btfsc  Last_Byte_Rcv                  ; Is it the last byte to receive
   bsf    _SDA                           ; If so, send NACK by setting SDA high
   call   Delay47uSec                    ; guarantee min LOW TIME tLOW & Setup time
   bsf    _SCL                           ; Raise Clock back up
   call   Delay40uSec                    ; guarantee min HIGH TIME tHIGH
RcvEnd:
   bcf    _SCL                           ; reset clock
   bcf    Rcv_Progress                   ; reset bit in Bus Status
   bsf    Rcv_Success                    ; transmission successful
   bcf    ACK_Error                      ; ACK OK
   return

Delay47uSec:
   movlw ((_47uS_Delay-5)/3 + 1)         ; move delay into W
DlyK
   movwf DelayCount                      ; move what is in W to DelayCount
   decfsz   DelayCount, F                ; Decrement DelayCount
   goto  $-1                             ; Loop until 0
   return                                ; return

Delay40uSec:
   movlw ((_40uS_Delay-8)/3 + 1)         ; move delay into W
   goto  DlyK                            ; goto DlyK loop

 

本站內(nèi)容除特別聲明的原創(chuàng)文章之外,轉(zhuǎn)載內(nèi)容只為傳遞更多信息,并不代表本網(wǎng)站贊同其觀點(diǎn)。轉(zhuǎn)載的所有的文章、圖片、音/視頻文件等資料的版權(quán)歸版權(quán)所有權(quán)人所有。本站采用的非本站原創(chuàng)文章及圖片等內(nèi)容無法一一聯(lián)系確認(rèn)版權(quán)者。如涉及作品內(nèi)容、版權(quán)和其它問題,請及時通過電子郵件或電話通知我們,以便迅速采取適當(dāng)措施,避免給雙方造成不必要的經(jīng)濟(jì)損失。聯(lián)系電話:010-82306118;郵箱:aet@chinaaet.com。
主站蜘蛛池模板: 久久精品中文字幕大胸| 国产午夜亚洲精品不卡| 丰满老熟好大bbb| 1a级毛片免费观看| 成人观看网站a| 亚洲一卡二卡三卡| 玉蒲团之天下第一| 国产三级精品在线观看| 拍拍拍无挡免费视频网站| 宅男66lu国产乱在线观看| 久久天天躁狠狠躁夜夜不卡| 欧美精品18videosex性欧美| 啊灬啊灬啊灬快灬深久久| 国产精品蜜芽在线观看| 在线免费观看韩国a视频| 中文字幕123区| 日韩视频中文字幕| 亚洲欧美一区二区三区四区| 精品国产一区二区三区2021| 国产寡妇偷人在线观看视频 | 日本亚洲精品色婷婷在线影院| 女人被男人狂躁视频免费| 久久9精品久久久| 日韩美女性生活视频| 亚洲最大在线视频| 男人桶女人视频30分钟看看吧| 国产91最新在线| 黄色国产免费观看| 国产精品国三级国产aⅴ| 99视频精品全国在线观看| 性宝福精品导航| 久久www成人看片| 日韩精品无码人妻一区二区三区| 亚洲大片免费看| 热久久综合这里只有精品电影| 午夜欧美日韩在线视频播放| 超碰aⅴ人人做人人爽欧美| 国产无遮挡吃胸膜奶免费看| 3571色影院| 国语对白avxxxooo| sss欧美一区二区三区|