![]() |
Forum Index : Microcontroller and PC projects : DS1307 on a Micromite issues with seconds
Author | Message | ||||
easterly81 Newbie ![]() Joined: 08/04/2015 Location: United StatesPosts: 15 |
here is the code I have been testing, but i cannot get the seconds to work. I guess I don't understand how the RTCbuff(0) array is functioning. Why does it not go: sec------ RTCbuff(0) min------ RTCbuff(1) hour----- RTCbuff(2) day------ RTCbuff(3) date----- RTCbuff(4) month---- RTCbuff(5) year----- RTCbuff(6) control-- RTCbuff(7) i have messed with the order and strange thing happen. I could just be far off base, but if anyone can point me in the right direction it would be much appreciated. ReadTime:
' I2C RTC based On DS1307 Secs,Mins,Hours,Day,Date,Month,Year,Control Dim RTCbuff(8) i2caddr = &h68 ' DS1307 I2C address I2C open 100,100 ' Enable I2C I2C write i2caddr, 0, 1, &h0 I2C Read i2caddr, 0, 8, RTCbuff(0) I2C Close sec$ = Str$(decimal) BCDTEMP = RTCBuff(1) BCDtoDec BCDTEMP min$ = Str$(decimal) BCDTEMP = RTCBuff(2) BCDtoDec BCDTEMP hours$ = Str$(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$ print t$, " ", d$ 'Convert to Decimal Sub BCDtoDec (BCDTEMP) Decimal = Fix(BCDTemp / 16) * 10 Decimal = Decimal + (BCDTEMP And &hF) End Sub |
||||
BobD![]() Guru ![]() Joined: 07/12/2011 Location: AustraliaPosts: 935 |
You read your data into RTCBuff(0) and incrementing. The seconds is in that variable. When you process the data you start from array address 1 as seconds. edit It's more complex than that. I'll get back. |
||||
BobD![]() Guru ![]() Joined: 07/12/2011 Location: AustraliaPosts: 935 |
try that. I put two statements in labelled NEW [code] ReadTime: ' I2C RTC based On DS1307 Secs,Mins,Hours,Day,Date,Month,Year,Control Dim RTCbuff(8) i2caddr = &h68 ' DS1307 I2C address I2C open 100,100 ' Enable I2C I2C write i2caddr, 0, 1, &h0 I2C Read i2caddr, 0, 8, RTCbuff(0) I2C Close BCDTEMP = RTCBuff(0) ' NEW BCDtoDec BCDTEMP ' NEW sec$ = Str$(decimal) BCDTEMP = RTCBuff(1) BCDtoDec BCDTEMP min$ = Str$(decimal) BCDTEMP = RTCBuff(2) BCDtoDec BCDTEMP hours$ = Str$(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$ print t$, " ", d$ 'Convert to Decimal Sub BCDtoDec (BCDTEMP) Decimal = Fix(BCDTemp / 16) * 10 Decimal = Decimal + (BCDTEMP And &hF) End Sub [/code] |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9593 |
Are you using a Maximite or a Micromite chip? If the latter, then any reason for not using one of the supported RTC chips or modules? Then it is as simple as RTC SETTIME and RTC GETTIME.... See page 53 of the manual, if you are using a MicroMite chip, that is. Smoke makes things work. When the smoke gets out, it stops! |
||||
easterly81 Newbie ![]() Joined: 08/04/2015 Location: United StatesPosts: 15 |
I tried that with no success. I am using the ds1307 I have 4 of them already. |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10247 |
The syntax should be "I2C Read i2caddr, 0, 8, RTCBuff()" and not RTCBuff(0) |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1576 |
This applies to µM: Syntax for MAXIMITES:(p. 57, Maximite manual 4.5) Older libraries were often written for Maximites. btw. the DS3231 is a better RTC, IMHO. edit: I just found out RTCBuff(0) should also work on MicroMites. RTCBuff() is of course the better style! causality ≠correlation ≠coincidence |
||||
easterly81 Newbie ![]() Joined: 08/04/2015 Location: United StatesPosts: 15 |
Thanks I will try that when I get home. I thought the RTCBuffer(0) seemed strange () makes sense. Do the control bits in the middle of the data effect the data read? I have seen on other c based libraries were they use masks to deal with this. For example Wire.requestFrom(clockAddress, 7);
// A few of these need masks because certain bits are control bits second = bcdToDec(Wire.read() & 0x7f); minute = bcdToDec(Wire.read()); // Need to change this if 12 hour am/pm hour = bcdToDec(Wire.read() & 0x3f); dayOfWeek = bcdToDec(Wire.read()); dayOfMonth = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1576 |
This works for my DS3231 rtc: ' RTC.ReadTime for Mircomite MMbasic 4.6B/DS3231 RTC on 0x68 (also for DS1307) ' Begin DEMO option base 0 ? Time$, Date$ RTC.ReadTime ? Time$, Date$ END ' DEMO '******************************** Sub RTC.ReadTime 'from DS3231 I2C '******************************** Local float i2caddr = &h68 ' my DS3231 I2C address Local RTCbuff(7) Local string sec$, min$, hours$, DOW$, day$, month$, year$, T$,D$ I2C open 100,100 ' Enable I2C I2C write I2caddr, 0, 1, 0 I2C Read i2caddr, 0, 7, RTCbuff() ' Read Time, fills the buffer ' or ... RTCbuff(0) I2C Close 'capture I2C-error here? sec$ = Str$(BCDtoDec(RTCBuff(0))) min$ = Str$(BCDtoDec(RTCBuff(1))) hours$ = Str$(BCDtoDec(RTCBuff(2))) DOW$ = Str$(BCDtoDec(RTCBuff(3))) 'day of week?? always=0? day$ = Str$(BCDtoDec(RTCBuff(4))) month$ = Str$(BCDtoDec(RTCBuff(5))) year$ = Str$(BCDtoDec(RTCBuff(6)) + 2000 ) Time$ = hours$+":"+min$+":"+sec$ Date$ = day$+"/"+month$+"/"+year$ End Sub '********************* ' Convert to Decimal '********************* Function BCDtoDec(BCDTEMP As integer) As integer BCDtoDec = Fix(BCDTemp / 16) * 10 BCDtoDec = BCDtoDec + (BCDTEMP And &hF) End Function Normally Micromite users will get the Time/Date from DS1307 with RTC GETTIME. causality ≠correlation ≠coincidence |
||||
easterly81 Newbie ![]() Joined: 08/04/2015 Location: United StatesPosts: 15 |
I just ordered one on amazon ds3231 will be here on sat,now I am just curious. Here is a post that someone used the ds1307 and it looks like it worked. It likes very similar I don't really she the difference. Can anyone Point out the difference. http://www.thebackshed.com/forum/forum_posts.asp?TID=6810&KW=ds1307 |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9593 |
The 1307 is drifty - it's time will be wrong after a few weeks to a month. Not by a HUGE amount, but it does drift. The 3231 or 3232 have built-in laser-trimmed crystal oscillator, and are accurate to the second over years on end, in theory. If you don't care about absolute accuracy, then the 1307 is fine, but if you want the clock to stay accurate over months on end, then the 3231 or 3232 is the way to go. Smoke makes things work. When the smoke gets out, it stops! |
||||
easterly81 Newbie ![]() Joined: 08/04/2015 Location: United StatesPosts: 15 |
I agree the ds1307 drifts i have seen it on other projects I have done. I am glad I have ordered the ds3231 and cannot wait to experiment with it. To be honest the code from the first post is what i have used in my current project and it works to set the micromites internal clock with this statement: t$ = hours$+":"+min$+":"+sec$
D$ = month$+"/"+day$+"/"+year$ time$ = T$ Date$ = D$ Then i print to the LCD with the time$, date$ I was concerned this method would gain error over time since the d31307 was only used to set the MM clock on initial power up. I was using this as a band-aid because when tried to print the T$ or D$ I would get mostly gibberish with the occasional correct reading. BTW the code was different then below i had a sub that pulled the RTC data and formatted it. It is just strange to me when the ds1307 is called once to set the mm clock it works 100% of the time. Here is the code i have been working on if curious. Sorry it is a rather mess but working ![]() I originally thought the issue was coming from the I2C LCD. I dont believe this is the case because the test code i used from post 1 does not function either. I should just give it up, get some sleep and await the ds3221, but that just not my style. Dim PWM1
Dim PH dim Tempr Dim RTCbuff(8) dim pwmdir LCDI2C_Init 63,1,0,2,3 LCDI2C_BacklightOn i2caddr = &h68 ' DS1307 I2C address I2C open 100,100 ' Enable I2C I2C write i2caddr, 0, 1, &h0 I2C Read i2caddr, 0, 8, RTCbuff(0) 'I2C Close sec$ = Str$(decimal) BCDTEMP = RTCBuff(1) BCDtoDec BCDTEMP min$ = Str$(decimal) BCDTEMP = RTCBuff(2) BCDtoDec BCDTEMP hours$ = Str$(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$ = month$+"/"+day$+"/"+year$ time$ = T$ Date$ = D$ Main: 'pwm1= pwm1 +5 if pwm1 = 100 then pwmdir = 1 end if if pwm1 = 0 then pwmdir = 0 end if if pwmdir = 0 then pwm1= pwm1 + .1 end if if pwmdir = 1 then pwm1= pwm1 - .1 end if readTEMP ReadPH LCDI2C 1,1,"Temperature " LCDI2C 1,13, str$(tempr*1.8+32) LCDI2C 2,1, "Time " LCDI2C 2,6, time$ LCDI2C 3,1, "Date " LCDI2C 3,6, date$ LCDI2C 4,1, "PH " LCDI2C 4,4, str$(ph) pwm 1,10000,PWM1 print pwm1 pause 250 goto Main '-------------------------------------------- 'Get PH '--------------------------------------------- sub ReadPH SETPIN 2, AIN PH =PIN(2)*3.5+.21 end sub '---------------------------------------------- 'TEMP '---------------------------------------------- sub readTEMP tempr= DS18B20(14) end sub '--------------------------------------------- 'Get Ime and format it from DS1307 '---------------------------------------------- '--------------------------------- 'Convert BCD to Decimal '--------------------------------- Sub BCDtoDec (BCDTEMP) Decimal = Fix(BCDTemp / 16) * 10 Decimal = Decimal + (BCDTEMP And &hF) End Sub '----------------------------------------------------------- ------- ' ' Print String to LCD ' SUB LCDI2C(LineNum,CharPos,Text$) LOCAL I IF LineNum=1 THEN I=(&H80 + CharPos-1) IF LineNum=2 THEN I=(&HC0 + CharPos-1) IF LineNum=3 THEN I=(148 + CharPos-1) IF LineNum=4 THEN I=(212 + CharPos-1) LCDI2C_CMD(I) FOR I=1 TO LEN(Text$) LCDI2C_DATA(ASC(MID$(Text$,I,1))) NEXT I END SUB '----------------------------------------------------------- ------- ' ' Clear LCD ' ' SUB LCDI2C_Clear() LCDI2C_CMD(&H01) END SUB '----------------------------------------------------------- ------- ' ' INITIALIZE LCD ' ' SUB LCDI2C_Init I2CAddr,NibPos,RSBitNo,ENBitNo,BLBitNo 'Backlight Control bit DIM LCDI2C_BackLight LCDI2C_BackLight=2^BLBitNo 'Current Backlight state - default to ON DIM LCDI2C_LCDBackLight LCDI2C_LCDBackLight=LCDI2C_BackLight 'I2C Bus Address DIM LCDI2C_I2CAddr LCDI2C_I2CAddr=I2CAddr 'RS Control bit DIM LCDI2C_RSDataMask LCDI2C_RSDataMask=2^RSBitNo 'EN Control Bit DIM LCDI2C_EMask LCDI2C_EMask=2^ENBitNo 'Save if P0~P3 or P4~P7 bits of PCF8574 port DIM LCDI2C_NibPos LCDI2C_NibPos=NiBPos 'Start I2C I2C OPEN 400,100 '%0011---- %0011---- 8-bit / 8-bit LCDI2C_CMD(&H33) '%0011---- %0010---- 8-bit / 4-bit LCDI2C_CMD(&H32) ' Byte commands - To configure the LCD ' Display Format ' 4bit mode, 2 lines, 5x7 ' ' 001LNF00 ' %00101000 LCDI2C_CMD(&H28) ' L : 0 = 4-bit Mode 1 = 8-bit Mode ' N : 0 = 1 Line 1 = 2 Lines ' F : 0 = 5x7 Pixels 1 = N/A ' Setup Display ' Display ON, Cursor On, Cursor Steady ' ' 00001DCB ' %00001110 LCDI2C_CMD(&H0C) ' D : 0 = Display Off 1 = Display On ' C : 0 = Cursor Off 1 = Cursor On ' B : 0 = Cursor Steady 1 = Cursor Flash ' Setup Cursor/Display ' Inc Cursor Cursor Move ' ' 000001IS LCDI2C_CMD(&H06) ' I : 0 = Dec Cursor 1 = Inc Cursor ' S : 0 = Cursor Move 1 = Display Shift LCDI2C_CMD(&H01) 'Turn Off LCDBacklight LCDI2C_BackLightOff() END SUB '----------------------------------------------------------- ------- ' ' Close the I2C LCD ' ' SUB LCDI2C_Close() 'Close the I2C Bus I2C CLOSE END SUB '----------------------------------------------------------- ------- ' ' Turn Backlight On ' ' SUB LCDI2C_BacklightOn LCDI2C_LCDBacklight=LCDI2C_Backlight LCDI2C_WireWrite(0) END SUB '----------------------------------------------------------- ------- ' ' Turn Backlight Off ' ' SUB LCDI2C_BacklightOff LCDI2C_LCDBackLight=&H00 LCDI2C_WireWrite(0) END SUB '----------------------------------------------------------- ------- ' ' Send Command Byte to LCD ' ' SUB LCDI2C_CMD(Byte) 'Send Hi Nibble LCDI2C_DirectSend(Byte AND &HF0) 'Send Low Nibble LCDI2C_DirectSend((Byte AND &H0F) * 16) END SUB '----------------------------------------------------------- ------- ' ' Send Data Byte to LCD ' ' SUB LCDI2C_DATA(Byte) 'Send Hi Nibble LCDI2C_DirectSend((Byte AND &HF0) OR LCDI2C_RSDataMask) 'Send Lo Nibble LCDI2C_DirectSend(((Byte AND &H0F) * 16) OR LCDI2C_RSDataMask) END SUB '----------------------------------------------------------- ------- ' ' Send Byte to LCD over I2C ' NB, we don't call LCDI2C_WireWrite in this SUB because that results in ' MUCH slower execution time than inlin'ing the I2C Write's ' ' SUB LCDI2C_DirectSend(Byte) LOCAL B,B1 'Use a copy so that we don't mess up caller's argument 'IS D4-D7 of LCD mapped to P0~P3 of PCD8574 ? IF LCDI2C_NibPos=0 THEN B1=Byte\16 ELSE B1=Byte ENDIF 'Take EN high, use B 'cos it's quicker than doing the OR's in the I2C Write command itself B=B1 OR LCDI2C_EMask OR LCDI2C_LCDBacklight I2C WRITE LCDI2C_I2CAddr,0,1,B 'Take EN Low I2C WRITE LCDI2C_I2CAddr,0,1,B1 OR LCDI2C_LCDBacklight END SUB '----------------------------------------------------------- ------- ' ' Send Byte over I2C ' ' SUB LCDI2C_WireWrite(Byte) I2C WRITE LCDI2C_I2CAddr,0,1,Byte OR LCDI2C_LCDBacklight END SUB |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |