Microcontroller Calendar Code in C - Page 1
Forum section: electronics / general

author: @rt
author_rank: Super Contributor
timestamp: July 08, 2017, 07:38:58 am
content: Hi GuysThe video...Microcontroller calendar code with a graphic demonstration.This is ported from PicBASIC code I wrote a couple of years ago,and still retains global variables, that could be private in C.Not hard to make it more formal.Cheers, Brek.

author: @rt
author_rank: Super Contributor
timestamp: July 08, 2017, 07:41:10 am
content: The source code ported from PicBASIC to C, and compiled with MPLAB & C16....Code:// variables for calendar code//unsigned char caldim = {31,28,31,30,31,30,31,31,30,31,30,31}; // days in monthsunsigned char caldow;// calendar day of the weekunsigned char calleap;// current year leap status bitunsigned char calbadentry;// bad calendar input flagunsigned int calday;// calendar dayunsigned int calmonth;// calendar monthunsigned int calyear;// calendar yearunsigned int cacnt;// counter for calendarunsigned int cwork;// work buffer for calendarunsigned int xwork;// work buffer for calendar// 500 year calendar calculator - Brek Martin 2015 - 2017.// outputs a day of week for a given day, month, and year.// originally intended and tested range of years 1800-2300.//void setcal() {calleap = 0;// clear leap year statusif (calyear % 4 == 0) {calleap = 1;// set leap year statusif (calyear % 100 == 0) {calleap = 0;// clear leap year statusif (calyear % 400 == 0) {calleap = 1;// set leap year status}}}caldim = 28 + calleap;calbadentry = 0;// check for invalid date inputif (calmonth == 0) {// check for invalid date inputcalbadentry = 1;calmonth = 1;}if (calmonth > 12) {calbadentry = 1;calmonth = 1;}if (calday == 0) {calbadentry = 1;calday = 1;}if (calday > caldim[calmonth-1]) {calbadentry = 1;calday = 1;}if (calyear < 1800) {// enforce lower year range restrictioncalbadentry = 1;// must be greater than the lowest epochcalyear = 1800;}if (calyear > 2300) {// implement higher year range restrictioncalbadentry = 1;// not needed but will slow down programcalyear = 2300;}// the hardware probably won't last that long//xwork = 2310; cacnt = 12; // find best epoch year to iterate fromwhile (xwork >= calyear) {epochyears(); cacnt--;}cwork = 0; // working variable for dayscacnt = xwork; // iterate years from epoch yearwhile (cacnt < calyear) { //int calleapx = 0; // clear leap year statusif (cacnt % 4 == 0) {calleapx = 1;// set leap year statusif (cacnt % 100 == 0) {calleapx = 0;// clear leap year statusif (cacnt % 400 == 0) {calleapx = 1;// set leap year status}}}cwork = (cwork + calleapx)%7;// add days over the years from epochcacnt++;} // cacnt//caldim = 28 + calleap;// fix day of month table for current yearcacnt = calmonth-1;// add days for elapsed months of current yearwhile (cacnt > 0) {//xwork = caldim[cacnt-1];//cwork = (cwork + xwork)%7;//cacnt = cacnt - 1;//} // wend////cacnt = calday-1;// add days elapsed of current monthxwork = 0;//while (cacnt > 0) {//xwork = xwork + 1;//cacnt = cacnt - 1;//} // wendcwork = (cwork + xwork)%7;//caldow = cwork + 1;// shift the day of week rangeif (caldow == 7) {caldow = 0;}//} // setcalvoid epochyears() {const int epochtable = {1798,1849,1900,1951,1990,2035,2052,2091,2125,2153,2198,2227,2244};if (cacnt < 13) {xwork = epochtable;} else {calbadentry = 1;}} // epochyears

author: @rt
author_rank: Super Contributor
timestamp: July 08, 2017, 07:42:33 am
content: C source for the microcontroller demo in the video.This makes some assumptions, such as having a 128x64 GLCDthat you can talk to with some print text string function,and the platform has a joystick or some input method.Code:// draw graphic table format monthly calendar// provides preview and rewind with joystick//void calgrid() {const char cgdow = {"SMTWTFS"};int calrows = 0;int calcolumns = 0;int printdays = 0;int calcalc = 0;int caldaybuf = 0; // for current day highlightint cjoytimer = 0;int calloop = 1;startvoice();clrframebuffer(); // clear frame bufferif (gotdate == 0) { // date was acquired from gpsdrawroundedrectangle(25,26,78,12); // message bordersprintf (text,"CHECKING GPS"); // explain the delayty = 29; drawtextcentered();drawscreen(); // draw message screencalmonth = 1; calyear = 2017; // set default monthhandlegps(); handlegps(); applytzoffset(); // gps attempt} // gotdateif (gotdate != 0) {calyear = gpsyear; calmonth = gpsmonth; caldaybuf = gpsday;} // gotdate//while (calloop == 1) { // main calendar loopclrgraphics(); // clear graphics bufferscalday = 1; setcal(); // get day of weekprintdays = caldim[calmonth-1];sprintf (text,"%02d-%04d",calmonth,calyear);if (text == 0x30) {text = 0x20;} // zero blankty = 1; drawtextcentered(); // draw table titlewhile (ty < 8) { // invert titlex = 0;while (x < 128) {y = ty; flippixel(); x++;}ty++;} // tycalcolumns = 0; calrows = caldow;while (calcolumns < 8) {while (calrows < 7 && printdays > 0) {text = cgdow; text = 0; // print days of weekty = 9; tx = (calrows * 18) + 7; drawtext();calcalc = (caldim[calmonth-1]-printdays)+1;sprintf (text,"%02d",calcalc);if (text == 0x30) {text = 0x20;} // zero blanktx = (calrows * 18) + 3; ty = (calcolumns * 8) + 17;drawtext();if (calcalc == caldaybuf) { // check for current dayif (calyear == gpsyear && calmonth == gpsmonth && gotdate != 0) {if ((framecount & 0b00010000) == 0b00010000) {if (calcalc < 10) {tx = tx + 6;} // move bullet point closer to single digitdrawfilledrectangle(tx-15,ty+2,3,3); // draw bullet point} // framecount}} // calcalcprintdays--; calrows++;} // calrowscalrows = 0; calcolumns++;} // calcolumnsdrawscreen();//shiftbytein(); // read joystickif (joyleft == 1) {playblopsound();if (cjoytimer < 36) {calmonth--; cjoytimer++;if (calmonth < 1) {calmonth = 12;if (calyear > 1800) {calyear--;}} // calmonthdelay_gen(360);} else {if (calyear > 1800) {calyear--;}} // cjoytimer} // joyleftif (joyright == 1) {playblopsound();if (cjoytimer < 36) {calmonth++; cjoytimer++;if (calmonth > 12) {calmonth = 1; calyear++;}delay_gen(360);} else {calyear++;} // cjoytimer} else { // joyright is clearif (joyleft == 0) {cjoytimer = 0;} // reset joystick timer} // joyrightif (joyfire == 1) {calloop = 0;} // exit calendarframecount++;} // calloop// exit to menusamplenum = 1; load_sample(); playvoice(); // play exit sampledelay_gen(1400);resettomenu();}//