Crate ds1307[−][src]
This is a platform agnostic Rust driver for the DS1307 real-time clock,
based on the embedded-hal
traits.
This driver allows you to:
- Read and set date and time in 12-hour and 24-hour format. See:
get_datetime
- Enable and disable the real-time clock. See:
set_running
- Read and write user RAM. See:
read_ram
- Control square-wave output. See:
enable_square_wave_output
The device
The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus.
The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.
The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.
Datasheet: DS1307
Usage examples (see also examples folder)
Get the year
extern crate linux_embedded_hal as hal; extern crate ds1307; use ds1307::DS1307; let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); let mut rtc = DS1307::new(dev); let year = rtc.get_year().unwrap(); println!("Year: {}", year);
Similar methods exist for month, day, weekday, hours, minutes and seconds.
Set the year
extern crate linux_embedded_hal as hal; extern crate ds1307; use ds1307::DS1307; let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); let mut rtc = DS1307::new(dev); rtc.set_year(2018).unwrap();
Similar methods exist for month, day, weekday, hours, minutes and seconds.
Set the current date and time at once
extern crate linux_embedded_hal as hal; extern crate ds1307; use ds1307::{DS1307, DateTime, Hours}; let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); let mut rtc = DS1307::new(dev); let datetime = DateTime { year: 2018, month: 08, day: 15, weekday: 4, hour: Hours::H24(19), minute: 59, second: 58 }; rtc.set_datetime(&datetime).unwrap();
Get the current date and time at once
extern crate linux_embedded_hal as hal; extern crate ds1307; use ds1307::{DS1307, Hours}; let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); let mut rtc = DS1307::new(dev); let datetime = rtc.get_datetime().unwrap(); // The hours depend on the RTC running mode match datetime.hour { Hours::H24(h) => println!("{}-{}-{}, {} {}:{}:{}", datetime.year, datetime.month, datetime.day, datetime.weekday, h, datetime.minute, datetime.second), Hours::AM(h) => println!("{}-{}-{}, {} {}:{}:{} AM", datetime.year, datetime.month, datetime.day, datetime.weekday, h, datetime.minute, datetime.second), Hours::PM(h) => println!("{}-{}-{}, {} {}:{}:{} PM", datetime.year, datetime.month, datetime.day, datetime.weekday, h, datetime.minute, datetime.second), } // This will print something like: 2018-08-15, 4 19:59:58
Read and write user RAM
extern crate linux_embedded_hal as hal; extern crate ds1307; use ds1307::DS1307; let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); let mut rtc = DS1307::new(dev); let data = [171; 3]; rtc.write_ram(2, &data).unwrap(); let mut data = [0; 3]; rtc.read_ram(2, &mut data).unwrap(); println!("{}, {}, {}", data[0], data[1], data[2]); // This will print: 171, 171, 171
Enable square-wave output and select rate
extern crate linux_embedded_hal as hal; extern crate ds1307; use ds1307::{DS1307, SQWOUTRateBits}; let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); let mut rtc = DS1307::new(dev); rtc.enable_square_wave_output().unwrap(); let rate_bits = SQWOUTRateBits { rs0: true, rs1: false }; rtc.set_square_wave_output_rate(rate_bits).unwrap();
Structs
DS1307 |
DS1307 driver |
DateTime |
Date and time |
SQWOUTRateBits |
Square-wave output rate bits. |
Enums
Error |
All possible errors in this crate |
Hours |
Hours in either 12-hour (AM/PM) or 24-hour format |