本帖最后由 liht1634 于 2009-12-1 14:07 编辑
LIST P=16F946 ; list directive to define processor
#INCLUDE <P16F946.INC> ; processor specific variable definitions
;*******************************************
ORG 00H
NOP
GOTO INIT_IIC
ORG 04H
GOTO INT_SERV
;*******************************************
INIT_IIC
BANKSEL PORTC
BCF PORTC,6 ;Set SDA SCL low when not tri-stated
BCF PORTC,7
MOVLW B'00000100'
MOVWF FLAG_SW4
BANKSEL SSPADD
MOVLW B'10100000' ;Slave address
MOVWF SSPADD
CLRF SSPSTAT
BANKSEL SSPCON
MOVLW B'00111011' ;Setup SSP module for I2C 7 bit 固件控制主控模式
MOVWF SSPCON
BANKSEL PIE1 ;Enable interrupts
BSF PIE1,SSPIE
BSF TRISC,SDA
BSF TRISC,SCL
BSF INTCON,PEIE ;Enable all peripheral interrupts
BSF INTCON,GIE ;Enable global interrupts
BANKSEL PIR1
BCF PIR1,SSPIF ;Clear SSP interrupt flag
CALL IIC_WRITE
GOTO $
;*******************************************
IIC_WRITE
CALL IIC_START
CALL IIC_WAIT
BANKSEL SSPADD
MOVF SSPADD,0 ;从器地址+写控制位 1010 000 0
BANKSEL SSPBUF
MOVWF SSPBUF
CALL IIC_WAIT
BANKSEL SSPBUF
MOVLW 01H ;待写入的从器件地址01H
MOVWF SSPBUF
CALL IIC_WAIT
MOVF FLAG_SW4,0 ;待写入的的数据
MOVWF SSPBUF
CALL IIC_WAIT
CALL IIC_STOP
CALL IIC_WAIT
RETURN
;==============发开始命令===================
IIC_START ;SCL is high while SDA goes from high to low transition
BANKSEL TRISC
BSF TRISC,SDA
BSF TRISC,SCL
NOP
NOP
NOP
BCF TRISC,SDA
NOP
NOP
NOP
BCF TRISC,SCL
RETURN
;==============发停止命令===================
IIC_STOP ;SDA goes from low to high during SCL high state
BANKSEL TRISC
BCF TRISC,SDA
BSF TRISC,SCL
NOP
NOP
NOP
BSF TRISC,SDA
NOP
NOP
NOP
BCF TRISC,SCL
RETURN
;==============等待发送结束=================
IIC_WAIT
BANKSEL PIR1
BTFSS PIR1,SSPIF
GOTO $-1
BCF PIR1,SSPIF
RETURN
;*******************************************
INT_SERV ;中断服务
RETFIE
;*******************************************
END
希望程序将FLAG_SW4单元的内容写入到I2C的01H单元中,现在程序执行到第二个CALL IIC_WAIT就出不来了,该怎么改?望赐教!!! |