Demo program for the Oled display
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "OLED32028P1T.h" 00003 00004 00005 OLED32028P1T oled(PTE0,PTE1,PTA12); // Oled Display tx, rx, rs 00006 InterruptIn dcfSignalIn(PTC0); // conection of output NON-inverting DCF module 00007 DigitalOut SignalLED(LED3); // indicates DCF signal on Mbed 00008 00009 Timer T1,T2,T3; 00010 Ticker Ticker50ms; // DCF IRQ timer 00011 00012 #define clkx 110 //this point represent the x center of the clock where maths is done 00013 #define clky 138 //this point represent the y center of the clock where maths is done 00014 00015 char timebuf[40]; 00016 char datebuf[40]; 00017 char paritycheck,paritym,parityu,paritydmy; 00018 char testu,testm,testdmy,summertime; 00019 char min,minh,minl,hourh,hourl,day,dayh,dayl,monthh,monthl,yearh,yearl; 00020 char weekDayName[7][4] = {"Sun","Mon","Tue","Wed","Thu", "Fri","Sat"}; 00021 char monthName[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 00022 char MAX_DAY[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Max days in a month for a non-leap year 00023 char statusText[6][20] = {"Waiting Synchronise"," Wait for 21st bit ","Reading Signal data"," Checking Parity "," Last Minute Okay "," Signal error "}; 00024 char leapSymbol[2][10] = {" ", "Leap Year"}; 00025 00026 int hour,minute,second,dayofweek,dayofmonth,month,year,nextsec; 00027 int RTCsecond,RTCminute,RTChour,RTCdayofweek,RTCdayofmonth,RTCmonth,RTCyear; 00028 int xbuffer,ybuffer,IRQ; 00029 int xs,ys,xm,ym,xh,yh,x,y; 00030 int handHour=60,handMin=75,handSec=78; //RTC clock hand sizes 00031 int numposx=105,numposy=135; // RTC clock number screen posistion 00032 00033 int start,sync,SignalStatus,nosignal,laststart,loop,interrupt_counter,dcf_sec,error_count; 00034 int tz,rtcset,rtcset_times; 00035 int r=255,g=255,b=255; 00036 int draw_graph,dcf_good,signal_error,dcf_error,display; 00037 int p_minute,p_hour,p_dayofweek,p_dayofmonth,p_month,p_year; 00038 int dcf_array[61]; 00039 00040 float angleH,angleM,angleS; 00041 float fsecond,fminute; 00042 00043 typedef unsigned char byte; 00044 00045 struct tm t; 00046 00047 void RTCsetclock(),RTCset(),RTCread(),RTCclock(),RTCclockdraw(),RTCclockupdate(); 00048 void dcfISR(),dcfIRQ(),checkSIGNAL(),signal(),DCFbitprint(); 00049 void DCFclock(),DCFloop(),DCFshowClock(),DCFtestparity(); 00050 void DCFcheck(),bitprint(),DCFdrawgraph(); 00051 void DCFbitmapdraw(),DCFparitycalc(),DCFbitstatus(),DCFbitstatus2(); 00052 void DCFsetTime(); 00053 00054 struct SignalStatus { 00055 bool is_leap; // Leap year flag (NOT actually transmitted but calculated) 00056 bool DCFsample50; // dcf sample at 50mS into the start of the second, seocnd marker pulse 00057 bool DCFsample150; // dcf sample at 150mS into the start of the second, bit=1 if high, 0 if low 00058 bool DCFsample300; // dcf sample at 300mS into the start of the second, error if high 00059 bool DCFsample500; // dcf sample at 500mS into the start of the second, error if high 00060 bool DCFsample600; // dcf sample at 600mS into the start of the second, error if high 00061 unsigned char frame; // Received MSF framing code 01111110 00062 int second; // MSF-DCF second (NOT actually transmitted but calculated) 00063 } dcf_status; 00064 00065 00066 // Return the maximum day in month for a given month & year 00067 byte maxDay(byte year, byte month) 00068 { 00069 byte lastday, leap; 00070 leap = year%4 == 0 && year%100 !=0 || year%400 == 0; 00071 lastday = MAX_DAY[month - 1]; 00072 dcf_status.is_leap = leap > 0 ? 1 : 0; 00073 if ((leap > 0) && (month == 2)) 00074 lastday++; 00075 return lastday; 00076 } 00077 00078 int main() { 00079 00080 SignalLED=1; 00081 oled.init(); 00082 oled.clear(); 00083 oled.setTextBackgroundType(TEXT_OPAQUE); 00084 oled.setFontSize(FONT12X16);oled.setFontColor(oled.toRGB(255,255,0)); 00085 oled.drawText(2,0,(FONT12X16),"--drawText vs. printf--",oled.toRGB(255,0,255)); 00086 oled.drawText(2,22,(FONT5X7),"Use drawText as much as possible, it's nearly twice",oled.toRGB(255,255,255)); 00087 oled.drawText(2,23,(FONT5X7),"as fast as printf. Make sure the serial stream is",oled.toRGB(255,255,255)); 00088 oled.drawText(2,24,(FONT5X7),"NOT",oled.toRGB(255,0,0)); 00089 oled.drawText(6,24,(FONT5X7),"interrupted when printing.",oled.toRGB(255,255,255)); 00090 if (time(NULL) < 1396310400) {set_time(1396310400);} 00091 T3.start(); 00092 00093 while(T3<10){ 00094 oled.setFontColor(oled.toRGB(0,255,255)); 00095 oled.locate (12,18); 00096 oled.printf("Next page in %2.1f seconds ",10 - T3.read()); 00097 00098 time_t seconds = time(NULL); 00099 strftime(timebuf, 32, "RTC %I:%M:%S %p", localtime(&seconds)); 00100 strftime(datebuf, 32, "%a %d %b %Y ", localtime(&seconds)); 00101 00102 T1.start(); 00103 oled.drawText(2,2,(FONT12X16),timebuf,oled.toRGB(255,255,0)); 00104 oled.drawText(2,3,(FONT12X16),datebuf,oled.toRGB(255,255,0)); 00105 T1.stop(); 00106 00107 T2.start(); 00108 oled.setFontColor(oled.toRGB(0,255,0)); 00109 oled.setFontSize(FONT12X16); 00110 oled.locate (2,6); 00111 oled.printf("%s",timebuf); 00112 oled.locate (2,7); 00113 oled.printf("%s",datebuf); 00114 T2.stop(); 00115 00116 oled.setFontColor(oled.toRGB(255,255,255)); 00117 oled.setFontSize(FONT8X12); 00118 oled.locate (4,6); 00119 oled.printf("drawText time %3.2f mS",T1.read()*1000);T1.reset(); 00120 oled.locate (4,11); 00121 oled.printf("printf time %3.2f mS",T2.read()*1000);T2.reset(); 00122 oled.locate (4,12); 00123 oled.printf("%d",seconds); 00124 } 00125 T3.reset(); 00126 while(1){ 00127 IRQ=0; 00128 SignalLED=1; 00129 RTCclock(); 00130 } 00131 00132 } 00133 00134 void RTCclock() 00135 { 00136 while(IRQ==0){ 00137 oled.disableTouch();oled.clear(); 00138 oled.setTextBackgroundType (TEXT_TRANSPARENT); 00139 RTCclockdraw(); 00140 oled.drawTextButton(1, 200, 210,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "DCF Clock"); 00141 oled.drawTextButton(1, 252, 185,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "Set RTC"); 00142 oled.setTextBackgroundType (TEXT_OPAQUE); 00143 oled.drawText(0,0,(FONT12X16),"RTC Clock",oled.toRGB(0,255,0)); 00144 oled.drawText(38,7,(FONT5X7),"Touch position",oled.toRGB(255,0,0)); 00145 oled.enableTouch();oled.resetTouchArea(); 00146 T1.start(); 00147 00148 while(IRQ==0){ 00149 oled.setFontSize(FONT12X16);oled.locate(0,1); 00150 oled.setFontColor(oled.toRGB(255,255,255)); 00151 time_t seconds = time(NULL); 00152 strftime(timebuf, 32, "%I:%M:%S.", localtime(&seconds)); 00153 strftime(datebuf, 32, "%a %d %b %Y ", localtime(&seconds)); 00154 oled.drawText(11,0,(FONT12X16),timebuf,oled.toRGB(255,255,255)); 00155 oled.drawText(11,1,(FONT12X16),datebuf,oled.toRGB(255,255,255)); 00156 00157 if (seconds != nextsec){T1.reset();RTCclockupdate();nextsec=seconds;} 00158 oled.locate(20,0); 00159 oled.printf("%02.f ",T1.read()*100); 00160 oled.getTouch(&xbuffer,&ybuffer); 00161 00162 oled.setFontSize(FONT5X7);oled.locate(40,9); // this indicates touch position 00163 oled.printf("X %03d Y %03d ",xbuffer,ybuffer); // can be removed 00164 00165 if((xbuffer>195 && xbuffer<300) && (ybuffer>205 && ybuffer<220)){DCFclock();} 00166 if((xbuffer>240 && xbuffer<300) && (ybuffer>182 && ybuffer<200)){RTCsetclock();} 00167 } 00168 } 00169 } 00170 00171 void RTCclockdraw() 00172 { 00173 (x)=0; 00174 while ((x)<60){ 00175 x++;angleS=(x)*6; // clock minute markers in angle form 00176 xs=(sin((angleS*3.14)/180)) * 90; // X component of markers 00177 ys=(cos((angleS*3.14)/180)) * 90; // Y component of markers 00178 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(255,255,255)); 00179 } 00180 (x)=0; 00181 while ((x)<13){ 00182 x++;angleS=(x)*30; // clock hour markers in angle form 00183 xs=(sin((angleS*3.14)/180)) * 90; // X component of markers 00184 ys=(cos((angleS*3.14)/180)) * 90; // Y component of markers 00185 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(0,255,255)); 00186 if (x==1) oled.drawTextGraphic(numposx +(xs+8),numposy + (ys+8), FONT5X7, "5", 1, 1, oled.toRGB(255,255,255)); 00187 if (x==2) oled.drawTextGraphic(numposx +(xs+10),numposy + (ys+4), FONT5X7, "4", 1, 1, oled.toRGB(255,255,255)); 00188 if (x==3) oled.drawTextGraphic(numposx +(xs+10),numposy + (ys), FONT5X7, "3", 1, 1, oled.toRGB(255,255,255)); 00189 if (x==4) oled.drawTextGraphic(numposx +(xs+10),numposy + (ys-4), FONT5X7, "2", 1, 1, oled.toRGB(255,255,255)); 00190 if (x==5) oled.drawTextGraphic(numposx +(xs+8),numposy + (ys-8), FONT5X7, "1", 1, 1, oled.toRGB(255,255,255)); 00191 if (x==6) oled.drawTextGraphic(numposx +(xs),numposy + (ys-8), FONT5X7, "12", 1, 1, oled.toRGB(255,255,255)); 00192 if (x==7) oled.drawTextGraphic(numposx +(xs-4),numposy + (ys-8), FONT5X7, "11", 1, 1, oled.toRGB(255,255,255)); 00193 if (x==8) oled.drawTextGraphic(numposx +(xs-10),numposy + (ys-4), FONT5X7, "10", 1, 1, oled.toRGB(255,255,255)); 00194 if (x==9) oled.drawTextGraphic(numposx +(xs-4),numposy + (ys), FONT5X7, "9", 1, 1, oled.toRGB(255,255,255)); 00195 if (x==10) oled.drawTextGraphic(numposx +(xs-4),numposy + (ys+4), FONT5X7, "8", 1, 1, oled.toRGB(255,255,255)); 00196 if (x==11) oled.drawTextGraphic(numposx +(xs),numposy + (ys+8), FONT5X7, "7", 1, 1, oled.toRGB(255,255,255)); 00197 if (x==12) oled.drawTextGraphic(numposx +(xs+3),numposy + (ys+8), FONT5X7, "6", 1, 1, oled.toRGB(255,255,255)); 00198 } 00199 oled.setPenSize(0); 00200 oled.drawCircle(clkx,clky,80,oled.toRGB(0,0,0)); 00201 oled.setPenSize(1); 00202 oled.drawCircle(clkx,clky,80,oled.toRGB(255,255,255)); 00203 oled.drawCircle(clkx,clky,90,oled.toRGB(255,255,255)); 00204 (x)=0; 00205 } 00206 00207 void RTCsetclock() 00208 { 00209 oled.disableTouch();oled.clear(); 00210 oled.drawText(0,0,(FONT12X16)," --Manual set RTC Time--",oled.toRGB(0,255,0)); 00211 oled.setTextBackgroundType (TEXT_TRANSPARENT); 00212 oled.drawTextButton(1, 265, 200,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, " Back "); 00213 oled.drawTextButton(1, 10, 200,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Reset RTC "); 00214 oled.drawTextButton(1, 120, 200,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Set RTC "); 00215 oled.drawTextButton(1, 16, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+"); 00216 oled.drawTextButton(1, 16, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-"); 00217 oled.drawTextButton(1, 52, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+"); 00218 oled.drawTextButton(1, 52, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-"); 00219 oled.drawTextButton(1, 88, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+"); 00220 oled.drawTextButton(1, 88, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-"); 00221 oled.drawTextButton(1, 174, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+"); 00222 oled.drawTextButton(1, 174, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-"); 00223 oled.drawTextButton(1, 208, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " + "); 00224 oled.drawTextButton(1, 208, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " - "); 00225 oled.drawTextButton(1, 270, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " + "); 00226 oled.drawTextButton(1, 270, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " - "); 00227 oled.setTextBackgroundType (TEXT_OPAQUE); 00228 oled.setFontSize(FONT8X12);oled.locate (6,14); 00229 oled.setFontColor(oled.toRGB(255,255,255)); 00230 time_t seconds = time(NULL); 00231 oled.printf("time_t = %d seconds",seconds); 00232 RTCread(); 00233 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear; 00234 00235 while(IRQ==0){ 00236 oled.enableTouch(); 00237 time_t seconds = time(NULL); 00238 strftime(timebuf, 26, "%H:%M:%S", localtime(&seconds)); 00239 strftime(datebuf, 26, "%a %d %b' %Y", localtime(&seconds)); 00240 oled.drawText(1,2,(FONT12X16),timebuf,oled.toRGB(255,255,0)); 00241 oled.drawText(10,2,(FONT12X16),datebuf,oled.toRGB(255,255,0)); 00242 oled.setFontSize(FONT8X12);oled.locate (6,5); 00243 oled.setFontColor(oled.toRGB(255,255,255)); 00244 oled.printf("time_t = %d seconds",seconds); 00245 00246 oled.getTouch(&xbuffer,&ybuffer); 00247 if((xbuffer>240 && xbuffer<290) && (ybuffer>190 && ybuffer<210)){IRQ=1;} 00248 if((xbuffer>20 && xbuffer<109) && (ybuffer>190 && ybuffer<210)){ 00249 set_time(1420070400);time_t seconds = time(NULL);RTCread(); 00250 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear; 00251 oled.setFontSize(FONT8X12);oled.locate (6,14); 00252 oled.setFontColor(oled.toRGB(255,255,255)); 00253 oled.printf("time_t = %d seconds",seconds); 00254 } 00255 if((xbuffer>120 && xbuffer<195) && (ybuffer>195 && ybuffer<210)){RTCset();time_t seconds = time(NULL);RTCread(); 00256 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear; 00257 oled.setFontSize(FONT8X12);oled.locate (6,14); 00258 oled.setFontColor(oled.toRGB(255,255,255)); 00259 oled.printf("time_t = %d seconds",seconds); 00260 } 00261 00262 if((xbuffer>25 && xbuffer<40) && (ybuffer>124 && ybuffer<140)){hour++;} 00263 if((xbuffer>25 && xbuffer<40) && (ybuffer>146 && ybuffer<160)){hour--;} 00264 if((xbuffer>58 && xbuffer<74) && (ybuffer>124 && ybuffer<140)){minute++;} 00265 if((xbuffer>58 && xbuffer<74) && (ybuffer>146 && ybuffer<160)){minute--;} 00266 if((xbuffer>90 && xbuffer<105) && (ybuffer>124 && ybuffer<140)){second++;} 00267 if((xbuffer>90 && xbuffer<105) && (ybuffer>146 && ybuffer<160)){second--;} 00268 if((xbuffer>170 && xbuffer<186) && (ybuffer>124 && ybuffer<140)){dayofmonth++;} 00269 if((xbuffer>170 && xbuffer<186) && (ybuffer>146 && ybuffer<160)){dayofmonth--;} 00270 if((xbuffer>200 && xbuffer<230) && (ybuffer>124 && ybuffer<140)){month++;} 00271 if((xbuffer>200 && xbuffer<230) && (ybuffer>146 && ybuffer<160)){month--;} 00272 if((xbuffer>258 && xbuffer<288) && (ybuffer>124 && ybuffer<140)){year++;} 00273 if((xbuffer>258 && xbuffer<288) && (ybuffer>146 && ybuffer<160)){year--;} 00274 oled.disableTouch(); 00275 00276 if (second >= 60) {second = 0;} 00277 if (second < 0) {second = 59;} 00278 if (minute >= 60) {minute = 0;} 00279 if (minute < 0) {minute = 59;} 00280 if (hour >= 24) {hour = 0;} 00281 if (hour < 0) {hour = 23;} 00282 if (dayofweek > 6){dayofweek = 0;} 00283 if (dayofweek < 0){dayofweek = 6;} 00284 if (month > 12) {month = 1;} 00285 if (month < 1) {month = 12;} 00286 if (dayofmonth <1) {dayofmonth = maxDay(year, month);} 00287 if (dayofmonth > maxDay(year, month)) {dayofmonth = 1;} 00288 if (year > 99) {year = 1;} 00289 if (year < 0) {year = 99;} 00290 00291 00292 oled.setFontSize(FONT12X16);oled.locate(1,6); 00293 oled.setFontColor(oled.toRGB(128,255,255)); 00294 oled.printf("%2d:%02d:%02d ",hour,minute,second); 00295 oled.printf("%s %02d %s' 20%02d",weekDayName[dayofweek],dayofmonth,monthName[month-1],year); 00296 } 00297 00298 } 00299 void RTCclockupdate() 00300 { 00301 RTCread(); 00302 fsecond = (RTCsecond/12); 00303 fminute = (RTCminute/12); 00304 if (x==1){ //Erase all hands 00305 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(0,0,0)); // Erase Second's hand 00306 oled.drawLine(clkx,clky,clkx+xm,clky-ym,oled.toRGB(0,0,0)); // Erase Minute's hand 00307 oled.drawLine(clkx,clky,clkx+xh,clky-yh,oled.toRGB(0,0,0)); // Erase Hour's hand 00308 } 00309 //Calculations to get the second point of the clock hands. (first point is always the center of the clock) 00310 angleS=(RTCsecond*6); //seconds in angle form, 360 degrees divided by 60 seconds = 6, x6 to get current angle 00311 xs=(sin((angleS*3.14)/180)) * handSec; //get X component of the second's hand 00312 ys=(cos((angleS*3.14)/180)) * handSec; //get Y component of the second's hand 00313 angleM=((RTCminute*6) + fsecond); //minutes in angle form, 360 degrees divided by 60 minutes = 6, add seconds fraction, x6 to get current angle 00314 xm=(sin((angleM*3.14)/180)) * handMin; //get X component of the minutes's hand 00315 ym=(cos((angleM*3.14)/180)) * handMin; //get Y component of the minutes's hand 00316 angleH=(((RTChour*5) + (fminute))*6); //hours in angle form, 360 degrees multiply hours by 5 hours = 60, add minute fraction, x6 to get angle 00317 xh=(sin((angleH*3.14)/180)) * handHour; //get X component of the hours's hand 00318 yh=(cos((angleH*3.14)/180)) * handHour; //get Y component of the hours's hand 00319 x=1; 00320 //Draw current time hands 00321 oled.drawLine(clkx,clky,clkx+xm,clky-ym,oled.toRGB(255,255,255)); 00322 oled.drawLine(clkx,clky,clkx+xh,clky-yh,oled.toRGB(0,255,255)); 00323 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(255,255,0)); 00324 oled.setPenSize(0); 00325 oled.drawCircle(clkx,clky,5,oled.toRGB(255,255,255)); // Draw the center of the second's hand 00326 } 00327 00328 void RTCread() 00329 { 00330 time_t seconds = time(NULL); 00331 char buffer[80]; 00332 strftime(buffer, 2,"%S", localtime(&seconds)); 00333 RTCsecond = atoi(buffer); 00334 strftime(buffer, 2,"%M", localtime(&seconds)); 00335 RTCminute = atoi(buffer); 00336 strftime(buffer, 2,"%H", localtime(&seconds)); 00337 RTChour = atoi(buffer); 00338 strftime(buffer, 2,"%d", localtime(&seconds)); 00339 RTCdayofmonth = atoi(buffer); 00340 strftime(buffer, 2,"%w", localtime(&seconds)); 00341 RTCdayofweek = atoi(buffer); 00342 strftime(buffer, 2,"%m", localtime(&seconds)); 00343 RTCmonth = atoi(buffer); 00344 strftime(buffer, 2,"%y", localtime(&seconds)); 00345 RTCyear = atoi(buffer); 00346 } 00347 00348 void RTCset() 00349 { 00350 t.tm_sec = (second); // 0-59 00351 t.tm_min = (minute); // 0-59 00352 t.tm_hour = (hour); // 0-23 00353 t.tm_mday = (dayofmonth); // 1-31 00354 t.tm_mon = (month-1); // 0-11 DCF "0" = Jan, -1 added for Mbed RCT clock format 00355 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year 00356 set_time(mktime(&t)-3600); // set RTC clock to DCF -1 hour for GMT time zone 00357 rtcset=1; 00358 } 00359 00360 // **************************** DCF clock ********************************************** 00361 00362 void DCFclock() 00363 { 00364 oled.clear(); 00365 oled.drawText(0,1,(FONT12X16),("--- DCF77 Atomic Clock ---"),oled.toRGB(255,255,255)); 00366 oled.drawText(3,3,(FONT12X16),("Checking Signal..."),oled.toRGB(255,255,255)); 00367 T1.reset();T1.start(); 00368 while(dcfSignalIn == 1 && T1.read()<.5){} 00369 while(dcfSignalIn == 0 && T1.read()<.5){} 00370 T1.stop(); 00371 if (T1.read()>.5){ 00372 T1.stop(); 00373 oled.drawText(0,5,(FONT12X16),(" !! No signal detected !! "),oled.toRGB(255,0,0)); 00374 oled.drawText(2,9,(FONT8X12),("Connect Non inverting signal of DCF"),oled.toRGB(255,255,255)); 00375 oled.drawText(2,10,(FONT8X12),("receiver module to specified Pin."),oled.toRGB(255,255,255)); 00376 oled.drawText(5,12,(FONT8X12),("Running Demo mode."),oled.toRGB(255,255,255)); 00377 wait(5); 00378 } 00379 wait(1); 00380 oled.clear();DCFbitmapdraw();RTCread(); 00381 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear; 00382 if(tz==0) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (GMT)",oled.toRGB(255,0,0));} 00383 if (tz==1) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (CET)",oled.toRGB(255,0,0));} 00384 if (tz==2) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (EET)",oled.toRGB(255,0,0));} 00385 if (tz==3) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (MSK)",oled.toRGB(255,0,0));} 00386 oled.drawText(22,5,(FONT8X12)," DCF Time (CET)",oled.toRGB(255,255,0)); 00387 oled.setTextBackgroundType (TEXT_TRANSPARENT); 00388 oled.drawTextButton(1, 270, 140,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, " Back "); 00389 oled.setTextBackgroundType (TEXT_OPAQUE); 00390 draw_graph=0;DCFdrawgraph(); 00391 oled.drawLine(155,190,169,190, oled.toRGB(225, 255, 255)); 00392 oled.drawLine(169,190,169,203, oled.toRGB(225, 255, 255)); 00393 oled.drawLine(170,203,287,203, oled.toRGB(255,255,255)); 00394 oled.drawLine(287,190,287,203, oled.toRGB(225, 255, 255)); 00395 oled.drawLine(288,190,305,190, oled.toRGB(225, 255, 255)); 00396 oled.drawText(0,26,(FONT5X7)," 0 start min hour Dt dy mon year 60",oled.toRGB(255,0,0)); 00397 oled.setTouchArea(240,140,290,160); 00398 start=0;sync=1;x=0;y=5;r=0;SignalStatus=0;laststart=2;loop=0; 00399 interrupt_counter = 0;dcf_sec=0;error_count=0,display=0;signal_error=0;rtcset_times=0; 00400 hour = 0;minute = 0,second = 0,dayofweek = 6;dayofmonth = 1;month = 1;year = 0; 00401 00402 T1.start(); 00403 while(dcfSignalIn == 1 && T1.read()<.5){} 00404 while(dcfSignalIn == 0 && T1.read()<.5){} 00405 T1.stop(); 00406 dcfSignalIn.rise(dcfIRQ); //Trigger dcfISR Ticker on rising edge of DCF pulse 00407 interrupt_counter = 0;sync=0; 00408 00409 Ticker50ms.attach(& dcfISR, .05); 00410 00411 while (IRQ==0) { 00412 if (loop==0) { 00413 DCFloop(); // Continuously get dcf time and Display data every second interrupted by the Ticker every 50ms 00414 loop=1;} 00415 } 00416 oled.clear(); 00417 } 00418 00419 void DCFloop() 00420 { 00421 if (interrupt_counter == 0) { 00422 if (start == 0) {dcf_sec=0;} 00423 if (dcf_sec == 0){dcf_array[58] = 0;} 00424 if (dcf_good==1 && dcf_sec==2) {RTCset();} 00425 } 00426 00427 if (interrupt_counter == 1) { 00428 oled.setFontSize(FONT12X16); 00429 oled.locate(15,5);oled.setFontColor(oled.toRGB(255,255,255)); 00430 oled.printf("%02d:%02d:%02d", hour, minute, second); 00431 oled.locate(3,13);oled.setFontColor(oled.toRGB(255,255,255)); 00432 oled.setFontSize(FONT12X16);oled.printf("%02d",dcf_sec); 00433 } 00434 00435 if (interrupt_counter==2){ 00436 if (rtcset==0) { 00437 oled.drawText(33,2,(FONT5X7),"RTC -- un-set -- ",oled.toRGB(255,0,0));} 00438 if (rtcset==1) { 00439 oled.drawText(33,2,(FONT5X7),"RTC *** set *** ",oled.toRGB(0,255,0));} 00440 oled.setFontSize(FONT12X16);oled.locate(0,1); 00441 oled.setFontColor(oled.toRGB(255,255,255)); 00442 RTCread(); 00443 oled.printf("%2d:%02d:%02d",RTChour,RTCminute,RTCsecond); 00444 } 00445 00446 if (interrupt_counter == 4) { 00447 if (dcf_sec > 59) {dcf_sec = 0;} 00448 if (dcf_sec>20 && signal_error==1) {DCFbitmapdraw();DCFdrawgraph();start=0;signal_error=0;SignalStatus=5;} 00449 if (dcf_sec == 57) {oled.drawText(36,7,(FONT8X12)," ",oled.toRGB(0,0,0));} 00450 if (start == 0 && signal_error==0) {SignalStatus=0;} 00451 } 00452 00453 if (interrupt_counter == 6 && dcf_sec > 0){ 00454 if (dcf_sec==1){DCFdrawgraph();SignalStatus = 1;} 00455 oled.setPenSize(1); oled.drawRectangle(11, 229, (dcf_sec*5), 8, oled.toRGB(0,255,0));draw_graph=0; 00456 if (signal_error==1) {DCFdrawgraph();signal_error=0;SignalStatus=5;} 00457 } 00458 00459 if (interrupt_counter == 7){ 00460 DCFbitstatus(); 00461 if (dcf_sec == 21){SignalStatus=2;r = !r;} 00462 if (dcf_sec > 20 && dcf_sec < 59 ) {DCFbitprint();} 00463 } 00464 00465 if (interrupt_counter == 8){ 00466 if (start==1){dcf_array[dcf_sec]=dcf_status.DCFsample150;} 00467 oled.setFontSize(FONT12X16);oled.locate(0,2); 00468 oled.setFontColor(oled.toRGB(255,255,255)); 00469 oled.printf("%s %02d %s' 20%02d",weekDayName[RTCdayofweek],RTCdayofmonth,monthName[RTCmonth-1],RTCyear); 00470 } 00471 00472 if (interrupt_counter == 10) { 00473 oled.setFontSize(FONT8X12);oled.locate(23,8);oled.setFontColor(oled.toRGB(255,255,255)); 00474 oled.printf("%s %02d %s' 20%02d", weekDayName[dayofweek], dayofmonth, monthName[month-1], year); 00475 if (summertime) oled.drawText(23,9,(FONT8X12),"Summer Time",oled.toRGB(255,128,0)); 00476 else oled.drawText(23,9,(FONT8X12),"Winter Time",oled.toRGB(0,255,255)); 00477 oled.drawText(23,10,(FONT8X12),leapSymbol[dcf_status.is_leap],oled.toRGB(0,255,160)); 00478 } 00479 00480 if (interrupt_counter == 11 & second == 1) { 00481 oled.setFontSize(FONT5X7);oled.locate(34,5); 00482 oled.setFontColor(oled.toRGB(255,255,255)); 00483 oled.printf("RTC set x %d", rtcset_times); 00484 } 00485 00486 if (interrupt_counter == 12){ 00487 if (SignalStatus==0) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(255,255,255));} 00488 if (SignalStatus==1) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(255,255,0));} 00489 if (SignalStatus==2) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(0,255,255));} 00490 if (SignalStatus==3) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(0,0,255));} 00491 if (SignalStatus==4) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(0,255,0));} 00492 if (SignalStatus==5) { 00493 oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(255,0,0)); 00494 oled.drawText(36,7,(FONT8X12)," ",oled.toRGB(0,0,0)); 00495 start=0;SignalStatus=0;sync=0; 00496 } 00497 } 00498 00499 if (interrupt_counter==13){ 00500 if (start==0 && laststart != start) {oled.drawText(26,18,(FONT5X7),("No Sync "),oled.toRGB(255,0,0));laststart=start;} 00501 if (start==1 && laststart != start) {oled.drawText(26,18,(FONT5X7),("In Sync - running"),oled.toRGB(0,255,0));laststart=start;} 00502 } 00503 00504 if (interrupt_counter>13){ 00505 oled.getTouch(&xbuffer,&ybuffer); 00506 if (display==0){ 00507 if((xbuffer>2 && xbuffer<318) && (ybuffer>2 && ybuffer<238)){ 00508 oled.displayControl(0x01,0x01);display=1; 00509 oled.resetTouchArea();}} 00510 if((xbuffer>240 && xbuffer<290) && (ybuffer>140 && ybuffer<160)){ 00511 dcfSignalIn.rise(NULL);Ticker50ms.detach();IRQ=1;} 00512 } 00513 00514 if (interrupt_counter == 19){DCFbitstatus();} 00515 00516 } 00517 00518 void DCFtestparity() 00519 { 00520 rtcset=0;DCFparitycalc(); 00521 paritycheck= testu or testm or testdmy; 00522 if (p_year>99) paritycheck=1; 00523 if (p_month>12) paritycheck=1; 00524 if (p_dayofmonth>31) paritycheck=1; 00525 if (p_hour>23) paritycheck=1; 00526 if (p_minute>59) paritycheck=1; 00527 if (paritycheck) {dcf_good=0;SignalStatus = 5;} // bad parity 00528 else {dcf_good=1;SignalStatus = 4;} // good parity 00529 } 00530 00531 void DCFsetTime() 00532 { 00533 second = dcf_sec; 00534 minute=p_minute; 00535 hour=p_hour; 00536 dayofweek=p_dayofweek; 00537 dayofmonth=p_dayofmonth; 00538 month=p_month; 00539 year=p_year; 00540 oled.drawText(36,7,(FONT8X12),"*",oled.toRGB(0,255,0)); 00541 rtcset_times++; 00542 } 00543 void DCFbitprint() 00544 { 00545 if (r==1) {oled.setFontColor(oled.toRGB(0,0,255));} // alternate bit draw colour 00546 else {oled.setFontColor(oled.toRGB(255,255,255));} 00547 oled.setFontSize(FONT8X12); 00548 if (dcf_sec==21){x=5;y=7;} 00549 if (dcf_sec==28){x=5;y=15;oled.locate(5,15);oled.printf(" ");} 00550 if (dcf_sec==29){x=7;y=7;} 00551 if (dcf_sec==35){x=7;y=15;} 00552 if (dcf_sec==36){x=9;y=7;} 00553 if (dcf_sec==42){x=11;y=7;} 00554 if (dcf_sec==45){x=13;y=7;} 00555 if (dcf_sec==50){x=15;y=7;} 00556 oled.locate(x,y); 00557 oled.printf("%d",dcf_status.DCFsample150); 00558 y++; 00559 } 00560 00561 void DCFbitmapdraw() 00562 { 00563 x=2,y=6; 00564 oled.drawText(x-2,y,(FONT8X12)," Dcf M H D d M Y",oled.toRGB(255,255,0)); 00565 oled.drawText(x,y+1,(FONT8X12)," 1 i o a a o e",oled.toRGB(255,255,0)); 00566 oled.drawText(x,y+2,(FONT8X12)," 2 n u t y n a",oled.toRGB(255,255,0)); 00567 oled.drawText(x,y+3,(FONT8X12)," 4 u r e t r",oled.toRGB(255,255,0)); 00568 oled.drawText(x,y+4,(FONT8X12)," 8 t h ",oled.toRGB(255,255,0)); 00569 oled.drawText(x,y+5,(FONT8X12),"10 e ",oled.toRGB(255,255,0)); 00570 oled.drawText(x,y+6,(FONT8X12),"20 ",oled.toRGB(255,255,0)); 00571 oled.drawText(x,y+7,(FONT8X12),"40 ",oled.toRGB(255,255,0)); 00572 oled.drawText(x,y+8,(FONT8X12),"80 ",oled.toRGB(255,255,0)); 00573 oled.drawText(x,y+9,(FONT8X12)," P arity ",oled.toRGB(255,255,0)); 00574 oled.drawLine(8,83,135,83, oled.toRGB(0, 255, 0)); 00575 oled.drawLine(35,70,35,195, oled.toRGB(0, 255, 0)); 00576 } 00577 void DCFdrawgraph() 00578 { 00579 if (draw_graph==0){ 00580 oled.setPenSize(0); 00581 oled.drawRectangle(10,228,300,8,oled.toRGB(0,0,0)); 00582 oled.setPenSize(1); 00583 oled.drawRectangle(10,227,302,10,oled.toRGB(255,255,255)); 00584 oled.drawLine(10,220,10,237,oled.toRGB(255,255,255)); 00585 oled.drawLine(312,220,312,237,oled.toRGB(255,255,255)); 00586 oled.drawLine(111,220,111,237,oled.toRGB(255,255,255)); //start 00587 oled.drawLine(156,220,156,237,oled.toRGB(225,255,255)); //min 00588 oled.drawLine(186,220,186,237,oled.toRGB(255,255,225)); //hour 00589 oled.drawLine(216,220,216,237,oled.toRGB(225,255,225)); //date 00590 oled.drawLine(231,220,231,237, oled.toRGB(225, 255, 225));//day 00591 oled.drawLine(256,220,256,237, oled.toRGB(225, 255, 225));//month 00592 oled.drawLine(301,220,301,237, oled.toRGB(225, 255, 225));//year 00593 draw_graph=1; 00594 } 00595 } 00596 void DCFbitstatus() 00597 { 00598 oled.locate(29,22); 00599 oled.setFontSize(FONT5X7); 00600 oled.setFontColor(oled.toRGB(255,0,0)); 00601 oled.printf("%d %d %d %d %d",dcf_status.DCFsample50,dcf_status.DCFsample150,dcf_status.DCFsample300,dcf_status.DCFsample500,dcf_status.DCFsample600); 00602 oled.setPenSize(0); 00603 if (!dcf_status.DCFsample50) {oled.drawRectangle(170,190,11,12, oled.toRGB(0,0,0));} 00604 else {oled.drawRectangle(170,190,11,12, oled.toRGB(255,255,255));} 00605 if (!dcf_status.DCFsample150) {oled.drawRectangle(182,190,11,12, oled.toRGB(0,0,0));} 00606 else {oled.drawRectangle(182,190,11,12, oled.toRGB(255,255,255));} 00607 if (!dcf_status.DCFsample300) {oled.drawRectangle(194,190,11,12, oled.toRGB(0,0,0));} 00608 else {oled.drawRectangle(194,190,11,12, oled.toRGB(255,255,255));} 00609 if (!dcf_status.DCFsample500) {oled.drawRectangle(206,190,23,12, oled.toRGB(0,0,0));} 00610 else {oled.drawRectangle(206,190,23,12, oled.toRGB(255,255,255));} 00611 if (!dcf_status.DCFsample600) {oled.drawRectangle(230,190,56,12, oled.toRGB(0,0,0));} 00612 else {oled.drawRectangle(230,190,56,12, oled.toRGB(0,0,255));} 00613 } 00614 00615 void DCFparitycalc() 00616 { 00617 //calculate summer/winter time---------------------------------------------------------------------- 00618 summertime = dcf_array[17] & 1; 00619 //calculate hour-------------------------------------------------------------------------------------- 00620 hourh = dcf_array[34] * 20 + dcf_array[33] * 10; 00621 hourl = dcf_array[32] * 8 + dcf_array[31] * 4 + dcf_array[30] * 2 + dcf_array[29] * 1; 00622 p_hour = hourh + hourl; 00623 //calculate minutes------------------------------------------------------------------------------------ 00624 minl = dcf_array[24] * 8 + dcf_array[23] * 4 + dcf_array[22] * 2 + dcf_array[21] * 1; 00625 minh = dcf_array[27] * 40 + dcf_array[26] * 20 +dcf_array[25] * 10; 00626 p_minute = minh + minl; 00627 //calculate day of week-------------------------------------------------------------------------------- 00628 p_dayofweek = dcf_array[44] * 4 +dcf_array[43] * 2 + dcf_array[42] * 1; 00629 //calculate day---------------------------------------------------------------------------------------- 00630 dayl = dcf_array[39] * 8 + dcf_array[38] * 4 + dcf_array[37] * 2 + dcf_array[36] * 1; 00631 dayh = dcf_array[41] * 20 + dcf_array[40] * 10; 00632 p_dayofmonth=dayh+dayl; 00633 //calculate month-------------------------------------------------------------------------------------- 00634 monthh = dcf_array[49] * 10; 00635 monthl = dcf_array[48] * 8 + dcf_array[47] * 4 + dcf_array[46] * 2 + dcf_array[45] * 1; 00636 p_month = monthh +monthl; 00637 //calculate year--------------------------------------------------------------------------------------- 00638 yearh = dcf_array[57] * 80 + dcf_array[56] * 40 + dcf_array[55] * 20 + dcf_array[54] * 10; 00639 yearl = dcf_array[53] * 8 +dcf_array[52] * 4 + dcf_array[51] * 2 + dcf_array[50] * 1; 00640 p_year = yearh+yearl; 00641 //calculate parity 00642 paritym = dcf_array[21] + dcf_array[22] + dcf_array[23] + dcf_array[24] + dcf_array[25] + dcf_array[26] +dcf_array[27] +dcf_array [28]; 00643 parityu =dcf_array[29] + dcf_array[30] + dcf_array[31] + dcf_array[32] + dcf_array[33] + dcf_array[34] + dcf_array[35]; 00644 paritydmy =dcf_array[36] + dcf_array [37] + dcf_array [38] + dcf_array [39] + dcf_array[40] + dcf_array[41] + dcf_array [42] + dcf_array [43] + dcf_array[44] + dcf_array [45] + dcf_array[46] + dcf_array [47] + dcf_array[48] + dcf_array[49] + dcf_array[50] + dcf_array[51] + dcf_array [52] + dcf_array[53] + dcf_array[54] + dcf_array[55] + dcf_array[56] + dcf_array[57] + dcf_array[58]; 00645 //test parity------------------------------ 00646 testu=parityu & 1; 00647 testm=paritym & 1; 00648 testdmy=paritydmy & 1; 00649 } 00650 00651 void dcfIRQ(void) 00652 { 00653 if (sync==0){ // align Ticker with DCF 59th bit start signal every minute 00654 interrupt_counter = 0;sync=1;dcf_sec=0;error_count=0; 00655 Ticker50ms.attach(& dcfISR, .05); 00656 } 00657 } 00658 00659 void dcfISR() //This is the interrupt service routine (ISR) that is called every 50ms 00660 { 00661 interrupt_counter++;loop=0; 00662 00663 SignalLED = !dcfSignalIn; // Show dcfSignal state on LED 00664 00665 if (interrupt_counter == 20) { // 50mS x 20 = 1000mS = 1 Second 00666 interrupt_counter = 0;second++;dcf_sec++; 00667 if (start==0) dcf_sec=0; 00668 } 00669 if (interrupt_counter == 0){ 00670 if (dcf_sec==58) {SignalStatus = 3;} 00671 if (dcf_sec==59) {DCFtestparity();} 00672 if (dcf_good==1 && dcf_sec==1) {DCFsetTime();} 00673 } 00674 if (second >= 60) {++minute;second -=60;} 00675 if (minute >= 60) {++hour;minute-=60;} 00676 if (hour >= 24) {hour -=24;++dayofweek;++dayofmonth;} 00677 if (dayofweek > 6) dayofweek = 0; 00678 if (dayofmonth > maxDay(year, month)) {dayofmonth = 1;month++;} 00679 if (month > 12) {month = 1;year++;} 00680 if (year > 99) year = 1; 00681 00682 switch (interrupt_counter) { 00683 case 1: { // 50mS after start of second pulse 00684 dcf_status.DCFsample50 = (dcfSignalIn); 00685 break;} 00686 case 3: { // 150mS after start of second pulse (bit "1" dcf Data) 00687 dcf_status.DCFsample150 = (dcfSignalIn); 00688 break;} 00689 case 6: { // 300mS after start of second (signal error if true) 00690 dcf_status.DCFsample300 = (dcfSignalIn); 00691 if (dcfSignalIn) {dcf_error = 1;} 00692 break;} 00693 case 10: { // 500mS after start of second (signal error if true) 00694 dcf_status.DCFsample500 = (dcfSignalIn); 00695 if (dcfSignalIn) {dcf_error = 1;} 00696 break;} 00697 case 12: { // 600mS after start of second (signal error if true) 00698 dcf_status.DCFsample600 = (dcfSignalIn); 00699 if (dcfSignalIn) {dcf_error = 1;} 00700 break;} 00701 } 00702 if (dcf_status.DCFsample50==0 && dcf_sec<58){sync=0;} 00703 00704 if (interrupt_counter==1){ 00705 if (dcfSignalIn){nosignal=1;} 00706 else nosignal++; 00707 if (nosignal>5){nosignal=2; SignalStatus = 5;signal_error=1;} 00708 } 00709 00710 if (interrupt_counter==15){ 00711 if (!dcf_status.DCFsample150 && !dcf_status.DCFsample50) {sync=0;start=1;} 00712 } 00713 if (interrupt_counter==18){ 00714 if (dcf_error==1) {error_count++;dcf_error=0;} 00715 if (error_count > 3 && sync==1) {error_count = 0;signal_error=1;} 00716 } 00717 00718 } // End of ISR 00719
Generated on Thu Jul 21 2022 11:32:25 by
1.7.2