⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c-rout.s43

📁 MSP430系列I2C总线操作源代码
💻 S43
字号:
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%                                                                              %
;%     File : I2C interface Access Handling(Read,Write,Start/Stop Condition)    %
;%                                                                              %
;%     MCU  : TI MSP430F133/135/147/149                                         %
;%  Assemble/Link/Debug : IAR Kickstart for MSP430 V3.30A.                      %
;%                                                                              %
;%  Version : 1.0               Author : Alan Fong                              %
;%                                                                              %
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%  History:                                                                    %
;%   1. Initial Version on October 17,2005                                      %
;%                                                                              %
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%                                                                              %
;% Set Table Stop Value is 12 Space to View this Code.                          %
;%                                                                              %
;% Register Used For these Routine: R8,R9,R14                                   %
;%                                                                              %
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
; Define the Pin Name for I2C interface.
; Note : Must Set I2C Clock and I2C SDAT Pin to Input mode. 
;        If need to Output 1 : set it Input mode (Pull-Up).
;        If need to Output 0 : set it Output mode (Output latch must be 0).

#define	P_SDAT	Bit7
#define	P_SCLK	Bit6

#define	I2C_POUT	P2OUT
#define	I2C_PDIR	P2DIR
#define	I2C_PIN	P2IN

;======================================================================
; Define some Macr ofor I2C interface.
;======================================================================
I2C_SCLK	MACRO	V1		; Set Clock = V1
	IF	V1>0
	BIC.B	#P_SCLK,I2C_PDIR	; Clock = 1.
	ELSE
	BIS.B	#P_SCLK,I2C_PDIR	; Clock = 0.
	ENDIF
	ENDM
	;------------------------------------------------
I2C_SDAT	MACRO	V1		; Set SDAT = V1
	IF	V1>0
	BIC.B	#P_SDAT,I2C_PDIR	; SDAT = 1.
	ELSE
	BIS.B	#P_SDAT,I2C_PDIR	; SDAT = 0.
	ENDIF
	ENDM
;------------------------------------------------------------------
I2C_SCLK_1	MACRO			; Set Clock = 1.
	BIC.B	#P_SCLK,I2C_PDIR
	ENDM
	;------------------------------------------------
I2C_SCLK_0	MACRO			; Set Clokc = 0.
	BIS.B	#P_SCLK,I2C_PDIR
	ENDM
	;------------------------------------------------
I2C_SDAT_0	MACRO			; SDAT = 0
	BIS.B	#P_SDAT,I2C_PDIR	; Output Mode = Output 0.
	ENDM			;
	;------------------------------------------------
I2C_SDAT_1	MACRO			; SDAT = 0
	BIC.B	#P_SDAT,I2C_PDIR	; Input Mode = Output 1.
	ENDM			;
	;------------------------------------------------
I2C_SDAT_IN	MACRO			; Read SDAT Input.
	BIT.B	#P_SDAT,I2C_PIN	;
	ENDM	
;============================================================================
; Start Condition for I2C device.
;============================================================================
I2C_Start:				; Start Condition.
	I2C_SDAT	1		; SDAT = 1.
	CALL	#DummyCycle
	I2C_SCLK	1		; SCLK = 1.
	CALL	#DummyCycle
	I2C_SDAT	0		; SDAT = 0.
	CALL	#DummyCycle
	I2C_SCLK	0		; SCLK = 0.
	CALL	#DummyCycle
DummyCycle:				; When use 4MHz OSC.
	NOP			;
	RET	
;============================================================================
; Stop Condition for I2C Device.
;============================================================================
I2C_Stop:				; Stop Condition.
	I2C_SCLK	0		; SCLK = 0.
	CALL	#DummyCycle
	I2C_SDAT	0		; SDAT = 0.
	CALL	#DummyCycle
	I2C_SCLK	1		; SCLK = 1.
	CALL	#DummyCycle
	I2C_SDAT	1		; SDAT = 1.
	CALL	#DummyCycle
	RET
;============================================================================
; Send Ackowledge signal to I2C device after read one byte.
;============================================================================
I2C_SendAck:
	I2C_SDAT	0		; SDAT = 0.
	CALL	#DummyCycle
	I2C_SCLK	1		; SCLK = 1.
	CALL	#DummyCycle
	I2C_SCLK	0		; SCLK = 0.
	CALL	#DummyCycle
	I2C_SDAT	1		; SDAT = 1(Input Mode).
	RET
;=============================================================================
;  Get one byte to R8 from I2C.
;
;  Register Used : R8,R9 
;
;=============================================================================
I2C_RxByte:			; Send one byte to I2C device.
	MOV.B	#8,R9		; Total 8 Bits.
	CLR.B	R8		; Clear buffer first.	
I2C_RxByte1:
	RLA.B	R8		; Shift to Left.
	I2C_SCLK	1		; SCLK = 1.
	CALL	#DummyCycle
	I2C_SDAT_IN			; Check SDAT.
	JZ	I2C_RxByte2		; Data = 0.
	BIS.B	#Bit0,R8		; Data = 1.
