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

DS1302 troubles

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







DS1302 troubles
PostPosted: Mon Apr 12, 2004 8:09 am     Reply with quote

I get strange reading when trying to get my time values from DS1302. i've used the example schem as supplied with the DS1302 datasheet.
from ds1302.c:
Code:

#define RTC_SCLK Pin_A2 
#define RTC_IO   Pin_A3
#define RTC_RST  Pin_A4

the example that's provided with CCS uses more pins than i, ... what gives?
from ex_rtcclk.c
Code:

#define RTC_SCLK  PIN_B1
#define RTC_RST   PIN_B2

#define RTC_CE    PIN_D0
#define RTC_IO    PIN_D1
#define RTC_CLK   PIN_D2
#define RTC_DATA  PIN_D3

#include <ds1302.c>

my approach:
i get the need values for time from rs232 input and write them with the supplied function from the ds1302 driver. (tried with BCD formating too)
when i read the clock it returns unreasanable values...

Code:

BYTE day=5,mth=6,year=7,dow=1,hour=8,min=1,sec=23;         

//macro from bcd 2 dec
#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])
//from dec 2 bcd
byte get_bcd() {
  char first,second;

  do {
    first=getc();
  } while ((first<'0') || (first>'9'));
  putc(first);
  first-='0';

  do {
    second=getc();
  } while ((second<'0') || (second>'9'));
  putc(second);

  return((first<<4)|(second-'0'));
}

// set clock
void set_clock(){
   byte day,mth,year,dow,hour,min;

   puts("Year 20: ");
   year=get_bcd();
   puts("Month: ");
   mth=get_bcd();
   puts("Day: ");
   day=get_bcd();
   puts("Weekday 1-7: ");
   dow=get_bcd();
   puts("Hour: ");
   hour=get_bcd();
   puts("Min: ");
   min=get_bcd();

   printf("\n\r %2d,%2d,%2d,  %2d um %2d:%2d\n",fromBCD(day),fromBCD(mth),fromBCD(year),fromBCD(dow),fromBCD(hour),fromBCD(min));
   rtc_set_datetime(day,mth,year,dow,hour,min);
      printf("\n\r %2d,%2d,%2d,  %2d um %2d:%2d\n",fromBCD(day),fromBCD(mth),fromBCD(year),fromBCD(dow),fromBCD(hour),fromBCD(min));
 

   printf("\n\r Uhr gestellt");
}

void get_clock()
{

   printf("\n\r...%2d:%2d:%2d...\n",fromBCD(hour),fromBCD(min),fromBCD(sec));

}


void main()
{


   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   rtc_init();

                                                   /////////////////////////
                       /////////////////////////

 printf("\n\r...start...\n");

      set_clock();


  do {

      get_clock();
      delay_ms(3000);
      get_clock();
 

     } while (TRUE);

}



PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 12, 2004 10:15 am     Reply with quote

Pin A4 will require a pull-up resistor on it. Do you have one ?
Quote:
#define RTC_RST Pin_A4
Guest








PostPosted: Mon Apr 12, 2004 10:54 am     Reply with quote

PCM programmer wrote:
Pin A4 will require a pull-up resistor on it. Do you have one ?
Quote:
#define RTC_RST Pin_A4

nope, which value should the pull up resistor have?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 12, 2004 12:00 pm     Reply with quote

I don't know what your PIC crystal frequency is --
it determines how quickly the clock line will toggle.
At higher ds1302 clock speeds, you need a faster
rise time on the clock edges. Just to be safe,
I would suggest using a 1K pull-up on pin A4.
Guest








PostPosted: Mon Apr 12, 2004 12:04 pm     Reply with quote

I'm using maxim's ds1302 but ist does not work correctly.

My problem is that I am able to set the time when it is running it only echos nonsense

code:
Code:

#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 

#include <DS1302.C>

#define RTC_SCLK Pin_A2
#define RTC_IO   Pin_A3
#define RTC_RST  Pin_A4

BYTE day=1,mth=2,year=3,dow=4,hour=5,min=6,sec=7; 
//bcd def
const unsigned int conv[16] = {
0,10,20,30,40,50,60,70,80,90,90,90,90,90,90,90 };

#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])
unsigned int toBCD(unsigned int val)
{
int ctr;
for (ctr=0;ctr<10;ctr++)
{
if (val == conv[ctr])
return(ctr<<4);
else if (val < conv[ctr]) break;
}
--ctr;
return((val-conv[ctr])+(ctr<<4));
} // end bcd def

byte get_bcd() {
  char first,second;

  do {
    first=getc();
  } while ((first<'0') || (first>'9'));
  putc(first);
  first-='0';

  do {
    second=getc();
  } while ((second<'0') || (second>'9'));
  putc(second);

  return((first<<4)|(second-'0'));
   }

void set_clock()
   {

   puts("Year 20: ");
   year=get_bcd();
   puts("Month: ");
   mth=get_bcd();
   puts("Day: ");
   day=get_bcd();
   puts("Weekday 1-7: ");
   dow=get_bcd();
   puts("Hour: ");
   hour=get_bcd();
   puts("Min: ");
   min=get_bcd();
   puts("sec");
   sec=get_bcd();

printf("\n\r %2d,%2d,%2d,  %2d um %2d:%2d\n",fromBCD(day),fromBCD(mth),fromBCD(year),fromBCD(dow),fromBCD(hour),fromBCD(min));

   rtc_set_datetime(day,mth,year,dow,hour,min);

   printf("\n\r clock setup");
  }

