以下是16F1503测试程序:
ORG 0000H
NOP
GOTO START ;
ORG 0004H
GOTO INT_IN
ORG 0005H
START
CLRF STATUS
BANKSEL IOCAN
MOVLW B'00000000'
MOVWF IOCAN
BANKSEL WPUA
MOVLW B'00001111'
MOVWF WPUA
BANKSEL ANSELA
MOVLW B'00000000'
MOVWF ANSELA
MOVLW B'00000000'
MOVWF ANSELC
BANKSEL LATA
CLRF LATA
CLRF LATC
BANKSEL TRISA
MOVLW B'01101000' ;配置时钟为4M
MOVWF OSCCON
MOVLW B'10000100' ;TIMER0 32分频
MOVWF OPTION_REG
MOVLW B'00001000' ;允许SSPIF中断
MOVWF PIE1
MOVLW B'00000000'
MOVWF TRISA
MOVLW B'00000011' ;配置RC0/SCL、RC1/SDA配置为输入
MOVWF TRISC
MOVLW B'00000000'
MOVWF PIR1
MOVLW B'00010000' ;
MOVWF PORTA
MOVLW 0FH
MOVWF PORTC
MOVLW B'11000000' ;开中断
MOVWF INTCON
BANKSEL SSP1STAT
MOVLW B'10000000' ;初始化I2C
MOVWF SSP1STAT
MOVLW 09H ;设置波特率
MOVWF SSP1ADD
CLRF SSP1CON2
MOVLW B'00001000' ;配置为I2C主模式,时钟=Fosc/(4*(SSPADD+1))
MOVWF SSP1CON1
MOVLW B'01100000' ;允许停止条件、启动条件中断
MOVWF SSP1CON3
BANKSEL PIR1
BCF PIR1,SSP1IF
BCF PIR2,BCL1IF
BANKSEL SSP1STAT
BSF SSP1CON1,SSPEN ;使能同步串行端口
;CLRF SSPBUF
MAIN
CALL I2C_IDLE ;检测总线空闲 (断点1)
BANKSEL SSP1CON2
BSF SSP1CON2,SEN
(断点2)
BANKSEL PIR1 ;或BANKSEL SSP1CON2
BTFSS PIR1,SSP1IF ;或BTFSC SSP1CON2,SEN
GOTO $-1
(断点3)
BCF PIR1,SSP1IF
SENDWRTCOMM1
MOVLW B'01001000' ;模式命令48H
BANKSEL SSP1BUF
MOVWF SSP1BUF
BANKSEL PIR1
BTFSS PIR1,SSP1IF
GOTO $-1
BCF PIR1,SSP1IF
SENDWRTCOMM2
BANKSEL SSP1STAT
BTFSC SSP1STAT,BF
GOTO $-1
MOVLW B'01010001' ;5级亮度,7段显示,开显示
BANKSEL SSP1BUF
MOVWF SSP1BUF
BANKSEL PIR1
BTFSS PIR1,SSP1IF
GOTO $-1
BCF PIR1,SSP1IF
CALL WRTSTOP
CALL WRTSTART
BANKSEL SSP1CON2
BSF SSP1CON2,SEN
BANKSEL PIR1
BTFSS PIR1,SSP1IF
GOTO $-1
BCF PIR1,SSP1IF
SENDADDRESS
BANKSEL SSP1STAT
BTFSC SSP1STAT,BF
GOTO $-1
MOVLW 6CH ;DIG3
MOVWF SSP1BUF
BANKSEL PIR1
BTFSS PIR1,SSP1IF
GOTO $-1
BCF PIR1,SSP1IF
SENDDATA
BANKSEL SSP1STAT
BTFSC SSP1STAT,BF
GOTO $-1
MOVLW 6DH ;5
MOVWF SSP1BUF
BANKSEL PIR1
BTFSS PIR1,SSP1IF
GOTO $-1
BCF PIR1,SSP1IF
CALL WRTSTOP
GOTO MAIN
I2C_IDLE
BANKSEL SSP1STAT
BTFSC SSP1STAT,R_NOT_W
GOTO $-1
MOVF SSP1CON2,W
ANDLW 1FH
BTFSS STATUS,Z
GOTO $-3
RETURN
WRTSTART
BANKSEL SSP1STAT
BSF SSP1CON2,SEN
BANKSEL PIR1
BTFSS PIR1,SSP1IF
GOTO $-1
BCF PIR1,SSP1IF
RETURN
WRTSTOP
BANKSEL SSP1CON2
BSF SSP1CON2,PEN
BANKSEL PIR1
BTFSS PIR1,SSP1IF
GOTO $-1
BCF PIR1,SSP1IF
RETURN
INT_IN
RETFIE
END |