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 support@ccsinfo.com

get/set time on DS1307

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



Joined: 20 Nov 2008
Posts: 79
Location: white Plains, NY

View user's profile Send private message Yahoo Messenger

get/set time on DS1307
PostPosted: Thu Feb 27, 2014 10:15 am     Reply with quote

I'm trying to write alarm clock, with the DS1307 but instead of using LCD I want to communicate via serial and I haven't done c programming in so long I forgot my basic, I would google this but I dont know the keywords that I need to search for.
but I want to be able to type "time" and get the time and be able to type timeset=02/27/14 9:00:00 and be able to set the time, but I'm lost I dont even know what to search for :(
Code:

void main()
{
  BYTE sec;
  BYTE min;
  BYTE hrs;
  BYTE day;
  BYTE month;
  BYTE yr;
  BYTE dow;
 
  ds1307_init();
 
  // Set date for -> 15 June 2005 Tuesday
  // Set time for -> 15:20:55
  ds1307_set_date_time(2,27,14,4,8,20,55);
 
  while(1)
  {
    delay_ms(1000);
   
    ds1307_get_date(day,month,yr,dow);
    ds1307_get_time(hrs,min,sec);
     
    printf("\f\%02d/\%02d/\%02d\r\n",day,month,yr);
    printf("\%02d:\%02d:\%02d", hrs,min,sec);
  }
}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Feb 27, 2014 10:36 am     Reply with quote

Hi,

I assume you'll want to connect your hardware to a PC? If so, you'll need a voltage level translator IC between the PIC UART, and the COM port on your PC. A good choice is the 'MAX232'. That should take care of the hardware aspect of your request.

Next, you'll need to write the serial reception code to receive characters from the PIC UART and put them into a buffer. Your data will (presumably) be sent on an infrequent basis, and it will be of limited scope, so a simple 'linear buffer' will be sufficient. You will need a serial interrupt handler (search for 'int_rda') to receive incoming characters. You can add the code necessary to ignore incoming data until a 't' is received, and then start buffering until a <CR> is received. At that point, you'd set a 'Rx_Command_Ready' flag, and your Main() would then parse the receive buffer to determine if you had a time query, or a time set command, and then act on it!

Here is an interrupt handler that will do what you want for this portion of your project. Just change the 'start' character to your own!

Code:

#INT_RDA   // Interrupt driven RS232 routine
void rs232_isr(void)
{
   char temp;   // local data storage
   
   temp = fgetc(PC);   // get rx data

   // If we got a '#', then the command is beginning.
   if (temp == '#')
      {   
         Index = 0;
         RxBuffer[Index] = temp;
         Index++;
         return;
      }
   // If we got a CR, then the command is completed.
   if (temp == CR)
      {
         RxBuffer[Index] = temp;
         RX_Command_Ready = TRUE;
         return;
      }

   // Save the character to the receive buffer.
   RxBuffer[Index]=temp;

   // Check for buffer overflow.
   if ( Index >= (RX_SIZE - 1) )
      Index = 0;
   else
      Index++;
}


Note that you'll have to define RxBuffer, Index, and Rx_Command_Ready' as global variables.

Good Luck!

John
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