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

need help with program to interface DS1307 with 16F877

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



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

need help with program to interface DS1307 with 16F877
PostPosted: Sat Feb 16, 2008 7:22 am     Reply with quote

hello everyone.

i'm new here, i would like to ask for some assistance regarding my project, which on interfacing DS1307 with 16F877. basically i will be using this on my programmable medicine dispenser, and i need DS1307 as my clock source and synchronization so that the medications will always be on time. how do i call the DS1307 so that if a preset time is reached (input through keypad), the microcontroller will signal an alarm and a blinking LED indicator?

Thank you very much.

Elizabeth Ann
Ateneo de Naga University
Philippines
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

RTC interrupt
PostPosted: Sat Feb 16, 2008 8:05 am     Reply with quote

Does it have to be a DS1307? If you use a DS1305 or a DS1306 then you can set two alarms and these will cause an interrupt when the time matches the preset. This eliminates you having to poll the RTC the whole time and avoids complicated time comparison routines. You can search the forum for some drivers on the DS1305/DS1306.
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

need help with program to interface DS1307 with 16F877
PostPosted: Sun Feb 17, 2008 9:49 pm     Reply with quote

thanks crystal_lattice! Very Happy

the thing is, i already have a set of free samples of DS1307 from maxim. Confused i have developed a code for the driver and stuff, only that i can't figure out how to call the routines for a more synchronized time retrieval yet. our final presentation is fast approaching and i am doomed.

i will try the DS1305/06 and hope that by any luck i will get a free sample too. meanwhile, do you have any other suggestions regarding the DS1307 operation?

thanks a lot.

elizabeth ann
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

RTC problems
PostPosted: Mon Feb 18, 2008 6:57 am     Reply with quote

Tell me about fast approaching proposal deadlines...

So what i understand is that you have your driver code working for reading/writing time. You basically need a function to compare time? Well, set up an timer interrupt for say 250ms. After interrupt read + compare the RTC values and do what you have to when the time matches. Not sure what you mean by "more synchronized time retrieval".

I also did project using DS1305 and requested samples - they arrived after i submitted my project... Your country might be faster though.

Regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 18, 2008 5:38 pm     Reply with quote

Here is one method of getting an alarm from the ds1307.
It uses the ds1307 driver from the CCS code library:
http://www.ccsinfo.com/forum/viewtopic.php?t=23255

The test program shown below displays the following output.
This proves that it can display an alarm at the specified time.
Quote:

15:20:55
15:20:56
15:20:57
15:20:58
15:20:59
15:21:00
15:21:01
15:21:02
15:21:03
15:21:04
15:21:05 Alarm !
15:21:06
15:21:07
15:21:08
15:21:09
15:21:10


You have to make two changes to the ds1307 driver.
There are two lines where it sets the Control Register to 0x80.
Example:
Quote:

i2c_write(0x80); // Disable squarewave output pin

They both need to be changed to set it to 0x10. Example:
Code:

i2c_write(0x10);  // Enable squarewave output pin at 1 Hz


Also, you need to add a 4.7K pull-up resistor to the SQW pin on
the ds1307, and then connect it to Pin B0 on the PIC. This is the
external interrupt pin on the 16F877.

The positive edge of the 1 Hz SQW output signal occurs in the middle
of the time "cell" in the ds1307 (I tested this). So when you get the
INT_EXT interrupt, you have 500 ms to read the time before it
changes again.

This code doesn't use an interrupt service routine for INT_EXT.
It just polls the INTF interrupt flag bit in the INTCON register.
If you want to change it to use an interrupt routine, it's up to you.
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <ds1307.c>

// Set an alarm time of 15:21:05 for testing.
int8 alarm_hrs = 15;
int8 alarm_min = 21;
int8 alarm_sec = 05;

int8 check_alarm(int8 hrs, int8 min, int8 sec);

