⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtc.c

📁 butterflylogger_src_20060822 for atmel avr
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************   Function name : ShowDate**   Returns :       char ST_state (to the state-machine)**   Parameters :    char input (from joystick)**   Purpose :       Shows the date on the LCD******************************************************************************/char ShowDate(char input){    char YH, YL, MH, ML, DH, DL;        YH = CHAR2BCD2(gYEAR);    YL = (YH & 0x0F) + '0';    YH = (YH >> 4) + '0';        MH = CHAR2BCD2(gMONTH);    ML = (MH & 0x0F) + '0';    MH = (MH >> 4) + '0';        DH = CHAR2BCD2(gDAY);    DL = (DH & 0x0F) + '0';    DH = (DH >> 4) + '0';            // mtA - based on jw    // TODO: check poss. opt. with pgm_read_word    // LCD_putc( *(DATE_FORMAT_NR[dateformat] + 0), YH);    // LCD_putc( *(DATE_FORMAT_NR[dateformat] + 1), YL);    LCD_putc( pgm_read_byte(DATE_FORMAT_NR[dateformat] + 0), YH);    LCD_putc( pgm_read_byte(DATE_FORMAT_NR[dateformat] + 1), YL);        // LCD_putc( *(DATE_FORMAT_NR[dateformat] + 2), MH);    // LCD_putc( *(DATE_FORMAT_NR[dateformat] + 3), ML);    LCD_putc( pgm_read_byte(DATE_FORMAT_NR[dateformat] + 2), MH);    LCD_putc( pgm_read_byte(DATE_FORMAT_NR[dateformat] + 3), ML);            // LCD_putc( *(DATE_FORMAT_NR[dateformat] + 4), DH);    // LCD_putc( *(DATE_FORMAT_NR[dateformat] + 5), DL);    LCD_putc( pgm_read_byte(DATE_FORMAT_NR[dateformat] + 4), DH);    LCD_putc( pgm_read_byte(DATE_FORMAT_NR[dateformat] + 5), DL);    // mtE            LCD_putc(6, '\0');        LCD_Colon(1);        LCD_UpdateRequired(TRUE, 0);            if (input == KEY_PREV)        return ST_TIME_DATE;    else if (input == KEY_NEXT)        return ST_TIME_DATE_ADJUST;    else           return ST_TIME_DATE_FUNC;}/*******************************************************************************   Function name : PrintDate**   Returns :       none**   Parameters :    The date in compressed 2 byte binary format**   Purpose :       Prints a date to the Uart YY/MM/DD******************************************************************************/void PrintDate(char D0,char D1){	/*	 D0[0..4] = Day	 D0[5..7], D1[0] = Month	 D1[1..7] = year	 */	USART_Tx_Byte((D1>>1));    USART_Tx('/');    USART_Tx_Byte((D0>>5) + ((D1 & 1)<<3) );    USART_Tx('/');    USART_Tx_Byte((D0 & 0x1F));	}/*******************************************************************************   Function name : LogDate**   Returns :       none**   Parameters :    none**   Purpose :       Saves date to the Flash in a comprressed 2 byte format******************************************************************************/void LogDate(void){    char D0,D1;	/*	 D0[0..4] = Day	 D0[5..7], D1[0] = Month	 D1[1..7] = year	 */	D1 = (gYEAR<<1);	D1 += (gMONTH & 0x0F)>>3;		D0 = (gDAY & 0x1F);	D0 += (gMONTH & 0x07)<<5;		DF_SPI_RW(D1);	DF_SPI_RW(D0);  }/*******************************************************************************   Function name : SetDate**   Returns :       char ST_state (to the state-machine)**   Parameters :    char input (from joystick)**   Purpose :       Adjusts the date******************************************************************************/char SetDate(char input){    static char enter_function = 1;    // mtA    // static char date[3];    // table holding the temporary date setting    // static char mode = DAY;    // char YH, YL, MH, ML, DH, DL;    // char MonthLength_temp;    // char LeapMonth;    static uint8_t date[3];    // table holding the temporary date setting    static uint8_t mode = DAY;    uint8_t YH, YL, MH, ML, DH, DL;    uint8_t MonthLength_temp;    uint8_t LeapMonth;    // mtE        if (enter_function)    {        date[YEAR] = gYEAR;        date[MONTH] = gMONTH;        date[DAY] = gDAY;    }        if (mode == YEAR)    {        YH = CHAR2BCD2(date[YEAR]);        YL = (YH & 0x0F) + '0';        YH = (YH >> 4) + '0';                LCD_putc( 0, ' ');        LCD_putc( 1, ' ');           LCD_putc( 2, 'Y');        LCD_putc( 3, 'Y');                LCD_putc( 4, YH);        LCD_putc( 5, YL);    }    else if (mode == MONTH)    {        MH = CHAR2BCD2(date[MONTH]);        ML = (MH & 0x0F) + '0';        MH = (MH >> 4) + '0';		        LCD_putc( 0, ' ');        LCD_putc( 1, ' ');           LCD_putc( 2, 'M');        LCD_putc( 3, 'M');                LCD_putc( 4, MH);        LCD_putc( 5, ML);    }    else if (mode == DAY)    {        DH = CHAR2BCD2(date[DAY]);        DL = (DH & 0x0F) + '0';        DH = (DH >> 4) + '0';		        LCD_putc( 0, ' ');        LCD_putc( 1, ' ');           LCD_putc( 2, 'D');        LCD_putc( 3, 'D');                LCD_putc( 4, DH);        LCD_putc( 5, DL);    }        LCD_putc(6, '\0');        LCD_Colon(0);        if (input != KEY_NULL)        LCD_FlashReset();        LCD_UpdateRequired(TRUE, 0);            enter_function = 1;        // Increment/decrement years, months or days    if (input == KEY_PLUS)        date[mode]++;    else if (input == KEY_MINUS)        date[mode]--;    else if (input == KEY_PREV)    {        if (mode == YEAR)            mode = DAY;        else            mode--;    }    else if (input == KEY_NEXT)    {        if (mode == DAY)            mode = YEAR;        else            mode++;    }    else if (input == KEY_ENTER)    {        // store the temporary adjusted values to the global variables        cli(); // mt __disable_interrupt();        gYEAR = date[YEAR];        gMONTH = date[MONTH];        gDAY = date[DAY];        sei(); // mt __enable_interrupt();        mode = YEAR;        return ST_TIME_DATE_FUNC;    }        /* OPTIMIZE: Can be solved by using a modulo operation */    if (date[YEAR] == 255)        date[YEAR] = 99;    if (date[YEAR] > 99)        date[YEAR] = 0;        if (date[MONTH] == 0)        date[MONTH] = 12;    if (date[MONTH] > 12)        date[MONTH] = 1;        // Check for leap year, if month == February    if (gMONTH == 2)        if (!(gYEAR & 0x0003))              // if (gYEAR%4 == 0)            if (gYEAR%100 == 0)                if (gYEAR%400 == 0)                    LeapMonth = 1;                else                    LeapMonth = 0;            else                LeapMonth = 1;        else            LeapMonth = 0;    else        LeapMonth = 0;        if (LeapMonth)        MonthLength_temp = 29;    else        MonthLength_temp = MonthLength[date[MONTH]];        if (date[DAY] == 0)        date[DAY] = MonthLength_temp;    if (date[DAY] > MonthLength_temp)        date[DAY] = 1;        enter_function = 0;        return ST_TIME_DATE_ADJUST_FUNC;}/*******************************************************************************   Function name : SetDateFormat**   Returns :       char ST_state (to the state-machine)**   Parameters :    char input (from joystick)**   Purpose :       Adjusts the Dateformat******************************************************************************/char SetDateFormat(char input){    static char enter = 1;        if(enter)    {        enter = 0;        		LCD_puts_f(DATEFORMAT_TEXT[dateformat], 1);    }    if (input == KEY_PLUS)    {        if(dateformat >= 2)            dateformat = 0;        else            dateformat++;		        LCD_puts_f(DATEFORMAT_TEXT[dateformat], 1);            }    else if (input == KEY_MINUS)    {        if(dateformat == 0)            dateformat = 2;        else            dateformat--;		        LCD_puts_f(DATEFORMAT_TEXT[dateformat], 1);                }    else if (input == KEY_ENTER)        {        enter = 1;        return ST_TIME_DATE_FUNC;    }            return ST_TIME_DATEFORMAT_ADJUST_FUNC;}/********************************************************************************   Timer/Counter2 Overflow Interrupt Routine**   Purpose: Increment the real-time clock*            The interrupt occurs once a second (running from the 32kHz crystal)*			 (or 16 times a second if FASTCLOCK is defined)********************************************************************************/// mtA// #pragma vector = TIMER2_OVF_vect// __interrupt void TIMER2_OVF_interrupt(void)SIGNAL(SIG_OVERFLOW2)// mtE{    static char LeapMonth;#ifdef FASTCLOCK	gTICK++;		if (gTICK==TICKSPERSECOND)	{		gTICK=0;#endif		gSECOND++;               // increment second				glastSpeed = TCNT1L + (TCNT1H <<8); // Read windspeeed for last sercond		TCNT1L=0;				// Reset Counter for next second		TCNT1H=0;				if (gSECOND == 60)		{			gSECOND = 0;			gMINUTE++;						gPowerSaveTimer++;						if (gMINUTE > 59)			{				gMINUTE = 0;				gHOUR++;								if (gHOUR > 23)				{										gHOUR = 0;					gDAY++;										// Check for leap year if month == February					if (gMONTH == 2)						if (!(gYEAR & 0x0003))              // if (gYEAR%4 == 0)							if (gYEAR%100 == 0)								if (gYEAR%400 == 0)									LeapMonth = 1;								else									LeapMonth = 0;							else								LeapMonth = 1;						else							LeapMonth = 0;					else						LeapMonth = 0;										// Now, we can check for month length					if (gDAY > (MonthLength[gMONTH] + LeapMonth))					{						gDAY = 1;						gMONTH++;												if (gMONTH > 12)						{							gMONTH = 1;							gYEAR++;						}					}				}						// start of every hour do this....				if (gLogTimeUnit == HOUR) 					check_log();				//			}				// start of every minute do this....				if (gLogTimeUnit == MINUTE) 					check_log();				//				}				// start of every second do this....				// check alarm every second clock to start logging				if (ALARM_check(gYEAR,gMONTH,gDAY,gHOUR,gMINUTE,gSECOND)){					gLogging=TRUE;					ALARM_clear();				}								if (gLogTimeUnit == SECOND)					check_log();								gSpeedTotal +=glastSpeed;								if (glogSpeed){ // we are logging wind speed for SECONDSTOLOG seconds					if(gSpeedSeconds++ >=gSpeedSecondsToLog){//log all the data						gLogging = TRUE;						glogSpeed = FALSE;						gLogNow=TRUE;					}				}				//#ifdef FASTCLOCK				}			// every tick do this....			if (gLogTimeUnit == TICK){ 				check_log();				if (glogSpeed){					// we don't log wind speed on this setting,					// skip straight to saving to flash					gLogging = TRUE;					glogSpeed = FALSE;					gLogNow=TRUE;				}			}			#endif}/*******************************************************************************   Function name : check_log**   Returns :       none**   Parameters :    none**   Purpose :       check log timer and and increment if needed, Sets flags *					if it it time to log to the data to the flash******************************************************************************/void check_log(void){	if (gLogging && gLogTime>0) {		gLogTimeCounter++;		if (gLogTimeCounter>=gLogTime){			glogSpeed = TRUE; // start logging wind speed for SECONDSTOLOG seconds			gSpeedTotal=0;			gLogTimeCounter=0;			gSpeedSeconds = 0;			gLogging = FALSE;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -