Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DipCortex-EEprom RTC flw mbed
RTC/ds1302.cpp@0:f6e68b4ce169, 2015-02-09 (annotated)
- Committer:
- Backstrom
- Date:
- Mon Feb 09 13:40:46 2015 +0000
- Revision:
- 0:f6e68b4ce169
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Backstrom | 0:f6e68b4ce169 | 1 | /* |
Backstrom | 0:f6e68b4ce169 | 2 | * VFD Modular Clock - mbed |
Backstrom | 0:f6e68b4ce169 | 3 | * (C) 2011-14 Akafugu Corporation |
Backstrom | 0:f6e68b4ce169 | 4 | * |
Backstrom | 0:f6e68b4ce169 | 5 | * This program is free software; you can redistribute it and/or modify it under the |
Backstrom | 0:f6e68b4ce169 | 6 | * terms of the GNU General Public License as published by the Free Software |
Backstrom | 0:f6e68b4ce169 | 7 | * Foundation; either version 2 of the License, or (at your option) any later |
Backstrom | 0:f6e68b4ce169 | 8 | * version. |
Backstrom | 0:f6e68b4ce169 | 9 | * |
Backstrom | 0:f6e68b4ce169 | 10 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
Backstrom | 0:f6e68b4ce169 | 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
Backstrom | 0:f6e68b4ce169 | 12 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Backstrom | 0:f6e68b4ce169 | 13 | * |
Backstrom | 0:f6e68b4ce169 | 14 | */ |
Backstrom | 0:f6e68b4ce169 | 15 | |
Backstrom | 0:f6e68b4ce169 | 16 | #include "global.h" |
Backstrom | 0:f6e68b4ce169 | 17 | #include "ds1302.h" |
Backstrom | 0:f6e68b4ce169 | 18 | |
Backstrom | 0:f6e68b4ce169 | 19 | DS1302::DS1302(PinName sclk, PinName io, PinName rst) |
Backstrom | 0:f6e68b4ce169 | 20 | : m_sclk(sclk) |
Backstrom | 0:f6e68b4ce169 | 21 | , m_io(io) |
Backstrom | 0:f6e68b4ce169 | 22 | , m_rst(rst) |
Backstrom | 0:f6e68b4ce169 | 23 | { |
Backstrom | 0:f6e68b4ce169 | 24 | } |
Backstrom | 0:f6e68b4ce169 | 25 | |
Backstrom | 0:f6e68b4ce169 | 26 | void DS1302::begin() |
Backstrom | 0:f6e68b4ce169 | 27 | { |
Backstrom | 0:f6e68b4ce169 | 28 | uint8_t x; |
Backstrom | 0:f6e68b4ce169 | 29 | |
Backstrom | 0:f6e68b4ce169 | 30 | m_rst = 0; |
Backstrom | 0:f6e68b4ce169 | 31 | wait_us(2); |
Backstrom | 0:f6e68b4ce169 | 32 | m_sclk = 0; |
Backstrom | 0:f6e68b4ce169 | 33 | |
Backstrom | 0:f6e68b4ce169 | 34 | write_byte(0x8e,0); |
Backstrom | 0:f6e68b4ce169 | 35 | write_byte(0x90,0xa4); |
Backstrom | 0:f6e68b4ce169 | 36 | |
Backstrom | 0:f6e68b4ce169 | 37 | x=read_byte(0x81); |
Backstrom | 0:f6e68b4ce169 | 38 | |
Backstrom | 0:f6e68b4ce169 | 39 | if((x & 0x80) != 0) // start oscillator |
Backstrom | 0:f6e68b4ce169 | 40 | write_byte(0x80,0); |
Backstrom | 0:f6e68b4ce169 | 41 | } |
Backstrom | 0:f6e68b4ce169 | 42 | |
Backstrom | 0:f6e68b4ce169 | 43 | |
Backstrom | 0:f6e68b4ce169 | 44 | time_t DS1302::time() |
Backstrom | 0:f6e68b4ce169 | 45 | { |
Backstrom | 0:f6e68b4ce169 | 46 | return m_time; |
Backstrom | 0:f6e68b4ce169 | 47 | } |
Backstrom | 0:f6e68b4ce169 | 48 | |
Backstrom | 0:f6e68b4ce169 | 49 | struct tm* DS1302::getTime() |
Backstrom | 0:f6e68b4ce169 | 50 | { |
Backstrom | 0:f6e68b4ce169 | 51 | m_tm.tm_sec = bcd2dec(read_byte(0x81)); |
Backstrom | 0:f6e68b4ce169 | 52 | m_tm.tm_min = bcd2dec(read_byte(0x83)); |
Backstrom | 0:f6e68b4ce169 | 53 | m_tm.tm_hour = bcd2dec(read_byte(0x84)); |
Backstrom | 0:f6e68b4ce169 | 54 | |
Backstrom | 0:f6e68b4ce169 | 55 | return &m_tm; |
Backstrom | 0:f6e68b4ce169 | 56 | } |
Backstrom | 0:f6e68b4ce169 | 57 | |
Backstrom | 0:f6e68b4ce169 | 58 | void DS1302::getTime(uint8_t* hour, uint8_t* min, uint8_t* sec) |
Backstrom | 0:f6e68b4ce169 | 59 | { |
Backstrom | 0:f6e68b4ce169 | 60 | if (sec) *sec = bcd2dec(read_byte(0x81)); |
Backstrom | 0:f6e68b4ce169 | 61 | if (min) *min = bcd2dec(read_byte(0x83)); |
Backstrom | 0:f6e68b4ce169 | 62 | if (hour) *hour = bcd2dec(read_byte(0x84)); |
Backstrom | 0:f6e68b4ce169 | 63 | } |
Backstrom | 0:f6e68b4ce169 | 64 | |
Backstrom | 0:f6e68b4ce169 | 65 | void DS1302::setTime(time_t t) |
Backstrom | 0:f6e68b4ce169 | 66 | { |
Backstrom | 0:f6e68b4ce169 | 67 | } |
Backstrom | 0:f6e68b4ce169 | 68 | |
Backstrom | 0:f6e68b4ce169 | 69 | void DS1302::setTime(uint8_t hour, uint8_t min, uint8_t sec) |
Backstrom | 0:f6e68b4ce169 | 70 | { |
Backstrom | 0:f6e68b4ce169 | 71 | write_byte(0x84, dec2bcd(hour)); |
Backstrom | 0:f6e68b4ce169 | 72 | write_byte(0x82, dec2bcd(min)); |
Backstrom | 0:f6e68b4ce169 | 73 | write_byte(0x80, dec2bcd(sec)); |
Backstrom | 0:f6e68b4ce169 | 74 | } |
Backstrom | 0:f6e68b4ce169 | 75 | |
Backstrom | 0:f6e68b4ce169 | 76 | void DS1302::setTime(struct tm* tm) |
Backstrom | 0:f6e68b4ce169 | 77 | { |
Backstrom | 0:f6e68b4ce169 | 78 | /* |
Backstrom | 0:f6e68b4ce169 | 79 | write_byte(0x86, dec2bcd(day)); |
Backstrom | 0:f6e68b4ce169 | 80 | write_byte(0x88, dec2bcd(month)); |
Backstrom | 0:f6e68b4ce169 | 81 | write_byte(0x8c, dec2bcd(year)); |
Backstrom | 0:f6e68b4ce169 | 82 | write_byte(0x8a, dec2bcd(dayofweek)); |
Backstrom | 0:f6e68b4ce169 | 83 | write_byte(0x84, dec2bcd(hour)); |
Backstrom | 0:f6e68b4ce169 | 84 | write_byte(0x82, dec2bcd(min)); |
Backstrom | 0:f6e68b4ce169 | 85 | write_byte(0x80, dec2bcd(sec)); |
Backstrom | 0:f6e68b4ce169 | 86 | |
Backstrom | 0:f6e68b4ce169 | 87 | void ds1302::set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min) |
Backstrom | 0:f6e68b4ce169 | 88 | { |
Backstrom | 0:f6e68b4ce169 | 89 | write(0x86,get_bcd(day)); |
Backstrom | 0:f6e68b4ce169 | 90 | write(0x88,get_bcd(mth)); |
Backstrom | 0:f6e68b4ce169 | 91 | write(0x8c,get_bcd(year)); |
Backstrom | 0:f6e68b4ce169 | 92 | write(0x8a,get_bcd(dow)); |
Backstrom | 0:f6e68b4ce169 | 93 | write(0x84,get_bcd(hr)); |
Backstrom | 0:f6e68b4ce169 | 94 | write(0x82,get_bcd(min)); |
Backstrom | 0:f6e68b4ce169 | 95 | write(0x80,get_bcd(0)); |
Backstrom | 0:f6e68b4ce169 | 96 | } |
Backstrom | 0:f6e68b4ce169 | 97 | */ |
Backstrom | 0:f6e68b4ce169 | 98 | } |
Backstrom | 0:f6e68b4ce169 | 99 | |
Backstrom | 0:f6e68b4ce169 | 100 | void DS1302::setAlarm(time_t t) |
Backstrom | 0:f6e68b4ce169 | 101 | { |
Backstrom | 0:f6e68b4ce169 | 102 | } |
Backstrom | 0:f6e68b4ce169 | 103 | |
Backstrom | 0:f6e68b4ce169 | 104 | void DS1302::setAlarm(uint8_t hour, uint8_t min, uint8_t sec) |
Backstrom | 0:f6e68b4ce169 | 105 | { |
Backstrom | 0:f6e68b4ce169 | 106 | } |
Backstrom | 0:f6e68b4ce169 | 107 | |
Backstrom | 0:f6e68b4ce169 | 108 | struct tm* DS1302::getAlarm(void) |
Backstrom | 0:f6e68b4ce169 | 109 | { |
Backstrom | 0:f6e68b4ce169 | 110 | return 0; |
Backstrom | 0:f6e68b4ce169 | 111 | } |
Backstrom | 0:f6e68b4ce169 | 112 | |
Backstrom | 0:f6e68b4ce169 | 113 | void DS1302::getAlarm(uint8_t* hour, uint8_t* min, uint8_t* sec) |
Backstrom | 0:f6e68b4ce169 | 114 | { |
Backstrom | 0:f6e68b4ce169 | 115 | } |
Backstrom | 0:f6e68b4ce169 | 116 | |
Backstrom | 0:f6e68b4ce169 | 117 | bool DS1302::checkAlarm(void) |
Backstrom | 0:f6e68b4ce169 | 118 | { |
Backstrom | 0:f6e68b4ce169 | 119 | return false; |
Backstrom | 0:f6e68b4ce169 | 120 | } |
Backstrom | 0:f6e68b4ce169 | 121 | |
Backstrom | 0:f6e68b4ce169 | 122 | void DS1302::SQWEnable(bool enable) |
Backstrom | 0:f6e68b4ce169 | 123 | { |
Backstrom | 0:f6e68b4ce169 | 124 | } |
Backstrom | 0:f6e68b4ce169 | 125 | |
Backstrom | 0:f6e68b4ce169 | 126 | void DS1302::SQWSetFreq(enum RTC_SQW_FREQ freq) |
Backstrom | 0:f6e68b4ce169 | 127 | { |
Backstrom | 0:f6e68b4ce169 | 128 | } |
Backstrom | 0:f6e68b4ce169 | 129 | |
Backstrom | 0:f6e68b4ce169 | 130 | uint8_t DS1302::read_byte(uint8_t cmd) |
Backstrom | 0:f6e68b4ce169 | 131 | { |
Backstrom | 0:f6e68b4ce169 | 132 | uint8_t data=0; |
Backstrom | 0:f6e68b4ce169 | 133 | |
Backstrom | 0:f6e68b4ce169 | 134 | m_rst = 1; |
Backstrom | 0:f6e68b4ce169 | 135 | write_byte(cmd); |
Backstrom | 0:f6e68b4ce169 | 136 | |
Backstrom | 0:f6e68b4ce169 | 137 | m_io.input(); |
Backstrom | 0:f6e68b4ce169 | 138 | wait_us(1); |
Backstrom | 0:f6e68b4ce169 | 139 | |
Backstrom | 0:f6e68b4ce169 | 140 | for (uint8_t i = 0; i <= 7; i++) |
Backstrom | 0:f6e68b4ce169 | 141 | { |
Backstrom | 0:f6e68b4ce169 | 142 | data += m_io<<i; |
Backstrom | 0:f6e68b4ce169 | 143 | m_sclk = 1; |
Backstrom | 0:f6e68b4ce169 | 144 | wait_us(1); |
Backstrom | 0:f6e68b4ce169 | 145 | m_sclk = 0; |
Backstrom | 0:f6e68b4ce169 | 146 | wait_us(1); |
Backstrom | 0:f6e68b4ce169 | 147 | } |
Backstrom | 0:f6e68b4ce169 | 148 | |
Backstrom | 0:f6e68b4ce169 | 149 | m_rst = 0; |
Backstrom | 0:f6e68b4ce169 | 150 | |
Backstrom | 0:f6e68b4ce169 | 151 | return data; |
Backstrom | 0:f6e68b4ce169 | 152 | } |
Backstrom | 0:f6e68b4ce169 | 153 | |
Backstrom | 0:f6e68b4ce169 | 154 | void DS1302::write_byte(uint8_t cmd, uint8_t data) |
Backstrom | 0:f6e68b4ce169 | 155 | { |
Backstrom | 0:f6e68b4ce169 | 156 | m_rst = 1; |
Backstrom | 0:f6e68b4ce169 | 157 | wait_us(1); |
Backstrom | 0:f6e68b4ce169 | 158 | |
Backstrom | 0:f6e68b4ce169 | 159 | write_byte(cmd); |
Backstrom | 0:f6e68b4ce169 | 160 | write_byte(data); |
Backstrom | 0:f6e68b4ce169 | 161 | |
Backstrom | 0:f6e68b4ce169 | 162 | m_rst = 0; |
Backstrom | 0:f6e68b4ce169 | 163 | } |
Backstrom | 0:f6e68b4ce169 | 164 | |
Backstrom | 0:f6e68b4ce169 | 165 | void DS1302::write_byte(uint8_t cmd) |
Backstrom | 0:f6e68b4ce169 | 166 | { |
Backstrom | 0:f6e68b4ce169 | 167 | m_io.output(); |
Backstrom | 0:f6e68b4ce169 | 168 | |
Backstrom | 0:f6e68b4ce169 | 169 | for (uint8_t i=0; i<=7; i++) |
Backstrom | 0:f6e68b4ce169 | 170 | { |
Backstrom | 0:f6e68b4ce169 | 171 | m_io = (cmd >> i) & 0x01; |
Backstrom | 0:f6e68b4ce169 | 172 | wait_us(1); |
Backstrom | 0:f6e68b4ce169 | 173 | m_sclk = 1; |
Backstrom | 0:f6e68b4ce169 | 174 | wait_us(1); |
Backstrom | 0:f6e68b4ce169 | 175 | m_sclk = 0; |
Backstrom | 0:f6e68b4ce169 | 176 | } |
Backstrom | 0:f6e68b4ce169 | 177 | |
Backstrom | 0:f6e68b4ce169 | 178 | m_io.input(); |
Backstrom | 0:f6e68b4ce169 | 179 | } |
Backstrom | 0:f6e68b4ce169 | 180 | |
Backstrom | 0:f6e68b4ce169 | 181 | void DS1302::writeRam(uint8_t addr, uint8_t data) |
Backstrom | 0:f6e68b4ce169 | 182 | { |
Backstrom | 0:f6e68b4ce169 | 183 | write_byte(addr|0xC0, data); |
Backstrom | 0:f6e68b4ce169 | 184 | } |
Backstrom | 0:f6e68b4ce169 | 185 | |
Backstrom | 0:f6e68b4ce169 | 186 | uint8_t DS1302::readRam(uint8_t addr) |
Backstrom | 0:f6e68b4ce169 | 187 | { |
Backstrom | 0:f6e68b4ce169 | 188 | return read_byte(addr|0xC1); |
Backstrom | 0:f6e68b4ce169 | 189 | } |