DS3234 RTC Code

Simmicht

Senior Member
Here is a give-back to the forum.
I have the code for setting and reading a DS3234 RTC chip that come on a break out board from the land of SparkFun.

Here is a basic working version. Enjoy.

Code:
#PICAXE 28X2
#terminal 19200
#no_data
#no_table




SYMBOL StdFreq = M16         'Has 4MHz Xtal installed
setfreq StdFreq    


SYMBOL abyte      =         B55   ' W27    
SYMBOL aWord    =        W27

SYMBOL SS =            B10
SYMBOL NN =            B11
SYMBOL HH =            B12
SYMBOL DOW =        B13
SYMBOL DD =            B14
SYMBOL MM =            B15
SYMBOL YY =            B16
SYMBOL T    =        B17
SYMBOL TF    =        B18


'===-[ DS3234 RTC Pins ]-========================================
symbol CS_RTC=             C.2    ' DS3234 RTC chip select
symbol SData =             C.5    ' MAX7219 serial data
symbol CS1     =             B.2    ' MAX7219 chip select
symbol CS2     =             B.6    ' MAX7219 chip select
symbol CS3     =             B.7    ' MAX7219 chip select

SYMBOL MOSI    =             C.5    ' Pin 16 hspi sdO   to MOSI
SYMBOL MISO    =             pinC.4' Pin 15 hspi sdI   to MISO
SYMBOL CLK =             C.3    ' Pin 14 hspi sck   to SCK
    
'-------------------------------------------------
'Variable
Symbol     _Tmp_1         = b0
Symbol     _Tmp_2         = b1
Symbol     _Tmp_Data_Snd     = b2
Symbol     _Tmp_Data_Rcv     = b3
Symbol     _TMP_bit         = b4 ' MISO Bit Value
Symbol     _Register_Name     = b5 ' Register Name
Symbol     _Register_Value_W = W3 ' Register Value as WORD
Symbol     _Register_Value_L = b6 ' Register Value LSB
Symbol     _Register_Value_M = b7 ' Register Value MSB


'set HIGH  CS pin
HIGH CS_RTC





Test1:

_Register_Name=0x81
_Register_Value_L=0x30

'LOW CS_RTC
'SHIFTOUT CLK,SDATA,1,(maxreg,maxdata)
'HIGH CS_RTC



'TRY TO SET the MINUTES to 0 just to see if i can set something
'HIT F5 about 14 seconds before the time you wish to set (on my PC)

SS = 0x30                        'Seconds in BCD
NN = 0x48                        'MINUTES IN BCD
HH = 0x15                        'HOUR       IN BCD    
DOW= 0x03                        'Day of week 1 to 7
DD = 0x07                        'Date of month in BCD
MM = 0x06                        'Month of year in BCD
YY = 0x11                        'YY of the year in BCD


LOW CS_RTC
_Tmp_Data_Snd=0x80
gosub Snd_Rcv
_Tmp_Data_Snd=SS
gosub Snd_Rcv
_Tmp_Data_Snd=NN
gosub Snd_Rcv
_Tmp_Data_Snd=HH
gosub Snd_Rcv
_Tmp_Data_Snd=DOW
gosub Snd_Rcv
_Tmp_Data_Snd=DD
gosub Snd_Rcv
_Tmp_Data_Snd=MM
gosub Snd_Rcv
_Tmp_Data_Snd=YY
gosub Snd_Rcv

HIGH CS_RTC




do




'Read from Temperure registers
    _Register_Name=0x11                'Send start register to read from
    _Register_Value_L=0x00

    LOW CS_RTC
    _Tmp_Data_Snd=_Register_Name
    gosub Snd_Rcv
    _Tmp_Data_Snd=0
    gosub Snd_Rcv:T  =_TMP_Data_Rcv        'Read Temp
    gosub Snd_Rcv:TF =_TMP_Data_Rcv        'Read fraction of degree
    HIGH CS_RTC

'    sertxd(" tempMSB reg 0x0E =",#T)
'    sertxd(" LSB reg 0x0E =",#TF)
    SerTxd(" TEMP = ")                'Display the temperature
    aWord=T
    GoSub SerTxdWriteWord00
    SerTxd(".")                        'And the decimal part of the temp 0.25 deg res
    TF=TF>>6                        'shift from bits 7 and 6 to bits 1 and 0
    TF=TF*25                        'Scale it
    aWord=TF                        
    GoSub SerTxdWriteWord00
    SerTxd("C",cr,lf)





    'READ Time and DATE
    _Register_Name=0x00
    _Register_Value_L=0

    LOW CS_RTC
    _Tmp_Data_Snd=_Register_Name            'Send register to start readign from
    gosub Snd_Rcv
    _Tmp_Data_Snd=0


    gosub Snd_Rcv:SS =_TMP_Data_Rcv        'multibyte reads
    gosub Snd_Rcv:NN =_TMP_Data_Rcv
    gosub Snd_Rcv:HH =_TMP_Data_Rcv
    gosub Snd_Rcv:DOW =_TMP_Data_Rcv
    gosub Snd_Rcv:DD =_TMP_Data_Rcv
    gosub Snd_Rcv:MM =_TMP_Data_Rcv
    gosub Snd_Rcv:YY =_TMP_Data_Rcv

    HIGH CS_RTC

    SS = BCDTOBIN SS                    'Bit of converson of BCD to Decimal
    NN = BCDTOBIN NN
    HH = BCDTOBIN HH

    DD = BCDTOBIN DD                    'Bit of converson of BCD to Decimal
    MM = BCDTOBIN MM
    YY = BCDTOBIN YY


                            'write out the values    
    'sertxd(#SS,":",#NN,":",#HH,cr,lf)
    aWord=HH
    GoSub SerTxdWriteWord00
    sertxd(":")
    Aword= NN
    GoSub    SerTxdWriteWord00
    sertxd(":")
    aWord=SS
    GoSub SerTxdWriteWord00
    sertxd(" ",#DOW," ")

    aWord=DD
    GoSub SerTxdWriteWord00
    sertxd("/")
    aWord=MM
    GoSub SerTxdWriteWord00
    sertxd("/")
    aWord=YY
    GoSub SerTxdWriteWord00
    Sertxd(cr,lf)


    pause 2000
loop







'=======================================================
Snd_Rcv:
' Send Receive Data With Spi Clock
For _Tmp_2=1 to 8 ' 8 bit Frame
    LOW CLK
'Send Data MOSI ------------------------------------
    _Tmp_1=128
    _TMP_bit=_Tmp_Data_Snd & _Tmp_1
    _Tmp_Data_Snd=_Tmp_Data_Snd<<1

    IF _TMP_bit = 128 then
        HIGH MOSI
    Else
        LOW MOSI
    Endif
'Receve Data MISO ------------------------------------
    High CLK
    _Tmp_Data_Rcv=_Tmp_Data_Rcv<<1

    IF MISO = 1 then            'MOSI
        _Tmp_Data_Rcv=_Tmp_Data_Rcv+1
    Endif
Next _Tmp_2 ' Next Bit of Frame

RETURN







'=======================================================
'Pad to left with 0
SerTxdWriteWord00000:
B0= aWord / 10000
B0 = B0 + 48
SerTxd (B0)                 ; output 10 thousands value data

SerTxdWriteWord0000:
aWord = aWord //10000
B0= aWord / 1000
B0 = B0 + 48
SerTxd (B0)                 ; output Thousands value data

SerTxdWriteWord000:
aWord = aWord //1000
B0= aWord / 100
B0 = B0 + 48
SerTxd (B0)                 ; output Hundreds value data

SerTxdWriteWord00:
aWord = aWord //100
B0= aWord / 10
B0 = B0 + 48
SerTxd (B0)                 ; output Tens value data

SerTxdWriteWord0:
aWord = aWord //10
B0= aWord
B0 = B0 + 48
SerTxd (B0)                 ; output units data
RETURN
 
Last edited:
Top