![Smile :-)](./images/smilies/icon_smile.gif)
attach wc2.c file here so I can check.
Moderators: pedja089, stojke369, trax, InTheStillOfTheNight
Code: Select all
// setting the time and date
// NOTE: RTC is being set in 24hr format as well as date in DD.MM.YYYY
void menu_set_rtc()
{
uint8_t setup_part = 0; // first we are going to setup the HH
btn_inactivity_timer = BTN_INACTIVITY_TMR; // if the user ignores the clock
bs(bRTC_Pause,BAPP1); // pause reading clock data from I2C!
display_clear(0);
bs(bDispChg,BAPP1);
while(setup_part < 6) // we are setting 6 parts in total, from 0..5 (HH, MM, dd, mm, yyyy, DOW)
{
if(btn_inactivity_timer==0) break; // get out at the exit point of this function
char buff[25]; // text buffer
// display current setting
switch(setup_part)
{
// hh
case 0:
sprintf(buff,"%02u:",RTC[TIME_H]); // prepare text for display
break;
// mm
case 1:
sprintf(buff,":%02u",RTC[TIME_M]); // prepare text for display
break;
// DD.
case 2:
sprintf(buff,"%02u.",RTC[DATE_D]); // prepare text for display
break;
// MM
case 3:
sprintf(buff,".%02u",RTC[DATE_M]); // prepare text for display
break;
// (20)YY
case 4:
sprintf(buff,".%02u'",RTC[DATE_Y]); // prepare text for display
break;
// DOW
case 5:
// Print SHORT day-names here! 3 letters.
switch(dow)
{
case 1:
sprintf(buff,"Sun");
break;
case 2:
sprintf(buff,"Mon");
break;
case 3:
sprintf(buff,"Tue");
break;
case 4:
sprintf(buff,"Wed");
break;
case 5:
sprintf(buff,"Thu");
break;
case 6:
sprintf(buff,"Fri");
break;
case 7:
sprintf(buff,"Sat");
break;
}
break;
}
// we need a mechanism not to flood up the display very fast because it flickers for some reason
if( bv(bDispChg,BAPP1) )
{
text_to_display(buff,1,0); // send the text to display - it will overwrite previous text
display_progressbar(setup_part+1,6); // also show the progressbar, it could have advanced
bc(bDispChg,BAPP1); // done with updating
}
// BTN-A? held or pressed
if( (btn_press|btn_hold) & 0b00000001)
{
bs(bDispChg,BAPP1); // mark that display will probably change
uint16_t delay_value = BTN_REPEAT_RATE; // speed of auto-increment when holding a button
switch(setup_part)
{
// the HH:mm dd.mm.yyyy
case 0:
RTC[TIME_H] = (RTC[TIME_H]+1) % 24; // +1
break;
// the hh:MM dd.mm.yyyy
case 1:
RTC[TIME_M] = (RTC[TIME_M]+1) % 60; // +1
break;
// the hh:mm DD.mm.yyyy
case 2:
if(++RTC[DATE_D] > 31) RTC[DATE_D] = 1; // +1 with check
break;
// the hh:mm dd.MM.yyyy
case 3:
if(++RTC[DATE_M] > 12) RTC[DATE_M] = 1; // +1 with check
break;
// the hh:mm dd.mm.yYYY
case 4:
RTC[DATE_Y] = RTC[DATE_Y]+1; // +1 .. might be boring to wait for overflow at year 2099 when setting :)
if(RTC[DATE_Y]>99) RTC[DATE_Y]=0; // because DS323X RTC IC can store only 00-99
delay_value = BTN_REPEAT_RATE/2; // so this will go twice as fast!
break;
// set dow
case 5:
dow++;
if( dow > 7 ) dow=1;
break;
}
if(btn_hold & 0b00000001) // if the user is holding it, increment hour by 1s
{
delay_ms_(delay_value); // slow down but not too much
}
btn_press &= ~0b00000001; // button handled
if( !(BTNA_PINREG & _BV(BTNA_PIN)) ) btn_hold &= ~0b00000001; // if it is held no more, we can handle it
btn_inactivity_timer = BTN_INACTIVITY_TMR; // reload
}
// BTN-B?
if(btn_press & 0b00000010)
{
bs(bDispChg,BAPP1); // mark that display will probably change
setup_part++;
btn_press &= ~0b00000010; // button handled
btn_hold &= ~0b00000010; // also handle the hold
btn_inactivity_timer = BTN_INACTIVITY_TMR; // reload to stay in programming mode
}
} // while(setup_part < *)
// now we need to send settings to DS323X RTC IC!
twi_start(DS323X_ADDR_WR);
twi_tx_byte(0x00); // address seconds
twi_tx_byte( 0 ); // seconds=0
twi_tx_byte( DECTOBCD(RTC[TIME_M]) ); // minutes
twi_tx_byte( DECTOBCD(RTC[TIME_H]) & 0b00111111 ); // hours, but clear upper two bits
twi_tx_byte( dow ); // day of week - now used!
twi_tx_byte( DECTOBCD(RTC[DATE_D]) ); // day
twi_tx_byte( DECTOBCD(RTC[DATE_M]) & 0b01111111 ); // month, but clear the century bit
twi_tx_byte( DECTOBCD(RTC[DATE_Y]) ); // year 00-99
twi_stop();
text_scroll("&&&&Ok",100,1);
display_clear(0);
bc(bRTC_Pause,BAPP1); // continue reading clock data from I2C!
btn_inactivity_timer = BTN_INACTIVITY_TMR; // reload to stay in programming mode if timeout happened above
return;
}
Code: Select all
// setting day of week
void menu_set_dow(void)
{
uint8_t dow = 0;
btn_inactivity_timer = BTN_INACTIVITY_TMR; // if the user ignores the clock
bs(bRTC_Pause,BAPP1); // pause reading clock data from I2C!
display_clear(0);
bs(bDispChg,BAPP1);
while(dow < 7) // we are setting 7 parts in total, from 0..6 (Son, Mon, Die, Mit, Don, Fre, Sam)
{
if(btn_inactivity_timer==0) break; // get out at the exit point of this function
char buff[10]; // text buffer
// display current setting
switch(dow)
{
case 0:
sprintf(buff,"Son");
break;
case 1:
sprintf(buff,"Mon");
break;
case 2:
sprintf(buff,"Die");
break;
case 3:
sprintf(buff,"Mit");
break;
case 4:
sprintf(buff,"Don");
break;
case 5:
sprintf(buff,"Fre");
break;
case 6:
sprintf(buff,"Sam");
break;
}
// we need a mechanism not to flood up the display very fast because it flickers for some reason
if( bv(bDispChg,BAPP1) )
{
text_to_display(buff,1,0); // send the text to display - it will overwrite previous text
display_progressbar(dow+1,6); // also show the progressbar, it could have advanced
bc(bDispChg,BAPP1); // done with updating
}
// BTN-A? held or pressed
if( (btn_press|btn_hold) & 0b00000001)
{
bs(bDispChg,BAPP1); // mark that display will probably change
uint16_t delay_value = BTN_REPEAT_RATE; // speed of auto-increment when holding a button
switch(dow)
{
case 1:
dow++;
if(dow > 7) dow=1;
break;
}
if(btn_hold & 0b00000001) // if the user is holding it, increment hour by 1s
{
delay_ms_(delay_value); // slow down but not too much
}
btn_press &= ~0b00000001; // button handled
if( !(BTNA_PINREG & _BV(BTNA_PIN)) ) btn_hold &= ~0b00000001; // if it is held no more, we can handle it
btn_inactivity_timer = BTN_INACTIVITY_TMR; // reload
}
// BTN-B?
if(btn_press & 0b00000010)
{
bs(bDispChg,BAPP1); // mark that display will probably change
dow++;
btn_press &= ~0b00000010; // button handled
btn_hold &= ~0b00000010; // also handle the hold
btn_inactivity_timer = BTN_INACTIVITY_TMR; // reload to stay in programming mode
}
} // while(setup_part < *)
// now we need to send settings to DS323X RTC IC!
twi_start(DS323X_ADDR_WR);
twi_tx_byte(dow); // day of week - now used
twi_stop();
text_scroll("&&&&Ok",100,1);
display_clear(0);
bc(bRTC_Pause,BAPP1); // continue reading clock data from I2C!
btn_inactivity_timer = BTN_INACTIVITY_TMR;
return;
}
Code: Select all
while(dow < 7) // we are setting 7 parts in total, from 0..6 (Son, Mon, Die, Mit, Don, Fre, Sam)
{
Code: Select all
// now we need to send settings to DS323X RTC IC!
twi_start(DS323X_ADDR_WR);
twi_tx_byte(dow); // day of week - now used
twi_stop();
Code: Select all
// now we need to send settings to DS323X RTC IC!
twi_start(DS323X_ADDR_WR);
twi_tx_byte(0x03); // address DOW register
twi_tx_byte(dow); // day of week - now used
twi_stop();
Code: Select all
// set day of week
void menu_set_dow(void)
{
uint8_t setup_part = 0; // first we are going to setup the HH
display_clear(0);
btn_inactivity_timer = BTN_INACTIVITY_TMR; // if the user ignores the clock
bs(bDispChg,BAPP1);
while(setup_part < 1) // we are setting 1 part in total, on/off value actually
{
if(btn_inactivity_timer==0) break; // get out at the exit point of this function
char buff [25];
//display current setting
switch(setup_part)
{
//dow
case 0:
switch(dow)
{
case 1:
sprintf(buff,"So");
break;
case 2:
sprintf(buff,"Mo");
break;
case 3:
sprintf(buff,"Di");
break;
case 4:
sprintf(buff,"Mi");
break;
case 5:
sprintf(buff,"Do");
break;
case 6:
sprintf(buff,"Fr");
break;
case 7:
sprintf(buff,"Sa");
break;
}
break;
}
if( bv(bDispChg,BAPP1) )
{
text_to_display(buff,1,0);
display_progressbar(dow+1,6);
bc(bDispChg,BAPP1);
}
// BTN-A? held or pressed
if( (btn_press|btn_hold) & 0b00000001)
{
bs(bDispChg,BAPP1);
uint16_t delay_value = BTN_REPEAT_RATE;
switch(setup_part)
{
//set dow
case 0:
dow++;
if( dow > 7 ) dow=1;
break;
}
if(btn_hold & 0b00000001)
{
delay_ms_(delay_value);
}
btn_press &= ~0b00000001;
if( !(BTNA_PINREG & _BV(BTNA_PIN)) ) btn_hold &= ~0b00000001;
btn_inactivity_timer = BTN_INACTIVITY_TMR; // reload
}
// BTN-B?
if(btn_press & 0b00000010)
{
bs(bDispChg,BAPP1);
setup_part++;
btn_press &= ~0b00000010; // button handled
btn_hold &= ~0b00000010; // also handle the hold
btn_inactivity_timer = BTN_INACTIVITY_TMR; // reload to stay in programming mode
}
} // while(setup_part < *)
twi_start(DS323X_ADDR_WR);
twi_tx_byte(0x03); // address seconds
twi_tx_byte( dow ); // day of week - used
twi_stop();
text_scroll(" Ok",100,1);
display_clear(0);
return;
}
//##############################
// Displaying stuff functions //
//##############################
// display date, in scrolling fashion
void display_date(void)
{
char buff[20];
char day_name[20];
{
switch(dow)
{
case 1:
sprintf(day_name,"Sonntag");
break;
case 2:
sprintf(day_name,"Montag");
break;
case 3:
sprintf(day_name,"Dienstag");
break;
case 4:
sprintf(day_name,"Mittwoch");
break;
case 5:
sprintf(day_name,"Donnerstag");
break;
case 6:
sprintf(day_name,"Freitag");
break;
case 7:
sprintf(day_name,"Samstag");
break;
}
}
sprintf(buff," %02u/%02u/%04u %s",RTC[DATE_D],RTC[DATE_M],(2000+RTC[DATE_Y]),day_name);
// clear all but dots in the corners
display_clear(0);
// scroll this stuff
text_scroll(buff,100,1);
return;
}