Timestamp problem

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
I know that a bit of long code, but could you told me if the way I work
to calculate the TimeStamp is good?. The OneWire code work

Thanks
Sylvain Bissonnette

void OneWireSetTime(TimeStruct *Time)
{
    ulong TimeStamp;
    ushort TimeBuffer[4];

    TimeStamp = Time->Hour24;
    TimeStamp *= 3600UL;
    TimeStamp += (Time->Minute * 60UL);
    TimeStamp += Time->Second;

    TimeBuffer[0] = (TimeStamp & 0x000000ff) >> 0;
    TimeBuffer[1] = (TimeStamp & 0x0000ff00) >> 8;
    TimeBuffer[2] = (TimeStamp & 0x00ff0000) >> 16;
    TimeBuffer[3] = (TimeStamp & 0xff000000) >> 24;

    OneWireReset(ONEWIRE_PIN_DEVICE);
    OneWireWriteByte((ONEWIRE_PIN_DEVICE),0xcc); // Skip ROM
    OneWireWriteByte((ONEWIRE_PIN_DEVICE),0x99); // Write command
    OneWireWriteByte((ONEWIRE_PIN_DEVICE),0xac); // Write 32k768 on
    OneWireWriteByte((ONEWIRE_PIN_DEVICE),TimeBuffer[0]);
    OneWireWriteByte((ONEWIRE_PIN_DEVICE),TimeBuffer[1]);
    OneWireWriteByte((ONEWIRE_PIN_DEVICE),TimeBuffer[2]);
    OneWireWriteByte((ONEWIRE_PIN_DEVICE),TimeBuffer[3]);
}

/**********************************************************

Name:           void OneWireGetTime(void)

Description:

Input:          none

Output:         none

Misc:

**********************************************************/
void OneWireGetTime(TimeStruct *Time)
{
    ulong TimeStamp = 0;
    ushort TimeBuffer[4];
    ushort i;

    OneWireReset(ONEWIRE_PIN_DEVICE);

    OneWireWriteByte(ONEWIRE_PIN_DEVICE,0xcc);  // Skip ROM
    OneWireWriteByte(ONEWIRE_PIN_DEVICE,0x66);  // Read Time
    OneWireReadByte(ONEWIRE_PIN_DEVICE);        // Controle bytes

    for (i=0;i<4;i++) TimeBuffer[i] = OneWireReadByte(ONEWIRE_PIN_DEVICE);// Read all time bytes

    TimeStamp =  TimeBuffer[3];
    TimeStamp =  TimeStamp << 8;
    TimeStamp += TimeBuffer[2];
    TimeStamp =  TimeStamp << 8;
    TimeStamp += TimeBuffer[1];
    TimeStamp =  TimeStamp << 8;
    TimeStamp += TimeBuffer[0];

    Time->Hour24   = (TimeStamp % 86400UL) / 3600UL;
    Time->Minute = (TimeStamp % 3600UL)  / 60UL;
    Time->Second = (TimeStamp % 60UL);

    if (Time->Hour24 >= 12) Time->Hour = Time->Hour24 - 12;
    else Time->Hour = Time->Hour24;
}

Yours truly,
Sylvain Bissonnette
www.microsyl.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please use code tags when posting code.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

to calculate the TimeStamp is good?

Surely that depends what format you need it in? Is it "Unixtime" you are trying to create?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don`t need any special format, the RTC is a simple 1 second adder in a 32 bit register, I need to save my time in the RTC and be able to get it back, so the format is not important

Yours truly,
Sylvain Bissonnette
www.microsyl.com