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: AustraliaPosts: 935 |
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. |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
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: AustraliaPosts: 1329 |
A great one for the library Bob. Greg |
||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
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: AustraliaPosts: 935 |
|
||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
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: AustraliaPosts: 935 |
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: AustraliaPosts: 6102 |
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: AustraliaPosts: 1329 |
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 StatesPosts: 925 |
Thanks to all. I will give this a shot and report back. |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 722 |
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: AustraliaPosts: 935 |
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 |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
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. |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 722 |
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 |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
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: GermanyPosts: 722 |
Thanks Bob, now i noticed what messed me up.... Too much wine in my blood;-) |
||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
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 StatesPosts: 925 |
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!!!!! |
||||
Page 3 of 3 |
Print this page |