PDA

View Full Version : example code for DS1337



TONIGALEA
- 13th August 2004, 07:25
Hello Folks,
i really do need your help on this one as i have being strugling
for the the past few days .
Here is what i am trying to do.
i want to use the DS1337 to display Time and date which i have
managed to do as i upgraded from ds1307 .
The part i struglling with is the alarm part as i haven't used this
before .
i want to set the alarm intb which i connected a led to when the
after 5 minutes
How do i check to know the alarm time is up ?
i have tried everything (i know) with no joy so if anyone has
used this Ds1337 please give me a helping hand


Toni


DEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"
Define OSC 20
clear

' Setup Hardware for uart
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1


DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line

DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)


@ device pic16F877, XT_osc, wdt_off, pwrt_on, protect_off
'** DEFINE LCD Control Constants **
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2

' Clock Defines
' ----------------
DS_SCL VAR PORTC.3 ' I2C clock pin
DS_SDA VAR PORTC.4 ' I2C data pin
RTC CON %11010000 ' RTC device address (byte addressing)

'Setting the RTC so we can get out 1Hz tick output
'-------------------------------------------------
' -------------- RTC definitions -----------------
SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
ContReg CON $0E ' control register
StatusReg CON $0F ' STATUS register
RTCflag CON 0 ' RTC flag (location 0 of internal EEPROM)
RTCset VAR BIT ' bit to check if RTC has been set
cntrl CON %00000110 ' sets the SQW/OUT to 1Hz pulse, logic level low
'Sta CON %00000000
' The variable below holds the values entered:
' entered by the user




sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year

'ALARM VARIABLES
'===============

A_MINs VAR BYTE ' minutes
A_hr VAR BYTE ' hours

'
' Initialise Hardware
' -------------------
ADCON1=%00000111
TRISA =%00100000
TRISB =%11100111
TRISC =%10000001
TRISD =%00000000


Pause 1000
LCDOut I,Clr:Pause 30
LCDOut I,Line1+2," RTC TEST "
LCDOut I,Line2+2,"..Power On.. !!"

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Loop:


I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A_mins,A_hr]

' Read Ds1337
' Display Section
' ---------------
LCDOut I,Clr
LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
'LCDOut I,Line2,"Date : ",HEX2 date,"-",HEX2 mon,"-",HEX2 yr
LCDOut I,Line2,"Alarm: ",HEX2 A_hr, ":", HEX2 A_MINs

Pause 500 ' Allow Time for user to view
GoTo Loop

End



SetTimeAndDate:
'=============
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0

' The constants below set the RTC to: 16:30:00 on 13-08-2004
yr=$04
mon=$08
date=$13
hr=$16
sec=$00
mins=$30
'SET ALARM FOR 16:35:00
A_hr=$16
A_mins=$35

I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr,cntrl]

Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, Alm2min,[A_mins,A_hr,cntrl]

Pause 10
Return

TONIGALEA
- 13th August 2004, 13:27
Do i have to read the status register using 12C command
or just read the value of the register ?
i tried to see if bit 1 is set .
i did it as below but its i just get the value of the register as
%00001111 and it dosen't change when the time gets to the
alarm time.

DEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"
Define OSC 20
clear

' Setup Hardware for uart
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1


DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line

DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)


@ device pic16F877, XT_osc, wdt_off, pwrt_on, protect_off
'** DEFINE LCD Control Constants **
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2

' Clock Defines
' ----------------
DS_SCL VAR PORTC.3 ' I2C clock pin
DS_SDA VAR PORTC.4 ' I2C data pin
RTC CON %11010000 ' RTC device address (byte addressing)

'Setting the RTC so we can get out 1Hz tick output
'-------------------------------------------------
' -------------- RTC definitions -----------------
SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)
'Alarm 1
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)
'Alarm2
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
ContReg CON $0E ' control register
StatusReg CON $0F ' STATUS register
RTCflag CON 0 ' RTC flag (location 0 of internal EEPROM)
RTCset VAR BIT ' bit to check if RTC has been set
cntrl CON %00000110 ' sets the SQW/OUT to 1Hz pulse, logic level low
'set interupt on INTB

sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year
STAD VAR BYTE
'ALARM VARIABLES
'===============

A_MINs VAR BYTE ' minutes
A_hr VAR BYTE ' hours

'
' Initialise Hardware
' -------------------
ADCON1=%00000111
TRISA =%00100000
TRISB =%11100111
TRISC =%10000001
TRISD =%00000000


Pause 1000
LCDOut I,Clr:Pause 30
LCDOut I,Line1+2," RTC TEST "
LCDOut I,Line2+2,"..Power On.. !!"

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Loop:


I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]

pause 10


' Read Ds1337
' Display Section
' ---------------
LCDOut I,Clr
LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
LCDOut I,Line2,"STATUS ",BIN8 StatusReg
Pause 500 ' Allow Time for user to view
GoTo Loop

End



SetTimeAndDate:
'=============
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0

' The constants below set the RTC to: 16:30:40 on 13-08-2004
yr=$04
mon=$08
date=$13
hr=$16
sec=$40
mins=$30
'SET ALARM FOR 16:31:00
A_hr=$16
A_mins=$31

'Set the Time
I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr,cntrl]
Pause 10
'Set the Alarm 1 Time
I2CWrite DS_SDA, DS_SCL, RTC, Alm2min,[A_mins,A_hr,cntrl]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[$00,cntrl]
PAUSE 10
Return




Toni

TONIGALEA
- 14th August 2004, 13:49
i am now trying to use the INTA but i still get the same thing
the pin doeen't pull low when the time time matchs that of the
alarm.
Am i doing anything right at all ?
i have followed the data sheet as below

http://www.allic.net/resource/ds1337.pdf

Toni



DEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"
Define OSC 20
clear

' Setup Hardware for uart
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

' LCD DEFINES
'============
DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line
DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)

'** DEFINE LCD Control Constants **
'====================================
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2

@ device pic16F877, XT_osc, wdt_off, pwrt_on, protect_off

' Clock Defines
' ----------------
DS_SCL VAR PORTC.3 ' I2C clock pin
DS_SDA VAR PORTC.4 ' I2C data pin
RTC CON %11010000 ' RTC device address (byte addressing)


' -------------- RTC Address definitions -----------------
SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)

'Alarm 1 Address definitions
'===========================
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)

'Alarm 2 Address definitions
'===========================
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
ContReg CON $0E ' control register
StatusReg CON $0F ' STATUS register

cntrl CON %00000101 ' sets the SQW/OUT to 1Hz pulse, logic level low
'set interupt on INTB (From pg 9 of data sheet)

sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year

'ALARM1 VARIABLES
'===============
A1sec VAR BYTE ' seconds
A1MINs VAR BYTE ' minutes
A1hr VAR BYTE ' hours
A1day VAR BYTE ' day


' Initialise Hardware
' -------------------
ADCON1=7
TRISA =%00100000
TRISB =%11100111
TRISC =%10000001
TRISD =%00000000


Pause 1000
LCDOut I,Clr:Pause 30
LCDOut I,Line1+2," RTC TEST "
LCDOut I,Line2+2,"..Power On.. !!"

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Loop:


I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
I2CRead DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
pause 10


' Read Ds1337
' Display Section
' ---------------
LCDOut I,Clr
LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
LCDOut I,Line2,"Alarm: ",HEX2 A1hr, ":", HEX2 A1MINs, ":", HEX2 A1sec
Pause 500 ' Allow Time for user to view
GoTo Loop

End



SetTimeAndDate:
'=============
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0
A1sec=0:A1MINs=0:A1hr=0: A1day=0

' The constants below set the RTC to: 16:30:40 on 13-08-2004
yr=$04
mon=$08
date=$13
day=$01
hr=$16
sec=$40
mins=$30
'SET ALARM FOR 16:31:00
A1hr=$16
A1MINs=$31
A1sec=$0
A1day=$13


'Set the Time
I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr]
Pause 10
'Set the Alarm 1 Time



I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10
I2CRead DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
'A1sec.7=0
'A1MINs.7=1
'A1hr.7=1
'A1day.7=1
'Set DT/DT bit 6 of alarm reg to date by writting 0 to it
I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC,Alm1Day ,[$01]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00000110]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[$00]
PAUSE 10
Return

Bruce
- 15th August 2004, 20:16
Hi Toni,

I just looked at the DS1337 datasheet, and it looks like you need to write to bit 7 in all of the alarm1 registers to configure the alarm1 mask bits.

A1hr=$16 ' = %00010110
A1MINs=$31 ' = %00110001
A1sec=$0 ' = %00000000
A1day=$13 ' = %00010011

I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]

Without setting bits A1M1, A1M2, A1M3, A1M4 and bit 6 in DY/DT registers your alarm output should only happen when Date, Hours, Minutes and Seconds match.

Refer to Table 2. ALarm Mask Bits in the datasheet. If I'm interpreting the datasheet correctly, to have the alarm trigger when seconds match you would need to add 128 to whatever values you're writing to A1M2 (minutes), A1M3 (hour) and A1M4 (day/date).

See if this works for triggering alarm1 when seconds match. I've just added 128d to each of your previous values below A1hr.

' A1hr=$16 ' = %00010110
' A1MINs=$31 ' = %00110001
' A1sec=$0 ' = %00000000
' A1day=$13 ' = %00010011


A1hr=%00010110
A1MINs=%10110001
A1sec=%10000000
A1day=%10010011

I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]

This looks like an interesting RTC. I just ordered a few samples to tinker with. Maybe I can offer more help with a code example or two when I have one to mess with.

TONIGALEA
- 18th August 2004, 21:52
EXCELLENT Bruce
Works like a dream

toni

Santana
- 27th October 2004, 23:08
Hello Bruce & Toni
i need your help in triggering the alarm on the DS1337
i connected led to pin 3 of the RTC but each time the alarm time is reachDEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"
Define OSC 20
clear

@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H

' Setup Hardware for uart
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

' LCD DEFINES
'============
DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line
DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)

'** DEFINE LCD Control Constants **
'====================================
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2


' Clock Defines
' ----------------
DS_SCL VAR PORTC.3 ' I2C clock pin
DS_SDA VAR PORTC.4 ' I2C data pin
RTC CON %11010000 ' RTC device address (byte addressing)


' -------------- RTC Address definitions -----------------
SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)

'Alarm 1 Address definitions
'===========================
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)

'Alarm 2 Address definitions
'===========================
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
ContReg CON $0E ' control register
StatusReg CON $0F ' STATUS register

cntrl CON %00000101 ' sets the SQW/OUT to 1Hz pulse, logic level low
'set interupt on INTB (From pg 9 of data sheet)

sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year

'ALARM1 VARIABLES
'===============
A1sec VAR BYTE ' seconds
A1MINs VAR BYTE ' minutes
A1hr VAR BYTE ' hours
A1day VAR BYTE ' day


' Initialise Hardware
' -------------------
ADCON1=7
TRISA =%00100000
TRISB =%11100111
TRISC =%10000001
TRISD =%00000000


Pause 1000
LCDOut I,Clr:Pause 30
LCDOut I,Line1+2," DS1337 TEST "
LCDOut I,Line2+2,"..Power On.. !!"

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Loop:


I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
I2CRead DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
pause 10


' Read Ds1337
' Display Section
' ---------------
LCDOut I,Clr
LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
LCDOut I,Line2,"Alarm: ",HEX2 A1hr, ":", HEX2 A1MINs, ":", HEX2 A1sec
Pause 500 ' Allow Time for user to view
GoTo Loop

End



SetTimeAndDate:
'=============
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0
A1sec=0:A1MINs=0:A1hr=0: A1day=0

' The constants below set the RTC to: 16:30:40 on 13-08-2004
yr=$04
mon=$08
date=$13
day=$01
hr=$16
sec=$40
mins=$30
'SET ALARM FOR 16:31:00
'A1hr=$16
'A1MINs=$31
'A1sec=$0
'A1day=$01
A1hr=%00010110
A1MINs=%10110001
A1sec=%10000000
A1day=%10010011

'Set the main Time

I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr,cntrl]
Pause 10
'Set the Alarm 1 Time

I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10

I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00000110]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[$00]
PAUSE 10
Return
ed the led still stays ON
could you see where i am going wrong

All the Best
Santana

Bruce
- 28th October 2004, 00:16
Are you clearing the alarm flag in the Status register after servicing the alarm?

Santana
- 28th October 2004, 07:06
Yes Bruce
i do that in the main loop

DEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"
Define OSC 20
clear

@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H

' Setup Hardware for uart
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

' LCD DEFINES
'============
DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line
DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)

'** DEFINE LCD Control Constants **
'====================================
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2


' Clock Defines
' ----------------
DS_SCL VAR PORTC.3 ' I2C clock pin
DS_SDA VAR PORTC.4 ' I2C data pin
RTC CON %11010000 ' RTC device address (byte addressing)


' -------------- RTC Address definitions -----------------
SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)

'Alarm 1 Address definitions
'===========================
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)

'Alarm 2 Address definitions
'===========================
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
ContReg CON $0E ' control register
StatusReg CON $0F ' STATUS register

cntrl CON %00000011 ' sets the SQW/OUT to 1Hz pulse, logic level low
'set interupt on INTB (From pg 9 of data sheet)

sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year

'ALARM1 VARIABLES
'===============
A1sec VAR BYTE ' seconds
A1MINs VAR BYTE ' minutes
A1hr VAR BYTE ' hours
A1day VAR BYTE ' day


' Initialise Hardware
' -------------------
ADCON1=7
TRISA =%00100000
TRISB =%11100111
TRISC =%10000001
TRISD =%00000000


Pause 1000
LCDOut I,Clr:Pause 30
LCDOut I,Line1+2," DS1337 TEST "
LCDOut I,Line2+2,"..Power On.. !!"

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Loop:


I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
I2CRead DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
pause 10


' Read Ds1337
' Display Section
' ---------------
LCDOut I,Clr
LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
LCDOut I,Line2,"Alarm: ",HEX2 A1hr, ":", HEX2 A1MINs, ":", HEX2 A1sec
Pause 500 ' Allow Time for user to view

I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[%00000000]' clear status reg<------
PAUSE 10
GoTo Loop

End



SetTimeAndDate:
'=============
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0
A1sec=0:A1MINs=0:A1hr=0: A1day=0

' The constants below set the RTC to: 16:30:40 on 13-08-2004
yr=$04
mon=$08
date=$13
day=$01
hr=$16
sec=$40
mins=$30
'SET ALARM FOR 16:31:00
A1hr=$16
A1MINs=$31
A1sec=$0
A1day=$01
'A1hr=%00010110
'A1MINs=%10110001
'A1sec=%10000000
'A1day=%10010011

'Set the main Time

I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr,cntrl]
Pause 10
'Set the Alarm 1 Time

I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10

I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00000110]
Pause 10

I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[%00000000]
PAUSE 10
Return

Bruce
- 28th October 2004, 14:07
OK I see that in your second version. In the first you cleared it only on power-up when the GoSub SetTimeAndDate was used.

Are you driving the LED directly from the alarm output pin or using a transistor?

Santana
- 28th October 2004, 17:50
No Bruce
i connected as below:

5v I----/\/\/\/\/\----- alarm1(pin 3 of ds1337)
|
|
--
| |
| | 470R
--
|
|
----
\/ led
|
-----
--- gnd
-

Is there anyone else who has used this RTC, please help if u can

Best Regards

Santana
- 28th October 2004, 17:54
Basically i connected a 10k pullup resistor to 5v then connected the led from pin 3 through a 470R resistor to ground

Bruce
- 28th October 2004, 18:07
Isn't the alarm pin "MAX" output limited to 3mA...?

That may explain why it's not working properly when you attempt to directly drive your LED.

Have you tried removing the LED, and testing the alarm output with a meter or logic probe? Or using a transistor to drive your LED?

Santana
- 28th October 2004, 18:12
yes i have done that
i removed the led and just left the the 10k pull up resistor and then monitored this on a scope but its never pulled down.
but i did find someexample code on maxins web site the problem
is that its written in C
so i am just trying to figure out what the program is doing

http://www.maxim-ic.com/appnotes.cfm/appnote_number/3300

All The Best

Santana
- 28th October 2004, 19:47
This code now whats thanks to you bruce
when the main time match the alarm time an interupt is generated on INTA (PIN 3)
my led goes low then back high .
all the time does not match the led is high

Very Best Regards



DEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"
Define OSC 20
clear

@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H

' Setup Hardware for uart
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

' LCD DEFINES
'============
DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line
DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)

'** DEFINE LCD Control Constants **
'====================================
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2


' Clock Defines
' ----------------
DS_SCL VAR PORTC.3 ' I2C clock pin
DS_SDA VAR PORTC.4 ' I2C data pin
RTC CON %11010000 ' RTC device address (byte addressing)


' -------------- RTC Address definitions -----------------
SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)

'Alarm 1 Address definitions
'===========================
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)

'Alarm 2 Address definitions
'===========================
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)
ContReg CON $0E ' control register
StatusReg CON $0F ' STATUS register

cntrl CON %00000011 ' sets the SQW/OUT to 1Hz pulse, logic level low
'set interupt on INTB (From pg 9 of data sheet)

sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year

'ALARM1 VARIABLES
'===============
A1sec VAR BYTE ' seconds
A1MINs VAR BYTE ' minutes
A1hr VAR BYTE ' hours
A1day VAR BYTE ' day


' Initialise Hardware
' -------------------
ADCON1=7
TRISA =%00100000
TRISB =%11100111
TRISC =%10000001
TRISD =%00000000


Pause 1000
LCDOut I,Clr:Pause 30
LCDOut I,Line1+2," DS1337 TEST "
LCDOut I,Line2+2,"..Power On.. !!"

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Loop:


I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
I2CRead DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
pause 10


' Read Ds1337
' Display Section
' ---------------
LCDOut I,Clr
LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
LCDOut I,Line2,"Alarm: ",HEX2 A1hr, ":", HEX2 A1MINs, ":", HEX2 A1sec
Pause 500 ' Allow Time for user to view

I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[%00000000]' clear status reg<------
PAUSE 10
GoTo Loop

End



SetTimeAndDate:
'=============
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0
A1sec=0:A1MINs=0:A1hr=0: A1day=0

' The constants below set the RTC to: 16:30:40 on 13-08-2004
'################################################# ############
yr=$04
mon=$08
date=$13
day=$01
hr=$16
sec=$40
mins=$30

'SET ALARM FOR 16:31:50
A1hr=$16
A1MINs=$31
A1sec=$50
A1day=$13

'Set the main Time

I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr]
Pause 10
'Set the Alarm 1 Time



I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[$20]
Pause 10

I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[%00000000]
PAUSE 10



I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[5]
Pause 10
Return

jellis00
- 1st December 2009, 05:10
This code now whats thanks to you bruce
when the main time match the alarm time an interupt is generated on INTA (PIN 3)
my led goes low then back high .

Attempted to adapt your code to the 18F4550 as listed below. Interestingly, the LED I have on Pin3 is low all the time and doesn't go high when the time should be matching the A1 alarm time. Can't figure out why this is or why the alarm doesn't work. Would you please take a look at my code & see if you can see where I am going wrong? Thanks! Will list code in another post since this one exceeds character limits.

jellis00
- 1st December 2009, 05:21
Will list code in another post since this one exceeds character limits.
Here is code:


Include "Modedefs.Bas"
INCLUDE "ALLDIGITAL.pbp" ' Sets all registers for digital ops.

ASM ; 18F2550/4550, 8mhz crystal
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H ; PortB resets as digital
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM

'**--SETUP FOR USING 2x16 LCD THAT IS INSTALLED IN EASYPIC6
'( commented out during testing of DS1337 operations)
'*******************************************
' LCD DEFINES FOR USE WIH 2x16 LCD
'=============================
'DEFINE LCD_DREG PORTB ' Use PortB for LCD Data
'DEFINE LCD_DBIT 0 ' Use lower(4) 4 bits of PortB
' PORTB.0 thru PORTB.3 connects to
' LCD DB4 thru LCD DB-7 respectively
'DEFINE LCD_RSREG PORTB ' PORTB for RegisterSelect (RS) bit
'DEFINE LCD_RSBIT 4 ' PORTB.4 pin for LCD's RS line
'DEFINE LCD_EREG PORTB ' PORTB for Enable (E) bit
'DEFINE LCD_EBIT 5 ' PORTB.5 pin for LCD's E line
'DEFINE LCD_BITS 4 ' Using 4-bit bus
'DEFINE LCD_LINES 2 ' Using 2 line Display
'DEFINE LCD_COMMANDUS 5000 ' Command Delay (uS)
'DEFINE LCD_DATAUS 200 ' Data Delay (uS)

' DEFINE LCD Control Constants
'========================
'I CON 254 ' Control Byte
'Clr CON 1 ' Clear the display
'Line1 CON 128 ' Point to beginning of line 1
'Line2 CON 192 ' Point to beginning of line 2

'**SETUP FOR USING DS1337 Real Time Clock-
'**********************************
' Setup Hardware for uart
' ==================
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

