How to change date and time RTC

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
AlexanderSilvaB
Posts: 12
Joined: Tue May 08, 2012 6:19 am

How to change date and time RTC

Post by AlexanderSilvaB »

Hello, I'm trying to create a C function to change the time and date of the RTC, already have a function that reads data from the RTC. Please help me if you can.

Thank you.
Lemon X OS
LindusSystem
Member
Member
Posts: 63
Joined: Sat Apr 28, 2012 9:41 am
Location: Earth -> Asia

Re: How to change date and time RTC

Post by LindusSystem »

It is same as reading, use this code

Code: Select all

void write_cmos(unsigned char address, unsigned int value)
{
   outportb(ADDRESSREGISTER, address);
   outportb(DATAREGISTER,NMI_disable_bit << 7 | data);
}
And write to respective bits, but did u try this
Anyone has a idea of making a ntfs bootsector?if yes PM me , plz.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How to change date and time RTC

Post by Brendan »

Hi,
AlexanderSilvaB wrote:Hello, I'm trying to create a C function to change the time and date of the RTC, already have a function that reads data from the RTC. Please help me if you can.
The general idea is:
  • Split your time into "century, year, month, week, hour, minute, second" stored in temporary variables
  • Determine if the RTC is using binary format or BCD and change everything except "hour" to suit
  • Determine if the RTC is using 24 hour format or 12 hour format and change "hour" to suit (note: there's 4 cases here - 24 hour binary, 24 hour BCD, 12 hour binary, 12 hour BCD)
  • Determine where the "century" goes using ACPI (the "RTC century register" field at offset 108 in the Fixed ACPI Description Table), or discard it if there's no ACPI
  • Wait until the RTC's "update in progress" flag goes from "set" to "clear". There's 2 ways of doing this:
    • Polling: Loop until the flag becomes set, then loop until the flag becomes clear. This can waste up to 1 second of CPU time.
    • Use the RTC's "update IRQ" to avoid wasting CPU time. In this case the next step will be done in the IRQ handler.
  • Write the values you've already prepared into the corresponding RTC/CMOS registers, in order (seconds, then minutes, then hour, then ..., then century).
Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
AlexanderSilvaB
Posts: 12
Joined: Tue May 08, 2012 6:19 am

Re: How to change date and time RTC

Post by AlexanderSilvaB »

Thank you... =D>
Lemon X OS
Post Reply