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 & PIC16F877A problem...

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



Joined: 19 May 2008
Posts: 8
Location: Poland

View user's profile Send private message Visit poster's website ICQ Number

DS1307 & PIC16F877A problem...
PostPosted: Thu Jun 12, 2008 4:05 am     Reply with quote

Hi,
Please help me. I use driver from http://www.ccsinfo.com/forum/viewtopic.php?t=23255, my code:
Code:

#include "C:\CCS\SBC44B\T1\main.h"
#include "C:\CCS\SBC44B\T1\ds1307.c"

 
void main()
{
   byte sec;
   byte min;
   byte hrs;
   byte day;
   byte month;
   byte year;
   byte dow;
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   // TODO: USER CODE!!
 
 
  ds1307_init();
 
  ds1307_set_date_time(11,6,8,3,15,20,55);
 
  while(1)
  {
    delay_ms(1000);
   
    ds1307_get_date(day,month,year,dow);
    ds1307_get_time(hrs,min,sec);
     
    printf("\f\%02d/\%02d/\%02d\r\n",day,month,year);
    printf("\%02d:\%02d:\%02d", hrs,min,sec);
  }

}


I send
11/06/08 15:20:55
and read incorrect
11/02/08 15:20:55

if send 11/09/08 15:20:55, read correct 11/09/08 .....
send 11/04/08 read incorrect 11/00/08...... why? Any idea?
_________________
Best regards,
muerte
ckielstra



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

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 5:53 am     Reply with quote

In a string the backslash character ('\') indicates the next character is a special control character. Your code includes some illegal sequences. Change:
Code:
    printf("\f\%02d/\%02d/\%02d\r\n",day,month,year);
    printf("\%02d:\%02d:\%02d", hrs,min,sec);
to:
Code:
    printf("\f%02d/%02d/%02d\r\n",day,month,year);
    printf("%02d:%02d:%02d", hrs,min,sec);


What is your compiler version number?
muerte



Joined: 19 May 2008
Posts: 8
Location: Poland

View user's profile Send private message Visit poster's website ICQ Number

PostPosted: Thu Jun 12, 2008 8:38 am     Reply with quote

Thanks for answer, I change this line, but no effect.
Compiler : PCM Version 4.073.
_________________
Best regards,
muerte
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 10:51 am     Reply with quote

Are you testing this program with real hardware, or are you using
a simulator such as Proteus ?
muerte



Joined: 19 May 2008
Posts: 8
Location: Poland

View user's profile Send private message Visit poster's website ICQ Number

PostPosted: Thu Jun 12, 2008 11:23 am     Reply with quote

Real hardware. Config on board: PIC16F877A@20MHz, DS1307, 24LC256, MCP23008, LCD2x16 + keypad (via i2c). I replace this RTC, no effect. Time ok, date wrong (send 11/06/08 read 11/02/08). Crying or Very sad No idea....
_________________
Best regards,
muerte
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 11:34 am     Reply with quote

1. Post the main.h file.

2. Are all of these i2c devices on the same bus ?
What is the value of the pull-up resistors ?
muerte



Joined: 19 May 2008
Posts: 8
Location: Poland

View user's profile Send private message Visit poster's website ICQ Number

PostPosted: Thu Jun 12, 2008 11:38 am     Reply with quote

Ok.
main.h
Code:

#include <16F877A.h>
#device *=16
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES PUT                      //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

#use delay(clock=20000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3)


DS1307.C
Code:


//#define RTC_SDA  PIN_C4
//#define RTC_SCL  PIN_C3

//#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

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

int8 bin2bcd(int8 binary_value)
{
  int8 temp;
  char 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.
char bcd2bin(char bcd_value)
{
  char 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));
}

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 Register
   i2c_write(0x80);     // Disable squarewave output pin
   i2c_stop();

}

void ds1307_set_date_time(BYTE day, int8 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_set_time(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(0x80);            // REG 7 - Disable squarewave output pin
  i2c_stop();
}

void ds1307_set_date(byte day, byte mth, byte year, byte dow)
{
 
  i2c_start();
  i2c_write(0xD0);            // I2C write address
  i2c_write(0x03);            // Start at REG 0 - Seconds
  i2c_write(bin2bcd(dow));
  i2c_write(bin2bcd(day));
  i2c_write(bin2bcd(mth));
  i2c_write(bin2bcd(year));
  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();

}


2. yes
3. 4k7
_________________
Best regards,
muerte
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 11:57 am     Reply with quote

Remove all the other i2c devices from the i2c bus, except for the LCD
and the ds1307. Then test it again. See if you still get the problem.

Remove these devices:
24LC256
MCP23008
keypad (via i2c).



Is the LCD using i2c ? Or is it using a normal parallel data bus ?
muerte



Joined: 19 May 2008
Posts: 8
Location: Poland

View user's profile Send private message Visit poster's website ICQ Number

PostPosted: Thu Jun 12, 2008 12:26 pm     Reply with quote

LCD and keypad using I2c.

I remove 23008 and... problem solved. Interesting.... 23008 and 1307 different I2C adress.

Thanks and sorry for my bad English Wink
_________________
Best regards,
muerte
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 12:37 pm     Reply with quote

You could try to reduce the size of the pull-up resistors.
Change them to 2.2K or 1.8K. The minimum allowed pullup is 1.6K
for a 5v i2c bus.

Try the reduced pull-up value and see if it will work with the MCP23008
attached.
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