Hello guy, i am new here.
just finish reading a few posts and find this forum helpful
Well, i faced a problem programming DS1302 with PIC16C745.
my problem is:
i have write in time for the RTC and after 2 mins from it start, it have to turn off my LED which is initially on.
i got the program out already, but i don't know where i have went wrong that the program is unable to work.
i really hope someone can provide me with help
the following is my program.
LIST P=16C745
#INCLUDE <P16C745.INC>
#DEFINE CE PORTC,0 ;Define PORTC,0 as CE, so as to show the link between PIC16C745 and DS1302
#DEFINE SCLK PORTC,1 ;
#DEFINE IO PORTC,2 ;
errorlevel -302 ; suppress "register not in Bank 0, check page bits" message
Bank0 udata
REGWRITE RES 1 ;To define all the following label, use this method (XXXX RES 1)
TIME_COUNT RES 1
TIME_TEMP RES 1
;--------------------------------END OF DECLARATION-------------------------------------
ORG 0
MAIN
BCF STATUS,RP0 ;Activates Bank 0, because PORT(X) located in Bank 0 - See PIC16C745 memory table.
CLRF PORTB ;Resets PORTB, Set B'00000000' into PORTB
BSF STATUS,RP0 ;Activates Bank 1, because TRIS(X) located in Bank 1
MOVLW B'00000000' ;Set PORTB as output port
MOVWF TRISB ;Send the previous binary to this exact location
BCF STATUS,RP0 ;Activates Bank 0, because PORT(X) located in Bank 0 - See PIC16C745 memory table.
BSF PORTB,6 ;Turns on LED
BSF PORTB,7 ;Turns on LED
;------------------------DS1302 Real Time Counter-------------------------------
Start
CALL WRITE_EN ;Call this subroutine so as to start up the transmission between PIC and RTC
MOVLW 0X8E ;Insert B'10001110' into REGWRITE, to point to the address - Control
MOVWF REGWRITE ;
CALL WRITE_RTC ;Call this subroutine whenever you are inputing the number into RTC
MOVLW 0x00 ;Control register
MOVWF REGWRITE ;
CALL WRITE_RTC
;******************************************
CALL WRITE_EN
MOVLW 0X80 ;Insert B'10000000' into REGWRITE, to point to the address - Sec
MOVWF REGWRITE
CALL WRITE_RTC
MOVLW 0X00 ;Second will start at 0
MOVWF REGWRITE
CALL WRITE_RTC
;******************************************
CALL WRITE_EN
MOVLW 0X82 ;Insert B'10000010' into REGWRITE, to point to the address - Min
MOVWF REGWRITE
CALL WRITE_RTC
MOVLW 0X03 ;Minute will start at 3
MOVWF REGWRITE
CALL WRITE_RTC
;******************************************
CALL WRITE_EN
MOVLW 0X84 ;Insert B'10000100' into REGWRITE, to point to the address - Hour
MOVWF REGWRITE
CALL WRITE_RTC
MOVLW 0X15 ;Hour will start at 3pm
MOVWF REGWRITE
CALL WRITE_RTC
;******************************************
the code below is the part where i test whether is it 2 mins past, if yes, turn off the initially on LED, is there sth wrong?
TURNOFF_LED
CALL READ_MIN
MOVF TIME_TEMP,W
SUBLW 0X05
BTFSS STATUS,Z
GOTO TURNOFF_LED
BCF STATUS,C
BCF PORTB,7
GOTO MAIN
Until here..
;******************************************
READ_MIN
CALL WRITE_EN
MOVLW 0X83 ;In order to read, you need to mention which address you want to read
MOVWF REGWRITE
CALL WRITE_RTC
BSF STATUS,RP0
MOVLW B'00000100'
MOVWF TRISC
BCF STATUS,RP0
CALL READ_RTC
NOP
BCF CE
NOP
NOP
RETURN
;----------------------------------Subroutines----------------------------------------
WRITE_EN
CLRF PORTC ;Resets PORTC, Set Binary '00000000' into PORTC
BSF STATUS,RP0 ;Activates Bank 1, because TRIS(X) located in Bank 1
MOVLW B'00000000' ;Using this line and the following line together means
MOVWF TRISC ;to set binary '00000000' into PORTC, to set as output
BCF STATUS,RP0 ;Activates Bank 0
BSF CE ;Input logic '1' to RST of DS1302, all communication sequences are intitiated by driving RST high
RETURN
;******************************************
WRITE_RTC
MOVLW 0X08 ;'0X' is use for hexadecimal, 08 is the hexadecimal number.
MOVWF TIME_COUNT ;Move the hexadecimal into this TIME_COUNT label
WRITE_LOOP
BCF SCLK ;Disallow data movement
BSF IO ;Set I/O pin to 'high'
BTFSS REGWRITE,0 ;Ensure that bit 0 is a 0, so that it mean to write into the RTC
BCF IO ;Set I/O pin to 'low'
NOP ;No operation, coz' this command provide a timing break to the next command
BSF SCLK ;Allow data movement since there is a need to write to RTC
RRF REGWRITE,1 ;Rotates bit 1 in REGWRITE to the right, and put the result back in REGWRITE
DECFSZ TIME_COUNT,1;Decrements a file register and if the result is zero, skips the next instruction and result will be put back into file register
GOTO WRITE_LOOP ;Looping
RETURN
;******************************************
READ_RTC
CLRF TIME_TEMP
MOVLW 0X08 ;'0X' is use for hexadecimal, 08 is the hexadecimal number.
MOVWF TIME_COUNT ;Move the hexadecimal into this TIME_COUNT label
READ_LOOP
BSF SCLK
BSF STATUS,C
BTFSS IO
BCF STATUS,C
BCF SCLK
RRF TIME_TEMP,1
DECFSZ TIME_COUNT,1;Delay
GOTO READ_LOOP ;Looping
MOVF TIME_TEMP,0
RETURN
;******************************************
;------------------------------------------End of Subroutine-------------------------------------
END
just finish reading a few posts and find this forum helpful
Well, i faced a problem programming DS1302 with PIC16C745.
my problem is:
i have write in time for the RTC and after 2 mins from it start, it have to turn off my LED which is initially on.
i got the program out already, but i don't know where i have went wrong that the program is unable to work.
i really hope someone can provide me with help
the following is my program.
LIST P=16C745
#INCLUDE <P16C745.INC>
#DEFINE CE PORTC,0 ;Define PORTC,0 as CE, so as to show the link between PIC16C745 and DS1302
#DEFINE SCLK PORTC,1 ;
#DEFINE IO PORTC,2 ;
errorlevel -302 ; suppress "register not in Bank 0, check page bits" message
Bank0 udata
REGWRITE RES 1 ;To define all the following label, use this method (XXXX RES 1)
TIME_COUNT RES 1
TIME_TEMP RES 1
;--------------------------------END OF DECLARATION-------------------------------------
ORG 0
MAIN
BCF STATUS,RP0 ;Activates Bank 0, because PORT(X) located in Bank 0 - See PIC16C745 memory table.
CLRF PORTB ;Resets PORTB, Set B'00000000' into PORTB
BSF STATUS,RP0 ;Activates Bank 1, because TRIS(X) located in Bank 1
MOVLW B'00000000' ;Set PORTB as output port
MOVWF TRISB ;Send the previous binary to this exact location
BCF STATUS,RP0 ;Activates Bank 0, because PORT(X) located in Bank 0 - See PIC16C745 memory table.
BSF PORTB,6 ;Turns on LED
BSF PORTB,7 ;Turns on LED
;------------------------DS1302 Real Time Counter-------------------------------
Start
CALL WRITE_EN ;Call this subroutine so as to start up the transmission between PIC and RTC
MOVLW 0X8E ;Insert B'10001110' into REGWRITE, to point to the address - Control
MOVWF REGWRITE ;
CALL WRITE_RTC ;Call this subroutine whenever you are inputing the number into RTC
MOVLW 0x00 ;Control register
MOVWF REGWRITE ;
CALL WRITE_RTC
;******************************************
CALL WRITE_EN
MOVLW 0X80 ;Insert B'10000000' into REGWRITE, to point to the address - Sec
MOVWF REGWRITE
CALL WRITE_RTC
MOVLW 0X00 ;Second will start at 0
MOVWF REGWRITE
CALL WRITE_RTC
;******************************************
CALL WRITE_EN
MOVLW 0X82 ;Insert B'10000010' into REGWRITE, to point to the address - Min
MOVWF REGWRITE
CALL WRITE_RTC
MOVLW 0X03 ;Minute will start at 3
MOVWF REGWRITE
CALL WRITE_RTC
;******************************************
CALL WRITE_EN
MOVLW 0X84 ;Insert B'10000100' into REGWRITE, to point to the address - Hour
MOVWF REGWRITE
CALL WRITE_RTC
MOVLW 0X15 ;Hour will start at 3pm
MOVWF REGWRITE
CALL WRITE_RTC
;******************************************
the code below is the part where i test whether is it 2 mins past, if yes, turn off the initially on LED, is there sth wrong?
TURNOFF_LED
CALL READ_MIN
MOVF TIME_TEMP,W
SUBLW 0X05
BTFSS STATUS,Z
GOTO TURNOFF_LED
BCF STATUS,C
BCF PORTB,7
GOTO MAIN
Until here..
;******************************************
READ_MIN
CALL WRITE_EN
MOVLW 0X83 ;In order to read, you need to mention which address you want to read
MOVWF REGWRITE
CALL WRITE_RTC
BSF STATUS,RP0
MOVLW B'00000100'
MOVWF TRISC
BCF STATUS,RP0
CALL READ_RTC
NOP
BCF CE
NOP
NOP
RETURN
;----------------------------------Subroutines----------------------------------------
WRITE_EN
CLRF PORTC ;Resets PORTC, Set Binary '00000000' into PORTC
BSF STATUS,RP0 ;Activates Bank 1, because TRIS(X) located in Bank 1
MOVLW B'00000000' ;Using this line and the following line together means
MOVWF TRISC ;to set binary '00000000' into PORTC, to set as output
BCF STATUS,RP0 ;Activates Bank 0
BSF CE ;Input logic '1' to RST of DS1302, all communication sequences are intitiated by driving RST high
RETURN
;******************************************
WRITE_RTC
MOVLW 0X08 ;'0X' is use for hexadecimal, 08 is the hexadecimal number.
MOVWF TIME_COUNT ;Move the hexadecimal into this TIME_COUNT label
WRITE_LOOP
BCF SCLK ;Disallow data movement
BSF IO ;Set I/O pin to 'high'
BTFSS REGWRITE,0 ;Ensure that bit 0 is a 0, so that it mean to write into the RTC
BCF IO ;Set I/O pin to 'low'
NOP ;No operation, coz' this command provide a timing break to the next command
BSF SCLK ;Allow data movement since there is a need to write to RTC
RRF REGWRITE,1 ;Rotates bit 1 in REGWRITE to the right, and put the result back in REGWRITE
DECFSZ TIME_COUNT,1;Decrements a file register and if the result is zero, skips the next instruction and result will be put back into file register
GOTO WRITE_LOOP ;Looping
RETURN
;******************************************
READ_RTC
CLRF TIME_TEMP
MOVLW 0X08 ;'0X' is use for hexadecimal, 08 is the hexadecimal number.
MOVWF TIME_COUNT ;Move the hexadecimal into this TIME_COUNT label
READ_LOOP
BSF SCLK
BSF STATUS,C
BTFSS IO
BCF STATUS,C
BCF SCLK
RRF TIME_TEMP,1
DECFSZ TIME_COUNT,1;Delay
GOTO READ_LOOP ;Looping
MOVF TIME_TEMP,0
RETURN
;******************************************
;------------------------------------------End of Subroutine-------------------------------------
END