' Aliased Variables for CLock
' ====================
Alarm1 VAR PORTB.4 ' Output of Alarm1 signal
Alarm2 VAR PORTB.5 ' Output of Alarm2 signal
DS_SCL VAR PORTB.1 ' I2C clock pin
DS_SDA VAR PORTB.0 ' I2C data pin
RTC CON %11010000 ' RTC device write address(byte addressing)
'contrl CON %00000000 ' Starts oscillar, sets the SQW/OUT to
' 1Hz pulse, no Alarm interrupt enabled.
'contrl CON %00000001 ' Starts oscillator, sets the SQW/OUT to
' 1 HZ pulse, & enables INTA interrupt from
' A1F.
' w/ logic level low (see data sheet pg 10)
'contrl CON %00000010 ' Starts oscillator, sets the SQW/OUT to
' 1 HZ pulse, enables INTA interrupt from
' A2F.
contrl CON %00000011 ' Starts the oscillator, sets the SQW/OUT
' to 1 Hz, enables INTA interrupt from A2F
' or INTA interrupt from A1F.
'contrl CON %000xx100 ' Starts oscillator, no interrupts enabled.
'contrl CON %000xx101 ' Starts oscillator, enables INTA interrupt
' from A1F.
'contrl CON %000xx110 ' Starts oscillator, enables INTB interrupt
' from A2F.
'contrl CON %00000111 ' Starts oscillator, enables INTA interrupt
' from A1F or INTB from A2F.

' -------------- RTC Address definitions
SecReg CON $00 ' seconds address (00 - 59)
' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)

'Alarm 1 Address definitions
'====================
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)

'Alarm 2 Address definitions
'===================
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)

' Alias of Clock register addresses
'========================
ContReg CON $0E ' CONTROL register address
StatusReg CON $0F ' STATUS register address

' Clock Variables
'===========
sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year

'ALARM1 VARIABLES
'==============
A1sec VAR BYTE ' seconds
A1MINs VAR BYTE ' minutes
A1hr VAR BYTE ' hours
A1day VAR BYTE ' day

'ALARM2 VARIABLES
'==============
A2MINs VAR BYTE ' minutes
A2hr VAR BYTE ' hours
A2day VAR BYTE ' day

' Initialize Hardware
' -----------------
'Set registers
TRISA =%00000000
TRISB =%00000000
TRISC =%00000000
TRISD =%00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
INTCON2.7 = 0

' Initialize Display
' Display functions commented out due to RB0 & RB1 conflict with I2C
'Pause 1000 ' Delay to let LCD start up
'LCDOut I,CLR:Pause 50 ' Clear Display
'LCDOut I,Line1+2," RTC TEST " ' Display "RTC TEST" on 1st line
'Pause 500
'LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
'Pause 1000

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Main:
' Read the current time and ALARM settings from the DS1337
I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
Pause 10
I2CREAD DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 10
I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
pause 10

' Display Time and ALARM2 settings
'LCDOut I,Clr:Pause 50
'LCDOut I,Line1,"Time: ",HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec
'PAUSE 500
'LCDOut I,Line2,"Alarm: ",HEX2 A2hr, ":", HEX2 A2MINs, ":", HEX2 $0
'Pause 500 ' Allow Time for user to view
I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[$00] ' Clear STATUS register
PAUSE 10
GoTo Main
End

' START LIST OF SUBROUTINES
'***********************
'DisplayTest: 'Commented out during test of DS1337 code
'============
'LCDOut I,CLR:Pause 50 ' Clear Display
'LCDOut I,Line1+2," TEST " ' Display "TEST" on 1st line
'PAUSE 500
'LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
'Pause 1000
'RETURN

SetTimeAndDate:
'============
' Initialize variables to 0
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0
A1sec=0:A1MINs=0:A1hr=0:A1Day=0
A2MINs=0:A2hr=0:A2day=0
' The constants below set the RTC to: 16:30:00 on 11-28-2009
yr=$09
mon=$11
date=$28
day=$02
hr=$16
mins=$30
sec=$00
'Define ALARM1 FOR 16:31:00 & to alarm once per second
' (A1xxx + $80 or A1xxx + 128d sets bit 7 in AIM1,A1M2,A1M3,A1M4 for 1Hz)
A1hr = $16 + $80 '$16 + $80 = $96 = 150d = %10010110
'Bit6 must be 0 for 24 hour clock
A1MINs = $31 + $80 '$31 + $80 = $B1 = 177d = %10110001
A1sec = $00 + $80 '$00 + $80 = $80 = 128d = $10000000
A1day = $28 + $80 '$28 + $80 = $A8 = 168d = $10101000
'DY/_DT Bit6 = 0 for using Day of month alarm
'Define ALARM2 FOR 16:32:00 'Commented out during DS1337 testing of 1Hz
'A2hr=$16
'A2MINs=$32
'A2day=$28 'DY/_DT Bit6 = 0 for using Day of month alarm
'Set the main Time
I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr]
Pause 10
'Set the Alarm 1 Time
I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00000001] 'Clears INTCN, RS1
'& RS2 to enable 1Hz pulse &
' INTA interrupt.
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
' Sets Alarm1 once per second
Pause 10
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
Pause 10
'Set the Alarm 2 Time ' Commented out during test of DS1337 1Hz
'I2CWrite DS_SDA, DS_SCL, RTC,Alm2min,[A2MINs,A2hr,A2day,contrl]
'Pause 10
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
' Sets Alarm2 once per second
'A2MINs.7 =1 for A2M2 = 1
'A2hr.7 =1 for A2M3 = 1
'A2day.7 =1 for A2M4 = 1
'Set DT/DT bit 6 of alarm reg to date by writting 0 to it
I2CWrite DS_SDA, DS_SCL, RTC,Alm2min,[A2MINs,A2hr,A2day]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC,Alm2Day ,[$01]
Pause 10
'I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[contrl]
Pause 10
I2CWrite DS_SDA, DS_SCL, RTC, StatusReg,[$00] ' Clear STATUS register
PAUSE 10
Return

