Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:30 29 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : DS3231 code mmbasic

     Page 3 of 3    
Author Message
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 07:03pm 10 Mar 2014
Copy link to clipboard 
Print this post

  TassyJim said  
I am surprised that this line didn't give an error:
dim array(7) length 2

LENGTH is only valid for string arrays.

According to the MicroMite manual, You can send data as either individual bytes OR a one dimensional array (but not mixed)

You can start the data in the array from any position in the array.
This is valid:

dim mydata(10)
I2Csend &H68, 0, 7, mydata(2)

which would send 7 bytes starting at mydata(2)
Jim

Jim
I'm using a Maximite manual and it is not quite so specific about mixing the variables. Thanks for the info about Length.
Bob
oops I spelt your name wrong Jim. fixed.Edited by BobD 2014-03-12
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 01:44am 11 Mar 2014
Copy link to clipboard 
Print this post

Please see attached my programs for the Maximite to set and read the DS3231 RTC. The set program reads the values of TIME$ and DATE$ and loads them to the RTC. The read program gets the data from the RTC and sets TIME$ and DATE$ in the Maximite. The DS3231 has a temperature measurement and this is displayed.

These programs can be used on the MicroMite by changing the following I2C commands

I2Cen to I2C open
I2Csend to I2C write
I2CRCV to I2C read
I2Cdis to I2C close

2014-03-11_114240_RTC.zip
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 01:49pm 11 Mar 2014
Copy link to clipboard 
Print this post

A great one for the library Bob.

Greg
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 03:42pm 11 Mar 2014
Copy link to clipboard 
Print this post

Hello,

Ran BobD's code on the chronodot from adafruit. This uses the DS3231. All works well as far as reading and writing except for one thing...

I get this when running the rtcget code...

The time is 21:37:43 on Tuesday 11 March 2014
[77] t$=Format$(Temperature,"%+6.2f")
Error: Expected a number

Do you suppose this is unique to the chronodot, or am I doing something wrong?

I did change all the I2C commands to run using the 28 pin uMite and am running beta 10.

Thanks for any input!

 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 03:53pm 11 Mar 2014
Copy link to clipboard 
Print this post

  viscomjim said   Hello,

The time is 21:37:43 on Tuesday 11 March 2014
[77] t$=Format$(Temperature,"%+6.2f")
Error: Expected a number
[/quote]

No it's not related to the Chronodot. I developed the program on a Chronodot V2. It will be differences in the Format command between Maximite and MicroMite. I don't have the documentation for the Micro so I can't tell you what it should be. You may have to convert the variable to a string first. Use Str$(). The actual output from that Format command is +nnnn.nn That is a sign always expressed with the ability to show at least 3 significant places and two places to the right of the point. Example +27.75 and the temperature is in degrees Celsius. Conversion is possible by the formula F = 9*C/5 + 32

edit: sorry, changing to a string may not help. I don't know where I got that from. You could try a Print Temperature command just before the failure and post back what you got and what the ambient temperature was.Edited by BobD 2014-03-13
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 04:25pm 11 Mar 2014
Copy link to clipboard 
Print this post

I added...
print temperature before the t$ line and got the following...

RUN
The time is 22:22:35 on Tuesday 11 March 2014
23.5
[79] t$=Format$(Temperature,"%+6.2f")
Error: Expected a number

Thanks for looking at this BobD.
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 04:45pm 11 Mar 2014
Copy link to clipboard 
Print this post

Jim, the temperature is in the range of what could be expected. It is 23.5 degrees C or about 75F. Very pleasant. The line that fails is only intended to put a + sign on the front and always print two decimal places. Like so +23.50. You have two choices remove the line or fix it. I can't help much beyond that. Maybe someone with MicroMite documentation could confirm the syntax. If the syntax is correct then we may have a bug.
Bob
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6102
Posted: 04:53pm 11 Mar 2014
Copy link to clipboard 
Print this post

The FORMAT$ command is for the Maximite but not available on the MicroMite.
The equivalent is STR$

temperature = 23.3
t$=str$(Temperature,6,2)
print "Temperature is "+t$+" degrees C"


gives us:
Temperature is 23.30 degrees C


If you want to print a "+", you will have to do that yourself.

VK7JH
MMedit   MMBasic Help
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 09:13pm 11 Mar 2014
Copy link to clipboard 
Print this post

You should be able to force the '+' sign. Geoff mentioned this is the e-mail that came out with Beta9.

"You can specify a negative number for the second argument of the extended STR$() function. This will force the display of a + sign as well as padding with space characters as before."

Greg
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 01:29am 12 Mar 2014
Copy link to clipboard 
Print this post

