您当前的位置:五五电子网电子知识单片机-工控设备单片机通信基于pic单片机的模拟I2C通信 正文
基于pic单片机的模拟I2C通信

基于pic单片机的模拟I2C通信

点击数:7940 次   录入时间:03-04 11:41:32   整理:http://www.55dianzi.com   单片机通信
  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



www.55dianzi.com

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


以下为测试程序(pIC16f84)

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;                  Copyright (C) 1997 by Innovatus
;                        All Rights Reserved.
; 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: http://www.innovatu.com
s;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;    File Name: testI2C.asm

上一页  [1] [2] [3]  下一页


本文关键字:单片机  通信  单片机通信单片机-工控设备 - 单片机通信