Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Adding switch to Ds1307

Status
Not open for further replies.

koolguy

Active Member
Hello,
How to update the time setting in ds1307?


C:
#include <htc.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define SDATA RC4
#define SCLK RC3
void InitI2C(),I2C_start(void),I2C_write(char x), I2cSTOP(void);  
void i2c_Wait(void);
unsigned char DISP[3];
unsigned char Time[] = {01, 11 ,33, 22 , 10, 4, 15};
unsigned char digits[] = {  0b00010000,0b01111101,0b00100011,0b00101000,0b01001101,0b10001000,
0b10000000, 0b00111101,0X00,0b00001000};

void InitI2C()
   {
   TRISC3=1;
   TRISC4=1;
   SSPCON=0b00101000;
   SSPCON2=0;
   SSPSTAT=0b10000000;
   SSPADD=50;  //20000/(4*100);  //Fosc/(4*baud)
   SSPIF=0;
   BCLIF=0;
   }


void WaitSSP()
   {
   while(SSPIF==0);
   SSPIF=0;
   }


void SendStart()
   {
   SEN=1;
   WaitSSP();
   }

void SendStop()
   {
   PEN=1;
   WaitSSP();
   }


void SendRestart()
   {
   RSEN=1;
   WaitSSP();
   }


char SendByte(char Byte)
   {
   SSPBUF=Byte;
   WaitSSP();
   return(!ACKSTAT);
   }


char ReceiveByte()
   {
   RCEN=1;         // get byte
   WaitSSP();
   return(SSPBUF);
   }


void SendNack()
   {
   ACKDT=1;       // Not Acknowledge
   ACKEN=1;
   WaitSSP();
   }


void SendAck()
   {
   ACKDT=0;       // Acknowledge
   ACKEN=1;
   WaitSSP();
   }

void SendID(char DeviceID)
   {
   SendStart();
   if(SendByte(DeviceID)==1)     // Send ID until accepted..
     return;
   do
     {
     SendRestart();
     }while(SendByte(DeviceID)==0);
   }


char ReadRTC(char Address)
   {
   char Byte;
   SendID(0b11010000);         // Send 0xD0
  if(SendByte(Address&0xff)==0)   // Send address
     return(0);
   SendRestart();           // Re-start
   if(SendByte(0b11010001)==0)     // Read 0xD1
     return(0);
   Byte=ReceiveByte();         // Get byte
   SendNack();             // No more
   SendStop();             // Stop bus
   return(Byte);           // All done
   }

void WriteClock(char *array)       // Not used
   {
   char x;
   int tmp;
   for(x=0;x<7;x++)
     {
     tmp = array[x] / 10;
     tmp << = 4;
     tmp += array[x] % 16;
     WriteRTC(x,tmp);
     }
   }

void ReadClock(char *array)
   {
   char x;
   for(x=0;x<7;x++)
     array[x] = ReadRTC(x);       // No need to BCD-BIN
   }



           
void main(void)
   {
ANSELH = 0;
    TRISC3=1; //direction to input have be changed
    TRISC4=1;

RD0=0;
RD1=0;
RD2=0;
RD3=0;
TRISB=0X00;
TRISD=0X00;
PORTB=0B00000000;
unsigned char ch=0;
unsigned char i=0;
   int idx;         
   unsigned char DIGIT;       // A temporary store for manipulation
   TRISC = 0;             // portc tris bits
   TRISD = 0;             // portd tris bits
  InitI2C();
   while(1)
     {
     ReadClock(&Time[0]);            
     __delay_ms(250);                 
  
    DISP[2] = Time[1] & 0xf; 
     DISP[1] = Time[0]>>4; DISP[0] = Time[0] & 0xf; 
  
RD0=1;
RD1=0;
RD2=0;
RD3=0;

       PORTB = digits[DISP[0]];         // retrive 7 seg code
       __delay_ms(250);
RD0=0;
RD1=1;
RD2=0;
RD3=0;

       PORTB = digits[DISP[1]];         // retrive 7 seg code
       __delay_ms(250);

RD0=0;
RD1=0;
RD2=1;
RD3=0;

       PORTB = digits[DISP[2]];         // retrive 7 seg code

       __delay_ms(250);


     



     }
   }
 
It will be a lot more involved..

You only have 4 displays so only minutes and hours... You need a method of flashing the digit to change, then once it reads right then store the new value... You could in theory use the same four digits to read day and month and finally year...

I could do it easily, but I want you to try!!! make a function that will display the current time and flash each digit it turn... Then a facility to inc / dec that digit once all four digits are right... store those values in the RTC..

Good luck... I'm watching!!!!
 
i dnt know how to do

I know you don't.....

But every time I write something for you, it doesn't sink in so I will help with code you have posted!!
By now you should be looking at a sort of "engine" that will display what you want, when you want!!! I have shown you on many occasions how this is done..

Good luck... I'm watching!!!!
 
Hello,
What are the basic quality to be Electronics Engg?

What has that got to do with anything......

This thread is about programming.... Software Engineering is a different subject... Design and build of a embedded micro system is quite advanced stuff... I would expect a Electronic engineer to have many years fault finding, testing and repairing before being able to design complex systems...
 
how to learn better and easier way?
do you think i am better ?
and are the negative point you can see?
 
how to learn better and easier way?
I try to teach the best way.... Sometimes I don't come across well, But there are others that help me to help you!!
do you think i am better ?
Yes! I do think you are better... I just wish you would take stuff on board.... Its hard with the language difference..
and are the negative point you can see?
Many!!! You don't help yourself with your One word / line answers... You don't seem to understand my answers and then you claim that you do... You will never upset me with your questions, so ask as many as you like... I have written many time based systems and it is difficult to relay that information on... I am not a trained teacher!!
 
Hello,
Yes! I do think you are better... I just wish you would take stuff on board.... Its hard with the language difference..

I have done with switch.
The idea is that there will be 3 micro switch with:
1st will mode it will pass the array nest and
2nd and 3rd will increment and decrements the array.
RC1 RC2 RC3

if(RC1==1){
x=x+1;
}
if(x>=5){
x=0;
}
if(x==1){
if(RC2==1){
sec=sec+1;
Time[1]=sec;
PORTD=Time[1];
}
if(RC3==1){
sec=sec-1;
Time[1]=sec;
PORTD=Time[1];
}}}
 
Hello,
One thing more i want to say is that
how to use 1–12 mode hour in Ds1307?
right now the time is 18:12 i want to make in 12 or 00:00 mode only.

DISP[3] =(Time[2]>>4)&0b0001; // for hours 10th how to turn on for 11 mode??
DISP[2]=Time[2]&0xf; // for hours unit
DISP[1] = Time[1]>>4; DISP[0] = Time[1]&0xf; // for min
 

Attachments

  • dfdf.jpg
    dfdf.jpg
    230.2 KB · Views: 211
Last edited:
Bit 6 of the hour register (register 2) controls the 12:00 -> 24:00

If you set this bit then bit 5 is 0: AM or 1: PM..
If this bit is unset then bit 5 is 0x20 to allow 20~23 hours..

So you need to set bit 6 every time you write to the RTC... Then mask bit 5 for AM / PM
 
Status
Not open for further replies.

Latest threads

Back
Top