没用过DS1302,对这个芯片不是很了解。哪位高手替我看看是怎么回事?
程序如下;
#INCLUDE P16F877A.INC
#DEFINE D0 PORTD,7
#DEFINE D1 PORTD,6
#DEFINE D2 PORTD,5
#DEFINE D3 PORTD,4
#DEFINE D4 PORTD,3
#DEFINE D5 PORTD,2
;==================================================;
LED0W EQU 20H
LED1W EQU 21H
LED2W EQU 22H
LED3W EQU 23H
LED4W EQU 24H
LED5W EQU 25H
LEDSM_FLAG EQU 26H
#DEFINE SM0W LEDSM_FLAG,0
#DEFINE SM1W LEDSM_FLAG,1
#DEFINE SM2W LEDSM_FLAG,2
#DEFINE SM3W LEDSM_FLAG,3
#DEFINE SM4W LEDSM_FLAG,4
#DEFINE SM5W LEDSM_FLAG,5
;=================================================;
W_TEMP EQU 27H
STATUS_TEMP EQU 28H
PCLATH_TEMP EQU 29H
TIME_FLAG EQU 2AH
#DEFINE SJINT TIME_FLAG,0
#DEFINE LED_SMSJ TIME_FLAG,1
;==================DS1302总线定义==================;
#DEFINE SDAT PORTA,0
#DEFINE SCK PORTA,1
#DEFINE RST PORTA,2
#DEFINE TRISA_SDAT TRISA,0
#DEFINE TRISA_SCK TRISA,1
#DEFINE TRISA_RST TRISA,2
CNT EQU 2BH
RXBUF EQU 2CH
TXBUF EQU 2DH
;==================================================;
SECONDH EQU 31H
SECONDL EQU 32H
MINUTEH EQU 33H
MINUTEL EQU 34H
HOURH EQU 35H
HOURL EQU 36H
;=================================================;
ORG 0000H
GOTO START
ORG 0004H
GOTO INT_SEVER
ORG 0005H
INT_SEVER: MOVWF W_TEMP
SWAPF STATUS,W
MOVWF STATUS_TEMP
MOVF PCLATH,W
MOVWF PCLATH_TEMP
BTFSC INTCON,TMR0IF
GOTO T0INT
GOTO INT_SEVER_OK
T0INT: BCF INTCON,TMR0IF
MOVLW .200
MOVWF TMR0
BSF SJINT
GOTO INT_SEVER_OK
INT_SEVER_OK: MOVF PCLATH_TEMP,W
MOVWF PCLATH
SWAPF STATUS_TEMP,W
MOVWF STATUS
MOVF W_TEMP,W
RETFIE
;=========================================================;
START:
BANKSEL TRISB
CLRF TRISB
CLRF TRISD
CLRF TRISA
MOVLW .6
MOVWF ADCON1
MOVLW .2
MOVWF OPTION_REG
BANKSEL 20H
CLRF PORTB
CLRF PORTD
CLRF PORTA
MOVLW .200
MOVWF TMR0
BSF INTCON,GIE
BSF INTCON,PEIE
BSF INTCON,TMR0IE
CALL DS1302_INT
MAIN:
CALL READ1302
CALL LED_SM
GOTO MAIN
;=======================================================
READ1302:
CLRWDT
BCF RST
BCF SCK
BSF RST
MOVLW 0X81
MOVWF TXBUF
CALL Ds1302IutputByte
CALL DS1302OutputByte
MOVLW 0X0F
ANDWF RXBUF,W
MOVWF SECONDL
MOVLW 0X70
ANDWF RXBUF,W
MOVWF SECONDH
RRF SECONDH,F
RRF SECONDH,F
RRF SECONDH,F
RRF SECONDH,F
MOVLW 0X83
MOVWF TXBUF
CALL Ds1302IutputByte
CALL DS1302OutputByte
MOVLW 0X0F
ANDWF RXBUF,W
MOVWF MINUTEL
MOVLW 0X70
ANDWF RXBUF,W
MOVWF MINUTEH
RRF MINUTEH,F
RRF MINUTEH,F
RRF MINUTEH,F
RRF MINUTEH,F
MOVLW 0X85
MOVWF TXBUF
CALL Ds1302IutputByte
CALL DS1302OutputByte
MOVLW 0X0F
ANDWF RXBUF,W
MOVWF HOURL
MOVLW 0X70
ANDWF RXBUF,W
MOVWF HOURH
RRF HOURH,F
RRF HOURH,F
RRF HOURH,F
RRF HOURH,F
READ1302_OK: RETURN
;=======================================================;
Ds1302IutputByte:
CLRWDT
BANKSEL TRISA
BCF TRISA_SDAT ;设置为输出
BANKSEL 20H
BCF RST
BCF SCK
BSF RST
MOVLW .8
MOVWF CNT
SENT_LOOP:
BCF SCK
BCF SDAT
BTFSC TXBUF,0
BSF SDAT
RRF TXBUF,F
BCF TXBUF,7
BSF SCK
DECFSZ CNT,F
GOTO SENT_LOOP
BCF RST
BCF SCK
BCF SDAT
GOTO Ds1302IutputByte_OK
Ds1302IutputByte_OK: RETURN
;========================================================;
DS1302OutputByte:
CLRWDT
BANKSEL TRISA
BSF TRISA_SDAT ;设置为输入
BANKSEL 20H
BCF RST
BCF SCK
BSF RST
MOVLW .8
MOVWF CNT
RECIVE_LOOP:
BCF SCK
RRF RXBUF,F
BCF RXBUF,7
BTFSC SDAT
BSF RXBUF,7
BSF SCK
DECFSZ CNT,F
GOTO RECIVE_LOOP
BCF RST
BCF SCK
GOTO DS1302OutputByte_OK
DS1302OutputByte_OK:RETURN
;======================================================;
DS1302_INT:
BCF RST
BCF SCK
BSF RST
BANKSEL TRISA
BCF TRISA_SDAT
BANKSEL 20H
MOVLW 0X80 ;发送写秒命令
MOVWF TXBUF
CALL Ds1302IutputByte
MOVLW 0X00 ;清除CE,开始工作
MOVWF TXBUF
CALL Ds1302IutputByte
RETURN
;=======================================================;
LED_SM:
CLRWDT
BTFSS SJINT
GOTO LED_SM_OK
BCF SJINT
GOTO SM_CLOCK
SM_CLOCK: MOVF SECONDL,W
MOVWF LED5W
MOVF SECONDH,W
MOVWF LED4W
MOVF MINUTEL,W
MOVWF LED3W
MOVF MINUTEH,W
MOVWF LED2W
MOVF HOURL,W
MOVWF LED1W
MOVF HOURH,W
MOVWF LED0W
GOTO SMSMSM
SMSMSM:
BTFSS SM0W
GOTO LED0W_SM
BTFSS SM1W
GOTO LED1W_SM
BTFSS SM2W
GOTO LED2W_SM
BTFSS SM3W
GOTO LED3W_SM
BTFSS SM4W
GOTO LED4W_SM
GOTO LED5W_SM
GOTO LED_SM_OK
LED0W_SM:
BSF SM0W
BCF D0
MOVF LED0W,W
CALL TABLE
MOVWF PORTB
BSF D5
GOTO LED_SM_OK
LED1W_SM:
BSF SM1W
BCF D5
MOVF LED1W,W
CALL TABLE
MOVWF PORTB
BSF D4
GOTO LED_SM_OK
LED2W_SM:
BSF SM2W
BCF D4
MOVF LED2W,W
CALL TABLE
MOVWF PORTB
BSF D3
GOTO LED_SM_OK
LED3W_SM:
BSF SM3W
BCF D3
MOVF LED3W,W
CALL TABLE
MOVWF PORTB
BSF D2
GOTO LED_SM_OK
LED4W_SM:
BSF SM4W
BCF D2
MOVF LED4W,W
CALL TABLE
MOVWF PORTB
BSF D1
GOTO LED_SM_OK
LED5W_SM:
MOVLW B'00000000'
MOVWF LEDSM_FLAG
BCF D1
MOVF LED5W,W
CALL TABLE
MOVWF PORTB
BSF D0
GOTO LED_SM_OK
LED_SM_OK: RETURN
TABLE: ADDWF PCL,F
; RETLW ABCDEFGH
RETLW B'00000011'
RETLW B'10011111'
RETLW B'00100101'
RETLW B'00001101'
RETLW B'10011001'
RETLW B'01001001'
RETLW B'01000001'
RETLW B'00011111'
RETLW B'00000001'
RETLW B'00001001'
END
一周热门 更多>