Real Time Clock

Thread Starter

RG23

Joined Dec 6, 2010
304
Rich (BB code):
RTC: 
call read_RTC
read_regs:
movf SECS,0
call writebcd
;movwf temp15
;movlw 0xCB
;movwf PORTD
;call SND_CMD
;movf temp15,0
;movwf PORTD
;call SND_DTA 
return
read_RTC:
call I2C_STRT
movlw 0xD0
movwf I2C_Value 
call I2C_SEND
movlw 0x00
movwf I2C_Value
call I2C_SEND
call I2C_STRT
movlw 0xD1
movwf I2C_Value 
call I2C_SEND
movlw 0x00
movwf I2C_Value
call I2C_SEND
call I2C_RD
movwf SECS
call I2C_STP 
return 
writebcd:
movwf temp16
swapf temp16,0
andlw 0x0F
addlw 0x06
btfss STATUS,DC
call lessnine
addlw 0x31
goto digit1
 
lessnine:
addlw 0x2A
return 
digit1: 
movlw 0xCB
movwf PORTD
call SND_CMD
movf temp16,0
movwf PORTD
call SND_DTA
return 
I2C_RD:
bcf STATUS,RP1
bsf STATUS,RP0 
clrf TRISC
bsf TRISC,4
bcf STATUS,RP1
bsf STATUS,RP0 
clrf I2C_Value
 
movlw 0x08
movwf COUNTER5
goto I2C_read_loop
;rlf I2C_Value,1
;clrf I2C_Value
I2C_read_loop: 
rlf I2C_Value,1
bsf I2C_CLOCK 
btfsc PORTC,4
bsf I2C_Value,0
bcf I2C_CLOCK
decfsz COUNTER5,1
goto I2C_read_loop
 
movf I2C_Value,0 
return
 
Last edited by a moderator:
It's hardly complete code and won't work as is, also please learn comment your source code. Is it your source code or did you just copy it from somewhere?

Is this a school assignment?
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I have declared all the variables correctly

I just want to check if my RTC subroutine is fine

and is it the correct method to read thed data from RTC?
 

hgmjr

Joined Jan 28, 2005
9,027
If you can't post 100% of the code then nobody in the forum can help you. You can attach it instead of embedding it in the reply.

hgmjr
 

Thread Starter

RG23

Joined Dec 6, 2010
304
Rich (BB code):
#include <p16F887.inc>
org 0x2100
 
;
 
DELAY1 EQU 0x20
DELAY2 EQU 0x21
I2C_Value equ 0x35
 
COUNTER5 equ 0x61
temp15 equ 0x62
SECS equ 0x63
MINS equ 0x64 
HRS equ 0x65 
#define CARRY STATUS,0
#define ZERO STATUS,2
#define I2C_DTA PORTC,4
#define I2C_CLOCK PORTC,3
;} 
org 0
Start:
goto main
 
org 4 ; INTERRUPT HERE
goto ISR
 
org 10
 
