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 CCS Technical Support

DS1307 and SQW/OUT

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



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

DS1307 and SQW/OUT
PostPosted: Sun Jul 18, 2010 5:48 am     Reply with quote

Hi
I've used a DS1307 with PIC16f877a. I set time and date and read them from DS1307. Also I activated SQW/OUT pin
at 8.192KHz by writing the control register(in ds1307 driver). I tested my software in Proteus. Unfortunately PIN7 of DS1307(SQW/OUT)
has no pulse. I examined it on breadboard. The result was the same. I use the ds1307 driver that is in this forum.
My time and date works. I checked PIN7 On Oscope. There is a wave as same as a noise (10 mV). I pulled up the pin7
with a 3.3K ohm resistor.
Whats your idea?
Best wishes
jbmiller



Joined: 07 Oct 2006
Posts: 73
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 5:59 am     Reply with quote

There's only two answers I can see...

One...your software has an error. If you post JUST that section of code, others can cut and paste, compile and try it.

Two...the hardware has an error. Perhaps a bad DS1307, bad solder joint, etc.

Please remember that SIMULATION is NOT the REAL world. There's no guarantee that any simulator will correctly work 100%. Also it won't simulate broken traces, swapped pinouts, marginal power supply, noise, etc.
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 7:12 am     Reply with quote

I use the ds1307 driver that is in this forum

Code:


////////////////////////////////////////////////////////////////////////////////
///                               DS1307.C                                   ///
///                     Driver for Real Time Clock                           ///
///                                                                          ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
///                 used when PIC loses power and DS1307 run from 3V BAT     ///
///               - Disable squarewave output                                ///
///                                                                          ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec)  Set the date/time   ///
///                                                                          ///
/// ds1307_get_date(day,mth,year,dow)               Get the date             ///
///                                                                          ///
/// ds1307_get_time(hr,min,sec)                     Get the time             ///
///                                                                          ///
////////////////////////////////////////////////////////////////////////////////

#define RTC_SDA  PIN_C4
#define RTC_SCL  PIN_C3


#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
   BYTE seconds = 0;

   i2c_start();
   i2c_write(0xD0);// WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_start();
   i2c_write(0xD1);      // RD from RTC
   seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
   i2c_stop();
   seconds &= 0x7F;

   delay_us(3);

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_write(bin2bcd(seconds));     // Start oscillator with current "seconds value
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // Control Rgister
   i2c_write(0x92);   // Enable squarewave output on 8.192 KHz
   i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;

  i2c_start();
  i2c_write(0xD0);            // I2C write address
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));      // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));      // REG 6
  i2c_write(0x80);            // REG 7 - Disable squarewave output pin
  i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x03);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));            // REG 6
  i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;

  temp = binary_value;
  retval = 0;

  while(1)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  }

  return(retval);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));
}



the part of program that initializes the ds1307 and set and get the time & date
Code:

void main()
{
enable_interrupts(INT_AD);
enable_interrupts(INT_EXT);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_8);
setup_comparator(NC_NC_NC_NC);
ext_int_edge( H_TO_L );
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
setup_timer_0(RTCC_EXT_H_TO_L|RTCC_DIV_32);
set_timer0(0);
ds1307_init();
delay_ms(100);
lcd_init();
// Set date for -> 15 July 2005 Tuesday
// Set time for -> 15:20:55
ds1307_set_date_time(15,7,10,6,0,0,0);
ds1307_get_date(day,month,year,dow);
ds1307_get_time(hour,min,sec);


I checked all solderings.I've connected :
PIN1,2---> clock crystal
pin3-----> not connected
PIN4-----> GND
PIN5,6---> SDA ,SCL(each pin pulled up with 3k3 res)
PIN7-----> PIN6 micro(with a 3k3 pullup res)
PIN8-----> Vcc

what do you think about it?
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Jul 18, 2010 12:50 pm     Reply with quote

You probably set up the square wave correctly but then overwrote the ds1307 reg later in your code.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 2:56 pm     Reply with quote

mojsa2000 wrote:
pin3-----> not connected

From the DS1307 datasheet, chapter Pin Descriptions (page 6):
Quote:
If a backup supply is not required, VBAT must be grounded.
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 3:53 pm     Reply with quote

I checked the program again.I've used get and set functions in main program.I didn't use an I2C instructions in main directly.so control Register
has not changed.
The time and Date are right and Time increases every second.but on PIN7 (ds1307) there is no signal clock pulse. if anyone knows about this problem help me please.
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 3:55 pm     Reply with quote

I checked the program again. I've used get and set functions in main program. I didn't use I2C instructions in main directly. So control Register
has not changed.
The time and Date are correct and Time increases every second. But on PIN7 (ds1307) there is no signal or clock pulse. If anyone knows about this problem help me please.
jbmiller



Joined: 07 Oct 2006
Posts: 73
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 4:14 pm     Reply with quote

Is Pin 7 of the DS1307 connected to the PIC ? IF so, perhaps the pin on the PIC is being used for some peripheral subcomponent(spi,timer,etc) ?

Try just the pin 7 pulled up , without going to the PIC to see what happens
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 5:18 pm     Reply with quote

Have you tried connecting pin 3 to ground as I suggested?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 5:44 pm     Reply with quote

Quote:
If anyone knows about this problem help me please.

Read Douglas Kennedy's post, and then go fix it.

Quote:
so control Register has not changed.

Really ? What is the line in bold below ? What is it doing ?

Quote:

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
sec &= 0x7F;
hr &= 0x3F;

i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(bin2bcd(sec)); // REG 0
i2c_write(bin2bcd(min)); // REG 1
i2c_write(bin2bcd(hr)); // REG 2
i2c_write(bin2bcd(dow)); // REG 3
i2c_write(bin2bcd(day)); // REG 4
i2c_write(bin2bcd(mth)); // REG 5
i2c_write(bin2bcd(year)); // REG 6
i2c_write(0x80); // REG 7 - Disable squarewave output pin
i2c_stop();
}


Here are the bit definitions for the Control Register. What is your code
doing to the SQWE bit ?
Code:

CONTROL REGISTER
The DS1307 control register is used to control
the operation of the SQW/OUT pin.

BIT 7  BIT 6  BIT 5  BIT 4  BIT 3  BIT 2  BIT 1  BIT 0
OUT     0      0     SQWE     0     0     RS1    RS0

Bit 4: Square-Wave Enable (SQWE): This bit, when set to logic 1,
       enables the oscillator output.
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Sun Jul 18, 2010 6:56 pm     Reply with quote

I connected PIN6 of Micro to PIN7 of DS1307.
I will connect pin3 to ground and report the result as soon as possible.
thanks
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Mon Jul 19, 2010 2:34 am     Reply with quote

it is fixed.the problem was in the line That PCM programmer said. I changed the 0x80 to 0x92 and SQW/OUT has a square pulse now
thanks guys.
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