void get_clock()
  {
   rtc_get_time(hour,min,sec);
   printf("\n\r...%2d:%2d:%2d...\n",fromBCD(hour),fromBCD (min),fromBCD(sec));

   }


void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   rtc_init();

 printf("\n\r...start...\n");

      set_clock();

  do {

      get_clock();
      delay_ms(1000);
      get_clock();

     } while (TRUE);
} //end main


Hyperterminal:
Code:


start

Year 20:                                   
 04
Month:       
 04
Day:     
 13
Weekday 1-7:             
 03
Hour:       
 12
Min:     
 00
sec     
0 0 
 13, 4, 4,   3 um 12: 0                       

             
... 0: 0: 0...             

... 0: 0: 0...
.
.
.
ferdinand
Guest







PostPosted: Mon Apr 12, 2004 12:05 pm     Reply with quote

I'm using maxim's ds1302 but ist does not work correctly.

My problem is that I am able to set the time when it is running it only echos nonsense

code:
Code:

#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 

#include <DS1302.C>

#define RTC_SCLK Pin_A2
#define RTC_IO   Pin_A3
#define RTC_RST  Pin_A4

BYTE day=1,mth=2,year=3,dow=4,hour=5,min=6,sec=7; 
//bcd def
const unsigned int conv[16] = {
0,10,20,30,40,50,60,70,80,90,90,90,90,90,90,90 };

#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])
unsigned int toBCD(unsigned int val)
{
int ctr;
for (ctr=0;ctr<10;ctr++)
{
if (val == conv[ctr])
return(ctr<<4);
else if (val < conv[ctr]) break;
}
--ctr;
return((val-conv[ctr])+(ctr<<4));
} // end bcd def

byte get_bcd() {
  char first,second;

  do {
    first=getc();
  } while ((first<'0') || (first>'9'));
  putc(first);
  first-='0';

  do {
    second=getc();
  } while ((second<'0') || (second>'9'));
  putc(second);

  return((first<<4)|(second-'0'));
   }

void set_clock()
   {

   puts("Year 20: ");
   year=get_bcd();
   puts("Month: ");
   mth=get_bcd();
   puts("Day: ");
   day=get_bcd();
   puts("Weekday 1-7: ");
   dow=get_bcd();
   puts("Hour: ");
   hour=get_bcd();
   puts("Min: ");
   min=get_bcd();
   puts("sec");
   sec=get_bcd();

printf("\n\r %2d,%2d,%2d,  %2d um %2d:%2d\n",fromBCD(day),fromBCD(mth),fromBCD(year),fromBCD(dow),fromBCD(hour),fromBCD(min));

   rtc_set_datetime(day,mth,year,dow,hour,min);

   printf("\n\r clock setup");
  }

void get_clock()
  {
   rtc_get_time(hour,min,sec);
   printf("\n\r...%2d:%2d:%2d...\n",fromBCD(hour),fromBCD (min),fromBCD(sec));

   }


void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   rtc_init();

 printf("\n\r...start...\n");

      set_clock();

  do {

      get_clock();
      delay_ms(1000);
      get_clock();

     } while (TRUE);
} //end main


Hyperterminal:
Code:


start

Year 20:                                   
 04
Month:       
 04
Day:     
 13
Weekday 1-7:             
 03
Hour:       
 12
Min:     
 00
sec     
0 0 
 13, 4, 4,   3 um 12: 0                       

             
... 0: 0: 0...             

... 0: 0: 0...
.
.
.
Fabian
Guest







PostPosted: Mon Apr 12, 2004 1:17 pm     Reply with quote

PCM programmer wrote:
I don't know what your PIC crystal frequency is --
it determines how quickly the clock line will toggle.
At higher ds1302 clock speeds, you need a faster
rise time on the clock edges. Just to be safe,
I would suggest using a 1K pull-up on pin A4.


20Mhz crystal frequ.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 12, 2004 1:21 pm     Reply with quote

Does Fabian = Ferdinand ?

Did the pull-up resistor fix your problem,
or do you still have a problem ?
Fabian
Guest







PostPosted: Tue Apr 13, 2004 8:51 am     Reply with quote

jes we're working togehter on thist pro�ject.

The problem is still existing.
We are going to have a look at the hardware with an oscilloscope towmorrow.
I'll post again if I can't solve it or do you have any further suggestions.

Thanks Fabian
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Tue Apr 13, 2004 7:53 pm     Reply with quote

I have code that works with a DS1307 which is not exactly the same part as the DS1302. I can cut and paste my routines if you think that might help you. The code is working on a PIC16F76 and there is also an i2c EEPROM on the bus in addition to the DS1307.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Fabian
Guest







PostPosted: Tue Apr 13, 2004 10:03 pm     Reply with quote

rwyoung wrote:
I have code that works with a DS1307 which is not exactly the same part as the DS1302. I can cut and paste my routines if you think that might help you. The code is working on a PIC16F76 and there is also an i2c EEPROM on the bus in addition to the DS1307.

yes i think that might help me alot.
pleas post it or give me a link to download this file

thanks

fabian
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