Thanks to all. I will give this a shot and report back.
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 722
Posted: 04:27am 12 Mar 2014
Copy link to clipboard 
Print this post

Help needed,

Hi to all.

I want to write to a specific Register of the ds3231.
Here to the control Register with address 0EH.
To get a 1 second output on the sqw pin, intcn, rs2 and rs1 must be set to 0.

EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
| | | | | | | |
x x x 0 0 0 x x


i2c write &H0E, 0,1,0 ????

Mus i not first write to H68 ??


confused...

 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 05:44am 12 Mar 2014
Copy link to clipboard 
Print this post

  atmega8 said  
I want to write to a specific Register of the ds3231.
Here to the control Register with address 0EH.
To get a 1 second output on the sqw pin, intcn, rs2 and rs1 must be set to 0.

EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
| | | | | | | |
x x x 0 0 0 x x

i2c write &H0E, 0,1,0 ????

Mus i not first write to H68 ??

You could try this. It is untested. The idea is read this register first and leave untouched all bits except what we need.

[code]I2Caddress=&h68
I2C open 100,100
I2C write I2Caddress,0,1,&h0E
I2C read I2Caddress,0,1,ControlReg0E
ControlReg0E = ControlReg0E and &b11100011
I2C write I2Caddress,0,2,&h0E,ControlReg0E
I2C close
[/code]

The Maximite manual indicates that the first write and read can be combined. Not sure if this is valid for MicroMite. I have not read the manual.
[code]I2C read I2caddress,0,1,ControlReg0E,1,&h0E[/code]
where the 1,&h0E are an implied write. This would give you.
[code]I2Caddress=&h68
I2C open 100,100
I2C read I2caddress,0,1,ControlReg0E,1,&h0E
ControlReg0E = ControlReg0E and &b11100011
I2C write I2Caddress,0,2,&h0E,ControlReg0E
I2C close
[/code]
RTFM and caveat emptor et bon chance


Edited by BobD 2014-03-13
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 06:09am 12 Mar 2014
Copy link to clipboard 
Print this post

I just read the MicroMite manual and my suggestion for combining the read and write is NOT valid for a Micromite. I guess it was removed as part of saving space in the flash. Only the first code block, in the post above, is correct.
Edited by BobD 2014-03-13
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 722
Posted: 11:03am 12 Mar 2014
Copy link to clipboard 
Print this post

Hi Bob,

your code works at once.
What annoys me somewhat is the fact that this behavior of I2C write is not explained in the Manual.

I mean this code line: I2C write I2Caddress,0,2,&h0E,ControlReg0E

It seem that a 2 Byte Value (&H0E) is automatically interpreted as a new address and not as data.

I find this no where in the Manual.

But then the Manual explains it wrong:

"The data can be supplied in the command as individual bytes.
Example: I2C WRITE &H6F, 1, 3, &H23, &H43, &H25"

Where is my misunderstanding?
Maybe ist my bad english that let me make such mistakes or wrong Interpretation???


Just a view of the soothsayer in the glass sphere would help;-((
Chapeau that you're capable to do so without a micromite in your Hands!!!

Dietmar

'Lets the SQW Output blink a LED with 1 Hz

I2Caddress=&h68
I2C open 100,100
I2C write I2Caddress,0,1,&h0E
I2C read I2Caddress,0,1,ControlReg0E
ControlReg0E = ControlReg0E and &b11100011
I2C write I2Caddress,0,2,&h0E,ControlReg0E
I2C close

Edited by atmega8 2014-03-13
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 11:50am 12 Mar 2014
Copy link to clipboard 
Print this post

Dietmar, you are getting a bit excited. I saw nothing in the uM manual that was incorrect.

When you say
[quote]I mean this code line: I2C write I2Caddress,0,2,&h0E,ControlReg0E

It seem that a 2 Byte Value (&H0E) is automatically interpreted as a new address and not as data.

I find this no where in the Manual. [/quote]
You will find this in the DS3231 datasheet, never in the uM manual because it is specific to a particular chip and not part of uM or MM Basic.

When you write to the DS3231 (and many other chips) you must specify which register in the chip where the data is to be written. The &h0E is the address of register 14.

[quote]Chapeau that you're capable to do so without a micromite in your Hands!!!
[/quote] I don't own a hat.

 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 722
Posted: 12:40pm 12 Mar 2014
Copy link to clipboard 
Print this post

Thanks Bob,

now i noticed what messed me up....

Too much wine in my blood;-)



Edited by atmega8 2014-03-13
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 08:54pm 26 Mar 2014
Copy link to clipboard 
Print this post

Jman's code for the DS3231
No alarms or offset
Calculation to set Day of the week
Goto settime from the console will set the DS3231 with the current
time and date


' I2C RTC based On DS1307 Secs,Mins,Hours,Day,Date,Month,Year,Control
Dim RTCbuff(7)
Dim Temp(2)
Dim Days$(7)
GoSub Days
i2caddr = &h68 ' DS3231 I2C address
I2C open 100,100 ' Enable I2C
I2C write I2caddr, 0, 1, 0
I2C Read i2caddr, 0, 7, RTCbuff(0) ' Read Time
I2C write I2caddr, 0, 1, 17
I2C Read i2caddr, 0, 2, Temp(0) ' Read Temperature
I2C Close

BCDTEMP = RTCBuff(0)
BCDtoDec BCDTEMP
sec$ = Str$(decimal)

BCDTEMP = RTCBuff(1)
BCDtoDec BCDTEMP
min$ = Str$(decimal)

BCDTEMP = RTCBuff(2)
BCDtoDec BCDTEMP
hours$ = Str$(decimal)

BCDTEMP = RTCBuff(3)
BCDtoDec BCDTEMP
DayOfWeek = decimal

BCDTEMP = RTCBuff(4)
BCDtoDec BCDTEMP
day$ = Str$(decimal)

bcdtemp = rtcbuff(5)
BCDtoDec BCDTEMP
month$ = Str$(decimal)

bcdtemp = rtcbuff(6)
BCDtoDec BCDTEMP
year$ = Str$(decimal + 2000 )

t$ = hours$+":"+min$+":"+sec$
D$ = day$+"/"+month$+"/"+year$
Time$ = T$
Date$ = D$
Temp
Print "Time has been set to ";Time$
Print"Date has been set to ";Date$
Print "Today is ";Days$(DayOfWeek)
Print "Temperature is";Temperature
End


'Temperature
Sub Temp
If Temp(0) < &h80 Then
Temperature= Temp(0) + (Temp(1) / 256)
Else
Temperature=-((Temp(0) Xor &HFF) + 1) + (Temp(1) / 256))
EndIf
End Sub

'Convert to Decimal
Sub BCDtoDec (BCDTEMP)
Decimal = Fix(BCDTemp / 16) * 10
Decimal = Decimal + (BCDTEMP And &hF)
End Sub

Days:
For I = 1 To 7
Read Days$(I)
Next I
Return

Data "Sunday"
Data "Monday"
Data "Tuesday"
Data "Wednesday"
Data "Thursday"
Data "Friday"
Data "Saturday"

SetTime:
' Get time from time$ and date$
tempdec = Val(Left$(Time$, 2))
BCDtoHex TempDec
hours = hex

tempdec = Val(Mid$(Time$, 4, 2))
BCDtoHex TempDec
minutes = hex

tempdec = Val(Right$(Time$, 2))
BCDtoHex TempDec
seconds = hex

tempdec = Val(Left$(Date$, 2))
BCDtoHex TempDec
day = hex

tempdec = Val(Mid$(Date$, 4, 2))
BCDtoHex TempDec
month = hex

tempdec = (Val(Right$(Date$, 4)) - 2000)
BCDtoHex TempDec
year = hex

DayOfWeek

' Write Time to RTC
i2caddr = &h68 ' DS3231 I2C address
I2C Open 100,100 ' Enable I2C
I2C Write i2caddr,0,8,0,seconds,minutes,hours,dow,day,month,year
I2C Close

Print "0=ok 1=nack 2=timeout"; MM.I2C
Print "RTC has been set to ";Time$;" ";Date$
End

'Calculate Day Of Week TZAdvantage's DOW Calculation
Sub DayOfWeek
A = Int(14-Val(Mid$(Date$,4,2)))/12
M = Val(Mid$(Date$,4,2)) + 12*a - 2
Y = Val(Mid$(Date$,7,4)) -a
D = Val(Mid$(Date$,1,2))
Dow=(day+y+Int(y/4)-Int(y/400)+Int(31*m/12)) Mod 7
End Sub

' Convert to Hex
Sub BCDtoHex TempDec
hex = Fix(tempdec / 10) * 16
hex = hex Or ((tempdec / 10) - (Fix(tempdec / 10))) * 10
Return



Regards
Jman
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 12:06pm 27 Mar 2014
Copy link to clipboard 
Print this post

thanks Jman, I will load this and test. I am glad to see others using this RTC as it is readily available and very accurate.

EDIT:

copy and pasted code and am super glad to report that it works first time. AWESOME!!!!!Edited by viscomjim 2014-03-31
 
     Page 3 of 3    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024