PDA

View Full Version : Dallas 3231



jpeakall
- 3rd May 2012, 17:14
Hi All,

I m having trouble talking to a DS3231 RTC. I have searched and didn't find any posts directly related to this chip. I have been trying sample code from other Dallas RTC's but no luck so far.

I can talk to the RTC with another uC. Problem is that most of the code is in ASM, so I can't pluck out what I need, I don't grok ASM. I do know the chip is working though, as I can set and read the time.

Does anyone have a simple snip that works for sure with the 3231? I can post what I have tried so far, but I thought I'd see if anyone had a snip first and waste less of everyones time.

Very sorry if there are posts addressing this chip, I really did look.

Jonathan

SteveB
- 3rd May 2012, 22:42
The major issues will likely be (in order):
1) Understanding and implementing I2C communicaitons with a PIC and PBP (and get the hardware correct)
Lots of Info on the protocol is available online.
2) How to use the I2C protocol to access the DS3231 specific registers – Both to set the device and retrieve data
The datasheet will help here once you have #1 sorted out.
3) How to manipulate/convert the received data in PBP do display on an LCD.

This link may help you in overcoming the issues above. http://www.rentron.com/PicBasic/i2c_communication.htm

jpeakall
- 3rd May 2012, 22:57
Steve,

Thanks for the reply. I have used I2C stuff with PICbasic pro before and the display is no problem. I must not be sending it the right commands. As I understand it, with PBP pro which has the built in I2C commands, all I should have to do is something like:

I2CRead sda,scl,%11010000,$0,[seconds]

in order to retrieve the seconds data, but I ain't getting it. I can read and set the time from another uC, so I'm pretty sure I have the hardware connected correctly.

Thanks!

Jonathan

SteveB
- 3rd May 2012, 23:14
Something to remember with the DS3231, you need to write the address of the register you want to read first (no data)
Then, when you read, you will get the data in the register.

I've been happier using the hardware I2C. But try these PBP statments:
:
I2CWRITE sda, scl, %11010000,%0
I2CREAD sda, scl, %11010000, [seconds]

jpeakall
- 3rd May 2012, 23:48
Drat, no luck. I'll keep looking. Thanks!

SteveB
- 4th May 2012, 02:16
I'll offer these up if you are interested in trying the hardware I2C if it's available on your device.

Some things to mention:
The path of the include file needs to be specified for where you have it saved.
This is very bare bones and only ment to get you started in incorporating it into your own code.

jpeakall
- 4th May 2012, 14:53
Steve,

I am using a 16F88, which does have hardware I2C. Bummer is, I already made a PCB and populated it and the built in I2C pins are already in use. I think what I'll do is try the code you posted on a breadboard. If it works, I'll see if I can bodger up my board and switch the traces around.


Thanks a lot for the help! I really appreciate it. I was sure it would be no prob as I have used this chip before and this project is so close to done.


Jonathan

jpeakall
- 4th May 2012, 15:16
Time to add the code I am trying, this has to be something dumb and obvious that I am doing wrong.

mclr_on
hs_osc '
lvp_off
wdt_off
protect_off
define OSC 20
define shift_pauseus 100
define I2C_slow 1
lcdPin var portb.0
lcdBaud con 84
lcdAtn con $FE
lcdOn con $41
lcdCls con $51
lcdContrast con $52
lcdBackLight con $53
lcdCursor con $45
lineTwo con $40
rtc CON %11010000
sda var porta.1
scl var porta.0
SecReg CON $00 ' seconds address (00 - 59)
' MSB of SecReg must be set to a 0 to enable RTC
MinReg CON $01 ' minutes address (00 - 59)
HourReg CON $02 ' hours address (01 - 12) or (00 - 23)
DayReg CON $03 ' day address (1 - 7)
DateReg CON $04 ' date address (01 - 28/29, 30, 31)
MonthReg CON $05 ' month address (01 - 12)
YearReg CON $06 ' year address (00 - 99)
StatusReg CON $0F
cntrl CON $10'%00000110
hours var byte
minutes var byte
seconds var byte
days var byte
month var byte
year var byte
date var byte

hours = 12
minutes = 55
seconds = 14
days = 0
year = 0
month = 0

init:
serout2 lcdpin,lcdbaud,[lcdAtn,lcdOn,lcdAtn,lcdcls,"DS3231 Test",_
lcdatn,lcdCursor,lineTwo,"Version 0.1"]
pause 1000
'I2cwrite sda,scl, RTC, $D0, $00,[seconds,minutes,hours,days,date,month,year,cntrl]
'I2Cwrite sda,scl, RTC, StatusReg,[$0]
'I2CWrite sda, scl, RTC, Secreg,[$00,$00]
'I2CWrite SDA, SCL, RTC, StatusReg,[$00,cntrl]
'I2CWrite portb.0, portb.1, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
main:
'I2CRead SDA, SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr]
'I2CWrite SDA, SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr]
'I2CRead sda,scl, RTC, secreg,[seconds,minutes,hours,days,date,month,year]
'i2cread
I2Cwrite sda,scl,%1101000,$0
I2CRead sda,scl,%1101000,$0,[seconds]
'I2CRead sda,scl, RTC, minReg,[minutes]
serout2 lcdpin,lcdbaud,[lcdatn,lcdcls,dec2 hours,":",dec2 minutes,":",dec2 seconds,_
lcdatn,lcdcursor,linetwo,dec2 month,"/",dec2 days,"/", dec4 year]
pause 500

goto main

SteveB
- 4th May 2012, 21:26
Port A is configured as analog input by default at power on (see note on pg 51 of datasheet), so you will need to include the following configuartion setting to make them digital:

ANSEL = 0
Check out this thread for more info (http://www.picbasic.co.uk/forum/showthread.php?t=11100)



Although this is likely just an issue of copy-paste from your IDE to the forum, but make sure you pay attention to case. For instance, DEFINES are case sensetive.

And, for furture, you can use the "code" feature to post code. It is the "#" button in the tool bar of the forum posting window, or just type
...your code here...

jpeakall
- 5th May 2012, 02:05
Steve,

You da man! Ansel it was. Got it working, thanks so much! And now I can stop beating my head on the keybaord and get back to work.

Jonathan

SteveB
- 5th May 2012, 15:10
Glad that did the trick! :applouse:

But don't rule out more head pounding... it goes with territory. :D Some of my best thinking is after I have ASDFGHJKL: tattooed across my forehead.