Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18F2550 with real time counter (DS1302)

Status
Not open for further replies.

ash87

New Member
Hi im working on some project and im required to use PIC18F2550 with Real time Counter ( DS1302) using MPLAB in Assembly Language. Im reli a newbie at this.. I have tried to read up the PIC18F2550 and RTC datasheet. But there is still alot of things im not sure.

Im required to make this sample program work first:
When I press one pushbutton (RA4) and the LED (RBO) will light up and at the same time the real time counter(DS1302) will start counting one hour. After one hour later the RTC will stop counting and the LED will automatically turn off.

Is there anythin i need to look out for?? or make any setting?? any HElp and TIps?
 
If you are a newbie, then you have chosen. or been given, a rather involved project to start with.

The Maxim Real Time Clock requires interfacing with the pic - and looking at the Maxim site they do not show any pic assembler interface coding, although if you do some searching I'm sure you will find some code.

Suggest you start simply, make your pic turn the led on and off by pushing the switch - refer to the many pic tutorials by other members such as Nigel Goodwin.

Once you have learnt some basics then start looking at adding the RTC chip.

On a different tack, why not fit the 32k xtal to the pic and make it countdown the hour ?
 
thanks for ya reply..I have been given the project. Actually i have started off by doing a sample code like turning the LED ON/OFF using one push button..

list p=18F2550
#include<P18F2550.inc>

org 00000030h

Start
clrf PORTB ; clear PORTB output latch
movlw b'00000000' ; Set all pins PORTB as output
MOVWF TRISB ;

ON
btfsc PORTA,4 ; Has S2 been press? (Normally high,goes low when pressed.)
goto ON ; No, go back check again
movlw b'00000001' ; move it from register 1 to w.
movwf LATB ; move it to PORTB to light up the LED

DebounceA
btfss PORTA,4 ; Has key been released?
goto DebounceA ; No, wait somemore

OFF
btfsc PORTA,4 ; Has key been press? (Normally high, goes low when pressed.)
goto OFF ; No, check again
movlw b'00000000' ;
movwf LATB ; OFF the LED

DebounceB
btfss PORTA,4 ; Has key been released?
goto DebounceB ; No, wait again
goto Start ; Loop again
end ; directive indicates end of code


U mention that "The Maxim Real Time Clock requires interfacing with the pic - and looking at the Maxim site they do not show any pic assembler interface coding, although if you do some searching I'm sure you will find some code."

So only when i do put in the interfacing code then i can use the PIC with RTC?
Can u help me with the code by counting down 1hr?
 
Last edited:
You can use the PIC to count 1 hour for you. Don't need extra IC.
Do you need it to be really accurate?
 
