CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problems reading DS1307

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

Problems reading DS1307
PostPosted: Tue Aug 24, 2004 1:55 pm     Reply with quote

Colleagues,

When reading from DS1307 RTC I'm getting this artifact. Evey other packet I receive is identical and is also garbage. Could you take a look at the code and the output and tell me what's wrong?

Thanks,
Nick


Code:

#include "test13.h"

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   printf("start\n\r");
   while(1)
   { 
      // writer the read address for the seconds register (0x00)
      do i2c_start();
      while (1 == i2c_write(0b11010000)); // poll for ack
      printf("wr ack\n\r");
      i2c_write(0x00);                    // seconds address
     
      // read the seconds from the RTC and output them to RS232
      do i2c_start();
      while (1 == i2c_write(0b11010001)); // poll for ack     
      printf("rd ack\n\r");
      printf("0x%X ", i2c_read());      // read the seconds
      printf("0x%X ", i2c_read());      // read the minues
      printf("0x%X\n\r", i2c_read());     // read the hours
      i2c_stop();                         // end of transaction
     
      delay_ms(2000);
   }
}


Header file:
Code:

#include <18F452.h>
// #device ICD=TRUE
#device adc=10
#use delay(clock=12800000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, NOPUT, STVREN, NODEBUG, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB //, LVP
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)


Output:
Code:

0x51 0x54 0x20   // OK
0x03 0xFF 0xFF   // gardbage
0x55 0x54 0x20   // OK   
0x03 0xFF 0xFF   // garbage
0x59 0x54 0x20   // OK
0x03 0xFF 0xFF   // garbage
0x03 0x55 0x20   // OK, minutes change
0x03 0xFF 0xFF   // garbage
Nick
Guest







PostPosted: Tue Aug 24, 2004 1:59 pm     Reply with quote

Have you hooked up your 3v battery? is the connection loose? Thats how I usually end up getting garbage over my DS1307. I even made a check for it to tell me what was the problem since I'm using a soldierless bread board and connections arnt that great.

Nick
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 24, 2004 2:03 pm     Reply with quote

I didn't check your entire code in detail, but I see two things:

1. The last call to i2c_read(), in a series of calls, must have a 0
parameter. This does a "NAK" instead of an ACK. This is part
of the i2c specification.

Example:

i2c_read();
i2c_read();
i2c_read(0); // Last call does a NAK

2. If it was me, I would disable interrupts while doing the i2c operations.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Aug 24, 2004 2:04 pm     Reply with quote

My battery connection and battry voltage look OK. Also, both PIC and DS1307 are running of 5V regulator. I might be wrong, but I think that in this case the 3V battery doesn't have a lot of influence. Thanks for the tip though.

Nick
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Aug 24, 2004 2:08 pm     Reply with quote

i2c_read(0) worked. The data is valid all the time now.

Thank you!

Nick
Guest








PostPosted: Tue Aug 24, 2004 3:15 pm     Reply with quote

kender wrote:
I might be wrong, but I think that in this case the 3V battery doesn't have a lot of influence.


IME, the DS1307 will not function at all without the 3V battery and the 32 kHz xtal connected. Glad you got it going though.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Aug 24, 2004 3:20 pm     Reply with quote

Well, I have just removed the battery without turning off 5V, reset the PIC, and DS1307 was outputing the correct data.

Nick
ximo



Joined: 30 Sep 2004
Posts: 2

View user's profile Send private message

ds1307 driver
PostPosted: Thu Oct 07, 2004 9:03 am     Reply with quote

this is my driver for the ds1307 and function very good with 16f877a and 18f8720:

Code:


// first configure the i2c
#use I2C(MASTER, SDA=PIN_C4, SCL=PIN_C3, RESTART_WDT, FAST)

void get_rtc_time(){
   int i;

   i2c_start();
   i2c_write(code_escribe);
   i2c_write(0);
   i2c_start();
   i2c_write(code_escribe | 1);
   for (i=0;i<sizeof(rtc);i++)
      rtc[i]=i2c_read(i<sizeof(rtc)-1);  //rtc is a array with the read
   i2c_stop();
}

void set_rtc_time(){
   int i;

   i2c_start();   
   i2c_write(code_escribe);
   i2c_write(0);
   for(i=1;i<sizeof(config);i++)
      i2c_write(config[i]);      //config is a array with the hour etc...
   i2c_stop();
}