BobK
- 1st December 2009, 12:10
Hi Jellis,

I only have a few minutes before I need to go to work but I use the DS1337 quite a bit and this is all I use to set the time information and to display it. Nothing else and it works great. I will check back tonight when I get home.



SetTimeAndDate:
yr=0:date =0:mon =0:day=0:hr=0:mins=0:sec=0 'Clear RTC variables
A1sec=0:A1MINs=0:A1hr=0:A1day=0
A2MINS=0:A2hr=0:A2day=0
yr=$08 'This section sets the RTC Variables
mon=$06
date=$02
day=$01
hr=$19
mins=$20
sec=$30
A1hr=$10
A1MINS=$00
A1SEC=$00
A1day=%10000000
A2hr=$22
A2MINS=$00
A2day=%10000000
Pause 10
wRITE 5,1 'Write TO eeprom DST flag value (0 = EST 1 = DST)
pause 20
I2CWrite SDA, SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr] '**Set the main Time
Pause 20
I2CWrite SDA, SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day] '**Set Alarm1 Time
Pause 20
I2CWrite SDA, SCL, RTC, Alm2min,[A2MINs,A2hr,A2day] '**Set Alarm2 Time
Pause 20
I2CWrite SDA, SCL, RTC, StatusReg,[%00000000] '**Clear Status Register
PAUSE 20
I2CWrite SDA, SCL, RTC, ContReg,[%00000111] '**Set Control Register for Alarm1 & Alarm2 outputs separately
Pause 20
Return

' This routine resets the status register after an alarm event
Clear_Status:
I2CWrite SDA, SCL, RTC, StatusReg,[%00000000] 'Reset Alarm output from DS1337
PAUSE 20
return

Display_Time:
B2 = 0
I2CRead SDA, SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr] '**Get current time
pause 20
I2Cread SDA, SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day] '**Get Alarm1 Time
Pause 20
I2Cread SDA, SCL, RTC, Alm2min,[A2MINs,A2hr,A2day] '**Get Alarm2 Time
serout2 LCD,Baud,[I,Clr]
serout2 LCD,Baud,[I,Line1," ",hex2 mon,"/",hex2 date,"/",hex2 yr," ",HEX2 hr,":",HEX2 MINs,":",Hex2 sec]
Serout2 LCD,Baud,[I,Line2,"Alarms: ",HEX2 A1hr,":",hex2 A1MINs," ",Hex2 A2hr,":",Hex2 A2MINs]


HTH,

BobK

BobK
- 1st December 2009, 22:08
Hi Jellis,

I finally got a chance to look at your program. You have the pins of PortB setup for the LCD and you are also using PortB4. & 5 for the RTC alarms and PortB.0 and 1 for the I2C connections. You need to change them and set the RTC connections on a different Port.

BobK

jellis00
- 1st December 2009, 22:13
I only have a few minutes before I need to go to work but I use the DS1337 quite a bit and this is all I use to set the time information and to display it. Nothing else and it works great. I will check back tonight when I get home.

Thanks, BobK. I studied your code you posted and made some changes in my code to reflect what I saw. I have listed my latest code below. Also added a DEFINE OSC 48 statement that was pointed out to me by someone else as needed since I am using 48 MHz CPU clock derived from the 96 MHz clock for a USB interface I will also be adding to this code at later time.
I also notice an anomally in my breadboard setup of the DS1337 circuit when I connect my oscilloscope to the SQW/INTB output:
1) When power is applied to the DS1337 circuit but the code is erased from my controlling 18F4550 MCU, I see a 32.7787kHz clock signal on my oscilloscope at the SQW output of the DS1337.
2) When I then program the 18F4550 the clock signal on SQW output disappears (flat line at 0 volts).
This appears to be opposite to what should be happening. The clock should appear at SQW when the MCU is programmed, time has been set and DS1337 is clocking....this isn't happening.
I also checked the INTA output to see if the Alarm1 was happening 30 seconds after programming and setting the DS1337 time, and it isn't happening either.
Any ideas what may be causing all this?


Include "Modedefs.Bas"
INCLUDE "ALLDIGITAL.pbp" ' Sets all registers for digital ops.
DEFINE OSC 48

;--- if you un-comment these, you must comment the ones in the .inc file--
ASM ; 18F2550/4550, 8mhz crystal
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H ; PortB resets as digital
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM

'**--SETUP FOR USING 2x16 LCD THAT IS INSTALLED IN EASYPIC6----------
'( commented out during testing of DS1337 operations)
'************************************************* *******************
' LCD DEFINES FOR USE WIH 2x16 LCD..commented out during testing

' DEFINE LCD Control Constants **
'====================================
'I CON 254 ' Control Byte
'Clr CON 1 ' Clear the display
'Line1 CON 128 ' Point to beginning of line 1
'Line2 CON 192 ' Point to beginning of line 2

'**-------------SETUP FOR USING DS1337 Real Time Clock------------------
' Setup Hardware for uart
' ==================
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

