📄 d1643rtc.c
字号:
* rtcDateSet - set the date** RETURNS: N/A.*/void rtcDateSet ( int year, int month, int dayOfMonth, int dayOfWeek ) { rtc_Year = year%100; rtc_Month = month; rtc_DayOfMonth = dayOfMonth; rtc_DayOfWeek = dayOfWeek; rtcSpIoctl (SET_DATE, FALSE); }/********************************************************************************* rtcDateGet - get the date** RETURNS: N/A.*/void rtcDateGet ( void ) { rtcSpIoctl (GET_DATE, TRUE); }/********************************************************************************* rtcTimeSet - set the time** RETURNS: N/A.*/void rtcTimeSet ( int hour, int minute, int second ) { rtc_Hour = hour; rtc_Minute = minute; rtc_Second = second; rtcSpIoctl (SET_TIME, FALSE); }/********************************************************************************* rtcTimeGet - get the time** RETURNS: N/A.*/void rtcTimeGet ( void ) { rtcSpIoctl (GET_TIME, TRUE); }/********************************************************************************* rtcClockStop - stop the real time clock** RETURNS: N/A.*/void rtcClockStop ( void ) { rtcSpIoctl (STOP_RTC, FALSE); }/********************************************************************************* rtcClockStart - start the real time clock** RETURNS: N/A.*/void rtcClockStart ( void ) { rtcSpIoctl (START_RTC, FALSE); }/********************************************************************************* rtcSpIoctl - special device control routine** This routine must be called by the user to access the driver ioctl routine* correctly.** NOMANUAL*/LOCAL void rtcSpIoctl ( int request, /* request code */ BOOL print /* print if TRUE, else only set structure */ ) { int fd; fd = open (RTC_DEVICE, 0, 0); ioctl (fd, request, print); close (fd); }/********************************************************************************* rtcIoctl - special device control** This driver responds to the ioctl calls described in the header.*/LOCAL STATUS rtcIoctl ( FAST RTC_DEV *pRtcDv, /* device to control */ FAST int request, /* request code */ BOOL print /* print if TRUE, else only set structure */ ) { UCHAR calibration; UCHAR stop_bit; UCHAR seconds; STATUS state = OK; /* state of the ioctl calls */ int century; int temp; switch (request) { case SET_DATE: temp = (int)rtc_DayOfWeek; if (temp < 1 || temp > 7) { printErr ("Invalid day of week: %d\n", rtc_DayOfWeek); break; } temp = (int)rtc_Month; if (temp < 1 || temp > 12) { printErr ("Invalid month: %d\n", rtc_Month); break; } calibration = clockInByte(CLOCK_CONTROL) & CLOCK_CAL_BITS; stop_bit = clockInByte(CLOCK_SECONDS) & CLOCK_STOP_BIT; /* * put clock in write mode */ clockOutByte(CLOCK_CONTROL, calibration | CLOCK_WRITE_BIT); /* * update with passed in values */ clockOutByte(CLOCK_DAY, toBCD(rtc_DayOfWeek)); clockOutByte(CLOCK_DATE, toBCD(rtc_DayOfMonth)); clockOutByte(CLOCK_MONTH, toBCD(rtc_Month)); clockOutByte(CLOCK_YEAR, toBCD(rtc_Year % 100)); /* * restore control register (to set the time) */ clockOutByte(CLOCK_CONTROL, calibration); break; case GET_DATE: calibration = clockInByte(CLOCK_CONTROL) & CLOCK_CAL_BITS; /* * put clock in read mode */ clockOutByte(CLOCK_CONTROL, calibration | CLOCK_READ_BIT); /* * read current values */ rtc_DayOfWeek = fromBCD(clockInByte(CLOCK_DAY) & CLOCK_DAY_BITS); rtc_DayOfMonth = fromBCD(clockInByte(CLOCK_DATE) & CLOCK_DATE_BITS); rtc_Month = fromBCD(clockInByte(CLOCK_MONTH) & CLOCK_MONTH_BITS); rtc_Year = fromBCD(clockInByte(CLOCK_YEAR) & CLOCK_YEAR_BITS); /* * restore control register */ clockOutByte(CLOCK_CONTROL, calibration); if (print) { temp = (int)rtc_DayOfWeek; if (temp < 1 || temp > 7) { printErr ("Invalid day of week: %d\n", rtc_DayOfWeek); break; } temp = (int)rtc_Month; if (temp < 1 || temp > 12) { printErr ("Invalid month: %d\n", rtc_Month); break; } if (rtc_Year <= 70) century = 2000; else century = 1900; printf ("Date: %s, %02d-%s-%4d.\n", dayTable [rtc_DayOfWeek - 1], rtc_DayOfMonth, monthTable [rtc_Month - 1], century + rtc_Year); } break; case SET_TIME: calibration = clockInByte(CLOCK_CONTROL) & CLOCK_CAL_BITS; stop_bit = clockInByte(CLOCK_SECONDS) & CLOCK_STOP_BIT; /* * put clock in write mode */ clockOutByte(CLOCK_CONTROL, calibration | CLOCK_WRITE_BIT); /* * update with passed in values */ clockOutByte(CLOCK_SECONDS, toBCD(rtc_Second | stop_bit)); clockOutByte(CLOCK_MINUTES, toBCD(rtc_Minute)); clockOutByte(CLOCK_HOUR, toBCD(rtc_Hour)); /* * restore control register (to set the time) */ clockOutByte(CLOCK_CONTROL, calibration); break; case GET_TIME: calibration = clockInByte(CLOCK_CONTROL) & CLOCK_CAL_BITS; /* * put clock in read mode */ clockOutByte(CLOCK_CONTROL, calibration | CLOCK_READ_BIT); /* * read current values */ rtc_Second = fromBCD(clockInByte(CLOCK_SECONDS) & CLOCK_SECOND_BITS); rtc_Minute = fromBCD(clockInByte(CLOCK_MINUTES) & CLOCK_MINUTE_BITS); rtc_Hour = fromBCD(clockInByte(CLOCK_HOUR) & CLOCK_HOUR_BITS); /* * restore control register */ clockOutByte(CLOCK_CONTROL, calibration); if (print) { printf ("Time: %02d:%02d:%02d.\n", rtc_Hour, rtc_Minute, rtc_Second); } break; case STOP_RTC: /* * Read the current number of seconds and write it back with * the stop bit turned on. */ calibration = clockInByte(CLOCK_CONTROL) & CLOCK_CAL_BITS; /* * put clock in read AND write mode */ clockOutByte(CLOCK_CONTROL, calibration | CLOCK_READ_BIT | CLOCK_WRITE_BIT); seconds = clockInByte(CLOCK_SECONDS) & CLOCK_SECOND_BITS; clockOutByte(CLOCK_SECONDS, seconds | CLOCK_STOP_BIT); /* * restore control register */ clockOutByte(CLOCK_CONTROL, calibration); break; case START_RTC: /* * Read the current number of seconds and write it back with * the stop bit cleared. */ calibration = clockInByte(CLOCK_CONTROL) & CLOCK_CAL_BITS; /* * put clock in read AND write mode */ clockOutByte(CLOCK_CONTROL, calibration | CLOCK_READ_BIT | CLOCK_WRITE_BIT); seconds = clockInByte(CLOCK_SECONDS) & CLOCK_SECOND_BITS; clockOutByte(CLOCK_SECONDS, seconds); /* * restore control register */ clockOutByte(CLOCK_CONTROL, calibration); break; default: state = S_ioLib_UNKNOWN_REQUEST; break; } return (state); }/********************************************************************************* toBCD - convert to binary coded decimal*/LOCAL int toBCD ( int intValue ) { int bcdValue; bcdValue = intValue%10; intValue /= 10; intValue *= 0x10; bcdValue += intValue; return (bcdValue); }/********************************************************************************* fromBCD - convert from binary coded decimal*/LOCAL int fromBCD ( int bcdValue ) { int intValue; intValue = bcdValue&0xf; intValue += ((bcdValue&0xf0)/0x10)*10; return (intValue); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -