当前位置:首页 > 通信技术 > 通信技术
[导读] ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; File Name: i2c_low.asm ; Author: Alan G. Smith ; Purpose: This code is borrowed from Microchip with all o

 

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


以下为测试程序(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
;       Author: Alan G. Smith
;      Purpose: This is testing out the I2C code.
;
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    LIST P=16f84, F=INHX8M, C=100, N=59

#include "p16f84.inc"

XTAL_FREQ       equ     10000000        ; the crystal frequency we are using
ClkOut          equ     XTAL_FREQ / 4   ; the number of cycles per second

_40uS_Delay set   (ClkOut/250000)
_47uS_Delay set   (ClkOut/212766)
_50uS_Delay set   (ClkOut/200000)

#define SclPin  PORTA, 0                ; Pin for SCL (I2C)
#define SdaPin  PORTA, 1                ; Pin for SDA (I2C)

#define _SCL    TRISA, 0                ; How do we toggle SCL
#define _SDA    TRISA, 1                ; How do we toggle SDA

#define MSB   7
#define LSB   0
#define TRUE  1
#define FALSE 0

InitTrisA       equ     0x07            ; The Initial state to TRIS port A.

#define  Bus_Busy      BusStatus,0
#define  Abort         BusStatus,1
#define  Txmt_Progress BusStatus,2
#define  Rcv_Progress  BusStatus,3
#define  Txmt_Success  BusStatus,4
#define  Rcv_Success   BusStatus,5
#define  Fatal_Error   BusStatus,6
#define  ACK_Error     BusStatus,7

#define  Slave_RW      BusControl,0
#define  Last_Byte_Rcv BusControl,1
#define  SlaveActive   BusControl,2

CBLOCK     0x0C                        ; I2C Ram needed
   BusStatus                            ; The I2C Status register
   BusControl                           ; The I2C Control register
   I2CBitCount                          ; Number of bits left to send (or receive)
   I2CData                              ; Data (note: This is DESTROYED when sending)
   SlaveAddr                            ; Slave Address
ENDC

CBLOCK
   DelayCount                           ; used to figure out precise time delays
ENDC


   org 0                                 ; Reset Vector
   goto   start                          ; Goto Start

start
   bcf    INTCON, GIE                    ; Turn off interrupts in this critical part of code!
   bcf    STATUS, RP0                    ; Select Page 0 of registers
   movlw  0x0C                           ; Make sure there are 0‘s on SCL and SDA
   movwf  PORTA                          ; We write 1‘s to TX since 0 is a start bit
   bsf    STATUS, RP0                    ; Select Page 1 of registers
   movlw  InitTrisA                      ; Load W with the value for TRIS A
   movwf  TRISA                          ; movw the value from W into TRIS A
;*************** DEBUG CODE (let us use LEDs) *******************
   clrf   TRISB
;****************************************************************
   clrf   BusStatus                      ; Let‘s clear out busStatus before we start
   clrf   BusControl                     ; Let‘s clear out busControl before we start
;*************** TEST CODE *******************
   clrf   PORTB
main
   movlw  0xB0                           ; address of EEPROM
   movwf  SlaveAddr                      ; move into SlaveAddress register
   call   IsSlaveActive                  ; Check and see if the slave is active
   movlw  0xFF                           ; move FF into w (turn all LED‘s on)
   btfss  SlaveActive                    ; If the slave is active, leave it
   movlw  0xAA                           ; We didn‘t find it, turn off half.
   bcf    STATUS, RP0                    ; Select page 0 of registers
   movwf  PORTB                          ; move W to PortB

done                                     ; Game over man!
   goto   done                           ; endless loop

IsSlaveActive
   bcf    Slave_RW                       ; set for write operation
   call   TxmtStartBit                   ; Transmit Start Bit
   call   TxmtSlaveAddr                  ; Transmit Slave Address
   bcf    SlaveActive                    ; Assume not present
   btfss  ACK_Error                      ; skip if NACK, device is not present or not responding
   bsf    SlaveActive                    ; ACK received, device present & listening
   call   TxmtStopBit
   return

#include "i2c_low.asm"

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

销售额:440.5亿欧元,同比增长4.0%[1],合并增长1.3%。 又一年在逐步回暖的全球美妆市场中表现优异。 各事业部均实现增长[1],其中专业美发产品部表现亮眼。 各区域均实现增长[1],下半年呈...

关键字: BSP AI OV CD

2026年出境人次预计将突破2.25亿;富裕游客青睐深度长途游,引领全球旅游业格局重构 北京、上海和广州2026年2月13日 /美通社/ -- 中国出境游市场正迎来具有里程碑意义的转折点。预计2026年出境旅游人次将突...

关键字: 调研报告 TE NI BSP

阿联酋迪拜2026年2月12日 /美通社/ -- 继昨日宣布在迪拜设立具身智能数据合资公司后,纳斯达克上市公司 Robo.ai Inc.(NASDAQ: AIIO,以下简称"...

关键字: 数据采集 智能机器人 AI BSP

新加坡2026年2月11日 /美通社/ -- AI 基础设施解决方案提供商 SuperX AI Technology Limited(纳斯达克股票代码:SUPX,以下简称"S...

关键字: SUPER 通信 AI BSP

沙特阿拉伯吉达2026年2月11日 /美通社/ -- ABB国际汽联电动方程式世界锦标赛第12赛季的首个双赛分站即将拉开帷幕,第4轮与第5轮比赛将于2026年2月13日至14日在标志性的吉达滨海赛道举行。 第十二赛季F...

关键字: 电动 快充 BSP EV

上海2026年2月11日 /美通社/ -- 1月23日,由Global ConsignIndex跨盈指数举办的第二十三届跨盈年度B2B营销高管峰会(CC2026)在上海落下帷幕。大会聚焦主题"The...

关键字: AI NI BSP GO

新设中心将助力企业将AI愿景转化为企业级成果 汇聚6,000名DXC AI专家,支持跨行业客户协作 DXC将在英国和爱尔兰招聘150名AI专家,进一步拓展其业务能力 弗吉尼亚州阿什本2026年2月12...

关键字: AI BSP TECHNOLOGY CE

作者:陈旭东,IBM大中华区董事长、总经理 北京2026年2月12日 /美通社/ -- 日月其迈,时盛岁新。六十年一遇的"火马年"将至,我预祝各位IBM的客户、合作伙伴和大...

关键字: IBM BSP 人工智能 智能体

北京2026年2月11日 /美通社/ -- 当课堂的关键知识点讲解被错过,当同伴的欢笑像隔着一层模糊的“声音屏障”,听损学子正独自承受着比学业更重的压力:听不清、听得累。这让他们在课堂上费力追赶,消耗着本应用于思考的精力...

关键字: NUCLEUS 处理器 BSP LIMIT

广州2026年2月11日 /美通社/ -- 近日,全球领先的检验检测认证机构DEKRA德凯位于广州的测试中心正式获得ENEC(European Norms Electrica...

关键字: NEC 电池 BSP 应急照明
关闭