but i reli dno how.. = ( i oni knw the most basic like switch on/off led but i dno how to make the PIC link to RTC which the real time counter(DS1302) will start counting one hour. After one hour later the RTC will stop counting and the LED will automatically turn off.
Can u all help me with the code?? i wish get this code first before i reli start with my actual one.. actually my teacher wants me to start off with tis code first.
 
if can accurate will be beta..
well, now im also trying to learn the assembly language but bcz i have a ver storage of time as i only got weeks to learn and complete my project at the same time. hence i wish u all can help me with the code, so at the same time i can also learn it..and also i can try enhance the code by myself in the future. Bcz that is not the final code i need for my project. That is juz a starting off. Please guys i reli need help.
 
Hi Ash87,

The 1302 is not too tough to interface to; it is SPI. I've posted working assembly snippets on here, and I'm sure some others have too. Try doing a search on this forum. I really don't have time to find it this morning, perhaps you can find it. I'll check back later.

Like the others suggest, you can mark time with the pic alone, and I'm sure Nigel's tutorials have something on this.
 
i was told by my teacher that i dn need any interfacing code..Now she have change the PIC to PIC16C745 instead pic18f..and i was given tis source code and was ask to edit from there, to make it count 1hour, once the LED has lightup.i reli don understand the codes and i oni got 2weeks left and im still struggling to edit it now.. can u guys help me with it? this is the very first time im writing it..Help pls!

;-----------------REAL TIME CLOCK--------------------------------------------

START1

CALL WRITE_EN
MOVLW 0X8E
MOVWF REGWRITE
CALL WRITE_RTC
MOVLW 0 ; CONTROL REGISTER
MOVWF REGWRITE
CALL WRITE_RTC
;*******************************
CALL WRITE_EN
MOVLW 0X80
MOVWF REGWRITE
CALL WRITE_RTC
MOVF SEC,W ; SEC
MOVWF REGWRITE
CALL WRITE_RTC
;*******************************
CALL WRITE_EN
MOVLW 0X82
MOVWF REGWRITE
CALL WRITE_RTC
MOVF MIN,W ; MIN
MOVWF REGWRITE
CALL WRITE_RTC
;*******************************
CALL WRITE_EN
MOVLW 0X84
MOVWF REGWRITE
CALL WRITE_RTC
MOVF HR,W ; HR
MOVWF REGWRITE
CALL WRITE_RTC
;*********************************
; CALL WRITE_EN
; MOVLW 0X8A
; MOVWF REGWRITE
; CALL WRITE_RTC
; MOVF DAY,W ; DAY
; MOVWF REGWRITE
; CALL WRITE_RTC
;*********************************

BCF CE ;The reset signal of the RTC must be asserted high during a read or a write.
; CLRF PORTB

; THIS FUNCTION IS TO CHECK THE VALUE OF "w" by looking at the LED and a buzz means end of this function

; MOVWF TEST
; MOVWF TEST2
;HAHA
; BSF PORTB,6
; CALL DELAY1
; BCF PORTB,6
; CALL DELAY1
; DECFSZ 0X57
; GOTO HAHA
; BSF PORTB,7
; CALL DELAY1
; BCF PORTB,7
; CALL DELAY1
; MOVF TEST,W


Start
clrf PORTB ; clear PORTB output latch
clrf PORTA ; Clear PORTA output latch
movlw b'00000000' ; Set all pins PORTB as output
movwf TRISB
movlw b'00010000' ; Set RA4 as Input port,and the rest as output
MOVWF TRISA

ON
btfsc PORTA,4 ; Is GSM output ON?
goto ON ; No, go back check again
goto SMS_1HRON
; movf Tempholding,0

;----------------CHECK INTERVAL-----------------------------
SMS_1HRON ; turn on the LED for 1 hour
call READ_MIN
movf TIME_TEMP,W
movwf smstime_min ; store the minute first to compare later
bsf PORTB,7 ; turn on RB7 (LED) only!

SMS_1HR
call SMS_TIME

SMS_1HRLOOP
call READ_HR
movf TIME_TEMP,w
subwf SMS_TIME,0 ; substract current HR with HR you got from CALL SMS_TIME
btfss STATUS,Z ; if result is zero skip to the next instruction
goto SMS_1HRLOOP
bcf STATUS,C

SMS_MINLOOP
CALL READ_MIN ; Compare minute after hour
movf TIME_TEMP,w
subwf smstime_min,0 ; Substract Current MIN with MIN you got from CALL smstime_min
btfss STATUS,Z
goto SMS_MINLOOP
bcf STATUS,C
goto Start1

Start1
clrf PORTA ; clear PORTA output latch
movlw b'00010000' ; Set RA4 as Input port,and the rest as output
MOVWF TRISA
bsf PORTA,3 ; Set RA3 and Send signal '1' to modem input
goto SMS_1HRON

SMS_TIME
bcf STATUS,C
call READ_HR
movf TIME_TEMP,W
sublw 0x47

btfsc STATUS,Z
goto time47

bcf STATUS,C
movf TIME_TEMP,W
sublw 0x33

btfsc STATUS,Z
goto time47


bcf STATUS,C
movf TIME_TEMP,W
sublw 13

btfsc STATUS,Z
goto time13
goto posresult

time47 ; hour is at 23 which is 47h
movlw 0x01 ; Next hour of 23 is 0hour which is 01h
movwf SMS_TIME
RETURN

time33 ; hour is at 19
movlw 0x41
movwf SMS_TIME
RETURN

time13
movlw 0x21 ; hour is at 9
movwf SMS_TIME
RETURN

posresult
movf TIME_TEMP,w
movwf SMS_TIME
incf SMS_TIME,1
incf SMS_TIME,1
RETURN




;CALL READ_HR
;MOVF TIME_TEMP,W ; MOVE THE CURRENT TIME INTO W
;;SUBWF 0X2B,0 ; THE VALUE OF 0X2B , ITSELF IS NOT THE VALUE
;BTFSS STATUS,Z
;GOTO INT2
;BCF STATUS,C
;BSF PORTB,7 ; on buzzer
;BSF PORTB,6 ; on led
;MOVF 0X2B,W
;MOVWF TIME_TEMP1
;PAGESEL INTER_RUPT
;CALL INTER_RUPT
;GOTO CHECK_INT
;INT2
;CALL READ_HR
;MOVF TIME_TEMP,W
;SUBWF 0X2C,W
;BTFSS STATUS,Z
;GOTO INT3
;BCF STATUS,C
;BSF PORTB,7 ; turn on buzzer
;BSF PORTB,6 ; TURN ON led
;MOVF 0X2C,W
;MOVWF TIME_TEMP1
;PAGESEL INTER_RUPT
;CALL INTER_RUPT
;GOTO INT2
;INT3
;CALL READ_HR
;MOVF TIME_TEMP,W
;SUBWF 0X2D,W
;BTFSS STATUS,Z
;GOTO CHECK_INT
;BCF STATUS,C
;BSF PORTB,7 ; turn on buzzer
;BSF PORTB,6 ;turn on led
;MOVF 0X2D,W
;MOVWF TIME_TEMP1
;PAGESEL INTER_RUPT
;CALL INTER_RUPT
;GOTO INT3
;INT4
; CALL READ_HR
; MOVF TIME_TEMP,W
; SUBWF 0X2E,W
; BTFSS STATUS,Z
; GOTO CHECK_INT
; BCF STATUS,C
; BSF PORTB,7 ; turn onbuzzer
; BSF PORTB,6 ; TURN ON led
; MOVF 0X2E,W
; MOVWF TIME_TEMP1
; PAGESEL INTER_RUPT
; CALL INTER_RUPT
; GOTO INT4


;-------------END OF CHECK INTERVAL----------------------------------------------
READ_HR
CALL WRITE_EN
MOVLW 0X85 ;READ HOUR

movlw REGWRITE
call WRITE_RTC
bsf STATUS,RP0
movlw b'00000100'
movwf TRISC ; set b'111100' as output port
bcf STATUS,RP0 ; set register bank 0
call READ_RTC
nop ; must give sometime to RTC
bcf CE
nop
nop
RETURN


READ_MIN
CALL WRITE_EN
MOVLW 0X83 ;READ min

movlw REGWRITE
call WRITE_RTC
bsf STATUS,RP0
movlw b'00000100'
movwf TRISC ; set b'111100' as output port
bcf STATUS,RP0 ; set register bank 0
call READ_RTC
nop ; must give sometime to RTC
bcf CE
nop
nop
RETURN
READ_SEC
CALL WRITE_EN
MOVLW 0X81 ;READ sec

movlw REGWRITE
call WRITE_RTC
bsf STATUS,RP0
movlw b'00000100'
movwf TRISC ; set b'111100' as output port
bcf STATUS,RP0 ; set register bank 0
call READ_RTC
nop ; must give sometime to RTC
bcf CE
nop
nop
RETURN
;-----------------------------------------------------------
READ_DAY
CALL WRITE_EN
MOVLW 0X8B
MOVWF REGWRITE
CALL WRITE_RTC
BSF STATUS,RP0
MOVLW B'00000100'
MOVWF TRISC ; SET B'111100' AS OUTPUT PORT
BCF STATUS,RP0 ;SET REGISTER BANK 0
CALL READ_RTC
NOP
BCF CE
NOP ; MUST GIVE SOMETIME TO RTC
NOP ;
RETURN
;--------------WRITE SUBROUTINE-----------------------
WRITE_EN
CLRF PORTC ;CE = 0, SCLK=0

BSF STATUS,RP0 ;SET REGISTER BANK 1
MOVLW B'000000'
MOVWF TRISC ; SET B'00000000' AS OUTPUT PORT
BCF STATUS,RP0 ;SET REGISTER BANK 0
BSF CE ; CE=1
RETURN
;--------------WRITE DATA-------------------------------

WRITE_RTC
MOVLW 0X08
MOVWF TIME_COUNT
WRITE_LOOP
BCF SCLK ;SCLK =0
BSF IO
BTFSS REGWRITE,0
BCF IO ;SET IO=1
NOP ;DO NOTHING
BSF SCLK ;SCLK =1
RRF REGWRITE,1
DECFSZ TIME_COUNT,1
GOTO WRITE_LOOP
RETURN
;---------------------------READ SUBROUTINE--------------
READ_RTC
clrf TIME_TEMP
MOVLW 0X08
MOVWF TIME_COUNT
READ_LOOP
BSF SCLK
bsf STATUS,C
BTFSS IO
BCF STATUS,C
BCF SCLK
RRF TIME_TEMP,1
DECFSZ TIME_COUNT,1
GOTO READ_LOOP
MOVF TIME_TEMP, W
RETURN
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top