a
Dependencies: mbed
main.cpp@0:9e434e66545c, 2015-01-28 (annotated)
- Committer:
- bulmenwt
- Date:
- Wed Jan 28 07:55:08 2015 +0000
- Revision:
- 0:9e434e66545c
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bulmenwt | 0:9e434e66545c | 1 | /*M0-BOARD LPC11U24 VERSION 1.0 WF 01_2015 |
bulmenwt | 0:9e434e66545c | 2 | BULME Graz / Elektronik |
bulmenwt | 0:9e434e66545c | 3 | Autor: DI Franz Wolf |
bulmenwt | 0:9e434e66545c | 4 | |
bulmenwt | 0:9e434e66545c | 5 | RTC NXP |
bulmenwt | 0:9e434e66545c | 6 | PCF 8563 |
bulmenwt | 0:9e434e66545c | 7 | |
bulmenwt | 0:9e434e66545c | 8 | Datei: WF_LDR_V1/main.cpp |
bulmenwt | 0:9e434e66545c | 9 | */ |
bulmenwt | 0:9e434e66545c | 10 | |
bulmenwt | 0:9e434e66545c | 11 | #include "mbed.h" |
bulmenwt | 0:9e434e66545c | 12 | |
bulmenwt | 0:9e434e66545c | 13 | |
bulmenwt | 0:9e434e66545c | 14 | #define RTC8563_ADR 0xA2 |
bulmenwt | 0:9e434e66545c | 15 | |
bulmenwt | 0:9e434e66545c | 16 | #define CONTROL1 0x00 |
bulmenwt | 0:9e434e66545c | 17 | #define CONTROL2 0x01 |
bulmenwt | 0:9e434e66545c | 18 | #define SECONDS 0x02 |
bulmenwt | 0:9e434e66545c | 19 | #define MINUTES 0x03 |
bulmenwt | 0:9e434e66545c | 20 | #define HOURS 0x04 |
bulmenwt | 0:9e434e66545c | 21 | #define DAYS 0x05 |
bulmenwt | 0:9e434e66545c | 22 | #define WEEKDAYS 0x06 |
bulmenwt | 0:9e434e66545c | 23 | #define MONTHS 0x07 |
bulmenwt | 0:9e434e66545c | 24 | #define YEARS 0x08 |
bulmenwt | 0:9e434e66545c | 25 | #define MINUTE_ALARM 0x09 |
bulmenwt | 0:9e434e66545c | 26 | #define HOUR_ALARM 0x0A |
bulmenwt | 0:9e434e66545c | 27 | #define DAY_ALARM 0x0B |
bulmenwt | 0:9e434e66545c | 28 | #define WEEKDAY_ALARM 0x0C |
bulmenwt | 0:9e434e66545c | 29 | #define CLOCKOUT_FREQ 0x0D |
bulmenwt | 0:9e434e66545c | 30 | #define TIMER_CINTROL 0x0E |
bulmenwt | 0:9e434e66545c | 31 | #define TIMER 0x0F |
bulmenwt | 0:9e434e66545c | 32 | #define _READ 0x01 |
bulmenwt | 0:9e434e66545c | 33 | |
bulmenwt | 0:9e434e66545c | 34 | Serial pc(USBTX, USBRX); // tx, rx |
bulmenwt | 0:9e434e66545c | 35 | I2C i2c(p28, p27); |
bulmenwt | 0:9e434e66545c | 36 | |
bulmenwt | 0:9e434e66545c | 37 | char year, month, day, week; |
bulmenwt | 0:9e434e66545c | 38 | char hour, minute, sec; |
bulmenwt | 0:9e434e66545c | 39 | |
bulmenwt | 0:9e434e66545c | 40 | char week_val; |
bulmenwt | 0:9e434e66545c | 41 | char week_chr[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"}; |
bulmenwt | 0:9e434e66545c | 42 | |
bulmenwt | 0:9e434e66545c | 43 | //************************************ |
bulmenwt | 0:9e434e66545c | 44 | // write I2C |
bulmenwt | 0:9e434e66545c | 45 | void rtc_write(char address, char value) |
bulmenwt | 0:9e434e66545c | 46 | { |
bulmenwt | 0:9e434e66545c | 47 | i2c.start(); |
bulmenwt | 0:9e434e66545c | 48 | i2c.write(RTC8563_ADR); |
bulmenwt | 0:9e434e66545c | 49 | i2c.write(address); |
bulmenwt | 0:9e434e66545c | 50 | i2c.write(value); |
bulmenwt | 0:9e434e66545c | 51 | i2c.stop(); |
bulmenwt | 0:9e434e66545c | 52 | } |
bulmenwt | 0:9e434e66545c | 53 | |
bulmenwt | 0:9e434e66545c | 54 | // read I2C |
bulmenwt | 0:9e434e66545c | 55 | char rtc_read(char address) |
bulmenwt | 0:9e434e66545c | 56 | { |
bulmenwt | 0:9e434e66545c | 57 | char value; |
bulmenwt | 0:9e434e66545c | 58 | i2c.start(); |
bulmenwt | 0:9e434e66545c | 59 | i2c.write(RTC8563_ADR); |
bulmenwt | 0:9e434e66545c | 60 | i2c.write(address); |
bulmenwt | 0:9e434e66545c | 61 | i2c.start(); |
bulmenwt | 0:9e434e66545c | 62 | i2c.write(RTC8563_ADR | _READ); |
bulmenwt | 0:9e434e66545c | 63 | value = i2c.read(0); |
bulmenwt | 0:9e434e66545c | 64 | i2c.stop(); |
bulmenwt | 0:9e434e66545c | 65 | |
bulmenwt | 0:9e434e66545c | 66 | return value; |
bulmenwt | 0:9e434e66545c | 67 | } |
bulmenwt | 0:9e434e66545c | 68 | |
bulmenwt | 0:9e434e66545c | 69 | //Zeit einstellen der RTC |
bulmenwt | 0:9e434e66545c | 70 | void rtc_init() |
bulmenwt | 0:9e434e66545c | 71 | { |
bulmenwt | 0:9e434e66545c | 72 | pc.printf("Setting up RTC\n"); |
bulmenwt | 0:9e434e66545c | 73 | |
bulmenwt | 0:9e434e66545c | 74 | // Formatierung |
bulmenwt | 0:9e434e66545c | 75 | // 2015/01/24 |
bulmenwt | 0:9e434e66545c | 76 | // 10:32:00 |
bulmenwt | 0:9e434e66545c | 77 | |
bulmenwt | 0:9e434e66545c | 78 | week_val = 0x05; // SAT |
bulmenwt | 0:9e434e66545c | 79 | rtc_write(CONTROL1, 0x20); //stop |
bulmenwt | 0:9e434e66545c | 80 | rtc_write(CONTROL2, 0x00); |
bulmenwt | 0:9e434e66545c | 81 | rtc_write(YEARS, (0x15)); |
bulmenwt | 0:9e434e66545c | 82 | rtc_write(MONTHS, (0x01)); |
bulmenwt | 0:9e434e66545c | 83 | rtc_write(DAYS, (0x24)); |
bulmenwt | 0:9e434e66545c | 84 | rtc_write(HOURS, (0x10)); |
bulmenwt | 0:9e434e66545c | 85 | rtc_write(MINUTES, (0x34)); |
bulmenwt | 0:9e434e66545c | 86 | rtc_write(SECONDS, (0x00)); |
bulmenwt | 0:9e434e66545c | 87 | rtc_write(WEEKDAYS, week_val); |
bulmenwt | 0:9e434e66545c | 88 | rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz |
bulmenwt | 0:9e434e66545c | 89 | rtc_write(TIMER_CINTROL, 0x00); |
bulmenwt | 0:9e434e66545c | 90 | rtc_write(CONTROL1, 0x00); //start |
bulmenwt | 0:9e434e66545c | 91 | } |
bulmenwt | 0:9e434e66545c | 92 | //************************************ |
bulmenwt | 0:9e434e66545c | 93 | |
bulmenwt | 0:9e434e66545c | 94 | int main() { |
bulmenwt | 0:9e434e66545c | 95 | |
bulmenwt | 0:9e434e66545c | 96 | //RTC_init |
bulmenwt | 0:9e434e66545c | 97 | //rtc_init(); |
bulmenwt | 0:9e434e66545c | 98 | |
bulmenwt | 0:9e434e66545c | 99 | pc.printf("RTC8563 CLOCK" ); |
bulmenwt | 0:9e434e66545c | 100 | wait(2.0); |
bulmenwt | 0:9e434e66545c | 101 | |
bulmenwt | 0:9e434e66545c | 102 | while(1) { |
bulmenwt | 0:9e434e66545c | 103 | |
bulmenwt | 0:9e434e66545c | 104 | //lesen der Parameter vom RTC |
bulmenwt | 0:9e434e66545c | 105 | year = rtc_read(YEARS); |
bulmenwt | 0:9e434e66545c | 106 | month = rtc_read(MONTHS); |
bulmenwt | 0:9e434e66545c | 107 | day = rtc_read(DAYS); |
bulmenwt | 0:9e434e66545c | 108 | week = rtc_read(WEEKDAYS); |
bulmenwt | 0:9e434e66545c | 109 | hour = rtc_read(HOURS); |
bulmenwt | 0:9e434e66545c | 110 | minute = rtc_read(MINUTES); |
bulmenwt | 0:9e434e66545c | 111 | sec = rtc_read(SECONDS); |
bulmenwt | 0:9e434e66545c | 112 | |
bulmenwt | 0:9e434e66545c | 113 | //Datum Ausgabe |
bulmenwt | 0:9e434e66545c | 114 | pc.printf("20%x%x/%x%x/%x%x %s\n", |
bulmenwt | 0:9e434e66545c | 115 | ((year >> 4) & 0x03) , (year & 0x0F) , |
bulmenwt | 0:9e434e66545c | 116 | ((month >> 4) & 0x01), (month & 0x0F) , |
bulmenwt | 0:9e434e66545c | 117 | ((day >> 4) & 0x03), (day & 0x0F) , |
bulmenwt | 0:9e434e66545c | 118 | week_chr[week & 0x07]); |
bulmenwt | 0:9e434e66545c | 119 | |
bulmenwt | 0:9e434e66545c | 120 | //Zeit Ausgabe |
bulmenwt | 0:9e434e66545c | 121 | pc.printf("%x%x:%x%x:%x%x\n", |
bulmenwt | 0:9e434e66545c | 122 | ((hour >> 4) & 0x03), (hour & 0x0F), |
bulmenwt | 0:9e434e66545c | 123 | (minute >> 4), (minute & 0x0F) , |
bulmenwt | 0:9e434e66545c | 124 | (sec >> 4), (sec & 0x0F) ); |
bulmenwt | 0:9e434e66545c | 125 | |
bulmenwt | 0:9e434e66545c | 126 | wait(1.0); |
bulmenwt | 0:9e434e66545c | 127 | } |
bulmenwt | 0:9e434e66545c | 128 | } |
bulmenwt | 0:9e434e66545c | 129 | |
bulmenwt | 0:9e434e66545c | 130 |