Functions and formatted printing of time and date for RTC8563
Dependencies: mbed
I2C RTC on HIMBED
Definition der benannten Konstanten für die Register des PCF8563 (Tabelle 4)
Praeprozessor-Direktiven #define werden durch benannte Konstante ersetzt
const.h
/*********************************** name: const.h Version: 0.1 author: PE HTL BULME email: pe@bulme.at description: Named constants definitions for registers PCF8563 RTC on HIMBED M0 - LPC11U24 ***********************************/ #ifndef CONST_H #define CONST_H // Address of RTC const int RTC8563_ADR = 0xA2; // Control and status const int CONTROL1 = 0x00; const int CONTROL2 = 0x01; // Time and date const int SECONDS = 0x02; const int MINUTES = 0x03; const int HOURS = 0x04; const int DAYS = 0x05; const int WEEKDAYS = 0x06; const int MONTHS = 0x07; const int YEARS = 0x08; // Alarm const int MINUTE_ALARM = 0x09; const int HOUR_ALARM = 0x0A; const int DAY_ALARM = 0x0B; const int WEEKDAY_ALARM = 0x0C; // Clock and timer const int CLOCKOUT_FREQ = 0x0D; const int TIMER_CINTROL = 0x0E; const int _READ = 0x01; #endif
Register organisation
Terminal program
Mit einem Terminal Programm (z.B. HTERM) können die Werte von der seriellen Schnittstelle (COM-Port) angezeigt werden:
main.cpp@0:32ae5fd83722, 2015-04-05 (annotated)
- Committer:
- bulmecisco
- Date:
- Sun Apr 05 09:55:01 2015 +0000
- Revision:
- 0:32ae5fd83722
- Child:
- 1:554eb6675279
Functions and formatted printing of time and date for real time clock chip RTC8563
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bulmecisco | 0:32ae5fd83722 | 1 | /*********************************** |
bulmecisco | 0:32ae5fd83722 | 2 | name: main.cpp Version: 0.1 |
bulmecisco | 0:32ae5fd83722 | 3 | author: PE HTL BULME |
bulmecisco | 0:32ae5fd83722 | 4 | email: pe@bulme.at |
bulmecisco | 0:32ae5fd83722 | 5 | description: |
bulmecisco | 0:32ae5fd83722 | 6 | Real Time Clock (RTC8563) |
bulmecisco | 0:32ae5fd83722 | 7 | on HIMBED M0 - LPC11U24 |
bulmecisco | 0:32ae5fd83722 | 8 | prints formatted time and date values to serial port |
bulmecisco | 0:32ae5fd83722 | 9 | programed by Franz Wolf (wf@bulme.at) |
bulmecisco | 0:32ae5fd83722 | 10 | ***********************************/ |
bulmecisco | 0:32ae5fd83722 | 11 | #include "mbed.h" |
bulmecisco | 0:32ae5fd83722 | 12 | |
bulmecisco | 0:32ae5fd83722 | 13 | // Symbolic constants |
bulmecisco | 0:32ae5fd83722 | 14 | #define RTC8563_ADR 0xA2 |
bulmecisco | 0:32ae5fd83722 | 15 | #define CONTROL1 0x00 |
bulmecisco | 0:32ae5fd83722 | 16 | #define CONTROL2 0x01 |
bulmecisco | 0:32ae5fd83722 | 17 | #define SECONDS 0x02 |
bulmecisco | 0:32ae5fd83722 | 18 | #define MINUTES 0x03 |
bulmecisco | 0:32ae5fd83722 | 19 | #define HOURS 0x04 |
bulmecisco | 0:32ae5fd83722 | 20 | #define DAYS 0x05 |
bulmecisco | 0:32ae5fd83722 | 21 | #define WEEKDAYS 0x06 |
bulmecisco | 0:32ae5fd83722 | 22 | #define MONTHS 0x07 |
bulmecisco | 0:32ae5fd83722 | 23 | #define YEARS 0x08 |
bulmecisco | 0:32ae5fd83722 | 24 | #define MINUTE_ALARM 0x09 |
bulmecisco | 0:32ae5fd83722 | 25 | #define HOUR_ALARM 0x0A |
bulmecisco | 0:32ae5fd83722 | 26 | #define DAY_ALARM 0x0B |
bulmecisco | 0:32ae5fd83722 | 27 | #define WEEKDAY_ALARM 0x0C |
bulmecisco | 0:32ae5fd83722 | 28 | #define CLOCKOUT_FREQ 0x0D |
bulmecisco | 0:32ae5fd83722 | 29 | #define TIMER_CINTROL 0x0E |
bulmecisco | 0:32ae5fd83722 | 30 | #define TIMER 0x0F |
bulmecisco | 0:32ae5fd83722 | 31 | #define _READ 0x01 |
bulmecisco | 0:32ae5fd83722 | 32 | |
bulmecisco | 0:32ae5fd83722 | 33 | // Wiring |
bulmecisco | 0:32ae5fd83722 | 34 | Serial pc(USBTX, USBRX); |
bulmecisco | 0:32ae5fd83722 | 35 | I2C i2c(p28, p27); |
bulmecisco | 0:32ae5fd83722 | 36 | |
bulmecisco | 0:32ae5fd83722 | 37 | // Function prototyping |
bulmecisco | 0:32ae5fd83722 | 38 | void printTime(); |
bulmecisco | 0:32ae5fd83722 | 39 | void rtc_init(); |
bulmecisco | 0:32ae5fd83722 | 40 | char rtc_read(char address); |
bulmecisco | 0:32ae5fd83722 | 41 | void rtc_write(char address, char value); |
bulmecisco | 0:32ae5fd83722 | 42 | |
bulmecisco | 0:32ae5fd83722 | 43 | // Global variables |
bulmecisco | 0:32ae5fd83722 | 44 | char year, month, day, week; |
bulmecisco | 0:32ae5fd83722 | 45 | char hour, minute, sec; |
bulmecisco | 0:32ae5fd83722 | 46 | |
bulmecisco | 0:32ae5fd83722 | 47 | char week_val; |
bulmecisco | 0:32ae5fd83722 | 48 | char week_chr[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"}; |
bulmecisco | 0:32ae5fd83722 | 49 | |
bulmecisco | 0:32ae5fd83722 | 50 | // Functiondefinitions |
bulmecisco | 0:32ae5fd83722 | 51 | void printTime() |
bulmecisco | 0:32ae5fd83722 | 52 | { |
bulmecisco | 0:32ae5fd83722 | 53 | year = rtc_read(YEARS); |
bulmecisco | 0:32ae5fd83722 | 54 | month = rtc_read(MONTHS); |
bulmecisco | 0:32ae5fd83722 | 55 | day = rtc_read(DAYS); |
bulmecisco | 0:32ae5fd83722 | 56 | week = rtc_read(WEEKDAYS); |
bulmecisco | 0:32ae5fd83722 | 57 | hour = rtc_read(HOURS); |
bulmecisco | 0:32ae5fd83722 | 58 | minute = rtc_read(MINUTES); |
bulmecisco | 0:32ae5fd83722 | 59 | sec = rtc_read(SECONDS); |
bulmecisco | 0:32ae5fd83722 | 60 | |
bulmecisco | 0:32ae5fd83722 | 61 | //Datum Ausgabe |
bulmecisco | 0:32ae5fd83722 | 62 | pc.printf("20%x%x/%x%x/%x%x %s\n", |
bulmecisco | 0:32ae5fd83722 | 63 | ((year >> 4) & 0x03) , (year & 0x0F) , |
bulmecisco | 0:32ae5fd83722 | 64 | ((month >> 4) & 0x01), (month & 0x0F) , |
bulmecisco | 0:32ae5fd83722 | 65 | ((day >> 4) & 0x03), (day & 0x0F) , |
bulmecisco | 0:32ae5fd83722 | 66 | week_chr[week & 0x07]); |
bulmecisco | 0:32ae5fd83722 | 67 | |
bulmecisco | 0:32ae5fd83722 | 68 | //Zeit Ausgabe |
bulmecisco | 0:32ae5fd83722 | 69 | pc.printf("%x%x:%x%x:%x%x\n", |
bulmecisco | 0:32ae5fd83722 | 70 | ((hour >> 4) & 0x03), (hour & 0x0F), |
bulmecisco | 0:32ae5fd83722 | 71 | (minute >> 4), (minute & 0x0F) , |
bulmecisco | 0:32ae5fd83722 | 72 | (sec >> 4), (sec & 0x0F) ); |
bulmecisco | 0:32ae5fd83722 | 73 | } |
bulmecisco | 0:32ae5fd83722 | 74 | |
bulmecisco | 0:32ae5fd83722 | 75 | void rtc_init() |
bulmecisco | 0:32ae5fd83722 | 76 | { |
bulmecisco | 0:32ae5fd83722 | 77 | pc.printf("Setting up RTC\n"); |
bulmecisco | 0:32ae5fd83722 | 78 | |
bulmecisco | 0:32ae5fd83722 | 79 | // Formatierung |
bulmecisco | 0:32ae5fd83722 | 80 | // 2015/01/24 |
bulmecisco | 0:32ae5fd83722 | 81 | // 10:32:00 |
bulmecisco | 0:32ae5fd83722 | 82 | |
bulmecisco | 0:32ae5fd83722 | 83 | week_val = 0x04; // SAT |
bulmecisco | 0:32ae5fd83722 | 84 | rtc_write(CONTROL1, 0x20); //stop |
bulmecisco | 0:32ae5fd83722 | 85 | rtc_write(CONTROL2, 0x00); |
bulmecisco | 0:32ae5fd83722 | 86 | rtc_write(YEARS, (0x15)); |
bulmecisco | 0:32ae5fd83722 | 87 | rtc_write(MONTHS, (0x03)); |
bulmecisco | 0:32ae5fd83722 | 88 | rtc_write(DAYS, (0x13)); |
bulmecisco | 0:32ae5fd83722 | 89 | rtc_write(HOURS, (0x14)); |
bulmecisco | 0:32ae5fd83722 | 90 | rtc_write(MINUTES, (0x47)); |
bulmecisco | 0:32ae5fd83722 | 91 | rtc_write(SECONDS, (0x00)); |
bulmecisco | 0:32ae5fd83722 | 92 | rtc_write(WEEKDAYS, week_val); |
bulmecisco | 0:32ae5fd83722 | 93 | rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz |
bulmecisco | 0:32ae5fd83722 | 94 | rtc_write(TIMER_CINTROL, 0x00); |
bulmecisco | 0:32ae5fd83722 | 95 | rtc_write(CONTROL1, 0x00); //start |
bulmecisco | 0:32ae5fd83722 | 96 | } |
bulmecisco | 0:32ae5fd83722 | 97 | |
bulmecisco | 0:32ae5fd83722 | 98 | char rtc_read(char address) |
bulmecisco | 0:32ae5fd83722 | 99 | { |
bulmecisco | 0:32ae5fd83722 | 100 | char value; |
bulmecisco | 0:32ae5fd83722 | 101 | i2c.start(); |
bulmecisco | 0:32ae5fd83722 | 102 | i2c.write(RTC8563_ADR); |
bulmecisco | 0:32ae5fd83722 | 103 | i2c.write(address); |
bulmecisco | 0:32ae5fd83722 | 104 | i2c.start(); |
bulmecisco | 0:32ae5fd83722 | 105 | i2c.write(RTC8563_ADR | _READ); |
bulmecisco | 0:32ae5fd83722 | 106 | value = i2c.read(0); |
bulmecisco | 0:32ae5fd83722 | 107 | i2c.stop(); |
bulmecisco | 0:32ae5fd83722 | 108 | |
bulmecisco | 0:32ae5fd83722 | 109 | return value; |
bulmecisco | 0:32ae5fd83722 | 110 | } |
bulmecisco | 0:32ae5fd83722 | 111 | void rtc_write(char address, char value) |
bulmecisco | 0:32ae5fd83722 | 112 | { |
bulmecisco | 0:32ae5fd83722 | 113 | i2c.start(); |
bulmecisco | 0:32ae5fd83722 | 114 | i2c.write(RTC8563_ADR); |
bulmecisco | 0:32ae5fd83722 | 115 | i2c.write(address); |
bulmecisco | 0:32ae5fd83722 | 116 | i2c.write(value); |
bulmecisco | 0:32ae5fd83722 | 117 | i2c.stop(); |
bulmecisco | 0:32ae5fd83722 | 118 | } |
bulmecisco | 0:32ae5fd83722 | 119 | |
bulmecisco | 0:32ae5fd83722 | 120 | // main program |
bulmecisco | 0:32ae5fd83722 | 121 | int main() |
bulmecisco | 0:32ae5fd83722 | 122 | { |
bulmecisco | 0:32ae5fd83722 | 123 | rtc_init(); |
bulmecisco | 0:32ae5fd83722 | 124 | while(1) { |
bulmecisco | 0:32ae5fd83722 | 125 | printTime(); |
bulmecisco | 0:32ae5fd83722 | 126 | wait(1); |
bulmecisco | 0:32ae5fd83722 | 127 | } |
bulmecisco | 0:32ae5fd83722 | 128 | } |