' Aliased Variables for CLock
' ====================
Alarm1 VAR PORTB.4 ' Output of Alarm1 signal
Alarm2 VAR PORTB.5 ' Output of Alarm2 signal
DS_SCL VAR PORTB.1 ' I2C clock pin
DS_SDA VAR PORTB.0 ' I2C data pin
RTC CON %11010000 ' RTC device write address(byte addressing)
'contrl CON %00011001 ' Sets RS1 & RS2,
' clears INTCN to enable Alarm1 & 3.2768kHz
' outputs separately.

' -------------- RTC Address definitions
SecReg CON $00 ' seconds address (00 - 59)
' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)

'Alarm 1 Address definitions
'=================
Alm1sec CON $07 ' Alarm 1 seconds address (00 - 59)
Alm1min CON $08 ' Alarm 1 minutes address (00 - 59)
Alm1hr CON $09 ' Alarm 1 hours address (01 - 12) or (00 - 23)
Alm1Day CON $0A ' Alarm 1 day address (1 - 7)

'Alarm 2 Address definitions
'==================
Alm2min CON $0B ' Alarm 2 minutes address (00 - 59)
Alm2hr CON $0C ' Alarm 2 hours address (01 - 12) or (00 - 23)
Alm2Day CON $0D ' Alarm 2 day address (1 - 7)

' Alias of Clock register addresses
'========================
ContReg CON $0E ' CONTROL register address
StatusReg CON $0F ' STATUS register address

' Clock Variables
'=========
sec VAR BYTE ' seconds
MINs VAR BYTE ' minutes
hr VAR BYTE ' hours
day VAR BYTE ' day
date VAR BYTE ' date
mon VAR BYTE ' month
yr VAR BYTE ' year

'ALARM1 VARIABLES
'==============
A1sec VAR BYTE ' seconds
A1MINs VAR BYTE ' minutes
A1hr VAR BYTE ' hours
A1day VAR BYTE ' day

'ALARM2 VARIABLES
'==============
A2MINs VAR BYTE ' minutes
A2hr VAR BYTE ' hours
A2day VAR BYTE ' day

' Initialize Hardware
' -------------------
'Set registers
TRISA =%00000000
TRISB =%00000000
TRISC =%00000000
TRISD =%00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
INTCON2.7 = 0

' Initialize Display ' commented out during DS1337 testing
'Pause 1000 ' Delay to let LCD start up
'LCDOut I,CLR:Pause 50 ' Clear Display
'LCDOut I,Line1+2," RTC TEST " ' Display "RTC TEST" on 1st line
'Pause 500
'LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
'PAUSE 1000

GoSub SetTimeAndDate

' Main Program Loop
' -----------------
Main:
' Read the current time and ALARM settings from the DS1337
I2CRead DS_SDA, DS_SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr]
Pause 20
I2CREAD DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 20
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day] ' No Alarm2 during test
'PAUSE 20

' Display Time and ALARM2 settings on the LCD display 'No LCD during test

GoTo Main
End ' Insure program stops if reaches here

' START LIST OF SUBROUTINES
'***********************
'DisplayTest: 'Commented out during test of DS1337 code
'============
'LCDOut I,CLR:Pause 50 ' Clear Display
'LCDOut I,Line1+2," TEST " ' Display "TEST" on 1st line
'PAUSE 500
'LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
'Pause 1000
'RETURN

SetTimeAndDate:
'============
' Initialize clock variables to 0
yr=0:date =0:mon =0:day=0:hr=0:MINs=0:sec=0
A1sec=0:A1MINs=0:A1hr=0:A1Day=0
A2MINs=0:A2hr=0:A2day=0
' The BCD constants below set the RTC to: 19:20:30 on 06-02-2008
yr=$08
mon=$06
date=$02
day=$01
hr=$19
MINs=$20
sec=$30
'Define ALARM1 FOR 19:21:00 & to alarm when hours, minutes, & secs match.
' This requires AIM1=AIM2=AIM3=0 and AIM4=1
' (A1xxx + $80 or A1xxx + 128d sets bit 7 in AIM1,A1M2,A1M3,A1M4)
A1hr = $19 ' for AIM1 = 0
'Bit6 must be 0 for 24 hour clock
A1MINs = $21 'for AIM2 = 0
A1sec = $00 'for AIM3 = 0
A1day = $01 + $80 '$01 + $80 = $81 = 129d = $10000001 for AIM4 = 1
'DY/_DT Bit6 = 0 for using Day of month alarm
'Define ALARM2 FOR 19:22:00 'Commented out during DS1337 testing of 1Hz
'A2hr=$19
'A2MINs=$22
'A2day=$01 'DY/_DT Bit6 = 0 for using Day of month alarm
'Set the main Time
I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr]
Pause 20
'Set the Alarm1 Time
I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
Pause 20
'Set the Alarm2 Time ' Alarm2 Commented out during testing
'I2CWrite DS_SDA, DS_SCL, RTC,Alm2min,[A2MINs,A2hr,A2day,contrl]
'Pause 20
'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
' Clear STATUS and set CONTROl registers
I2CWrite DS_SDA,DS_SCL,RTC,StatusReg,[$00] ' Clear STATUS register
PAUSE 20
I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00011001]' Starts the oscillator, sets the SQW/OUT, enables INTA
Pause 20
Return

BobK
- 2nd December 2009, 00:23
Hi Jellis,

Try this line in place of the Cntrl you are now using:


cntrl CON %00000101 ' sets the SQW/OUT to 1Hz pulse, logic level low


I used a 4.7K pullup on the SQW/INTB pin then into a 1K resistor to the base
of a PNP transistor. Then I used the transistor to drive an LED.

Your current control register setting of %00011001 should cause the 1337 to
output the 32Khz pulse.

I would suggest you focus first on just getting the clock to work then add the
SQW output then the alarm1 features.

Unfortunately I don't have the circuit board with me that I had mine working
on.

Also I have a jumper on my clock board that I put through the uC so when I first powerup my clock it looks to see if the jumper is on (low) and if it is, then I go to the SetTimeand Date routine. If its high it skips over the routine.

Did you look at the wiring I mentioned previously? Do you have 4.7K pullups on the SCL and SDA lines?

BobK

jellis00
- 2nd December 2009, 00:42
Hi Jellis,

I finally got a chance to look at your program. You have the pins of PortB setup for the LCD and you are also using PortB4. & 5 for the RTC alarms and PortB.0 and 1 for the I2C connections. You need to change them and set the RTC connections on a different Port.

BobK
Yes, I am aware of that. It is because the development board I am using (EasyPic6) hardwires PortB RB0/RB1 to the LCD that the board uses, thus making a conflict with the RB0/RB1 pins that are used by my 18F4550 MCU for the I2C interface to the DS1337. That is why I am commenting out all of the LCD related code in this test version, which unfortunately makes it difficult to troubleshoot the DS1337 clock operations without being able to see the outpus on the LCD. I am therefore using my oscilloscope to check the DS1337 pins during ops as my only way to troubleshoot...awkward!
I have another LCD on order (two weeks delivery from Bulgaria) that will permit me to connect it as an external LCD to the EasyPic6 that will resolve this pin conflict, but in mean time I am still trying to troubleshoot code and get the clock working. Any suggestions how to troubleshoot without LCD??

One thing I am wondering....I have the DS1337 circuit with pullup resistors and capacitor installed on an Archer Universal Breadboard with wire connections to the EasyPic6 for the I2C interface and the ALarm1 input....would this be possible cause for the clock not working due to extra capacitance from wiring/breadboard hookup? I wanted to get code working before reducing everything to an integrated PCB.

I will try your contrl suggestion. Thanks for advice!

BobK
- 2nd December 2009, 02:44
Hi Jellis,

Do you have a crystal with a capacitance load of 6pf? If not, the clock won't be very accurate.

Yea it is kind hard to see if the RTC is working without a display. I have a serial LCD that I plug into my clock boards. The clocks run without the LCD but I only plug it in when I'm checking the times and testing the clock. I built a test program into it that lets me change the alarm times to one and two minutes from the current times and at the completion of the test it restores the alarm times to their normal settings and reboots the system. I've had 80 of them in service for almost 3 years now.

I just tried to get a picture of the board to send to you but the limits were exceded. If you send me your email address I'll send you a picture.

BobK

jellis00
- 3rd December 2009, 04:22
Do you have a crystal with a capacitance load of 6pf? If not, the clock won't be very accurate.

I am ordering one with 6pf capacitance now...the one I have been using has 27pf capacitance and I still haven't been able to get the DS1337 to run no matter what CONTROL word I send it. Even wit %00000000, which is no alarms and 32.768kHa on the SQW output, I don't see anything on the SQW output pin or an indication that the clock is running. Frustrating! Hopefully with a LCD I will make some progress in the troubleshooting.


Yea it is kind hard to see if the RTC is working without a display. I have a serial LCD that I plug into my clock boards. The clocks run without the LCD but I only plug it in when I'm checking the times and testing the clock. I built a test program into it that lets me change the alarm times to one and two minutes from the current times and at the completion of the test it restores the alarm times to their normal settings and reboots the system. I've had 80 of them in service for almost 3 years now.

I just tried to get a picture of the board to send to you but the limits were exceded. If you send me your email address I'll send you a picture.

I am ordering a serial serial LCD (delivery in 2 weeks) to resolve the Pin conflicts in my EasyPic6 with the I2C interface (RB0/RB1) from the 18F4550 MCU. Your test program sounds very interesting. How do you initiate the MCU to run just the test program instead of the application program?...is it when you plug something into that the MCU senses like an external PC to MCU serial cable or a USB cable? I would like to consider doing something similar in our application. Would definitely like to see the photo you offer...send to my email at [email protected].

Darrel Taylor
- 3rd December 2009, 06:17
John,

I'm not sure which pin you've connected SQW to, but ...
TRISA =%00000000
TRISB =%00000000
TRISC =%00000000
TRISD =%00000000

One of the first things you do is set almost all the pins to output.
That would stop any signal from SQW.
<br>

mackrackit
- 3rd December 2009, 12:53
If you think it is a capacitor issue and you are ordering more parts you may want to look at the DS1337C. Everything is built in.

What voltage are you running the RTC on?

jellis00
- 8th December 2009, 21:16
I'm not sure which pin you've connected SQW to, but ...
One of the first things you do is set almost all the pins to output.
That would stop any signal from SQW.
<br>
I seem to be having a lot of senior moments lately...this being another one. I should have caught that....once I set TRISB to provide for inputs on the pins I had connected to SQW/_INTB and _INTA the alarms worked as intended, thus indicating to me that current time and alarms were being correctly set at programming time even though I couldn't see that they were because of lack of a LCD.
Thanks again Darrel!