I2C_RxByte2:
	I2C_SCLK	0		; SCLK = 0.
	DEC.B	R9
	JNZ	I2C_RxByte1
	RET
;=============================================================================
;  Send one byte stored in R8 to I2C, return Ack in C flag.
;
;  Register Used : R8,R9 
;
;=============================================================================
I2C_TxByte:			; Send one byte to I2C device.
	MOV.B	#0x08,R9		; Total 8 Bits.
I2C_TxByte1:	
	RLA.B	R8
	JC	I2C_TxByte2
	I2C_SDAT	0		; SDAT = 0.
	JMP	I2C_TxByte3
I2C_TxByte2:	
	I2C_SDAT	1		; SDAT = 1.
I2C_TxByte3:
	CALL	#DummyCycle
	I2C_SCLK	1		; SCLK = 1.
	CALL	#DummyCycle
	I2C_SCLK	0		; SCLK = 0.
	DEC.B	R9
	JNZ	I2C_TxByte1
	;--------------------------------------------------------
I2C_Chk_ACK:			; Check the Ack.
	I2C_SDAT	1		; SDAT = 1(Input Mode).
	CALL	#DummyCycle
	I2C_SCLK	1		; SCLK = 1.
	CALL	#DummyCycle
	CLRC			; C=0.
	I2C_SDAT_IN			; Check SDAT.
	JZ	I2C_Chk_ACK1
	SETC
I2C_Chk_ACK1:	
	I2C_SCLK	0		; SCLK = 0.
	CALL	#DummyCycle
	RET
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%                                                                              %
;%  I2C_Read    : I2C Read Data from specifid address                           %
;%                                                                              %
;%  DevAddr     : Store the I2C Device Address.                                 %
;%  I2C_Addr    : Store the Memory Address data.                                %
;%  R14         : Point to the Memory which need to be written.                 %
;%  DataSize    : Store the Size which need to read.                            %   
;%                                                                              %
;%  Register Used : R8,R14                                                      %
;%                                                                              %
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I2C_Read:
	CALL	#I2C_Start		; Start Condition.
	MOV.B	DevAddr,R8		; Write Address.
	BIC.B	#Bit0,R8		; Write Mode.	
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
	MOV.B	I2C_Addr,R8		; Memory Address.
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
;	CALL	#I2C_Stop		; End write Address.	
;--------------------------------------------------------------------	
	CALL	#I2C_Start		; Restart.
	MOV.B	DevAddr,R8		; Send Read Command.
	CALL	#I2C_TxByte		; 
	JC	I2C_ErrHandl	; Handle Error.
	JMP	I2C_ReadLop1
I2C_ReadLop:
	CALL	#I2C_SendAck	; Continue Read.
I2C_ReadLop1:	
	CALL	#I2C_RxByte		; Read One Byte.
	MOV.B	R8,0(R14)		; Store to Buffer.
	INC	R14		; [R14] : Result Buffer.
	DEC.B	DataSize		; End of Read?
	JNZ	I2C_ReadLop		; No...
	CALL	#I2C_Stop		; End write Address.
I2C_ErrHandl:
	RET
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%                                                                              %
;%  I2C_Writ    : I2C Write Data to specifid address                            %
;%                                                                              %
;%  DevAddr     : Store the I2C Device Address.                                 %
;%  I2C_Addr    : Store the Address data.                                       %
;%  R14         : Point to the Memory which need to be written.                 %
;%  DataSize    : Store the Size which need to be written.                      %   
;%                                                                              %
;%  Register Used : R8,R14                                                      %
;%                                                                              %
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I2C_Write:
	CALL	#I2C_Start		; Start Condition.
	MOV.B	DevAddr,R8		; Write Address.
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
	MOV.B	I2C_Addr,R8		; Memory Address.
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
I2C_WritLop:
	MOV.B	@R14+,R8		;
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
	DEC.B	DataSize
	JNZ	I2C_WritLop
	CALL	#I2C_Stop		; End write Address.	
	RET
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%                                                                              %
;%  I2C_WritOne : I2C Write one Byte to specifid address                        %
;%                                                                              %
;%  DevAddr    : Store the I2C Device Address.                                  %
;%  I2C_Addr   : Store the Address data.                                        %
;%  R14-B0-b7  : Data to be Write.                                              %
;%                                                                              %
;%  Register Used : R8,R9                                                       %
;%                                                                              %
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I2C_WritOne:
	CALL	#I2C_Start		; Start Condition.
	MOV.B	DevAddr,R8		; Write Address.
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
	MOV.B	I2C_Addr,R8		; Memory Address.
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
	MOV.B	R14,R8		;
	CALL	#I2C_TxByte		; Send the Byte Out.
	JC	I2C_ErrHandl	; Handle Error.
	CALL	#I2C_Stop		; End write Address.
	RET
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;%                                                                              %
;%                                                                              %
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -