a

Dependencies:   mbed

Committer:
bulmenwt
Date:
Wed Jan 28 07:54:53 2015 +0000
Revision:
0:a1bcc4b82ad5
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bulmenwt 0:a1bcc4b82ad5 1 /*M0-BOARD LPC11U24 VERSION 1.0 WF 01_2015
bulmenwt 0:a1bcc4b82ad5 2 BULME Graz / Elektronik
bulmenwt 0:a1bcc4b82ad5 3 Autor: DI Franz Wolf
bulmenwt 0:a1bcc4b82ad5 4
bulmenwt 0:a1bcc4b82ad5 5 RTC NXP
bulmenwt 0:a1bcc4b82ad5 6 PCF 8563
bulmenwt 0:a1bcc4b82ad5 7
bulmenwt 0:a1bcc4b82ad5 8 Datei: WF_LDR_V1/main.cpp
bulmenwt 0:a1bcc4b82ad5 9 */
bulmenwt 0:a1bcc4b82ad5 10
bulmenwt 0:a1bcc4b82ad5 11 #include "mbed.h"
bulmenwt 0:a1bcc4b82ad5 12
bulmenwt 0:a1bcc4b82ad5 13
bulmenwt 0:a1bcc4b82ad5 14 #define RTC8563_ADR 0xA2
bulmenwt 0:a1bcc4b82ad5 15
bulmenwt 0:a1bcc4b82ad5 16 #define CONTROL1 0x00
bulmenwt 0:a1bcc4b82ad5 17 #define CONTROL2 0x01
bulmenwt 0:a1bcc4b82ad5 18 #define SECONDS 0x02
bulmenwt 0:a1bcc4b82ad5 19 #define MINUTES 0x03
bulmenwt 0:a1bcc4b82ad5 20 #define HOURS 0x04
bulmenwt 0:a1bcc4b82ad5 21 #define DAYS 0x05
bulmenwt 0:a1bcc4b82ad5 22 #define WEEKDAYS 0x06
bulmenwt 0:a1bcc4b82ad5 23 #define MONTHS 0x07
bulmenwt 0:a1bcc4b82ad5 24 #define YEARS 0x08
bulmenwt 0:a1bcc4b82ad5 25 #define MINUTE_ALARM 0x09
bulmenwt 0:a1bcc4b82ad5 26 #define HOUR_ALARM 0x0A
bulmenwt 0:a1bcc4b82ad5 27 #define DAY_ALARM 0x0B
bulmenwt 0:a1bcc4b82ad5 28 #define WEEKDAY_ALARM 0x0C
bulmenwt 0:a1bcc4b82ad5 29 #define CLOCKOUT_FREQ 0x0D
bulmenwt 0:a1bcc4b82ad5 30 #define TIMER_CINTROL 0x0E
bulmenwt 0:a1bcc4b82ad5 31 #define TIMER 0x0F
bulmenwt 0:a1bcc4b82ad5 32 #define _READ 0x01
bulmenwt 0:a1bcc4b82ad5 33
bulmenwt 0:a1bcc4b82ad5 34 InterruptIn alarm(P1_28);
bulmenwt 0:a1bcc4b82ad5 35
bulmenwt 0:a1bcc4b82ad5 36 DigitalOut myled1(LED1);
bulmenwt 0:a1bcc4b82ad5 37
bulmenwt 0:a1bcc4b82ad5 38 Serial pc(USBTX, USBRX); // tx, rx
bulmenwt 0:a1bcc4b82ad5 39 I2C i2c(p28, p27);
bulmenwt 0:a1bcc4b82ad5 40
bulmenwt 0:a1bcc4b82ad5 41 char year, month, day, week;
bulmenwt 0:a1bcc4b82ad5 42 char hour, minute, sec;
bulmenwt 0:a1bcc4b82ad5 43
bulmenwt 0:a1bcc4b82ad5 44 char hour_a, minute_a, week_a;
bulmenwt 0:a1bcc4b82ad5 45
bulmenwt 0:a1bcc4b82ad5 46
bulmenwt 0:a1bcc4b82ad5 47 char week_val;
bulmenwt 0:a1bcc4b82ad5 48 char week_chr[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
bulmenwt 0:a1bcc4b82ad5 49
bulmenwt 0:a1bcc4b82ad5 50 //************************************
bulmenwt 0:a1bcc4b82ad5 51 // write I2C
bulmenwt 0:a1bcc4b82ad5 52 void rtc_write(char address, char value)
bulmenwt 0:a1bcc4b82ad5 53 {
bulmenwt 0:a1bcc4b82ad5 54 i2c.start();
bulmenwt 0:a1bcc4b82ad5 55 i2c.write(RTC8563_ADR);
bulmenwt 0:a1bcc4b82ad5 56 i2c.write(address);
bulmenwt 0:a1bcc4b82ad5 57 i2c.write(value);
bulmenwt 0:a1bcc4b82ad5 58 i2c.stop();
bulmenwt 0:a1bcc4b82ad5 59 }
bulmenwt 0:a1bcc4b82ad5 60
bulmenwt 0:a1bcc4b82ad5 61 // read I2C
bulmenwt 0:a1bcc4b82ad5 62 char rtc_read(char address)
bulmenwt 0:a1bcc4b82ad5 63 {
bulmenwt 0:a1bcc4b82ad5 64 char value;
bulmenwt 0:a1bcc4b82ad5 65 i2c.start();
bulmenwt 0:a1bcc4b82ad5 66 i2c.write(RTC8563_ADR);
bulmenwt 0:a1bcc4b82ad5 67 i2c.write(address);
bulmenwt 0:a1bcc4b82ad5 68 i2c.start();
bulmenwt 0:a1bcc4b82ad5 69 i2c.write(RTC8563_ADR | _READ);
bulmenwt 0:a1bcc4b82ad5 70 value = i2c.read(0);
bulmenwt 0:a1bcc4b82ad5 71 i2c.stop();
bulmenwt 0:a1bcc4b82ad5 72
bulmenwt 0:a1bcc4b82ad5 73 return value;
bulmenwt 0:a1bcc4b82ad5 74 }
bulmenwt 0:a1bcc4b82ad5 75
bulmenwt 0:a1bcc4b82ad5 76 //Zeit einstellen der RTC
bulmenwt 0:a1bcc4b82ad5 77 void rtc_init()
bulmenwt 0:a1bcc4b82ad5 78 {
bulmenwt 0:a1bcc4b82ad5 79 pc.printf("Setting up RTC\n");
bulmenwt 0:a1bcc4b82ad5 80
bulmenwt 0:a1bcc4b82ad5 81 // Formatierung
bulmenwt 0:a1bcc4b82ad5 82 // 2015/01/24
bulmenwt 0:a1bcc4b82ad5 83 // 10:32:00
bulmenwt 0:a1bcc4b82ad5 84
bulmenwt 0:a1bcc4b82ad5 85 week_val = 0x05; // SAT
bulmenwt 0:a1bcc4b82ad5 86 rtc_write(CONTROL1, 0x20); //stop
bulmenwt 0:a1bcc4b82ad5 87 rtc_write(CONTROL2, 0x00);
bulmenwt 0:a1bcc4b82ad5 88 rtc_write(YEARS, (0x15) );
bulmenwt 0:a1bcc4b82ad5 89 rtc_write(MONTHS, (0x01));
bulmenwt 0:a1bcc4b82ad5 90 rtc_write(DAYS, (0x24));
bulmenwt 0:a1bcc4b82ad5 91 rtc_write(HOURS, (0x10));
bulmenwt 0:a1bcc4b82ad5 92 rtc_write(MINUTES, (0x34));
bulmenwt 0:a1bcc4b82ad5 93 rtc_write(SECONDS, (0x00));
bulmenwt 0:a1bcc4b82ad5 94 rtc_write(WEEKDAYS, week_val);
bulmenwt 0:a1bcc4b82ad5 95 rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
bulmenwt 0:a1bcc4b82ad5 96 rtc_write(TIMER_CINTROL, 0x00);
bulmenwt 0:a1bcc4b82ad5 97 rtc_write(CONTROL1, 0x00); //start
bulmenwt 0:a1bcc4b82ad5 98 }
bulmenwt 0:a1bcc4b82ad5 99 //************************************
bulmenwt 0:a1bcc4b82ad5 100
bulmenwt 0:a1bcc4b82ad5 101 //Zeit einstellen der RTC
bulmenwt 0:a1bcc4b82ad5 102 void rtc_alarm()
bulmenwt 0:a1bcc4b82ad5 103 {
bulmenwt 0:a1bcc4b82ad5 104 pc.printf("Setting up RTC\n");
bulmenwt 0:a1bcc4b82ad5 105
bulmenwt 0:a1bcc4b82ad5 106 // Formatierung
bulmenwt 0:a1bcc4b82ad5 107 // 2015/01/24
bulmenwt 0:a1bcc4b82ad5 108 // 10:32:00
bulmenwt 0:a1bcc4b82ad5 109
bulmenwt 0:a1bcc4b82ad5 110 // week_val = 0x05; // SAT
bulmenwt 0:a1bcc4b82ad5 111 rtc_write(CONTROL1, 0x20); //stop
bulmenwt 0:a1bcc4b82ad5 112 rtc_write(CONTROL2, 0x0F); // alarm AF alarm flag bit
bulmenwt 0:a1bcc4b82ad5 113 rtc_write(DAY_ALARM, (0x26) );
bulmenwt 0:a1bcc4b82ad5 114 rtc_write(HOUR_ALARM, (0x19));
bulmenwt 0:a1bcc4b82ad5 115 rtc_write(MINUTE_ALARM, (0x27));
bulmenwt 0:a1bcc4b82ad5 116 rtc_write(WEEKDAY_ALARM, (0x00));
bulmenwt 0:a1bcc4b82ad5 117 rtc_write(CONTROL1, 0x00); //start
bulmenwt 0:a1bcc4b82ad5 118
bulmenwt 0:a1bcc4b82ad5 119
bulmenwt 0:a1bcc4b82ad5 120 }
bulmenwt 0:a1bcc4b82ad5 121 //************************************
bulmenwt 0:a1bcc4b82ad5 122
bulmenwt 0:a1bcc4b82ad5 123 void push1() // Interruptfunktion alarm
bulmenwt 0:a1bcc4b82ad5 124 {
bulmenwt 0:a1bcc4b82ad5 125 pc.printf("ALARM" );
bulmenwt 0:a1bcc4b82ad5 126 myled1= 1;
bulmenwt 0:a1bcc4b82ad5 127 }
bulmenwt 0:a1bcc4b82ad5 128
bulmenwt 0:a1bcc4b82ad5 129 int main() {
bulmenwt 0:a1bcc4b82ad5 130
bulmenwt 0:a1bcc4b82ad5 131 alarm.rise(&push1);
bulmenwt 0:a1bcc4b82ad5 132 rtc_alarm();
bulmenwt 0:a1bcc4b82ad5 133
bulmenwt 0:a1bcc4b82ad5 134 //RTC_init
bulmenwt 0:a1bcc4b82ad5 135 //rtc_init();
bulmenwt 0:a1bcc4b82ad5 136
bulmenwt 0:a1bcc4b82ad5 137 pc.printf("RTC8563 CLOCK\n" );
bulmenwt 0:a1bcc4b82ad5 138 wait(2.0);
bulmenwt 0:a1bcc4b82ad5 139
bulmenwt 0:a1bcc4b82ad5 140 myled1= 0;
bulmenwt 0:a1bcc4b82ad5 141
bulmenwt 0:a1bcc4b82ad5 142 while(1) {
bulmenwt 0:a1bcc4b82ad5 143
bulmenwt 0:a1bcc4b82ad5 144 //lesen der Parameter vom RTC
bulmenwt 0:a1bcc4b82ad5 145 year = rtc_read(YEARS);
bulmenwt 0:a1bcc4b82ad5 146 month = rtc_read(MONTHS);
bulmenwt 0:a1bcc4b82ad5 147 day = rtc_read(DAYS);
bulmenwt 0:a1bcc4b82ad5 148 week = rtc_read(WEEKDAYS);
bulmenwt 0:a1bcc4b82ad5 149 hour = rtc_read(HOURS);
bulmenwt 0:a1bcc4b82ad5 150 minute = rtc_read(MINUTES);
bulmenwt 0:a1bcc4b82ad5 151 sec = rtc_read(SECONDS);
bulmenwt 0:a1bcc4b82ad5 152
bulmenwt 0:a1bcc4b82ad5 153
bulmenwt 0:a1bcc4b82ad5 154 hour_a = rtc_read(HOUR_ALARM);
bulmenwt 0:a1bcc4b82ad5 155 minute_a = rtc_read(MINUTE_ALARM);
bulmenwt 0:a1bcc4b82ad5 156 week_a = rtc_read(WEEKDAY_ALARM);
bulmenwt 0:a1bcc4b82ad5 157
bulmenwt 0:a1bcc4b82ad5 158
bulmenwt 0:a1bcc4b82ad5 159
bulmenwt 0:a1bcc4b82ad5 160
bulmenwt 0:a1bcc4b82ad5 161
bulmenwt 0:a1bcc4b82ad5 162
bulmenwt 0:a1bcc4b82ad5 163 //Datum Ausgabe
bulmenwt 0:a1bcc4b82ad5 164 pc.printf("20%x%x/%x%x/%x%x %s\n",
bulmenwt 0:a1bcc4b82ad5 165 ((year >> 4) & 0x03) , (year & 0x0F) ,
bulmenwt 0:a1bcc4b82ad5 166 ((month >> 4) & 0x01), (month & 0x0F) ,
bulmenwt 0:a1bcc4b82ad5 167 ((day >> 4) & 0x03), (day & 0x0F) ,
bulmenwt 0:a1bcc4b82ad5 168 week_chr[week & 0x07]);
bulmenwt 0:a1bcc4b82ad5 169
bulmenwt 0:a1bcc4b82ad5 170 //Zeit Ausgabe
bulmenwt 0:a1bcc4b82ad5 171 pc.printf("%x%x:%x%x:%x%x\n",
bulmenwt 0:a1bcc4b82ad5 172 ((hour >> 4) & 0x03), (hour & 0x0F),
bulmenwt 0:a1bcc4b82ad5 173 (minute >> 4), (minute & 0x0F) ,
bulmenwt 0:a1bcc4b82ad5 174 (sec >> 4), (sec & 0x0F) );
bulmenwt 0:a1bcc4b82ad5 175
bulmenwt 0:a1bcc4b82ad5 176 pc.printf("ALARM RTC8563 CLOCK\n" );
bulmenwt 0:a1bcc4b82ad5 177
bulmenwt 0:a1bcc4b82ad5 178 pc.printf("%x%x:%x%x %s\n",
bulmenwt 0:a1bcc4b82ad5 179 ((hour_a >> 4) & 0x03), (hour_a & 0x0F),
bulmenwt 0:a1bcc4b82ad5 180 (minute_a >> 4), (minute_a & 0x0F),
bulmenwt 0:a1bcc4b82ad5 181 week_chr[week_a & 0x07]);
bulmenwt 0:a1bcc4b82ad5 182
bulmenwt 0:a1bcc4b82ad5 183
bulmenwt 0:a1bcc4b82ad5 184 wait(1.0);
bulmenwt 0:a1bcc4b82ad5 185 }
bulmenwt 0:a1bcc4b82ad5 186 }
bulmenwt 0:a1bcc4b82ad5 187
bulmenwt 0:a1bcc4b82ad5 188