Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Ds1307 12-hour format.

Status
Not open for further replies.

Micro_brain

Member level 1
Joined
Oct 8, 2006
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
India
Activity points
1,503
ds1307 mikrobasic

Dear Electronics friends.
I want to display on lcd the current date and the time with 12-hour format.
Please tell me , how set 12-hour format mode.
(16F877 and CCSC)


Thanks micro_brain.
 

ds1307.c

To set 12h format, you should set bit 6 from reg. address 0x02. After that, the bit 5 from the same register indicate the AM/PM.
Here you can find a similar project with source code in C (microC not CCSC)
 
ds1307 interrupt

Thanks cristianp.
Please tell me how enable 12-hour format with this file
Code:
#include <16F876a.h> 
#include <ds1307.h> 
#fuses NOWDT,XT, PUT, NOPROTECT, BROWNOUT ,NODEBUG ,NOLVP
#use delay(clock=4000000) 
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
#zero_ram

int DATE,MTH,YR,HR,MT,SECDS,DOW; // 

void main() { 	

	setup_adc_ports(NO_ANALOGS);
	setup_adc(ADC_OFF);
	setup_spi(FALSE);
	setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
	setup_comparator(NC_NC_NC_NC);
	setup_vref(FALSE);

	ds1307_init(); 
	while(1){

		ds1307_get_time(HR,MT,SECDS);
		ds1307_get_date(DATE,MTH,YR,DOW);	
	
		printf("\rTime %2D:%02D:%02D",HR,MT,SECDS);
		printf("      Date  %2D/%02D/%02D",DATE,MTH,YR);
		delay_ms(100);
		
		if(!input(PIN_C0 ))		
			ds1307_set_date_time(28,1,8,2,11,50,0);  // Time 11:50:00      Date   28/01/08	
	}
}

this is 24-hour format. working Ok
but I want to change 12-hour format (AM/PM)
Please help me.

DS1307.C
Code:
////////////////////////////////////////////////////////////////////////////////// 
///                               DS1307.C                                   /// 
///                     Driver for Real Time Clock                           /// 
///                                                                          /// 
/// ds1307_init() - Enable oscillator without clearing the seconds register -/// 
///                 used when PIC loses power and DS1307 run from 3V BAT     /// 
///               - Disable squarewave output                                /// 
///                                                                          /// 
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec)  Set the date/time   /// 
///                                                                          /// 
/// ds1307_get_date(day,mth,year,dow)               Get the date             /// 
///                                                                          /// 
/// ds1307_get_time(hr,min,sec)                     Get the time             /// 
///                                                                          /// 
//////////////////////////////////////////////////////////////////////////////// 
#use delay(clock=4000000)
#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); 

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, BYTE 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_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(); 

} 

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


Thanks again.
 

proteus ds1307

I hope that the code works with the following changes:

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{

BYTE pm;

sec &= 0x7F;
hr &= 0x3F;

if (hr > 12)
{
hr -= 12;
pm = 0x20;
}
else pm = 0;

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) | pm | 0x40); // 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_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
BYTE hr_reg;

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_reg = i2c_read() & 0x3f;
i2c_stop();

if (hr_reg && 0x20)
{
hr = bcd2bin(hr_reg & 0x1F) ;
hr += 12;
}
else
hr = bcd2bin(hr_reg & 0x1F) ;

}
 

ds1307 proteus

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

Added after 4 minutes:

hey, it's me again.

by the way, i'm using C language and CCS C Compiler.

thanks again.

-elizabeth ann
 

ds1307 12 hours

Hi,
I do not use CCS but I might give u some good suggestions

1. you can use DS1337 it has alarms setting and dedicated pins of alarms, u may find my program (PC based) that give u good support.
2. for DS1307 u can use 1Hz signal from DS1307 pin to get interrupt and then compare the current time with the SET time.

Also you can use both above methods with power saving modes.

Thanks
sadat007
 

mikrobasic ds1307

hi sadat007, many thanks for your immediate response...

i really like your idea on suggestion #2, but frankly i'm still a newbie on using interrupts just the same.:cry: i hope you can give me some good tips or techniques on how to do it.

i have seen the datasheet of DS1337 and i agree with you, it's a lot easier to configure than DS1307.

the problem is, i am using PROTEUS VSM 7.1 as my simulation tool and it does not have a DS1337 model simulator. do you know of any other microcontroller simulators or a more updated version of PROTEUS that includes a DS1337 model simulator?

if in case there aren't any better simulators to provide a DS1337 model, how do i make things work using DS1307? i only have roughly 2 weeks to get everything working, or i won't make it to our graduation. :cry:

also, you said in suggestion #2 that i can just compare the current time with the SET time. i really hope you could give me a sample code for that...(more preferably in C language, please.thanks!)

thank you so much.

elizabeth ann
 

ds1307.exe

Hi. elizabeth
I wish to start properly programming in C but until now I have quite good setup of BASIC compilers and also quite happy with them.

Sorry I could not help u in this..... may be some one else on this forum will do.

For the Proteus yes I have 7.2 but no DS1337. Remember, using DS1337 is very simple just search DS1337.exe on edaboard and you will find my program that will help u a lot in setting the DS1337

If you like I can put here some codes in mikroBasic regarding interrupt and DS1337 handling.

Note that DS1337 and DS1307 are same for date/time writing and reading.

Thanks
sadat007
 
ds1307 c

sadat007 said:
Hi. elizabeth
I wish to start properly programming in C but until now I have quite good setup of BASIC compilers and also quite happy with them.

Sorry I could not help u in this..... may be some one else on this forum will do.

For the Proteus yes I have 7.2 but no DS1337. Remember, using DS1337 is very simple just search DS1337.exe on edaboard and you will find my program that will help u a lot in setting the DS1337

If you like I can put here some codes in mikroBasic regarding interrupt and DS1337 handling.

Note that DS1337 and DS1307 are same for date/time writing and reading.

Thanks
sadat007


thanks for the immediate response.

do you know where i can secure a copy of PROTEUS 7.2? also, i would really appreciate it if you could post your codes (BASIC language would be fine, i can translate that in C quite well because they're almost the same, do you agree?) .. i would really like to know more about the interrupt and DS1337 handling that you mentioned. also, setting the alarm and clock is my primary concern, be it in C or Basic. i hope you can help me with that.

thanks a million!

elizabeth ann
 

how can set 12hour mode in ds1307?

Hi,
Please do not ask for copyrighted software. it will be good to edit your post a little.

It is really hard for me to help you as I used mikroBasic, PIC18F4525 and DS1337. So here I am trying to explain you and you will have to implement by your self in CCS.

Please try my program DS1337.exe
Hope this will help u a lot.
Thanks
Sadat007
:arrow::arrow::arrow::!::?::?::idea::idea::D:D

In 16F877 there is one external interrupt INT on RB0 (pin-33 of DIP)

The bit-6 (INTEDG) of OPTION_REG will decide
1 = Interrupt on rising edge of RB0/INT pin
0 = Interrupt on falling edge of RB0/INT pin
I think it should be set to Zero
The bit-7 (RBPU) of OPTION_REG will decide
1 = PORTB pull-ups are disabled
0 = PORTB pull-ups are enabled by individual port latch values
I recommend u to put zero here and connect one external 10k pullup resistor on RB0/INT pin.


Now in INTCON REGISTER
GIE (bit-7) =0 to disable all interrupts and 1= all enable it
INTE (bit-4) =0 to disable INT and 1= all enable it
INTF (bit-1) =1 means The RB0/INT external interrupt occurred (must be cleared in software)
and =0 means external interrupt did not occur. This bit should be check to verify that the INT occurred
and should be clear after all routines performed (that need to be performed).


/////////////////////////////////////////////
interrupt sub routine

check for which interrupt occurred in your case it can be INT
so check for INTF flag/bit as described above

so finally if the if INTF=1 then u can call a routine of reading and comparing
the time and if matched generate an alarm

Also that it may be required to clear the bit INTCON.INTF = 0 when all done

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


Writing to DS1307

I2C_Start
I2C_WR($D0) 'I2C address 1101000 of DS1307 for writing mode Please recheck it
I2C_WR(pointer) 'normally pointer=0 which is the address of seconds

'following 3 lines may not be required
I2C_Repeated_Start
I2C_WR($D0) 'I2C address 1101000 of DS1307 for writing mode Please recheck it
I2C_WR(pointer) 'normally pointer=0 which is the address of seconds

I2C_WR(Seconds)
I2C_WR(Minutes)
I2C_WR(Hours)
I2C_WR(Day Of Week)
I2C_WR(DATE)
I2C_WR(Month)
I2C_WR(YEAR)
I2C_WR(10) '1Hz enabled 10 is in HEX
I2C_Stop

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


Reading DS1307

I2C_Start
I2C_Wr($D0) 'I2C address 1101000 of DS1307 for WRITING mode Please recheck it
I2C_Wr($00) 'normally pointer=0 which is the address of seconds
I2C_Repeated_Start
I2C_Wr($D1) 'I2C address of DS1307 for READING mode Please recheck it
Sec = I2C_Rd(1)
........
.........
...........
read one by one up to year
 
ds1307 bascom 24-h mode

sadat007 said:
Hi,
Please do not ask for copyrighted software. it will be good to edit your post a little.

It is really hard for me to help you as I used mikroBasic, PIC18F4525 and DS1337. So here I am trying to explain you and you will have to implement by your self in CCS.

Please try my program DS1337.exe
Hope this will help u a lot.
Thanks
Sadat007
:arrow::arrow::arrow::!::?::?::idea::idea::D:D

In 16F877 there is one external interrupt INT on RB0 (pin-33 of DIP)

The bit-6 (INTEDG) of OPTION_REG will decide
1 = Interrupt on rising edge of RB0/INT pin
0 = Interrupt on falling edge of RB0/INT pin
I think it should be set to Zero
The bit-7 (RBPU) of OPTION_REG will decide
1 = PORTB pull-ups are disabled
0 = PORTB pull-ups are enabled by individual port latch values
I recommend u to put zero here and connect one external 10k pullup resistor on RB0/INT pin.


Now in INTCON REGISTER
GIE (bit-7) =0 to disable all interrupts and 1= all enable it
INTE (bit-4) =0 to disable INT and 1= all enable it
INTF (bit-1) =1 means The RB0/INT external interrupt occurred (must be cleared in software)
and =0 means external interrupt did not occur. This bit should be check to verify that the INT occurred
and should be clear after all routines performed (that need to be performed).


/////////////////////////////////////////////
interrupt sub routine

check for which interrupt occurred in your case it can be INT
so check for INTF flag/bit as described above

so finally if the if INTF=1 then u can call a routine of reading and comparing
the time and if matched generate an alarm

Also that it may be required to clear the bit INTCON.INTF = 0 when all done

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


Writing to DS1307

I2C_Start
I2C_WR($D0) 'I2C address 1101000 of DS1307 for writing mode Please recheck it
I2C_WR(pointer) 'normally pointer=0 which is the address of seconds

'following 3 lines may not be required
I2C_Repeated_Start
I2C_WR($D0) 'I2C address 1101000 of DS1307 for writing mode Please recheck it
I2C_WR(pointer) 'normally pointer=0 which is the address of seconds

I2C_WR(Seconds)
I2C_WR(Minutes)
I2C_WR(Hours)
I2C_WR(Day Of Week)
I2C_WR(DATE)
I2C_WR(Month)
I2C_WR(YEAR)
I2C_WR(10) '1Hz enabled 10 is in HEX
I2C_Stop

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


Reading DS1307

I2C_Start
I2C_Wr($D0) 'I2C address 1101000 of DS1307 for WRITING mode Please recheck it
I2C_Wr($00) 'normally pointer=0 which is the address of seconds
I2C_Repeated_Start
I2C_Wr($D1) 'I2C address of DS1307 for READING mode Please recheck it
Sec = I2C_Rd(1)
........
.........
...........
read one by one up to year

thanks for the thought, i have already edited my post.

and thanks a lot for the program, i appreciate the effort! i'll tell you about the developments of my project as soon as i get these ideas together.

thanks a lot..

elizabeth ann
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top