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

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

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

 ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;    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

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


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