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.
main.cpp
00001 /* RTC8563 Real Time Clock 00002 * creator: Matthias Hemmer 00003 * date: 16.01.2017 00004 * handover date: 30.01.2017 00005 * 00006 * |-------------------------| 00007 * |!!the RTC must be reseted| 00008 * |-------------------------| 00009 * 00010 */ 00011 //include mbed library 00012 #include "mbed.h" 00013 //include i2c class 00014 #include "myi2c.h" 00015 //include string library from C++ string library 00016 #include <string> 00017 //include the addresses for the PCF8563 00018 #include "PCF8563_addr.h" 00019 using namespace std; 00020 00021 PCF8563 pcf8563(p28, p27); //SDA,SCL 00022 Serial pc(USBTX,USBRX); //RS232 connection 00023 00024 void send(); //read parameters and write it via serial to pc 00025 void send_hh_mm_ss(); //send only the time in format: hh:mm:ss 00026 void send_date(); //send only the date in format: dd:month:yyyy 00027 void set_date(int year, int month, int day, int hour, int minute, int second); //set day class 00028 void toggle(); //toogle the bool between ture and false 00029 string ConvertToDate(uint8_t value); //convert from hex to string 00030 00031 //global parameters 00032 bool check = false; //toogle variable 00033 uint8_t second, minute, hour, day, week, month, year; // uint8_t is a 8 bit integer 00034 string century_month(" "); //initialzied 00035 char c; //char from the pc keyboard entry 00036 00037 //interrupt for the serial port 00038 void callback() 00039 { 00040 c = pc.getc(); //save the Character which was send from the pc 00041 00042 switch(c) { 00043 case 'C': 00044 toggle(); 00045 break; 00046 case 'c': 00047 toggle(); 00048 break; 00049 case 't': 00050 send_hh_mm_ss(); 00051 break; 00052 case 'T': 00053 send_hh_mm_ss(); 00054 break; 00055 case 'd': 00056 send_date(); 00057 break; 00058 case 'D': 00059 send_date(); 00060 break; 00061 } 00062 } 00063 /* 00064 * the programm have to set the date and read it in a second clock about the serial port to the pc 00065 * 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 00066 * before you can send another option you HAVE TO stop with a 'C' or 'c' 00067 * if "T" or "t" are pressed than it send the current time from the rtc to the pc in format: hh:mm:ss 00068 * if "D" od "d" are pressed than it send the date from the rtc to the pc in format: dd.month.yyyy 00069 */ 00070 int main() 00071 { 00072 pc.attach(&callback); //if any key will be pressed on the keyboard from the pc it will call an interrupt 00073 00074 pc.baud(9600); //set the rate of the serial port to 9600; default: 9600 00075 00076 //set at first the date 00077 set_date(0x99, 0x01, 0x02, 0x04, 0x05, 0x00); 00078 00079 //shows up the menu 00080 pc.printf( "C/c: Gibt das Datum in sekunden dakt aus.\n\r" 00081 "T/t: Gibt die aktuelle Zeit der Echtzeituhr aus.\n\r" 00082 "D/d: Gibt das aktuelle Datum der Echtzeituhr aus.\n\r"); 00083 00084 while(1) { 00085 switch(c) { 00086 case 'C': 00087 send(); 00088 break; 00089 case 'c': 00090 send(); 00091 break; 00092 } 00093 } 00094 }//end main 00095 /* 00096 * set function, the first address is the register, the second is the argument 00097 * example: set(register, 0x01); set the reigster to one 00098 * 00099 * 1. set the STOP bit 1 (look shema datasheet) at the reigster CONTROL1 00100 * 2. set the time 00101 * 3. set the STOP bit 0 (look shema datasheet) at the register CONTROL1 00102 */ 00103 void set_date(int year, int month, int day, int hour, int minute, int second) 00104 { 00105 uint8_t week_val = 0x05; //SAT 00106 //set the STOP bit 00107 pcf8563.write(PCF8563_ADR_WR, CONTROL1, 0x20); //set CONTROL1 STOP bit = 1 0b00100000 00108 pcf8563.write(PCF8563_ADR_WR, CONTROL2, 0x00); //set CONTROL2 0b00000000 00109 //set date 00110 pcf8563.write(PCF8563_ADR_WR, YEARS, year); 00111 pcf8563.write(PCF8563_ADR_WR, MONTHS, month); 00112 pcf8563.write(PCF8563_ADR_WR, WEEKDAYS, week_val); 00113 pcf8563.write(PCF8563_ADR_WR, DAYS, day); 00114 pcf8563.write(PCF8563_ADR_WR, HOURS, hour); 00115 pcf8563.write(PCF8563_ADR_WR, MINUTES, minute); 00116 pcf8563.write(PCF8563_ADR_WR, SECONDS, second); 00117 //releas the STOP bit 00118 //CLOCKOUT_FREQ must set to 0, see shema datasheet 00119 pcf8563.write(PCF8563_ADR_WR, CLOCKOUT_FREQ, 0x00); //0x83 = TE on & 1Hz 00120 pcf8563.write(PCF8563_ADR_WR, TIMER_CINTROL, 0x00); // see datasheet 00121 pcf8563.write(PCF8563_ADR_WR, CONTROL1, 0x00); //set CONTROL1 STOP bit = 0 0b00000000 00122 }// end set 00123 00124 //invert the bool check 00125 void toggle() 00126 { 00127 check = !check; 00128 }//end toggle 00129 void send() 00130 { 00131 while(check == true) { 00132 //read all parameters and connect the read value with a '&' with a register entry to 1: 0b11111111 00133 second = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, SECONDS) &0x7F; 00134 minute = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MINUTES) &0x7F; 00135 hour = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, HOURS) &0x3F; 00136 day = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, DAYS) & 0x3F; 00137 week = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, WEEKDAYS) &0x07; //must not be used 00138 month = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MONTHS) &0x1F; 00139 year = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, YEARS) &0xFF; 00140 00141 century_month = ConvertToDate(month); //convert from hex into a string 00142 00143 /*serial export to pc 00144 *%d02 shows: 00 00145 *19%02 must be writen, becouse the register of the year goes only until 99 00146 */ 00147 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 00148 00149 wait(1); //to send only one value 00150 } 00151 }//end send 00152 void send_hh_mm_ss() 00153 { 00154 //read second, minute and hour from the rtc 00155 second = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, SECONDS) &0x7F; 00156 minute = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MINUTES) &0x7F; 00157 hour = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, HOURS) &0x3F; 00158 00159 pc.printf("Aktuelle Zeit: %02d:%02d:%02d\n\r", pcf8563.bcdToDec(hour), pcf8563.bcdToDec(minute), pcf8563.bcdToDec(second)); 00160 wait(1); //to send only one value 00161 }//end send_hh_mm_ss 00162 void send_date() 00163 { 00164 day = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, DAYS) & 0x3F; 00165 month = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, MONTHS) &0x1F; 00166 year = pcf8563.read(PCF8563_ADR_WR, PCF8563_ADR_RD, YEARS) &0xFF; 00167 00168 century_month = ConvertToDate(month); //convert from hex into a string 00169 00170 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 00171 wait(1); //to send only one value 00172 }//end send_date 00173 /* 00174 * have to be coded from the user 00175 * month are coded in hex 00176 * return the month in a string (string library used) 00177 */ 00178 string ConvertToDate(uint8_t value) 00179 { 00180 string month(" "); // initialized 00181 if(value == 0x01) //compare value with a hex coded number until 0x12 is reached (see datashet) 00182 month = "January"; //set the montch 00183 if(value == 0x02) 00184 month = "February"; 00185 if(value == 0x03) 00186 month = "March"; 00187 if(value == 0x04) 00188 month = "April"; 00189 if(value == 0x05) 00190 month = "May"; 00191 if(value == 0x06) 00192 month = "June"; 00193 if(value == 0x07) 00194 month = "July"; 00195 if(value == 0x08) 00196 month = "August"; 00197 if(value == 0x09) 00198 month = "September"; 00199 if(value == 0x10) 00200 month = "October"; 00201 if(value == 0x11) 00202 month ="November"; 00203 if(value == 0x12) 00204 month = "December"; 00205 00206 return month; 00207 }//end ConvertToDate
Generated on Sun Jul 24 2022 20:18:01 by
