void setTime1302(int seconds, int minutes, int hours, int dayofweek, int dayofmonth, int month, int year) { ds1302_struct rtc = {}; // Set a time and date // This also clears the CH (Clock Halt) bit, // to start the clock. // Fill the structure with zeros to make // any unused bits zero // memset ((char *) &rtc, 0, sizeof(rtc)); rtc.Seconds = bin2bcd_l( seconds); rtc.Seconds10 = bin2bcd_h( seconds); rtc.CH = 0; // 1 for Clock Halt, 0 to run; rtc.Minutes = bin2bcd_l( minutes); rtc.Minutes10 = bin2bcd_h( minutes); // To use the 12 hour format, // use it like these four lines: // rtc.h12.Hour = bin2bcd_l( hours); // rtc.h12.Hour10 = bin2bcd_h( hours); // rtc.h12.AM_PM = 0; // AM = 0 // rtc.h12.hour_12_24 = 1; // 1 for 24 hour format rtc.h24.Hour = bin2bcd_l( hours); rtc.h24.Hour10 = bin2bcd_h( hours); rtc.h24.hour_12_24 = 0; // 0 for 24 hour format rtc.Date = bin2bcd_l( dayofmonth); rtc.Date10 = bin2bcd_h( dayofmonth); rtc.Month = bin2bcd_l( month); rtc.Month10 = bin2bcd_h( month); rtc.Day = dayofweek; rtc.Year = bin2bcd_l( year - 2000); rtc.Year10 = bin2bcd_h( year - 2000); rtc.WP = 0; // Write all clock data at once (burst mode). DS1302_clock_burst_write( (uint8_t *) &rtc); }
int main(void) { //PORTS A,C,E,D are dedicated to matrix display DDRD = 0xFF; DDRE = 0xFF; DDRA = 0xFF; DDRC = 0xFF; //button port is for input BUTTON_DIR = 0x00; //pullups for PINB0 and PINB1 (button X and Z) BUTTON_PORT = 0x03; //just make sure pullups are NOT disabled MCUCR |= (0 << PUD); //A0-A3 : are the negative row 1-4 //C4-C7 : are the negative row 5-8 XDIV = 0x00; init_timer2_OVF(); init_timer0_OVF(); //DS1302 is plugged on the G port setupDS1302(); wormInit(); rainInit(); while (1){ //refresh display showMatrix(); if (mShowMode != MODE_SETTIME){ //check if BUTTON_X is pressed (back board button) if ((~BUTTON_INPUT & (1 << BUTTON_X)) != 0){ matrixClearAll(); mShowMode = (mShowMode + 1) % MODE_COUNT; //debounce _delay_ms(500); } //Z : go set time if ((~BUTTON_INPUT & (1 << BUTTON_Z)) != 0){ mShowMode = MODE_SETTIME; mSubModeSetTimeStep = 0; //debounce _delay_ms(500); } } else { //mode set time //X : hour or minute +1 if ((~BUTTON_INPUT & (1 << BUTTON_X)) != 0){ if (mSubModeSetTimeStep == 0){ //hours +=1 uint8_t vHours = rtc.h24.Hour10 * 10 + rtc.h24.Hour; vHours = (vHours +1) % 24; rtc.h24.Hour10 = vHours / 10; rtc.h24.Hour = vHours % 10; //write and read just after to see the result DS1302_clock_burst_write((uint8_t *) &rtc); DS1302_clock_burst_read( (uint8_t *) &rtc); } if (mSubModeSetTimeStep == 1){ //minutes +=1 uint8_t vMin = rtc.Minutes10 * 10 + rtc.Minutes; vMin = (vMin +1) % 60; rtc.Minutes10 = vMin / 10; rtc.Minutes = vMin % 10; //write and read just after to see the result DS1302_clock_burst_write((uint8_t *) &rtc); DS1302_clock_burst_read( (uint8_t *) &rtc); } //debounce _delay_ms(500); } //Z : go set time if ((~BUTTON_INPUT & (1 << BUTTON_Z)) != 0){ mSubModeSetTimeStep++; if (mSubModeSetTimeStep > 1){ //finished matrixClearAll(); mShowMode = MODE_TIME_RND; mSubModeAnimId = rand() % 3; } //debounce _delay_ms(500); } } } }