main:
;{
call init_Bank0
call init_Bank1
call init_Bank3
bsf PORTC,6 ; turn power ON
bsf PORTC,3
bsf PORTC,4 ; init I2C data & clock bits
 
 
call initLCD ; needed to display correctly
call RTC
 
 
initLCD:
bcf PORTB,1
bsf PORTB,2
bcf PORTB,4 ; SETUP DISPLAY TO OUTPUT
MOVLW 0x38
MOVWF PORTD ; SENT TO LCD
CALL SND_CMD
MOVLW 0x0C
MOVWF PORTD ; SENT TO LCD
CALL SND_CMD
MOVLW 0x01
MOVWF PORTD ; SENT TO LCD
CALL SND_CMD
clrf DELAY1 ; delay 1.53ms min
movlw 0xFE
movwf DELAY2
INCFSZ DELAY1,1
GOTO $-1
INCFSZ DELAY2,1
GOTO $-3
MOVLW 0x06
MOVWF PORTD ; SENT TO LCD
CALL SND_CMD
movlw 0x40
movwf PORTD
call SND_CMD ; setup RAM for custom characters
movlw 0x28
movwf i ; setup loop count
iLCD_1:
decf i,0 ; get the loop count-1
movwf PORTD
call SND_DTA ; 1st line, 1st char of custom characters
decfsz i,1
goto iLCD_1
return
 
RTC: 
call read_RTC
read_regs:
movf SECS,0
call writebcd
movwf temp15
movlw 0xCB
movwf PORTD
call SND_CMD
movf temp15,0
movwf PORTD
call SND_DTA 
return
read_RTC:
call I2C_STRT
movlw 0xD0
movwf I2C_Value 
call I2C_SEND
movlw 0x00
movwf I2C_Value
call I2C_SEND
call I2C_STRT
movlw 0xD1
movwf I2C_Value 
call I2C_SEND
movlw 0x00
movwf I2C_Value
call I2C_SEND
call I2C_RD
movwf SECS
call I2C_STP 
return 
writebcd:
movwf I2C_Value
swapf I2C_Value,0
andlw 0x0f
addlw 0x06
btfss STATUS,DC
call lessnine
addlw 0x31
goto digit1
return
lessnine:
addlw 0x2A
return 
digit1: 
movlw 0x88
movwf PORTD
call SND_CMD
movf temp15,0
movwf PORTD
call SND_DTA
 
 
SND_CMD:
;{
bcf PORTB,1
bcf PORTB,2
bcf PORTB,4 ;SETUP CONTROL BITS
bsf PORTB,4 ;Set ENABLE bit active
bcf PORTB,4 ;clear ENABle bit (causes write)
 
RETLW 0x00
;} 
SND_DTA:
;{
bsf PORTB,1
bcf PORTB,2
bcf PORTB,4 ;SETUP CONTROL BITS
bsf PORTB,4 ;Set ENABLE bit active
bcf PORTB,4 ;clear ENABle bit (causes write)
 
RETLW 0x00
;} 
 
 
init_Bank0:
;{
 
clrf PORTC ; init PORTC to all off
return
;}
init_Bank1:
;{ 
BSF STATUS, RP0 ; select Register Bank 1
MOVLW 0x61
MOVWF OSCCON
movlw 0x0B ; make PORTA.0,.1 & .3 analog
movwf TRISA
movlw 0x00
movwf TRISC 
clrf TRISD 
MOVLW 0x29
MOVWF TRISB 
 
BCF STATUS,RP0 ; back to Register Bank 0
return
;}
init_Bank3:
;{ 
BSF STATUS, RP0
BSF STATUS, RP1 
CLRF ANSELH ; no analog on Port B
 
BCF STATUS, RP1
BCF STATUS,RP0 ; back to Register Bank 0
return
;}
 
I2C_STRT:
;{
bsf I2C_CLOCK ; set clock high
bsf I2C_DTA ; set data high
bcf I2C_DTA ; clear data bit
bcf I2C_CLOCK ; set clock low 
return
I2C_STP:
;{
bsf I2C_CLOCK ; set clock high
bcf I2C_DTA ; set data low
bsf I2C_DTA ; set data high
return
I2C_SEND:
;{
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,7 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,6 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,5 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,4 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,3 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,2 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,1 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
btfsc I2C_Value,0 ; test CARRY for hi
bsf I2C_DTA ; hi, so set data bit
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
bcf I2C_DTA ; clear data bit
nop
bsf I2C_CLOCK ; set clock high
bcf I2C_CLOCK ; set clock low
nop
return
I2C_RD:
bcf STATUS,RP1
bsf STATUS,RP0 
bsf TRISC,4
bcf STATUS,RP1
bsf STATUS,RP0 
clrf I2C_Value
movlw 0x08
movwf COUNTER5
;rlf I2C_Value,1
;clrf I2C_Value
I2C_read_loop: 
rlf I2C_Value,1
bsf I2C_CLOCK 
btfsc I2C_DTA
bsf I2C_Value,0
bcf I2C_CLOCK
decfsz COUNTER5,1
goto I2C_read_loop
movf I2C_Value,0 
return
 
END
 
Last edited:

hgmjr

Joined Jan 28, 2005
9,027
Thanks for supplying the code.

I or someone with more PIC assembly language experience should be able to make suggestions.

hgmjr
 

GetDeviceInfo

Joined Jun 7, 2009
2,196
Are you providing a backup battery connected to the RTC?
My first attempts at implementing an RTC where frustrating. My notes are packed away, but I recall the device being put into sleep (non responsive) mode when the backup battery approached supply by some value.
 
Top