[/code]
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Oct 07, 2004 9:23 am     Reply with quote

kender wrote:
Well, I have just removed the battery without turning off 5V, reset the PIC, and DS1307 was outputing the correct data.

Nick


That is just luck. When you actually try using it in the field it WILL crash. It is possible to use the DS1307 without a battery but in this case you need to pull the +battery input pin to ground via a resistor. It may also work pulling it to plus 5 but that is not how I use the device. The reason it will crash without this is because it is a smart little bugger. If it sees the battery pin is a higher potential that the +5v rail, it assumes the +5v rail is failing and shuts down all interfaces to save power. And guess what, sooner or later the +battery terminal will float higher than the +5V rail - maybe when a cat comes in the room, or it starts raining outside or just for the hell of it. It is dead easy to test. I spent three weeks chasing this intermittent crash problem.


Last edited by asmallri on Thu Oct 07, 2004 5:49 pm; edited 1 time in total
Ttelmah
Guest







Re: Problems reading DS1307
PostPosted: Thu Oct 07, 2004 10:05 am     Reply with quote

kender wrote:
Colleagues,

When reading from DS1307 RTC I'm getting this artifact. Evey other packet I receive is identical and is also garbage. Could you take a look at the code and the output and tell me what's wrong?

Thanks,
Nick


Code:

#include "test13.h"

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   printf("start\n\r");
   while(1)
   { 
      // writer the read address for the seconds register (0x00)
      do i2c_start();
      while (1 == i2c_write(0b11010000)); // poll for ack
      printf("wr ack\n\r");
      i2c_write(0x00);                    // seconds address
     
      // read the seconds from the RTC and output them to RS232
      do i2c_start();
      while (1 == i2c_write(0b11010001)); // poll for ack     
      printf("rd ack\n\r");
      printf("0x%X ", i2c_read());      // read the seconds
      printf("0x%X ", i2c_read());      // read the minues
      printf("0x%X\n\r", i2c_read());     // read the hours
      i2c_stop();                         // end of transaction
     
      delay_ms(2000);
   }
}


Header file:
Code:

#include <18F452.h>
// #device ICD=TRUE
#device adc=10
#use delay(clock=12800000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, NOPUT, STVREN, NODEBUG, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB //, LVP
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)


Output:
Code:

0x51 0x54 0x20   // OK
0x03 0xFF 0xFF   // gardbage
0x55 0x54 0x20   // OK   
0x03 0xFF 0xFF   // garbage
0x59 0x54 0x20   // OK
0x03 0xFF 0xFF   // garbage
0x03 0x55 0x20   // OK, minutes change
0x03 0xFF 0xFF   // garbage


I have used the 1307, without problems, but have allways 'stopped' each transaction, rather than using a repeated start, and used the 'NACK', on the last byte before stopping. I use:
Code:

/* I2C address for chip, including direction flag */
#define CADDRWR   0xD0
#define CADDRRD   0xD1
#define NACK                    0

  I2C_START();
  WHILE (I2C_WRITE(CADDRWR));
  I2C_WRITE(0);      /* set address to 0 */
  I2C_STOP();
  I2C_START();
  WHILE (I2C_WRITE(CADDRRD));
  clock.secs=I2C_READ();
  clock.mins=I2C_READ() & 0x7F;
  clock.hours=I2C_READ() & 0x3F;
  clock.dayofweek=I2C_READ() & 7;      /* day of week */
  clock.dayofmnth=I2C_READ() & 0x3F;   /* day of month */
  clock.month=I2C_READ() & 0x1f;      /* month */
  clock.year=I2C_READ(NACK);      /* Year, and finish */
  I2C_STOP();


I suspect your problem is not using the 'NACK', on the last transaction. The data sheet, specifically says 'the DS1307, _must_ receive a NACK, to terminate a read'.

Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 07, 2004 10:18 am     Reply with quote

Note that this post is old and the guy solved his problem. Someone was just posting their driver code
Ttelmah
Guest







PostPosted: Thu Oct 07, 2004 2:17 pm     Reply with quote

Mark wrote:
Note that this post is old and the guy solved his problem. Someone was just posting their driver code

Yes.
For some reason, the post appeared at the head of the list as a single 'new' post (no 'driver code' visible), so I replied.
After I had replied, the 'hread' became visible.
I suspect there was a hiccup in the board somewhere.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group