Hi dave, does that code work?? you have $A0 for both read and write cycle .. I thought it was write $A0 and Read $A1
K
Hi dave, does that code work?? you have $A0 for both read and write cycle .. I thought it was write $A0 and Read $A1
K
It works, I have it running on a bread board right now.
PBP takes care of flipping that bit for you.I thought it was write $A0 and Read $A1
Dave
Always wear safety glasses while programming.
I am looking over you pic pin configuration for the SDA and CLK pins but and you have them on DPIN VAR PORTB.2 'I2C DATA PIN
CPIN VAR PORTB.1 'I2C CLOCK PIN
What is the purpose of having SDA, CLK pins on the chip if we can use any port. Unless I am reading another chip besides the PIC18F6680.
I also how do you make the difference between writing to the RTC and writing to the DS1337 then ?
K
The hardware ports are good for hardware interrupts and maybe a little speed. But I have not noticed much speed wise myself.What is the purpose of having SDA, CLK pins on the chip if we can use any port. Unless I am reading another chip besides the PIC18F6680.
I think you mean writing to the RTC and EEPROM...I also how do you make the difference between writing to the RTC and writing to the DS1337 then ?
It IS in the Control-Address part of the command as you mentioned earlier from the post Mel did.
The EEPROM used $A0 for the Control and a value for the location where the data is stored. In my code it is the VAR ADDR.
The RTC uses %11010000 for the Control and $00 for the Address part. The big thing is the control byte.
I2CWRITE DPIN,CPIN,$A0,ADDR,[DATO] 'EEPROM
I2CREAD DPIN, CPIN, RTC, SecReg, [sec,mins,hr,day,date,mon,yr]
Dave
Always wear safety glasses while programming.
Hi
thanks you for the hand. I still do not what is wrong with my code. I will work on it tonight. Your code does work on my chip with both RTC and EEPROM chip. thank you again. I corrected a few things with my code, but I cannot getthe full thing working. What is odd is that if I run my program, first thing I do is press PORTC.7 to LCDout, I see the time I put in the RTC, but if I do it again it shows me only 255 255 255 ...
K
Ouff, ok I got it narrowed down now to one function, and I really really would like to know why it is not working. Here is the mainloop that is not working, the red part is where the differences are, The output is always FF.
and is is the working good, looks similar, but in the none working code I need a loop.Code:Mainloop: lcdout $FE,1, "Appuye sur Record" lcdout $FE,$C0, "ou sur Show" pause 100 if PORTC.6 =0 then GOSUB READ_RTC I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec] Write_Address = Write_Address +1 PAUSE 10 I2CWRITE DPIN,CPIN,$A0,Write_Address,[mins] Write_Address = Write_Address +1 PAUSE 10 I2CWRITE DPIN,CPIN,$A0,Write_Address,[hr] Write_Address = Write_Address +1 PAUSE 10 Battery_Charge_Counter = Battery_Charge_Counter + 1 Endif if PORTC.7 =0 then bat =0 For i=0 to 2 I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr] PAUSE 10 LCDOUT $FE,1," Time ",DEC3 i+1 LCDOUT $FE,$C0,"AD",DEC Write_Address,":", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec PAUSE 1000 bat = bat+3 next i Endif GOTO Mainloop End
Code:Mainloop: lcdout $FE,1, "Appuye sur Record" lcdout $FE,$C0, "ou sur Show" pause 100 if PORTC.6 =0 then GOSUB READ_RTC I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec] Write_Address = Write_Address +1 PAUSE 10 I2CWRITE DPIN,CPIN,$A0,Write_Address,[mins] Write_Address = Write_Address +1 PAUSE 10 I2CWRITE DPIN,CPIN,$A0,Write_Address,[hr] Write_Address = Write_Address +1 PAUSE 10 Battery_Charge_Counter = Battery_Charge_Counter + 1 Endif if PORTC.7 =0 then Write_Address = 0 I2CREAD DPIN,CPIN,$A0,Write_Address,[sec,mins,hr] PAUSE 10 LCDOUT $FE,1," Time ",DEC3 1 LCDOUT $FE,$C0,"AD",DEC Write_Address," T:", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec PAUSE 1000 Endif GOTO Mainloop End
Last edited by lerameur; - 6th December 2010 at 04:12.
If this is for reading the RTC
DPIN,CPIN,$A0,bat,[sec,mins,hr]
then $A0 is the problem.
Dave
Always wear safety glasses while programming.
NO uts reading the EEPROM.
When PORTC.6 =0 I record from RTC to EEPROM
And when PORTC.7=0 I show on LCD what has been recorded to EEPROM
The only time I read RTC is in the first option C.6 using your routine GOSUB READ_RTC, that works fine.
The program works, but if I add an IF loop it do not work ( red code above)
I think this has been the problem since the beginning !
K
Last edited by lerameur; - 6th December 2010 at 13:17.
IS it possible to write a byte at a time on the EEPROM. From the document, 2 byte ( a word is written for teh address, but I guess this is at the same place..) and the internal clock is incremented. Maybe after writting, the internal clock did not reset and when reading it keeps incrementing where it left off !?!....
Just a thought
.... I think the address has to be a word, not a byte. will try it tonight.
K
Last edited by lerameur; - 6th December 2010 at 13:59.
OK it is now working.. somewhat, Needed to put a word instaed of a byte for the address. Now its more of a programming issue which I am stuck with. The lcd is showing recoded time, but the time does not seem to follow a normal clocking. I record at 5 sec interval, and the first three times are identical, then the next two are 15sec later but are the same...
Code:Mainloop: lcdout $FE,1, "Appuye sur Record" lcdout $FE,$C0, "ou sur Show" pause 100 if PORTC.6 =0 then GOSUB READ_RTC I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr] Write_Address = Write_Address +3 Battery_Charge_Counter = Battery_Charge_Counter + 1 Endif if PORTC.7 =0 then bat = 0 j=1 for i =0 to Battery_Charge_Counter/3 I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr] PAUSE 10 LCDOUT $FE,1," Time ",DEC3 j LCDOUT $FE,$C0,"WA",DEC Write_Address," T:", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec PAUSE 2500 bat = bat +3 j = j+1 next i Endif GOTO Mainloop End READ_RTC: I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] pause 10 RETURN SET_RTC: ' Do once at startup yr = $3 mon = $5 date = $05 sec = $09 mins = $05 hr = $03 I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] RETURN
Ok,
The chips are working but my outputs are erronous. I just want to record time, store it, and then replay it. I am getting all kinds of numbers on the LCD.
here is my latest code:
Code:'///////////////////////// '// DS1337 and test program '// Using the PIC16F887 '// From Dave Mackarit FUNCTIONAL '///////////////////////// '///////////////////////// '// Define section // '///////////////////////// '@ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF OSCCON = %01110000 '8 Mhz DEFINE OSC 8 ANSELH = 0 '///////////////////////// '// LCD configuration // '///////////////////////// DEFINE LCD_DREG PORTB ' Set LCD Data port DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus RB.4, RB.5, RB.6, RB.7 DEFINE LCD_RSREG PORTB ' Set LCD Register Select port DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit DEFINE LCD_EREG PORTB ' Set LCD Enable port DEFINE LCD_EBIT 0 ' Set LCD Enable bit DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD DEFINE LCD_LINES 2 ' Set number of lines on LCD DEFINE LCD_COMMANDUS 2500 DEFINE LCD_DATAUS 250 DEFINE CHAR_PACING 2000 pause 1000 DPIN VAR PORTC.4 'I2C DATA PIN CPIN VAR PORTC.3 'I2C CLOCK PIN Write_Address VAR WORD i var byte j var byte bat var byte Battery_Charge_Counter var byte ' -------------- RTC definitions ----------------- RTC CON %11010000 SecReg CON $00 ' seconds Write_Addressess (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC ContReg CON $0E ' control register cntrl CON %00000000 ' sets the SQW/OUT to 1Hz pulse, logic level low I2CWRITE DPIN, CPIN, RTC, ContReg, [cntrl] ' The variable below holds the values entered: ' entered by the user sec VAR BYTE : mins VAR BYTE : hr VAR BYTE : day VAR BYTE :date VAR BYTE : mon VAR BYTE : yr VAR BYTE Battery_Charge_Counter = 0 PAUSE 1000 GOSUB SET_RTC Write_Address = 0 Mainloop: lcdout $FE,1, "Appuye sur Record" lcdout $FE,$C0, "ou sur Show" pause 100 if PORTC.6 =0 then GOSUB READ_RTC Write_Address =0 I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr] PAUSE 10 Write_Address = Write_Address +7 Battery_Charge_Counter = Battery_Charge_Counter + 1 Endif if PORTC.7 =0 then bat = 0 j=1 for i=0 to Battery_Charge_Counter I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr] PAUSE 10 LCDOUT $FE,1," Time ",DEC3 j LCDOUT $FE,$C0,"time ",DEC Battery_Charge_Counter,":", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec PAUSE 2000 bat = bat +7 j= j + 1 next i Endif GOTO Mainloop End READ_RTC: I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] RETURN SET_RTC: ' Do once at startup yr = $3 mon = $5 date = $05 sec = $09 mins = $04 hr = $02 I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] RETURN
Here you are always writing starting at Address 0. The ADD 7 is always being reset.
Unless I am missing something.Code:if PORTC.6 =0 THEN GOSUB READ_RTC Write_Address =0 I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr] PAUSE 10 Write_Address = Write_Address +7 Battery_Charge_Counter = Battery_Charge_Counter + 1 Endif
Dave
Always wear safety glasses while programming.
Ok that was not my latest one ...sorrryy !!
here it is:
Gining me very eronnous time (ie FF)
Code:'///////////////////////// '// DS1337 and test program '// Using the PIC16F887 '// From Dave Mackarit FUNCTIONAL '///////////////////////// '///////////////////////// '// Define section // '///////////////////////// '@ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF OSCCON = %01110000 '8 Mhz DEFINE OSC 8 ANSELH = 0 '///////////////////////// '// LCD configuration // '///////////////////////// DEFINE LCD_DREG PORTB ' Set LCD Data port DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus RB.4, RB.5, RB.6, RB.7 DEFINE LCD_RSREG PORTB ' Set LCD Register Select port DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit DEFINE LCD_EREG PORTB ' Set LCD Enable port DEFINE LCD_EBIT 0 ' Set LCD Enable bit DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD DEFINE LCD_LINES 2 ' Set number of lines on LCD DEFINE LCD_COMMANDUS 2500 DEFINE LCD_DATAUS 250 DEFINE CHAR_PACING 2000 pause 1000 DPIN VAR PORTC.4 'I2C DATA PIN CPIN VAR PORTC.3 'I2C CLOCK PIN Write_Address VAR WORD Battery_Charge_Counter var byte bat var byte i var byte j var byte ' -------------- RTC definitions ----------------- RTC CON %11010000 SecReg CON $00 ' seconds Write_Addressess (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC ContReg CON $0E ' control register cntrl CON %00000000 ' sets the SQW/OUT to 1Hz pulse, logic level low I2CWRITE DPIN, CPIN, RTC, ContReg, [cntrl] ' The variable below holds the values entered: ' entered by the user sec VAR BYTE : mins VAR BYTE : hr VAR BYTE : day VAR BYTE :date VAR BYTE : mon VAR BYTE : yr VAR BYTE Battery_Charge_Counter = 0 PAUSE 1000 GOSUB SET_RTC Write_Address = 0 Mainloop: lcdout $FE,1, "Appuye sur Record" lcdout $FE,$C0, "ou sur Show" pause 100 if PORTC.6 =0 then GOSUB READ_RTC I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr] PAUSE 10 Write_Address = Write_Address +7 Battery_Charge_Counter = Battery_Charge_Counter + 1 Endif if PORTC.7 =0 then bat = 0 j = 1 for i=0 to Battery_Charge_Counter I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr] PAUSE 10 LCDOUT $FE,1," Time ",DEC3 j LCDOUT $FE,$C0,"AD",DEC Write_Address," T:", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec PAUSE 1000 bat = bat + 7 j = j + 1 next i Endif GOTO Mainloop End READ_RTC: I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] RETURN SET_RTC: ' Do once at startup yr = $3 mon = $5 date = $05 sec = $09 mins = $04 hr = $02 I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] RETURN
I could not see the problem with your code so I made a test myself.
When the EEPROM address reaches 255 the data read back for Min and Sec is FF.
Must be crossing the boundary of rolling over because the EEPROM is full?
It has been a long day and I am getting my bits and bytes and K and k mixed up.
Anyone wants to clear me up we are using
http://www.microchip.com/wwwproducts...cName=en024639
Here is my test
Code:'FL PIC18F6680 ' 18F6680(PIC) 24FC1025(EEPROM) DS1337C(RTC) DEFINE OSC 20 @ __CONFIG _CONFIG1H, _OSC_HS_1H @ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_128_2H @ __CONFIG _CONFIG4L, _LVP_OFF_4L DEFINE LCD_DREG PORTG DEFINE LCD_DBIT 0 DEFINE LCD_RSREG PORTE DEFINE LCD_RSBIT 0 DEFINE LCD_EREG PORTE DEFINE LCD_EBIT 1 DEFINE LCD_BITS 4 DEFINE LCD_LINES 4 DEFINE LCD_COMMANDUS 3000 DEFINE LCD_DATAUS 150 DPIN VAR PORTB.2 'I2C DATA PIN CPIN VAR PORTB.1 'I2C CLOCK PIN ADDR VAR WORD DATI VAR BYTE DATO VAR BYTE ' -------------- RTC definitions ----------------- RTC CON %11010000 SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC ContReg CON $0E ' control register cntrl CON %00000000 ' sets the SQW/OUT to 1Hz pulse, logic level low I2CWRITE DPIN, CPIN, RTC, ContReg, [cntrl] ' 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 Esec VAR BYTE ' seconds Emins VAR BYTE ' minutes Ehr VAR BYTE ' hours I VAR BYTE PAUSE 1000 'GOSUB SET_RTC RUN: ADDR = 0 'SAVE DATA FOR I = 0 TO 100 ADDR = ADDR + 3 I2CREAD DPIN, CPIN, RTC, SecReg, [sec,mins,hr,day,date,mon,yr] PAUSE 100 I2CWRITE DPIN,CPIN,$A0,ADDR,[hr,mins,sec] PAUSE 100 I2CREAD DPIN,CPIN,$A0,ADDR,[Ehr,Emins,Esec] PAUSE 100 LCDOUT $FE,1,"RTC ", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec LCDOUT $FE,$C0,"EEP ",HEX2 Ehr, ":", HEX2 Emins, ":", HEX2 Esec LCDOUT $FE,$90,"ADDR ", DEC ADDR PAUSE 1000 NEXT I 'READ SAVED DATA ADDR = 0 FOR I = 0 TO 100 ADDR = ADDR + 3 I2CREAD DPIN,CPIN,$A0,ADDR,[Ehr,Emins,Esec] PAUSE 100 LCDOUT $FE,1,"READ SAVED " LCDOUT $FE,$C0,"EEP ",HEX2 Ehr, ":", HEX2 Emins, ":", HEX2 Esec LCDOUT $FE,$90,"ADDR ", DEC ADDR NEXT I GOTO RUN
Dave
Always wear safety glasses while programming.
Yes thats the chip I am using... 24LC1025 and the DS1337.
The memory is net getting full already, it shows screwed up time even in the beginning !!!
Ken
I mean the memory cannot be full. I am starting over each time the chip is power on and I just record a few few 'times' maybe 6 or 7. I must or ran 40 versions of this program. I cant find the problem I will use a different approach tonight
Just a thought...
Maybe being you are jumping 7 locations but writing to 3, when you go to read it is reading from locations that are empty.
Write_Address and bat are not starting from the same place?
Code:Write_Address = Write_Address +7 bat = bat + 7
Dave
Always wear safety glasses while programming.
Write_address is for recording, bat is for reading.
Lets say there are 3 reading:
first reading:
Read RTC then write at eeprom at 0
second reading:
Read RTC then write at eeprom at 7
third reading:
Read RTC then write at eeprom at 14
....
Then a show is made by putting C.7 to 0
Bat reads at 0
then bat reads at 7
then reads at 14.
...
A write is made again it is written at Write_address = 21
...
I am only using the first three anyway.
This is not the problem, the seconds for the first three reading are the same !!!!
eXAMPLE:
If I take 8 readings at 5 seconds interval, My results are that the first 3 are have the same time ie: 02:04:11 , the following 3 are: 02:04:27. last two are 02:04:38
Last edited by lerameur; - 7th December 2010 at 17:30.
Hola Dave,
I finally got the program to work like I wanted to, well almost. Its showing the same value twice. If I hit the record button. Then I hit the show button, then it will loop twice the show with the same time.. weird... it seems its doing the loop in red twice.. always.
Code:Mainloop: if PORTC.6 =0 then GOSUB READ_RTC I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr] PAUSE 10 Write_Address = Write_Address +3 Endif if PORTC.7 =0 then if Write_Address = 0 then goto Mainloop j = 0 For i=0 to (Write_Address / 3) -1 I2CREAD DPIN,CPIN,$A0,j,[sec,mins,hr] PAUSE 10 LCDOUT $FE,1," Time ",DEC2 i LCDOUT $FE,$C0,"WA ",DEC j," T:", dec2 hr, ":", dec2 mins, ":", dec2 sec PAUSE 1000 j =j + 3 next i Endif GOTO Mainloop End READ_RTC: I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] pause 10 TempVal=hr GoSub BCD_TO_BIN hr=TempVal TempVal=sec GoSub BCD_TO_BIN sec=TempVal TempVal=mins GoSub BCD_TO_BIN mins=TempVal RETURN SET_RTC: ' Do once at startup yr = $3 mon = $5 date = $05 sec = $09 mins = $04 hr = $02 I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr] RETURN BCD_TO_BIN: ' Convert the BCD values into BIN Temp1 = $0F & TempVal ' Clear off the top four bits Temp1 = Temp1 Dig 0 Temp2 = TempVal >> 4 ' Shift down four to read 2 BCD value Temp2 = Temp2 Dig 0 TempVal = Temp2 * 10 + Temp1 Return
Last edited by lerameur; - 11th December 2010 at 15:54.
OK, I found it, changed the if then for BUTTON action with a 255 debounce cycle. works great now )
Thanks Dave for all the time you spent with me on this.
I am glad to help. Good to see you got it.
Dave
Always wear safety glasses while programming.
Bookmarks