PDA

View Full Version : Another RTC, DS1287



DavidK
- 12th December 2006, 17:07
Hi
My first post !!
Well I was doing some cleaning of the work bench and found a bag of Old Dallas DS1287 chips that I got from a surplus store. Years ago I did some Basic Stamp code to talk to this chip. The DS1287 has a built in crystal and battery. This chip can be found on many old PC motherboards. My stamp clock ran for many years, but got pushed back on the bench for other projects. A couple of weeks ago I ported the code to a 16F873 using my bootloader. The DS1287 is not Y2K compliant, but the clock function works fine. The compile is less than 1K, so once you get the chip working there are many other things one can do with this project. So here is some code to play with.
THX, David

'************************************************* ***************
'* Name : DS1287_4_LCDBP.BAS *
'* Author : DavidK, EZENUF Solutions *
'* E-mail..... *
'* Notice : Copyright (c) 2006 *
'* : All Rights Reserved *
'* Date : 6/13/2006 *
'* Version : 4.0 *
'* Notes : Real Time Clock Demonstration using an old DS1287 *
'* : *
'************************************************* ***************

' -----[ Connections ]----------------------------------------------------

' 1287 (Function) 16F873 Other
' --------------------- ------ -----
' pin 1 MOT-------------------------GND
' pin 2 N/C
' pin 3 N/C
' pin 4 AD0-------------PortB.0 (PIN 21) 10K pullup to Vcc
' pin 5 AD1-------------PortB.1 (PIN 22) 10K pullup to Vcc
' pin 6 AD2-------------PortB.2 (PIN 23) 10K pullup to Vcc
' pin 7 AD3-------------PortB.3 (PIN 23) 10K pullup to Vcc
' pin 8 AD4-------------PortB.4 (PIN 24) 10K pullup to Vcc
' pin 9 AD5-------------PortB.5 (PIN 25) 10K pullup to Vcc
' pin 10 AD6-------------PortB.6 (PIN 26) 10K pullup to Vcc
' pin 11 AD7-------------PortB.7 (PIN 27) 10K pullup to Vcc
' pin 12 gnd-------------------------GND
' pin 13 CS--------------PortA.4 (PIN 6)
' pin 14 AS--------------PortA.3 (PIN 5)
' pin 15 R/W-------------PortA.2 (PIN 4)
' pin 16 N/C
' pin 17 DS--------------PortA.1 (PIN 3)
' pin 18 RST-------------------------5VDC
' pin 19 IRQ-------------
' pin 20 N/C
' pin 21 N/C PortC.0 (PIN 11) ------ Serial LCD Backpack
' pin 22 N/C
' pin 23 SQW--[2 Hz beat]------------neg of LED, plus 5VDC via 470 ohm
' pin 24 Vcc-------------------------5VDC
'
' -----[ Program Description ]---------------------------------------------
' Used an 16F873 with a 20 Mhz bootloader
' This program demonstrates the various standard features of a Dallas
' DS1287 Real Time Clock (RTC). This RTC chip is self contained with a
' battery and 32Khz xtal. The DS1287 is obsolete and is not Y2K compliant, but
' the clock function works just fine. You can find lots of them on old PC
' motherboards in the surplus stores. I have not tried the new DS12887 which is
' Y2K compliant. This code should get you in the ball park.
'
' This program initializes the various configuration registers, sets the time
' and reads the time for display on my 4 line LCD backpack. An LED is initialized
' at a 2Hz rate to indicate a "heart-beat." Note: Sometimes the internal
' battery has been exhausted. If you do not get the "heartbeat" that may be the
' problem. The DS1287 is setup for operation in the Intel mode, pin 1 to GND. I
' used a "bootloader" to load the program. BTW, when you hook up you PC serial
' port to the 16F873 serial port it might reset the processor or freeze it up.
' You might have to wait 15 seconds for the timeout for the time loading function.
' I have not tried the NVRAM function, but the basic code should be the same.
' One might try porting this code to a 16F628, but you loose the HSERout/HSERin
' functions to set the time. Initial design was for a Basic Stamp II that I
' built in November 1997. It ran for several years. Recently updated the code
' to work on a PIC. This code compiles to less than 1K, so there is room to add
' code for other things like alarm clock, temperature, humidity, scheduled clock
' updates from a GPS reciever or an RS-485 distribution network.
'
' -----[ Revision History ]------------------------------------------------
' Ported from 1997 Stamp Code

' -----[ I/O Definitions ]-------------------------------------------------
'
define LOADER_USED 1
define OSC 20

Include "MODEDEFS.BAS" ' Include Shiftin/out modes

ADCON1=%00000111 ' Disable Comparators diasable for 16F873
OPTION_REG.7=0 ' Enable Weak Pull-Ups

' -----[ Variables ]-------------------------------------------------------
DIN VAR Byte
SETUP VAR BYTE
REG VAR byte
DISPo VAR PortA.0 ' serial Pin out to drive serial LCD backpack
DS var PortA.1
R_W var PortA.2
AS var PortA.3
CS var PortA.4
hr var byte
mins var byte
sec var byte

' -----[ Constants ]-------------------------------------------------------

CLR CON 1 ' CLR LCD command
BAUD CON 16780 ' N2400 for serial LCD, driven/inverted/none
LINE1 CON 128 ' LCD line #1
LINE2 CON 192 ' LCD line #2
LINE3 CON 148 ' LCD line #3
LINE4 CON 212 ' LCD line #4
INS CON 254 ' LCD command mode parameter

' -----[ EEPROM Data ]-----------------------------------------------------


' -----[ Initialization ]--------------------------------------------------
' This first part will start the clock and start the 2Hz "heart-beat.

TRISA = %00000000 ' all Port A pins are outputs
TRISB = %00000000 ' all Port B pins are outputs
PORTA = %00010000 ' all pins LOW, CS high

HSEROUT [" Welcome to", 10,13]
HSEROUT [" EZ ENUF Solutions", 10,13]
HSEROUT [" copyright 2006", 10,13 ]
Pause 2000 ' Wait for logo display, sorry my credits

' Initialize RTC Registers A and B, Registers C and D are read only
'
'goto begin ' bypasses initialization, comment out to set clock
INIA: REG = 10 ' A register = 10
SETUP = %00101111 ' a pattern of 010 (bits 5-7) turns clock on, a pattern
' of 1111 (bits 1-4) set heartbeats to 2 Hz
'SETUP = %01111111 ' BTW, this pattern turns clock off
GOSUB TX 'send out

INIB: REG = 11 ' B register = 11
SETUP = %00001111 ' 15 DEC, 0F HEX
GOSUB TX ' send out

' Here you enter the time, make sure you enter two digits
' example 10 02 45 yields 10:02:45 am. After you have set the time on the
' DS1287, I suggest that you uncomment "GOTO begin" (above) and load the program
' again. If not, you have to wait 15 seconds for the LCD to display if a
' microprocessor reset is pushed at a later date. I use a simple terminal program
' like ProComm or the Serial Communicator in MicroCode Studio to talk
' through "HSERin/HSERout" (bootloader) port. My ProComm serial port is setup
' for COM1,2400,NONE,8,1,DUMB TERMINAL. Hyperterm may lockup the bootloader.

hour:
HSEROUT ["Enter hour", 10,13]
hserin 5000,minutes,[DEC2 setup]
reg = 4 ' hour register
GOSUB TX ' load hour register

minutes:
HSEROUT ["Enter minutes", 10,13]
hserin 5000,seconds,[DEC2 setup]
reg = 2 ' minutes register
GOSUB TX ' load minutes register

seconds:
HSEROUT ["Enter seconds", 10,13]
hserin 5000,begin,[DEC2 setup]
reg = 0 ' seconds register
GOSUB TX ' load seconds register

' -----[ Main Program ]--------------------------------------------------

Begin:
SEROUT2 PORTC.0,BAUD, [ins,clr,ins,line1,"The Time Is"] 'display 1st line

Start:
REG = 12 'register C, bit 4 is set every second
GOSUB RX 'wait until it is set before reading
IF din = 16 THEN READREG 'the registers 0 thru 9
GOTO start


READreg:
REG = 4 'register 0 is hours register
GOSUB RX 'read in seconds register
hr = din

REG = 2 'register 0 is minutes register
GOSUB RX 'read in seconds register
mins = din

REG = 0 'register 0 is seconds register
GOSUB RX 'read in seconds register
sec = din

hserout [dec hr,":",dec mins,":",dec sec, 10, 13]
SEROUT2 PORTC.0,BAUD, [ins,line2,dec hr,":",dec mins,":",dec sec]

goto start


' -----[ Subroutines ]-----------------------------------------------------
'
TX:
TRISB = %00000000 ' all Port B pins are outputs
PORTA = %00010000 ' all pins LOW, CS high
LOW CS
HIGH DS
HIGH R_W
PORTB = REG
PULSOUT AS,100
LOW R_W
'READ POINTR,SETUP
PORTB = SETUP
HIGH R_W
HIGH CS
RETURN

RX:
TRISB = %00000000 ' all Port B pins are outputs
PORTA = %00010000 ' all pins LOW, CS high
LOW CS
HIGH DS
HIGH R_W
PORTB = REG
PULSOUT AS,100
LOW DS
TRISB = %11111111 ' all Port B pins are inputs
DIN = PORTB
HIGH DS
HIGH CS
RETURN

End