View previous topic :: View next topic |
Author |
Message |
assaad
Joined: 09 Dec 2009 Posts: 37
|
DS1306 and spi problem |
Posted: Fri Aug 27, 2010 5:57 am |
|
|
Hi
I am again with new problem with SPI that took 2 days also and could not find solution.
I have DS1306 RTC connected to 18F8722.
Connection is :
VCC1 to +5v
VCCIF TO +5V
VCC2 TO GROUND
VBAT TO 3V BATTERY
After I wrote a sample program to read the seconds I noticed the seconds is incrementing every 2 minutes ! I replaced the crystal 32768hz 3 times I thought the crystal is bad, but it still give me the same result.
Here is the code:
Thank you in advance
Code: |
setup_spi(SPI_MASTER| SPI_MODE_0_0|SPI_CLK_DIV_64);
while(1) // loop for ever
{
output_h(0xd4); // enable rtc active high
spi_write(0x00); // address of seconds
second= spi_read(0);
output_h(0xd0); // disable rtc
glcd_gotoxy(5,3,1);
printf(glcd_putc,"%x",second); // put seconds in hex on glcd
delay_ms(10);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 27, 2010 11:21 am |
|
|
Quote: | setup_spi(SPI_MASTER| SPI_MODE_0_0|SPI_CLK_DIV_64);
while(1) // loop for ever
{
output_h(0xd4); // enable rtc active high
spi_write(0x00); // address of seconds
second= spi_read(0);
output_h(0xd0); // disable rtc
glcd_gotoxy(5,3,1);
printf(glcd_putc,"%x",second); // put seconds in hex on glcd
delay_ms(10);
} |
Why are you writing to all bits on Port H, just to set the Enable pin high
and low ? Most people would use output_high(DS1306_ENABLE_PIN)
and output_low(DS1306_ENABLE_PIN), where DS1306_ENABLE_PIN is
set to PIN_H2 with a #define statement.
Quote: |
setup_spi(SPI_MASTER| SPI_MODE_0_0|SPI_CLK_DIV_64); |
Why are you using SPI mode 0 ? The ds1306 data sheet tells you
the supported SPI modes, right on the front page ! It says:
Quote: |
Supports Motorola SPI (Serial Peripheral Interface)
Modes 1 and 3 or Standard 3-Wire
|
Download and read the data sheet carefully:
http://pdfserv.maxim-ic.com/en/ds/DS1306.pdf
Do you have the SERMODE pin connected to +5v on the ds1306 ?
You must do that, to configure it to use "4-wire" (i.e., SPI) mode.
---------
Edit:
Changed ds1305 to ds1306. All comments still apply.
Last edited by PCM programmer on Fri Aug 27, 2010 4:56 pm; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
assaad
Joined: 09 Dec 2009 Posts: 37
|
|
Posted: Fri Aug 27, 2010 12:00 pm |
|
|
Thank you again for reply,
In my Ds1306 when I use mode 1 and 3 the spi bus hangs and stuck.
I use port h because I am using encoder to drive 4 chips on the same spi bus, so at the time only one chip is active. For Ds1306 I have an inverter connected to the output of the encoder because the enable of it active high.
Yes I have SERMODE conencted to Vdd . |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 27, 2010 4:57 pm |
|
|
I suggest that you remove the encoder and the inverter chips, and
disconnect all the other SPI slave devices, and only test the ds1306
with the PIC. Get the ds1306 working alone. Then later try to make
the encoder version of the project work. |
|
![](templates/subSilver/images/spacer.gif) |
|