📄 i2c_master.s43
字号:
;-------------------------------------------------------------------------------
; I2C_Master.s43 - I2C Master Implementation in Software.
;
; The following library can be customized to use any MSP430 port and pin
; combination my modifying the header file I2C_Master.h. The functions use
; C calling conventions, preserve all registers and can therefore be used
; from C or assembly environment. C Prototypes are provided in the header
; file.
;
; Andreas Dannenberg
; Texas Instruments Inc.
; June 2004
; Built with IAR Embedded Workbench Version: 3.10A
;-------------------------------------------------------------------------------
#include "msp430x41x.h"
#include "I2C_Master.h"
;-------------------------------------------------------------------------------
PUBLIC I2CSetup ; Exported symbols
PUBLIC I2CRead16
PUBLIC I2CWrite8
;-------------------------------------------------------------------------------
RSEG CODE ; Program code
;-------------------------------------------------------------------------------
; void I2CSetup(void)
;
; IN: -
; OUT: -
;-------------------------------------------------------------------------------
I2CSetup bic.b #SDA+SCL,&PI2C_OUT ; Clear output latch bits
bic.b #SDA+SCL,&PI2C_DIR ; Set I2C pins to High-Z
ret
;-------------------------------------------------------------------------------
; unsigned int I2CRead16(unsigned char Addr)
;
; Reads two bytes (16 bit) of data transmitted from I2C slave
;
; IN: R12 I2C slave address
; OUT: R12 00000h - 0ffffh I2C device data
;-------------------------------------------------------------------------------
I2CRead16 push.w R15 ; Save R15
setc ; Set carry for "READ" and
rlc.w R12 ; left-shift into address
call #I2C_Start ; Send Start
call #I2C_TX ; Send Address+RD and Ack
call #I2C_RX ; Read Data and Ack
call #I2C_ACKn ; Acknowledge Byte Rcv'd
call #I2C_RX ; Read Data and Ack
call #I2C_NACKn ; NOT Acknowledge Byte Rcv'd
call #I2C_Stop ; Send Stop
pop.w R15 ; Restore R15
ret
;-------------------------------------------------------------------------------
; void I2CWrite8(unsigned char Addr, unsigned char Data)
;
; Writes one byte (8 bit) of data to I2C slave
;
; IN: R12 I2C slave address
; R14 000h - 0ffh I2C device data
; OUT: -
;-------------------------------------------------------------------------------
I2CWrite8 push.w R12 ; Save R12
push.w R15 ; Save R15
clrc ; Clear carry for "WRITE" and
rlc.w R12 ; left-shift into address
call #I2C_Start ; Send Start
call #I2C_TX ; Send Address and Ack
mov.b R14,R12 ; Load Out-Going Data
call #I2C_TX ; Send Data and Ack
call #I2C_Stop ; Send Stop
pop.w R15 ; Restore R15
pop.w R12 ; Restore R12
ret
;-------------------------------------------------------------------------------
; Help function to generate an I2C start condition.
;
; IN: -
; OUT: -
; REG: -
;-------------------------------------------------------------------------------
I2C_Start bic.b #SCL+SDA,&PI2C_DIR ; SCL and SDA to High-Z
bic.b #SCL,&PI2C_OUT ; SCL=1
I2C_Start2 bit.b #SCL,&PI2C_IN ; SCL==1?
jz I2C_Start2 ; wait for SCL==1
bic.b #SDA,&PI2C_OUT ; SDA=1
nop ; delay
nop ; (START condition needs
nop ; set-up time >4.7us)
nop
bis.b #SDA,&PI2C_DIR ; SDA=0
nop ; delay
nop ; (START condition needs
nop ; hold time >4.0us)
nop
bis.b #SCL,&PI2C_DIR ; SCL=0
ret
;-------------------------------------------------------------------------------
; Help function to transmit eight bit.
;
; IN: R12 Data to transmit
; OUT: -
; REG: R15
;-------------------------------------------------------------------------------
I2C_TX mov.b #8,R15 ; Number of bits to xfer
I2C_TX_Bit rla.b R12 ; Data bit -> carry
jc I2C_TX1 ; Test carry for 1 or 0
I2C_TX0 bis.b #SDA,&PI2C_DIR ; SDA=0
jmp I2C_TXx ; Toggle SCL
I2C_TX1 bic.b #SDA,&PI2C_DIR ; SDA=1
I2C_TXx bic.b #SCL,&PI2C_DIR ; SCL=1
I2C_TXx2 bit.b #SCL,&PI2C_IN ; SCL==1?
jz I2C_TXx2 ; Wait for SCL==1
bis.b #SCL,&PI2C_DIR ; SCL=0
dec.b R15 ; All bits read?
jnz I2C_TX_Bit ; Continue until 8 bits are sent
bic.b #SDA,&PI2C_DIR ; SDA=1
TX_Ackn bic.b #SCL,&PI2C_DIR ; SCL=1
TX_Ackn2 bit.b #SCL,&PI2C_IN ; SCL==1?
jz TX_Ackn2 ; Wait for SCL==1
bis.b #SCL,&PI2C_DIR ; SCL=0
ret
;-------------------------------------------------------------------------------
; Help function to receive 8 bit of I2C data. The data is shifted from
; into R12 from right.
;
; IN: R12
; OUT: R12 Received data
; REG: R15
;-------------------------------------------------------------------------------
I2C_RX mov.b #8,R15 ; Number of bits to rcv
I2C_RX_Bit bic.b #SCL,&PI2C_DIR ; SCL=1
I2C_RX_Bit2 bit.b #SCL,&PI2C_IN ; SCL==1?
jz I2C_RX_Bit2 ; Wait for SCL==1
bit.b #SDA,&PI2C_IN ; SDA bit -> carry
rlc.w R12 ; Store new bit in R12
bis.b #SCL,&PI2C_DIR ; SCL=0
dec R15 ; All bits read?
jnz I2C_RX_Bit ; Continue until 8 bits are read
ret
;-------------------------------------------------------------------------------
; Help function to generate an I2C ACK signal.
;
; IN: -
; OUT: -
; REG: -
;-------------------------------------------------------------------------------
I2C_ACKn bis.b #SDA,&PI2C_DIR ; SDA=0, Ack
bic.b #SCL,&PI2C_DIR ; SCL=1
I2C_ACKn2 bit.b #SCL,&PI2C_IN ; SCL==1?
jz I2C_ACKn2 ; Wait for SCL==1
bis.b #SCL,&PI2C_DIR ; SCL=0
bic.b #SDA,&PI2C_DIR ; SDA=1
ret
;-------------------------------------------------------------------------------
; Help function to generate an I2C NACK signal.
;
; IN: -
; OUT: -
; REG: -
;-------------------------------------------------------------------------------
I2C_NACKn bic.b #SDA,&PI2C_DIR ; SDA=1, NOT Ack
bic.b #SCL,&PI2C_DIR ; SCL=1
I2C_NACKn2 bit.b #SCL,&PI2C_IN ; SCL==1?
jz I2C_NACKn2 ; Wait for SCL==1
bis.b #SCL,&PI2C_DIR ; SCL=0
ret
;-------------------------------------------------------------------------------
; Help function to generate an I2C stop condition.
;
; IN: -
; OUT: -
; REG: -
;-------------------------------------------------------------------------------
I2C_Stop bis.b #SDA,&PI2C_DIR ; SDA = 0
nop ; Delay
nop ; (STOP condition needs
nop ; set-up time >4.0us)
nop
bic.b #SCL,&PI2C_DIR ; SCL=1
I2C_Stop2 bit.b #SCL,&PI2C_IN ; SCL==1?
jz I2C_Stop2 ; Wait for SCL==1
bic.b #SDA,&PI2C_DIR ; SDA = 1
ret
;-------------------------------------------------------------------------------
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -