|
extern unsigned char I2CReadBlock_1B(I2CDeviceType Slave, unsigned char Addres, void *Buffer, unsigned char Len);
extern unsigned char I2CWriteBlock_1B(I2CDeviceType Slave, unsigned char Addres, void *Buffer, unsigned char Len);#define _RTCStop (1<<7)
#define RTC_SecondsParts 0x00
#define RTC_Seconds 0x01
#define RTC_Minutes 0x02
#define RTC_Hours 0x03
#define RTC_Day 0x04
#define RTC_Date 0x05
#define RTC_Month 0x06
#define RTC_Year 0x07
#define RTC_Control 0x08
#define RTC_Watchdog 0x09
#define RTC_AlMonth 0x0A
#define RTC_AlDate 0x0B
#define RTC_AlHour 0x0C
#define RTC_AlMinute 0x0D
#define RTC_AlSecond 0x0E
#define RTC_Flags 0x0F
#define RTC_SQW 0x13extern cx_lock RTCLock;
typedef unsigned long ClockType;typedef struct {
unsigned char Min;
unsigned char Hour;
}TimeType;
typedef struct {
unsigned char Day;
unsigned char Month;
unsigned char Year;
}DateType;typedef struct {
TimeType Time;
DateType Date;
}RTCType;void RTCReadClock(RTCType *Destination) {
unsigned char Tmp;
cx_lock_w(&RTCLock);
I2CReadBlock_1B(M41Txx, RTC_Minutes, (unsigned char*)&Destination->Time,sizeof(TimeType));
I2CReadBlock_1B(M41Txx, RTC_Date, (unsigned char*)&Destination->Date,sizeof(DateType));
I2CReadBlock_1B(M41Txx, RTC_Control, &Tmp, 1); //dummy read to release data transfer
cx_unlock(&RTCLock);
Destination->Time.Hour &= 0x3F; /* mask CE, CEB */
}void RTCSetClock(RTCType *Source) {
cx_lock_w(&RTCLock);
I2CWriteBlock_1B(M41Txx, RTC_Minutes, (unsigned char*)&Source->Time, sizeof(TimeType));
I2CWriteBlock_1B(M41Txx, RTC_Date, (unsigned char*)&Source->Date, sizeof(DateType));
cx_unlock(&RTCLock);
}void RTCInit(void) {
unsigned char Tmp;
cx_lock_w(&RTCLock);
I2CReadBlock_1B(M41Txx, RTC_Seconds, &Tmp, 1);
if(Tmp & _RTCStop) {
Tmp &= ~_RTCStop;
I2CWriteBlock_1B(M41Txx, RTC_Seconds, &Tmp, 1);
}
Tmp = 0;
I2CWriteBlock_1B(M41Txx,RTC_AlHour, &Tmp, 1); //reset HT bit
cx_unlock(&RTCLock);
}
E-mail: info@telesys.ru