Crate ds1302[−][src]
Expand description
DS1302 platform agnostic driver crate
About
DS1302 is a real time clock/calendar (RTCC) chip, which communicates with SPI interface.
The device provides seconds, minutes, hours, day, date, month, and year information.
Driver is based on embedded-hal
traits.
Datasheet: DS1302
The driver allows to:
- Read and set clock and calendar data in 12-hour or 24-hour format .
- Changing hour format without reseting it.
set_clock_mode()
.
The driver does not allow to:
- Currently using RAM is not supported.
Initialization
// External crates for IO and strings manipulation
use core::fmt::Write;
use heapless::String;
// DS1302 driver crate
use ds1302::{DS1302, Hours, Clock, Calendar, Mode as ds1302_mode};
// Create with DS1302::new(), specify hour format mode: ds1302_mode::Hour12, in this case
let mut ds1302 = DS1302::new(spi, cs, ds1302_mode::Hour12).unwrap();
Read time and date
let mut data = String::<U32>::from(" ");
let mut text = " ";
let cl = ds1302.get_clock_calendar().unwrap();
// Check the current mode. If it is Hour12, check AM/PM value. Please refer to table description below.
match ds1302.mode {
ds1302_mode::Hour12 => {
if cl.0.hours.am_pm == 1 {text = " PM"}
else {text = " AM"}
}
ds1302_mode::Hour24 => text = ""
}
// Glue cl reads in string called "data", and use it later ...
let _=write!(data,"{} {}.{}.{}\n{:02}:{:02}:{:02} {}",
cl.1.day, cl.1.date, cl.1.month, cl.1.year,
cl.0.hours.hours, cl.0.minutes, cl.0.seconds, text);
From lib.rs:
pub struct Hours {
pub hours: u8,
pub am_pm: u8,
}
pub enum Mode {
Hour24,
Hour12,
}
Variants of time format depending on Mode::Hour24, Mode::Hour12 and Hours::am_mp
Mode | am_pm | time format |
---|---|---|
Hour12 | 0 | AM |
Hour12 | 1 | PM |
Hour24 | 0 | - |
Hour24 | 1 | - |
Set time and date
let h = Hours {hours: 4, am_pm: 1};
let clk = Clock {
hours: h,
minutes: 29,
seconds: 0
};
let cal = Calendar {
day: 2,
date: 27,
month: 10,
year: 2020
};
ds1302.set_clock_calendar(clk, cal).unwrap();
Structs
Calendar information
Clock information
DS1302 RTCC driver
Hour information: 12-hour (AM/PM) or 24-hour
Enums
Hour format: 12-hour (AM/PM) or 24-hour
Traits
For timing ds1302
uses fugit crate which only provides Duration
and Instant
types.
It does not provide any clock or timer traits.
Therefore ds1302
has its own Delay
trait that provides all timing capabilities that are needed for the library.
User must implement this trait for the timer by itself.