CCSgues Guest
|
RTC DS1302 an PIC 18F452 |
Posted: Sun May 16, 2004 7:08 am |
|
|
Has anyone experiences with maxim's DS1302 and DS32kHz.
I've connected the DS32Khz clock output directly with DS1302's X1 and X2 clock input and I'm using CCS' driverfile DS1302.c, but I got strange outputs for hour, min and sec.
Is there any bug in the soure?
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#include <DS1302.C> //Uhrentreiber
byte day,mth,year,dow,hour,min,sec;
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));
} //Definiton RTC_BCD_Code ende
////////////////////////////////////////////////////////////////////////////////
// RTD DS1302 Maxim //
////////////////////////////////////////////////////////////////////////////////
// DS1302.C //
// Driver for Real Time Clock //
// //
// rtc_init() Call after power up //
// //
// rtc_set_datetime(day,mth,year,dow,hour,min) Set the date/time //
// //
// rtc_get_date(day,mth,year,dow) Get the date //
// //
// rtc_get_time(hr,min,sec) Get the time //
// //
// rtc_write_nvr(address,data) Write to NVR //
// //
// data = rtc_read_nvr(address) Read from NVR //
// //
////////////////////////////////////////////////////////////////////////////////
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();
rtc_set_datetime(day,mth,year,dow,hour,min);
printf("\n\r Eingestelltes Datum: %2d,%2d,%2d, um %2d:%2d:%2d Uhr\n",fromBCD(day),fromBCD(mth),fromBCD(year),fromBCD(hour),fromBCD(min),fromBCD(sec));
}
void get_clock()
{
rtc_get_time(hour,min,sec);
printf("\n\r...%2d:%2d:%2d...\n",fromBCD(hour),fromBCD(min),fromBCD(sec));
delay_ms(1000);
}
void main()
{
rtc_init();
set_clock();
printf("\n\r...Begin Main...\n");
do {
get_clock();
} while (TRUE); //end true
}
|
|
|