Bertl2014 Bulme

Dependencies:   mbed

Committer:
hemmer_matthias
Date:
Sat May 06 20:12:01 2017 +0000
Revision:
0:84a4a0aa3ea6
Bertl2014

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hemmer_matthias 0:84a4a0aa3ea6 1 /* RTC8563 Real Time Clock
hemmer_matthias 0:84a4a0aa3ea6 2 * creator: Matthias Hemmer
hemmer_matthias 0:84a4a0aa3ea6 3 * date: 16.01.2017
hemmer_matthias 0:84a4a0aa3ea6 4 * handover date: 30.01.2017
hemmer_matthias 0:84a4a0aa3ea6 5 *
hemmer_matthias 0:84a4a0aa3ea6 6 * |-------------------------|
hemmer_matthias 0:84a4a0aa3ea6 7 * |!!the RTC must be reseted|
hemmer_matthias 0:84a4a0aa3ea6 8 * |-------------------------|
hemmer_matthias 0:84a4a0aa3ea6 9 *
hemmer_matthias 0:84a4a0aa3ea6 10 */
hemmer_matthias 0:84a4a0aa3ea6 11 //include mbed library
hemmer_matthias 0:84a4a0aa3ea6 12 #include "mbed.h"
hemmer_matthias 0:84a4a0aa3ea6 13 //include i2c class
hemmer_matthias 0:84a4a0aa3ea6 14 #include "myi2c.h"
hemmer_matthias 0:84a4a0aa3ea6 15 //include string library from C++ string library
hemmer_matthias 0:84a4a0aa3ea6 16 #include <string>
hemmer_matthias 0:84a4a0aa3ea6 17 //include the addresses for the PCF8563
hemmer_matthias 0:84a4a0aa3ea6 18 #include "PCF8563_addr.h"
hemmer_matthias 0:84a4a0aa3ea6 19 using namespace std;
hemmer_matthias 0:84a4a0aa3ea6 20
hemmer_matthias 0:84a4a0aa3ea6 21 PCF8563 pcf8563(p28, p27); //SDA,SCL
hemmer_matthias 0:84a4a0aa3ea6 22 Serial pc(USBTX,USBRX); //RS232 connection
hemmer_matthias 0:84a4a0aa3ea6 23
hemmer_matthias 0:84a4a0aa3ea6 24 void send(); //read parameters and write it via serial to pc
hemmer_matthias 0:84a4a0aa3ea6 25 void send_hh_mm_ss(); //send only the time in format: hh:mm:ss
hemmer_matthias 0:84a4a0aa3ea6 26 void send_date(); //send only the date in format: dd:month:yyyy
hemmer_matthias 0:84a4a0aa3ea6 27 void set_date(int year, int month, int day, int hour, int minute, int second); //set day class
hemmer_matthias 0:84a4a0aa3ea6 28 void toggle(); //toogle the bool between ture and false
hemmer_matthias 0:84a4a0aa3ea6 29 string ConvertToDate(uint8_t value); //convert from hex to string
hemmer_matthias 0:84a4a0aa3ea6 30
hemmer_matthias 0:84a4a0aa3ea6 31 //global parameters
hemmer_matthias 0:84a4a0aa3ea6 32 bool check = false; //toogle variable
hemmer_matthias 0:84a4a0aa3ea6 33 uint8_t second, minute, hour, day, week, month, year; // uint8_t is a 8 bit integer
hemmer_matthias 0:84a4a0aa3ea6 34 string century_month(" "); //initialzied
hemmer_matthias 0:84a4a0aa3ea6 35 char c; //char from the pc keyboard entry
hemmer_matthias 0:84a4a0aa3ea6 36
hemmer_matthias 0:84a4a0aa3ea6 37 //interrupt for the serial port
hemmer_matthias 0:84a4a0aa3ea6 38 void callback()
hemmer_matthias 0:84a4a0aa3ea6 39 {
hemmer_matthias 0:84a4a0aa3ea6 40 c = pc.getc(); //save the Character which was send from the pc
hemmer_matthias 0:84a4a0aa3ea6 41
hemmer_matthias 0:84a4a0aa3ea6 42 switch(c) {
hemmer_matthias 0:84a4a0aa3ea6 43 case 'C':
hemmer_matthias 0:84a4a0aa3ea6 44 toggle();
hemmer_matthias 0:84a4a0aa3ea6 45 break;
hemmer_matthias 0:84a4a0aa3ea6 46 case 'c':
hemmer_matthias 0:84a4a0aa3ea6 47 toggle();
hemmer_matthias 0:84a4a0aa3ea6 48 break;
hemmer_matthias 0:84a4a0aa3ea6 49 case 't':
hemmer_matthias 0:84a4a0aa3ea6 50 send_hh_mm_ss();
hemmer_matthias 0:84a4a0aa3ea6 51 break;
hemmer_matthias 0:84a4a0aa3ea6 52 case 'T':
hemmer_matthias 0:84a4a0aa3ea6 53 send_hh_mm_ss();
hemmer_matthias 0:84a4a0aa3ea6 54 break;
hemmer_matthias 0:84a4a0aa3ea6 55 case 'd':
hemmer_matthias 0:84a4a0aa3ea6 56 send_date();
hemmer_matthias 0:84a4a0aa3ea6 57 break;
hemmer_matthias 0:84a4a0aa3ea6 58 case 'D':
hemmer_matthias 0:84a4a0aa3ea6 59 send_date();
hemmer_matthias 0:84a4a0aa3ea6 60 break;
hemmer_matthias 0:84a4a0aa3ea6 61 }
hemmer_matthias 0:84a4a0aa3ea6 62 }
hemmer_matthias 0:84a4a0aa3ea6 63 /*
hemmer_matthias 0:84a4a0aa3ea6 64 * the programm have to set the date and read it in a second clock about the serial port to the pc
hemmer_matthias 0:84a4a0aa3ea6 65 * if "C" or "c" are pressed the date will send to the pc and if it will be presss a second time it will stop
hemmer_matthias 0:84a4a0aa3ea6 66 * before you can send another option you HAVE TO stop with a 'C' or 'c'
hemmer_matthias 0:84a4a0aa3ea6 67 * if "T" or "t" are pressed than it send the current time from the rtc to the pc in format: hh:mm:ss
hemmer_matthias 0:84a4a0aa3ea6 68 * if "D" od "d" are pressed than it send the date from the rtc to the pc in format: dd.month.yyyy
hemmer_matthias 0:84a4a0aa3ea6 69 */
hemmer_matthias 0:84a4a0aa3ea6 70 int main()
hemmer_matthias 0:84a4a0aa3ea6 71 {
hemmer_matthias 0:84a4a0aa3ea6 72 pc.attach(&callback); //if any key will be pressed on the keyboard from the pc it will call an interrupt
hemmer_matthias 0:84a4a0aa3ea6 73
hemmer_matthias 0:84a4a0aa3ea6 74 pc.baud(9600); //set the rate of the serial port to 9600; default: 9600
hemmer_matthias 0:84a4a0aa3ea6 75
hemmer_matthias 0:84a4a0aa3ea6 76 //set at first the date
hemmer_matthias 0:84a4a0aa3ea6 77 set_date(0x99, 0x01, 0x02, 0x04, 0x05, 0x00);
hemmer_matthias 0:84a4a0aa3ea6 78
hemmer_matthias 0:84a4a0aa3ea6 79 //shows up the menu
hemmer_matthias 0:84a4a0aa3ea6 80 pc.printf( "C/c: Gibt das Datum in sekunden dakt aus.\n\r"
hemmer_matthias 0:84a4a0aa3ea6 81 "T/t: Gibt die aktuelle Zeit der Echtzeituhr aus.\n\r"
hemmer_matthias 0:84a4a0aa3ea6 82 "D/d: Gibt das aktuelle Datum der Echtzeituhr aus.\n\r");
hemmer_matthias 0:84a4a0aa3ea6 83
hemmer_matthias 0:84a4a0aa3ea6 84 while(1) {
hemmer_matthias 0:84a4a0aa3ea6 85 switch(c) {
hemmer_matthias 0:84a4a0aa3ea6 86 case 'C':
hemmer_matthias 0:84a4a0aa3ea6 87 send();
hemmer_matthias 0:84a4a0aa3ea6 88 break;
hemmer_matthias 0:84a4a0aa3ea6 89 case 'c':
hemmer_matthias 0:84a4a0aa3ea6 90 send();
hemmer_matthias 0:84a4a0aa3ea6 91 break;
hemmer_matthias 0:84a4a0aa3ea6 92 }
hemmer_matthias 0:84a4a0aa3ea6 93 }
hemmer_matthias 0:84a4a0aa3ea6 94 }//end main
hemmer_matthias 0:84a4a0aa3ea6 95 /*
hemmer_matthias 0:84a4a0aa3ea6 96 * set function, the first address is the register, the second is the argument
hemmer_matthias 0:84a4a0aa3ea6 97 * example: set(register, 0x01); set the reigster to one
hemmer_matthias 0:84a4a0aa3ea6 98 *
hemmer_matthias 0:84a4a0aa3ea6 99 * 1. set the STOP bit 1 (look shema datasheet) at the reigster CONTROL1
hemmer_matthias 0:84a4a0aa3ea6 100 * 2. set the time
hemmer_matthias 0:84a4a0aa3ea6 101 * 3. set the STOP bit 0 (look shema datasheet) at the register CONTROL1
hemmer_matthias 0:84a4a0aa3ea6 102 */
hemmer_matthias 0:84a4a0aa3ea6 103 void set_date(int year, int month, int day, int hour, int minute, int second)
hemmer_matthias 0:84a4a0aa3ea6 104 {
hemmer_matthias 0:84a4a0aa3ea6 105 uint8_t week_val = 0x05; //SAT
hemmer_matthias 0:84a4a0aa3ea6 106 //set the STOP bit
hemmer_matthias 0:84a4a0aa3ea6 107 pcf8563.write(PCF8563_ADR_WR, CONTROL1, 0x20); //set CONTROL1 STOP bit = 1 0b00100000
hemmer_matthias 0:84a4a0aa3ea6 108 pcf8563.write(PCF8563_ADR_WR, CONTROL2, 0x00); //set CONTROL2 0b00000000
hemmer_matthias 0:84a4a0aa3ea6 109 //set date
hemmer_matthias 0:84a4a0aa3ea6 110 pcf8563.write(PCF8563_ADR_WR, YEARS, year);
hemmer_matthias 0:84a4a0aa3ea6 111 pcf8563.write(PCF8563_ADR_WR, MONTHS, month);
hemmer_matthias 0:84a4a0aa3ea6 112 pcf8563.write(PCF8563_ADR_WR, WEEKDAYS, week_val);
hemmer_matthias 0:84a4a0aa3ea6 113 pcf8563.write(PCF8563_ADR_WR, DAYS, day);
hemmer_matthias 0:84a4a0aa3ea6 114 pcf8563.write(PCF8563_ADR_WR, HOURS, hour);
hemmer_matthias 0:84a4a0aa3ea6 115 pcf8563.write(PCF8563_ADR_WR, MINUTES, minute);
hemmer_matthias 0:84a4a0aa3ea6 116 pcf8563.write(PCF8563_ADR_WR, SECONDS, second);
hemmer_matthias 0:84a4a0aa3ea6 117 //releas the STOP bit
hemmer_matthias 0:84a4a0aa3ea6 118 //CLOCKOUT_FREQ must set to 0, see shema datasheet
hemmer_matthias 0:84a4a0aa3ea6 119 pcf8563.write(PCF8563_ADR_WR, CLOCKOUT_FREQ, 0x00); //0x83 = TE on & 1Hz
hemmer_matthias 0:84a4a0aa3ea6 120 pcf8563.write(PCF8563_ADR_WR, TIMER_CINTROL, 0x00); // see datasheet
hemmer_matthias 0:84a4a0aa3ea6 121 pcf8563.write(PCF8563_ADR_WR, CONTROL1, 0x00); //set CONTROL1 STOP bit = 0 0b00000000
hemmer_matthias 0:84a4a0aa3ea6 122 }// end set
hemmer_matthias 0:84a4a0aa3ea6 123
hemmer_matthias 0:84a4a0aa3ea6 124 //invert the bool check
hemmer_matthias 0:84a4a0aa3ea6 125 void toggle()
hemmer_matthias 0:84a4a0aa3ea6 126 {
hemmer_matthias 0:84a4a0aa3ea6 127 check = !check;
hemmer_matthias 0:84a4a0aa3ea6 128 }//end toggle
hemmer_matthias 0:84a4a0aa3ea6 129 void send()
hemmer_matthias 0:84a4a0aa3ea6 130 {
hemmer_matthias 0:84a4a0aa3ea6 131 while(check == true) {
hemmer_matthias 0:84a4a0aa3ea6 132 //read all parameters and connect the read value with a '&' with a register entry to 1: 0b11111111
hemmer_matthias 0:84a4a0aa3ea6 133 second = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, SECONDS) &0x7F;
hemmer_matthias 0:84a4a0aa3ea6 134 minute = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MINUTES) &0x7F;
hemmer_matthias 0:84a4a0aa3ea6 135 hour = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, HOURS) &0x3F;
hemmer_matthias 0:84a4a0aa3ea6 136 day = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, DAYS) & 0x3F;
hemmer_matthias 0:84a4a0aa3ea6 137 week = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, WEEKDAYS) &0x07; //must not be used
hemmer_matthias 0:84a4a0aa3ea6 138 month = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MONTHS) &0x1F;
hemmer_matthias 0:84a4a0aa3ea6 139 year = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, YEARS) &0xFF;
hemmer_matthias 0:84a4a0aa3ea6 140
hemmer_matthias 0:84a4a0aa3ea6 141 century_month = ConvertToDate(month); //convert from hex into a string
hemmer_matthias 0:84a4a0aa3ea6 142
hemmer_matthias 0:84a4a0aa3ea6 143 /*serial export to pc
hemmer_matthias 0:84a4a0aa3ea6 144 *%d02 shows: 00
hemmer_matthias 0:84a4a0aa3ea6 145 *19%02 must be writen, becouse the register of the year goes only until 99
hemmer_matthias 0:84a4a0aa3ea6 146 */
hemmer_matthias 0:84a4a0aa3ea6 147 pc.printf("%d.%s.19%d\n\r%02d:%02d:%02d\n\r", pcf8563.bcdToDec(day), century_month, pcf8563.bcdToDec(year), pcf8563.bcdToDec(hour), pcf8563.bcdToDec(minute), pcf8563.bcdToDec(second)); //ignore warning, becouse it cames from the string library //Warning message: Warning: Non-POD class type passed through ellipsis in "main.cpp" dont' care
hemmer_matthias 0:84a4a0aa3ea6 148
hemmer_matthias 0:84a4a0aa3ea6 149 wait(1); //to send only one value
hemmer_matthias 0:84a4a0aa3ea6 150 }
hemmer_matthias 0:84a4a0aa3ea6 151 }//end send
hemmer_matthias 0:84a4a0aa3ea6 152 void send_hh_mm_ss()
hemmer_matthias 0:84a4a0aa3ea6 153 {
hemmer_matthias 0:84a4a0aa3ea6 154 //read second, minute and hour from the rtc
hemmer_matthias 0:84a4a0aa3ea6 155 second = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, SECONDS) &0x7F;
hemmer_matthias 0:84a4a0aa3ea6 156 minute = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MINUTES) &0x7F;
hemmer_matthias 0:84a4a0aa3ea6 157 hour = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, HOURS) &0x3F;
hemmer_matthias 0:84a4a0aa3ea6 158
hemmer_matthias 0:84a4a0aa3ea6 159 pc.printf("Aktuelle Zeit: %02d:%02d:%02d\n\r", pcf8563.bcdToDec(hour), pcf8563.bcdToDec(minute), pcf8563.bcdToDec(second));
hemmer_matthias 0:84a4a0aa3ea6 160 wait(1); //to send only one value
hemmer_matthias 0:84a4a0aa3ea6 161 }//end send_hh_mm_ss
hemmer_matthias 0:84a4a0aa3ea6 162 void send_date()
hemmer_matthias 0:84a4a0aa3ea6 163 {
hemmer_matthias 0:84a4a0aa3ea6 164 day = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, DAYS) & 0x3F;
hemmer_matthias 0:84a4a0aa3ea6 165 month = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MONTHS) &0x1F;
hemmer_matthias 0:84a4a0aa3ea6 166 year = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, YEARS) &0xFF;
hemmer_matthias 0:84a4a0aa3ea6 167
hemmer_matthias 0:84a4a0aa3ea6 168 century_month = ConvertToDate(month); //convert from hex into a string
hemmer_matthias 0:84a4a0aa3ea6 169
hemmer_matthias 0:84a4a0aa3ea6 170 pc.printf("Ich wurde geboren am %02d.%s.19%d.\n\r", pcf8563.bcdToDec(day), century_month, pcf8563.bcdToDec(year)); //ignore warning, becouse it cames from the string library
hemmer_matthias 0:84a4a0aa3ea6 171 wait(1); //to send only one value
hemmer_matthias 0:84a4a0aa3ea6 172 }//end send_date
hemmer_matthias 0:84a4a0aa3ea6 173 /*
hemmer_matthias 0:84a4a0aa3ea6 174 * have to be coded from the user
hemmer_matthias 0:84a4a0aa3ea6 175 * month are coded in hex
hemmer_matthias 0:84a4a0aa3ea6 176 * return the month in a string (string library used)
hemmer_matthias 0:84a4a0aa3ea6 177 */
hemmer_matthias 0:84a4a0aa3ea6 178 string ConvertToDate(uint8_t value)
hemmer_matthias 0:84a4a0aa3ea6 179 {
hemmer_matthias 0:84a4a0aa3ea6 180 string month(" "); // initialized
hemmer_matthias 0:84a4a0aa3ea6 181 if(value == 0x01) //compare value with a hex coded number until 0x12 is reached (see datashet)
hemmer_matthias 0:84a4a0aa3ea6 182 month = "January"; //set the montch
hemmer_matthias 0:84a4a0aa3ea6 183 if(value == 0x02)
hemmer_matthias 0:84a4a0aa3ea6 184 month = "February";
hemmer_matthias 0:84a4a0aa3ea6 185 if(value == 0x03)
hemmer_matthias 0:84a4a0aa3ea6 186 month = "March";
hemmer_matthias 0:84a4a0aa3ea6 187 if(value == 0x04)
hemmer_matthias 0:84a4a0aa3ea6 188 month = "April";
hemmer_matthias 0:84a4a0aa3ea6 189 if(value == 0x05)
hemmer_matthias 0:84a4a0aa3ea6 190 month = "May";
hemmer_matthias 0:84a4a0aa3ea6 191 if(value == 0x06)
hemmer_matthias 0:84a4a0aa3ea6 192 month = "June";
hemmer_matthias 0:84a4a0aa3ea6 193 if(value == 0x07)
hemmer_matthias 0:84a4a0aa3ea6 194 month = "July";
hemmer_matthias 0:84a4a0aa3ea6 195 if(value == 0x08)
hemmer_matthias 0:84a4a0aa3ea6 196 month = "August";
hemmer_matthias 0:84a4a0aa3ea6 197 if(value == 0x09)
hemmer_matthias 0:84a4a0aa3ea6 198 month = "September";
hemmer_matthias 0:84a4a0aa3ea6 199 if(value == 0x10)
hemmer_matthias 0:84a4a0aa3ea6 200 month = "October";
hemmer_matthias 0:84a4a0aa3ea6 201 if(value == 0x11)
hemmer_matthias 0:84a4a0aa3ea6 202 month ="November";
hemmer_matthias 0:84a4a0aa3ea6 203 if(value == 0x12)
hemmer_matthias 0:84a4a0aa3ea6 204 month = "December";
hemmer_matthias 0:84a4a0aa3ea6 205
hemmer_matthias 0:84a4a0aa3ea6 206 return month;
hemmer_matthias 0:84a4a0aa3ea6 207 }//end ConvertToDate