//============================
void main()
{
int8 sec;
int8 min;
int8 hrs;
 
ds1307_init();
 
// Set date for -> 15 June 2005 Tuesday
// Set time for -> 15:20:55
ds1307_set_date_time(15,6,5,2,15,20,55);
 
ext_int_edge(L_TO_H);

while(1)
  {
   while(!interrupt_active(INT_EXT)); 
   
   clear_interrupt(INT_EXT);

   ds1307_get_time(hrs, min, sec);

   printf("%02d:%02d:%02d", hrs, min, sec);

   if(check_alarm(hrs, min, sec))
      printf("  Alarm !");

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

//=============================================

int8 check_alarm(int8 hrs, int8 min, int8 sec)
{
if((hrs == alarm_hrs) &&
   (min == alarm_min) &&
   (sec == alarm_sec))
  return(TRUE);
else
  return(FALSE);
}
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

RTC alarm
PostPosted: Mon Feb 18, 2008 11:51 pm     Reply with quote

Very nice solution PCM Programmer, takes the strain off the PIC to produce interrupt. One concern with the method of "comparing the time" as opposed to an alarm is that if you use other interrupts in your code you stand a chance of missing the alarm. You could reduce the risk of this happening by modifying the seconds comparison to:
Code:

(sec >= alarm_sec)

This will however cause the alarm to "go off" 60-Alarm_sec times and you will have to implement a flag and maybe a counter to combat this. Just a thought.

Regards
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

Re: RTC problems
PostPosted: Tue Feb 19, 2008 9:11 am     Reply with quote

crystal_lattice wrote:
Tell me about fast approaching proposal deadlines...



sorry i didn't mean to pressure anyone, the burden is mine.


crystal_lattice wrote:
So what i understand is that you have your driver code working for reading/writing time. You basically need a function to compare time? Well, set up an timer interrupt for say 250ms. After interrupt read + compare the RTC values and do what you have to when the time matches. Not sure what you mean by "more synchronized time retrieval".

putting it in a nutshell, my project is literally a 100 set of medicine dispensers. i need 100 inputs from the user, each with different time interval of the patients' medications which should NEVER have lapses. so i need a reliable RTC and a series of interrupts, and an intelligent routine that will repeat itself whenever the user says that a medicine has to be taken like " four times a day", for example..
i hope i'm not boring you with the details though....
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

PostPosted: Tue Feb 19, 2008 9:13 am     Reply with quote

many thanks to PCM Programmer, i will try to run that code and tell you guys of the changes. i'll comment on this as soon as i get back.

thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 19, 2008 12:22 pm     Reply with quote

I want to make this statement:
The code that I posted should not be used in any real medical device.
As pointed out by Crystal Lattice, it doesn't have any safety features,
with respect to missing an alarm if perhaps a glitch occurs. The purpose
of this code was just to help you move along with what I thought was
a class project.
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

Number of patients
PostPosted: Tue Feb 19, 2008 12:53 pm     Reply with quote

No problem with helping others when i have time, gets mind off work and own problems!!

Just a few questions i am pondering about:
1. Do you have a 100 patients, or a 100 alarm events? I thought the unit is for 1 patient only, like those 7 segment boxes for a pill-a-day week.

2. Are YOU entering the "four times a day" times or is the pic calculating this based on a morning starting time of say 6:00.

3. Do you have to display which patient gets which meds(in the case of 100 patients)? eg. John Doe - Dose 1

Anyways good luck with the time comparison thing.
Regards
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

PostPosted: Tue Feb 19, 2008 9:45 pm     Reply with quote

PCM programmer wrote:
I want to make this statement:
The code that I posted should not be used in any real medical device.
As pointed out by Crystal Lattice, it doesn't have any safety features,
with respect to missing an alarm if perhaps a glitch occurs. The purpose
of this code was just to help you move along with what I thought was
a class project.


nothing to worry, i am only trying to collect as many information as i could for reference purposes ONLY. of course there will be definitely a lot of modifications that i promise i will share with you guys when everything gets finished.

also, it is a GROUP project, there are 5 of us in that particular project, and i'm the one who's doing the programming stuff.
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

Re: Number of patients
PostPosted: Tue Feb 19, 2008 9:54 pm     Reply with quote

crystal_lattice wrote:

Just a few questions i am pondering about:
1. Do you have a 100 patients, or a 100 alarm events? I thought the unit is for 1 patient only, like those 7 segment boxes for a pill-a-day week.

i have 10 patients, each with 10 medications each, so that makes it 100 medicine compartments. you're right, there will be 100 alarm events

crystal_lattice wrote:

2. Are YOU entering the "four times a day" times or is the pic calculating this based on a morning starting time of say 6:00.

the user will be entering any of these time intervals: 3,6,8,12 and 24. that means " every 3 hours", "every 6 hours", " every 8 hours", etc...
the termination will look like this:
say, if a patient chooses "every 8 hours" (so that's 3 times a day) and he has to take that for 1 day, the termination will be said as 24 hours.


crystal_lattice wrote:

3. Do you have to display which patient gets which spam9(in the case of 100 patients)? eg. John Doe - Dose 1


no, we only have to signal an alarm and a blinking LED which will only stop when the medicine is retrieved.
elizabeth ann



Joined: 16 Feb 2008
Posts: 33

View user's profile Send private message

PostPosted: Sat Mar 01, 2008 10:43 am     Reply with quote

hello,

even though nobody has responded since my last post, i'd like to tell everyone that our project is nearing its completion.

for the record, we have already successfully interfaced DS1307 with the PIC 16F877A, used several I/O Port Expanders to cater the alarm indicator of 100 alarm events and interfaced all the basic peripherals (LCD, keypad, etc) successfully.

with some necessary modifications, i find the DS1307 driver in this topic very useful:
http://www.ccsinfo.com/forum/viewtopic.php?t=23255&highlight=ds1307

the arising problem now is with the manual setting of time in DS1307, which has to be done with the help of the keypad, but everytime i access the register (say,seconds or minutes) and change the values, the LCD simulation only displays a series of zeroes for that particlar register. if i have accessed and changed the 'minutes' register, the output will be 06:00:56, even if the 'minutes' digits is actually nonzero.

i hope someone can help me correct the problem...thanks...
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